VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
Ā
Ray Tracing Rendering Technique Explained
1. RAY TRACING
ā¢The basic Ray Tracing Algorithm
ā¢Computing Viewing Rays
ā¢Ray-Object Intersection
ā¢A Ray Tracing Program
ā¢Shadows
ā¢Specular Reflection
ā¢Refraction
ā¢Instancing
ā¢Constructive Solid Geometry
ā¢Distribution Ray Tracing
2. RAY TRACING- INTRODUCTION
ā¢ RAY TRACING is a rendering technique for generating an image by tracing the
path of light as pixels in an image plane and simulating the effects of its
encounters with virtual objects.
ā¢ The technique is capable of producing a very high degree of visual realism, but at
a greater computational cost.
ā¢ Ray tracing is best suited
for applications where
taking a relatively long time
to render a frame can be
tolerated, such as in still
images and film
ā¢ Poorly suited for real-
time applications such
as video games where
speed is critical.
3. FORWARD RAY TRACING
Forward ray tracing follows the light particles
(photons) from the light source to the object. Although
forward ray tracing can most accurately determine the
coloring of each object, it is highly inefficient. This is
because many rays from the light source never come
through the viewplane and into the eye. Tracking every
light ray from the light source down means that many
rays will go to waste because they never contribute to
the final image as seen from the eye. Forward ray
tracing is also known as light ray tracing and photon
tracing.
ā¢ Rays as paths of photons in world space
ā¢ Forward ray tracing: follow photon from light
sources to viewer
RAY TRACING- INTRODUCTION
4. BACKWARD RAY TRACING
To make ray tracing more efficient, the method of backward ray tracing is introduced. In
backward ray tracing, an eye ray is created at the eye; it passes through the viewplane and
on into the world. The first object the eye ray hits is the object that will be visible from
that point of the viewplane. After the ray tracer allows that light ray to bounce around, it
figures out the exact coloring and shading of that point in the viewplane and displays it
on the corresponding pixel on the computer monitor screen. Backward ray tracing is also
known as eye ray tracing.
The downfall of backward ray tracing is that it assumes only the light rays that come through
the viewplane and on into the eye contribute to the final image of the scene. In certain
cases, this assumption is flawed. For example, if a lens is held at a distance on top of a
table, and is illuminated by a light source directly above, there will exist a focal point
beneath the lens with a large concentration of light. If backward ray tracing tries to re-
create this image, it will miscalculate because shooting light rays backward only confirms
that rays traveled through the lens; backward rays have no way of recognizing that
forward rays are bent when they go through the lens. Therefore, if only backward ray
tracing is performed, there will only be an even patch of light beneath the lens, just as if
the lens were a normal piece of glass and light is transmitted straight through it.
RAY TRACING- INTRODUCTION
5. BACKWARD RAY TRACING
Ray-casting: one ray from center of projection through each pixel in image plane
Illumination:
ā¢ Phong (Phong shading is an interpolation technique for surface shading in 3D
computer graphics)
ā¢ Shadow rays
ā¢ Specular reflection
ā¢ Specular transmission
RAY TRACING- INTRODUCTION
6. SHADOW RAYS
ā¢ Determine if light āreallyā hits surface point
ā¢ Cast shadow ray from surface point to light
ā¢ If shadow ray hits opaque object. No contribution
ā¢ Improved diffuse reflection
RAY TRACING- INTRODUCTION
7. REFLECTION RAYS
ā¢ Calculate specular component of illumination
ā¢ Compute reflection ray (recall: backward!)
ā¢ Call ray tracer recursively to determine color
ā¢ Add contributions
RAY TRACING- INTRODUCTION
8. BASIC RAY TRACING ALGORITHM
For( every pixel in the view port)
{
for(every object in the model world)
{
if(ray-object intersection)
{
select the front most intersection;
recursively trace the reflection and refraction rays;
calculate color;
}
}
}
RAY TRACING- INTRODUCTION
9. camera position: letās call it O=(Ox,Oy,Oz)
Viewport: (Vx,Vy,Vz)
So what color is the light reaching (Ox,Oy,Oz) after passing
through (Vx,Vy,Vz)?
EXAMPLE: RENDERING A SPHERE
weāll trace the rays āin reverseā; weāll start
with a ray originating from the camera,
going through a point in the viewport, and
following it until it hits some object in the
scene. This object is what is āseenā from
the camera through that point of the
viewport.
10. ā¢ We need a āframeā through which the scene was viewed. Weāll assume this
frame has dimensions Vw and Vh, itās frontal to the camera orientation (that is,
itās perpendicular to Z+ā) at a distance d,
EXAMPLE: RENDERING A SPHERE
11. THE RAY EQUATION
we can express any point P in the ray as
P=O+t(VāO)
the ray passes through O, and we know its direction (from O to V). where t
is an arbitrary real number.
Letās call (VāO), the direction of the ray, D Dā; then the equation
becomes simply
P=O+tD
EXAMPLE: RENDERING A SPHERE
12. THE SPHERE EQUATION
If C is the center and r is the radius of the sphere, the points P on the
surface of the sphere satisfy the following equation:
PāC,PāC =r2
EXAMPLE: RENDERING A SPHERE
13. RAY MEETS SPHERE
ā¢ We now have two equations, one describing the points
on the sphere, and one describing the points on the ray:
PāC,PāC =r2
P=O+tD
ā¢ The point P where the ray hits the sphere is both a point
in the ray and a point in the surface of the sphere, so it
must satisfy both equations at the same time
ā¢ substituting P in the first with the expression for P in the
second one. This gives us
O+tD āC, O+tD āC =r2
further solving for values of t satisfy this equation
gives:
EXAMPLE: RENDERING A SPHERE
varying t across all the real numbers will yield every point P in this ray.
14. ā¢ Now we need to do is to compute the
intersections of the ray and each sphere,
keep the closest one to the camera, and
paint the pixel in the canvas with the
appropriate color.
EXAMPLE: RENDERING A SPHERE
ray equation:
P=O+t(VāO)
we can divide the parameter space in three parts:
t<0 Behind the camera
0ā¤tā¤1 Between the camera and the projection plane
t>1 The scene
Fig. The parameter space
15. EXAMPLE: RENDERING A SPHERE
O = <0, 0, 0>
for x in [-Cw/2, Cw/2] {
for y in [-Ch/2, Ch/2] {
D = CanvasToViewport(x, y)
color = TraceRay(O, D, 1, inf)
canvas.PutPixel(x, y, color) } }
CanvasToViewport(x, y) {
return (x*Vw/Cw, y*Vh/Ch, d) }
PSEUDOCODE:
The main method now looks like this:
The CanvasToViewport function is :
16. EXAMPLE: RENDERING A SPHERE
The TraceRay method computes the intersection of the ray with every
sphere, and returns the color of the sphere at the nearest intersection
which is inside the requested range of t:
TraceRay(O, D, t_min, t_max) {
closest_t = inf
closest_sphere = NULL
for sphere in scene.Spheres {
t1, t2 = IntersectRaySphere(O, D, sphere)
if t1 in [t_min, t_max] and t1 < closest_t
closest_t = t1
closest_sphere = sphere
if t2 in [t_min, t_max] and t2 < closest_t
closest_t = t2 closest_sphere = sphere }
if closest_sphere == NULL
return BACKGROUND_COLOR
return closest_sphere.color }
finally, IntersectRaySphere just solves the quadratic equation:
17. EXAMPLE: RENDERING A SPHERE
Result of the above algorithm:
The reason they donāt quite look like spheres is that weāre missing a key
component of how human beings determine the shape of an object - the way it
interacts with light.
18. EXAMPLE: RENDERING A SPHERE
LIGHT- adding ārealismā to our rendering
Weāll consider a simple model with
assumptions:
1. All light is white. This lets us
characterize any light using a
single real number, i, called
the intensity of the light. Colored
light contains three intensity
values, one per color channel.
2. weāll ignore the atmosphere. This
means lights donāt become any
less bright no matter how far away
they are.
19. LIGHT SOURCES:
1. Point lights: A point light emits light from a fixed point in space, called
its position. Light is emitted equally in every direction.
2. Directional lights: a directional light has an intensity, but unlike them, it
doesnāt have a position; instead, it has a direction.
3. Ambient light: ambient light is characterized only by its intensity. Itās assumed
it unconditionally contributes some light to every point in the scene
EXAMPLE: RENDERING A SPHERE
Point light Directional light
20. ILLUMINATION OF A SINGLE POINT
To compute the illumination of a point:
ā¢ compute the amount of light contributed by each light source and add them
together to get a single number representing the total amount of light it receives.
ā¢ We can then multiply the color of the surface at that point by this number, to get
the appropriately lit color.
EXAMPLE: RENDERING A SPHERE
When a ray of light hits a matte object, because its surface is quite irregular at the
microscopic level, itās reflected back into the scene equally in every direction; hence
ādiffuseā reflection.
21. DIFFUSE REFLECTION
When a ray of light hits a matte object, because its surface is quite irregular at
the microscopic level, itās reflected back into the scene equally in every
direction; hence ādiffuseā reflection.
The amount of light reflected depends on the angle between the ray of light and
the surface.
EXAMPLE: RENDERING A SPHERE
22. MODELING DIFFUSE REFLECTION
EXAMPLE: RENDERING A SPHERE
The ray of light, with a width of I, hits the surface at P, at an angle Ī². The
normal at P is N , and the energy carried by the ray spreads over A. We
need to compute I/A.
23. From the figure,
cos(Ī±)=QR/PR
substituting QR with I/2 and PR with A/2 we get
cos(Ī±)=I/A
finally
I/A= N ,L / |N ||L |
EXAMPLE: RENDERING A SPHERE
For a sphere N is given by:
N =(PāC) / (|PāC|)
24. RENDERING WITH DIFFUSE REFLECTION
In first pseudocode add a couple of lights to the scene:
EXAMPLE: RENDERING A SPHERE
light {
type = ambient
intensity = 0.2 }
light {
type = point
intensity = 0.6
position = (2, 1, 0) }
light {
type = directional
intensity = 0.2
direction = (1, 4, 4) }
Note : that the intensities conveniently add up to 1.0; no point can have a greater
light intensity than this.
25. PSEUDOCODE FOR LIGHTING EQUATION :
EXAMPLE: RENDERING A SPHERE
ComputeLighting(P, N) {
i = 0.0
for light in scene.Lights {
if light.type == ambient {
i += light.intensity }
else {
if light.type == point
L = light.position ā P
else L = light.direction
n_dot_l = dot(N, L)
if n_dot_l > 0
i += light.intensity*n_dot_l/(length(N)*length(L))
} }
return i }
We can use ComputeLighting in TraceRay by replacing the line that returns the color of the
sphere by
return closest_sphere.color
27. EXAMPLE: RENDERING A SPHERE
SPECULAR REFLECTION
When a ray of light hits a perfectly regular surface, itās reflected in a single
direction, which is the symmetric of the incident angle respect to the mirror
normal. If we call the direction of the reflected light R , and we keep the
convention that L points to the light source, this is the situation:
For Ī±=0 , all of the light is reflected. For Ī±=90 , no light is reflected. Thus we take
cos(Ī±) .
28. EXAMPLE: RENDERING A SPHERE
MODELING SPECULAR REFLECTION
Shininess is a measure of how quickly the reflection function decreases
as Ī± increases. Thus we take cos(Ī±)s
Thus The bigger the value of s, the ānarrowerā the function becomes around 0,
and the shinier the object looks.
29. EXAMPLE: RENDERING A SPHERE
A ray L hits a surface at a point P, where the normal is N , and the specular
exponent is s. light reflected to the viewing direction V is cos(Ī±)s
R =LNā ā LPā
Equation for the specular reflection:
R =2N N ,L āL
30. EXAMPLE: RENDERING A SPHERE
RENDERING WITH SPECULAR REFLECTIONS
Letās add specular reflections to the scene weāve been working with so far. First, some
changes to the scene itself: sphere {
center = (0, -1, 3)
radius = 1
color = (255, 0, 0) # Red
specular = 500 # Shiny }
sphere {
center = (2, 0, 4)
radius = 1
color = (0, 0, 255) # Blue
specular = 500 # Shiny }
sphere {
center = (-2, 0, 4)
radius = 1
color = (0, 255, 0) # Green
specular = 10 # Somewhat shiny }
sphere {
color = (255, 255, 0) # Yellow
center = (0, -5001, 0)
radius = 5000
specular = 1000 # Very shiny }
31. EXAMPLE: RENDERING A SPHERE
We need to change ComputeLighting to compute the specular term when necessary and add
it to the overall light. Note that it now needs V and s:
ComputeLighting(P, N, V, s) {
i = 0.0
for light in scene.Lights
{ if light.type == ambient {
i += light.intensity }
else {
if light.type == point
L = light.position ā P
else L = light.direction # Diffuse
n_dot_l = dot(N, L)
if n_dot_l > 0
i += light.intensity*n_dot_l/(length(N)*length(L)) # Specular
if s != -1 {
R = 2*N*dot(N, L) - L
r_dot_v = dot(R, V)
if r_dot_v > 0
i += light.intensity*pow(r_dot_v/(length(R)*length(V)), s)
}
}
} return i }
32. TRACERAY WITH SPECULAR REFLECTION:
TraceRay(O, D, t_min, t_max) {
closest_t = inf
closest_sphere = NULL
for sphere in scene.Spheres {
t1, t2 = IntersectRaySphere(O, D, sphere)
if t1 in [t_min, t_max] and t1 < closest_t
closest_t = t1
closest_sphere = sphere
if t2 in [t_min, t_max] and t2 < closest_t
closest_t = t2
closest_sphere = sphere }
if closest_sphere == NULL
return BACKGROUND_COLOR
P = O + closest_t*D # Compute intersection
N = P - closest_sphere.center # Compute sphere normal at intersection
N = N / length(N)
return closest_sphere.color*ComputeLighting(P, N, -D, sphere.specular)
}
EXAMPLE: RENDERING A SPHERE
34. REFRACTION
The angle of the refracted ray can be determined by Snellās law:
Ī·1sin(Ļ1) = Ī·2sin(Ļ 2)
ā¢Ī·1 is a constant for medium 1
ā¢ Ī·2 is a constant for medium 2
ā¢ Ļ1 is the angle between the incident ray and the
surface normal
ā¢ Ļ2 is the angle between the refracted ray and
the surface normal
ā¢ In vector notation Snellās law can be
written:
k1(vĆn) = k2(v'Ćn)
ā¢ The direction of the refracted ray is
35. This equation only has a solution if
REFRACTION
This illustrates the physical phenomenon of the limiting
angle:
ā if light passes from one medium to another medium
whose index of refraction
is low, the angle of the refracted ray is greater than the
angle of the incident
ray
ā if the angle of the incident ray is large, the angle of the
refracted ray is larger
than 90o
āØ the ray is reflected rather than refracted
36. REFRACTION
Mix reflected and refracted light according to the Fresnel factor
L = kfresnelLreflected + (1- k fresnel)Lrefracted
FR =((Ī·2cosĪø1āĪ·1cosĪø2) / (Ī·2cosĪø1+Ī·1cosĪø2))2
FR =((Ī·1cosĪø2āĪ·2cosĪø1) / (Ī·1cosĪø2+Ī·2cosĪø1))2
FR=1/2(FR +FR ).
37. void fresnel(const Vec3f &I, const Vec3f &N, const float &ior, float &kr)
{
float cosi = clamp(-1, 1, dotProduct(I, N));
float etai = 1, etat = ior;
if (cosi > 0) { std::swap(etai, etat); }
// Compute sini using Snell's law
float sint = etai / etat * sqrtf(std::max(0.f, 1 - cosi * cosi));
// Total internal reflection
if (sint >= 1) {
kr = 1;
}
else {
float cost = sqrtf(std::max(0.f, 1 - sint * sint));
cosi = fabsf(cosi);
float Rs = ((etat * cosi) - (etai * cost)) / ((etat * cosi) + (etai * cost));
float Rp = ((etai * cosi) - (etat * cost)) / ((etai * cosi) + (etat * cost));
kr = (Rs * Rs + Rp * Rp) / 2;
}
// As a consequence of the conservation of energy, transmittance is given by:
// kt = 1 - kr;
}
REFRACTION
43. Following steps are taken to trace the shadows:
ā¢For each pixel hit on the screen (2D), we cast a ray from itās actual 3D position in
world space towards the light source
ā¢If the pixel is occluded, we store our occlusion value
SHADOWS
ā¢If the pixel is not occluded, we store a value of
1.0 in the buffer
44. SHADING
The actual shading step of each pixel that we have traced will be done post ray tracing:
ā¢Determine if pixel is occluded (ray traced shadow buffer entry smaller than 1.0). If it is
occluded, continue with 2.
ā¢Determine the sampling size (for our ray traced shadow buffer)
SHADOWS
47. INSTANCING
The basic idea of instancing is to distort all points on an object by a transformation matrix
before the object is displayed.
For example, if we transform the unit circle (in 2D)
by a scale factor (2, 1) in x and y, respectively, then
rotate it by 45ā¦, and move one unit in the x-
direction, the result is an ellipse with an eccentricity
of 2 and a long axis along the x = āy-direction
centered at (0, 1)
An instance of a circle with a series of
three transforms is an ellipse.
48. The ray intersection problem in the two spaces are just simple transforms
of each other. The object is specified as a sphere plus matrix M. The ray is specified in the
transformed (world) space by location a and direction b.
If the base object is composed of a set of points, one of which is p, then the transformed
object is composed of that set of points transformed by matrix M
INSTANCING
49. we can determine the intersection of a ray and an object transformed by matrix M. Let this
function be hit. If we create an instance class of type surface, we need to create a hit
function:
instance::hit(ray a + tb, real t0, real t1, hit-record rec)
ray r = M-1a + tM-1b
if (base-objectāhit(r, t0, t1, rec)) then
rec.n = (M-1)T rec.n
return true
else
return false
INSTANCING
59. DISTRIBUTION RAY TRACING
Distributed ray tracing is a ray tracing method based on randomly distributed
oversampling to reduce aliasing artifacts in rendered images.
ā¢Distributed ray tracing uses oversampling to reduce aliasing artifacts. Oversampling is a
process where instead of sampling a single value, multiple samples are taken and averaged
together
ā¢The intensity of a point in a scene can be represented analytically by an integral over the
illumination function and the reflectance function. The evaluation of this integral, while
extremely accurate, is too expensive for most graphics applications.
ā¢Instead of approximating an integral by a single scalar value, the function is point
sampled and these samples are used to define a more accurate scalar value. The practical
benefits of this are:
ā¢Gloss (fuzzy reflections)
ā¢Fuzzy translucency
ā¢Penumbras (soft shadows)
ā¢Depth of field
ā¢Motion blur
60. ā¢ Cast multiple rays from eye through different
parts of same pixel
ā¢ Image function f(x,y) ā Color that ray through
(x.y) returns
ā¢ Sampling function s(x,y) ā Controls where the
samples occur within the pixel ā One if sample
at (x,y), else zero
ā¢ Reconstruction filter r(x,y) ā Computes the
weighted average of resulting colors into a
single color ā Also weights according to area
DISTRIBUTION RAY TRACING
OVERSAMPLING
61. SOFT SHADOWS
DISTRIBUTION RAY TRACING
ā¢ Point light sources unrealistic
ā¢ Use area light source
ā¢ Cast shadow rays from surface to different
locations on light
ā¢ Hits/rays = % illuminated
ā¢ Integral is spatial average, Aliasing can occur and
be very nasty, particularly with uniform grid
62. GLOSSY SURFACES
DISTRIBUTION RAY TRACING
Surface microfacets perturb reflection
ray directions
ā¢ Nearby objects reflect more clearly
because distribution still narrow
ā¢ Farther objects reflect more blurry
because distribution has spread
ā¢ Helmholtz reciprocity allows us to
sample lobe of BRDF
63. MOTION BLUR
ā¢ Cast multiple rays from eye
through same point in each
pixel
ā¢ Each of these rays intersects the
scene at a different time
ā¢ Reconstruction filter controls
shutter speed, length
ā Box filter ā fast shutter
ā Triangle filter ā slow shutter
DISTRIBUTION RAY TRACING
64. DISTRIBUTION RAY TRACING
EXAMPLE:
This example shows a technique for generating soft shadows that is not based on
distributed ray tracing. This technique is based on uniformly sampling an area light
source. Although the penumbra and umbra of the shadow are clearly visible, the variation
between them is rigid.
65. DISTRIBUTION RAY TRACING
This example shows a soft shadow generated by distributed ray tracing. The light source was
sampled using 10 rays
66. DISTRIBUTION RAY TRACING
This example shows a soft shadow generated by distributed ray tracing. The light
source was sampled using 50 rays.
67. DISTRIBUTION RAY TRACING
This example shows a perfect reflection produced by a traditional (non-distributed) ray
tracer. Notice that the edges of the reflection are very sharp.