A Scene-Graph based Ray Tracer (3) – Light model, Anti-Aliasing, Ray-tracing

来源:互联网 发布:淘宝怎么加售后服务 编辑:程序博客网 时间:2024/06/11 05:45

This article will cover all graphical topics that are essential for the ray-tracer.

[Light Model]

I adopt the OpenGL light model (Phong Model):

color = emission(material) + ambient(light)*ambient(material) + SUM[0, light-num.-1]( (1 / (k(const)+k(linear)*d+k(quadratic)*d^2) * factor(spot-light) * (ambient(light)*ambient(material) + max(L.N, 0)*diffuse(light)*diffuse(material) + max(S.N, 0)^shininess * specular(light) * specular(material) ) ) )

Don’t forget to clamp the color between 0 and 1.

I also involve the shadow judgment within the light model implementation. When enumerating all lights, the code will judge whether the current ray could reach the light or not.

[Anti-Aliasing]

If we project only one ray for each pixel, there will be aliasing. Jaggies will happen among the pixels as below:

I use the multi-sampling to do anti-aliasing. For each pixel, I have projected 9 rays (3*3) and an average value has been calculated upon the 9 values sampled. Note: the deviation among the rays cannot be too large or the image would be blured obviously. See the anti-aliased image:

It looks more elegent right?

[Ray-tracing]


It is not hard to understand the process. When a ray reaches one opaque object, an reflected ray will be calculated and another ray (from the hit point with the reflected ray) will be projected again. It is a recursive process, which will stop when no objects are hit or max tracing depth has been reached. The color from reflected ray will be combined with local color of the hit object.

Here the tracing depth matters. As the first picture above, you cannot see the mirror image on the sphere from the mirror – it is because the tracing depth is only 2. The original ray takes one, and the reflected ray from the mirror takes the other one. But if we increase the tracing depth to 3, you can see the reflected image on the sphere from the mirror, as the second image shown.

[Another scene]

[Future Work]

* Transparent objects implemented. (refraction)

* Texture enabled. Then the scene will be much better.
* Using GPU… It is too slow for rendering the scene with many objects and lights.

原创粉丝点击