In ray tracing, I want to calculate the shading for a point where my ray hit. I "draw" lines to all light sources and check if they are blocked by objects or not. If they are not blocked then I calculate the intensity of the lighting according to their intensity and the degree between the "hit ray" and the surface normal.
But what if the light is blocked by a partially transparent surface? Then the lig开发者_Python百科ht should come to light the point, but its intensity and color are affected by the color of the surface it passes through, and in order to calculate that I need to do ray tracing for the point of passing of the light ray (actually for the 2 points, one of entry and one of exit), and this will be very costly, as well as potentially almost never ending (I guess that in the right positioning of light sources and surfaces you could put the tracer into an almost infinite loop for every hit).
Is there a fast and good way to approximate the color, or should I just take the color of the surface as the light color and its transparency as the intensity?
You don't need to do ray tracing starting at the entrance and exit points. Think about what kind of light is hitting these points. A ray of light that hits the translucent surface at an angle other than the one that hits your target object will not affect the color of the light that does hit your target object.
+ * +
+ * +
+ * +
+ * +
----------
| +*+ |
| + * + |
| + * + |
----------
+ * +
*
*
-------
This assumes, of course, that there is no refraction in the material.
Now if you wanted to extend your ray tracer to something a little more advanced like path tracing, then you would need to consider the light that bounces off of the translucent object and hits your final object, but for a ray tracer you do not need do worry about it.
For the translucent object, I would model the decrease in light intensity as a linear function of distance (most real world objects adhere closely to this assumption). If you are modeling light as having RGB components (not physically realistic...) then you would decrease each component in proportion to that component's value within the object.
If you want to get really advanced with what the light does while in the object, then you will need to turn to subsurface scattering (the reason milk in a glass does not look like a white solid and why humans are so hard to model in CGI).
EDIT: The phenomenon you mention of light bouncing back and forth infinitely and using many calculations is what real light behaves like. Advanced renderers nowadays can't possible integrate all of these light components and so instead randomly samples light distributions. The more samples taken, the closer the image converges to looking realistic, and the closer the light integration becomes to its true value. This is called monte carlo rendering. Path tracing, bidirectional path tracing, and metropolis light transport are all monte carlo algorithms that attempt to simulate light transport fully. Each algorithm, given enough time, will converge to the same final image, however, some are more efficient that others. (See path tracing on Wikipedia. At the bottom of the article is an image better than the one I attempted to draw).
If you want surfaces which can do both regular shading and transparency, the simplest thing is to ignore the regular shading for shadow rays: for illumination purposes, use only the filtering attributes of transparent surfaces. This avoids the potentially infinite lighting computation you described.
Note that there is a good way of approximating infinite ray trees, which goes by the colorful name,"Russian roulette": when any branch of the tree gets too unimportant, make a random choice as to whether to prune it. Branches are pruned with probability P
and contribute zero to the result (they are "dead" and do not need to be computed). Surviving branches ("winners") get their contribution multiplied by 1/(1-P)
, so that the resulting approximation is correct on average.
Russian roulette is a Monte Carlo technique; you may want to look into Monte Carlo ray tracing and other global illumination methods.
精彩评论