A ray tracer emits a ray from each pixel toward the scene to determine the color of the pixel. The process of computing the color can be split into three parts:
- Ray generation: The origin and direction of each pixel ray are computed.
- Ray intersection: The ray finds the closest object intersecting the viewing ray.
- Shading: The intersection point, surface normal, and other information are used to determine the color of the pixel.
A ray can be represented with a 3D parametric line from the eye
Note that:
- If
, then is closer to than . - If
, then is behind .
Camera Coordinate System
All the rays start from the origin of an orthonormal coordinate frame known as the camera/eye coordinate system. In this frame, the camera is looking at the negative
Camera
The coordinate system is built from:
- The viewpoint
, which is at the origin of the camera coordinate system. - The view direction, which is
. - The up vector, which is used to construct a basis that has
and in the plane defined by the view direction and the up vector.
Ray Generation
Pixel Coordinates
The image dimensions are defined with four numbers:
: the position of the left and right edges. : the position of the top and bottom edges.
Note that the coordinates are expressed in the camera coordinate frame defined in a plane parallel to the
The image has to be fitted within a rectangle of
Orthographic View
For an orthographic view, all the rays will have the direction
Orthographic View
Perspective View
For a perspective view, all the rays will have the same origin
Perspective View
Ray Intersection
Once a ray in the form
The following pseudocode tests for βhitsβ:
ray = e + td
t = infinity
for each `object` in the scene
if `object` is hit by `ray` and `ray's t` < `t`
hit object = `object`
t = `ray's t`
return hit t < infinity
Shading
Once the visible surface is known, the next step is to compute the value of the pixel using a shading model, which can be made out of simple heuristics or elaborate numeric computations.
A shading model is designed to capture the process of light reflection on a surface. The important variables in this process are:
(intersection point) - the intersection point between a surface and a ray. (light direction) - a unit vector pointing from the surface towards a light source, computed by normalizing the vector between the intersection point and the light source position .
(view direction) - a unit vector pointing from the surface towards the place the ray is emitted from. Itβs computed by normalizing the vector between the intersection point and the ray origin .
(surface normal) - a unit vector perpendicular to the surface at the point where the reflection is taking place.- Other characteristics of the light source and the surface, depending on the shading model.
Lambertian Shading
One of the simplest shading models, discovered by Lambert in the 18th century. The amount of energy from a light source that falls on a surface depends on the angle of the surface to the light.
Lambert
- A surface facing directly the light receives maximum illumination.
- A surface tangent to the light receives no illumination.
- A surface facing away from the light receives no illumination.
Thus, the illumination is proportional to the cosine of the angle between
Where:
is the diffuse coefficient, a characteristic of the surface. is the intensity of the light source.
Additional notes of this model:
- The model is view-independent.
- The color of the surface appears to have a very matte, chalky appearance.
Blinn-Phong Shading
Many surfaces show some degree of highlights (shininess) or specular reflections that appear to move as the viewpoint changes. The idea is to produce reflections when
Blinn-Phong
- The half vector
is a unit vector that goes through the bisector of the angle between and .
Also:
- If
is near , then the specular component should be bright; if itβs far away, it should be dim. Therefore, the illumination is proportional to the cosine of the angle between and , i.e., . - The specular component decreases exponentially when
is far away from . Therefore, the result is taken to the power, , to make it decrease faster.
The color of the pixel is then:
Where:
is the specular coefficient, a characteristic of the surface. is the intensity of the light source. is a variable that controls how fast the result decreases.
Note that the color of the pixel is the overall contribution of both the Lambertian shading model and the Blinn-Phong shading model.
Ambient Shading
Surfaces that receive no illumination are rendered completely black. To avoid this, a constant component is added to the shading model. The color depends entirely on the object hit, with no dependence on the surface geometry.
Where:
is the surface ambient coefficient. is the ambient light intensity.