Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Ray tracing

Introduction
o Rendering 3D objects to 2D screen
n Input: set of objects
n Output: array of pixels
o Object-order rendering
n For each object, all the pixels that it influences are
found and calculated/updated
o Image-order rendering
n For each pixel, all the objects that influence it are
found and the pixel value is computed
o Both approaches can compute same image
o Different flexibility and performance
n Image-order rendering more flexible but slower

}10/25/21 }Sarajevo School of Science and Technology }2


Rendering
o Two major methods
n Rasterization
n Ray tracing

o Many others
n Mostly an extension of the above

}10/25/21 }Sarajevo School of Science and Technology }3


A Pixel
A Pixel Is Not A Little Square,
A Pixel Is Not A Little Square,
A Pixel Is Not A Little Square!
(And a Voxel is Not a Little Cube)
Technical Memo 6
Alvy Ray Smith
July 17, 1995

o “My purpose here is to, once and for all, rid the world of the
misconception that a pixel is a little geometric square. This is not a
religious issue. This is an issue that strikes right at the root of correct
image (sprite) computing and the ability to correctly integrate
(converge) the discrete and the continuous. The little square model is
simply incorrect. It harms. It gets in the way. If you find yourself
thinking that a pixel is a little square, please read this paper…”

o It is a point sample reconstructed from a discrete to a continuous entity using a


reconstruction filter

}10/25/21 }Sarajevo School of Science and Technology }4


Raster devices
o Diplays
n Rectangular array of pixels
n Different colours achived by mixing varying intensities of red, green
and blue (RGB)
o Printers
n No physical array
n Image printed by depositing ink at selected points on a grid
n Uses CMYK colour mode
o Cameras
n Sensor with a grid of light-sensitive pixels
n Each pixel records the colour and intensity of light falling on it
o Scanners
n Linear array of pixels
n Makes many measurements per second to produce a grid of pixels

}10/25/21 }Sarajevo School of Science and Technology }5


Raster images
o Most common way to store and process
images
o A 2D array that stores the pixel value for
each pixel
o Stored in memory
o Each pixel in the stored image controls
the colour of one pixel of the display
n WHEN IS THIS TRUE?

}10/25/21 }Sarajevo School of Science and Technology }6


Raster images
o Most common way to store and process images
o A 2D array that stores the pixel value for each pixel
o Stored in memory
o Each pixel in the stored image controls the colour of
one pixel of the display
n If the image and display have the same number of pixels
o Transformations
n Move, scale, rotate
o Processing
n Change color, map to an object

}10/25/21 }Sarajevo School of Science and Technology }7


Vector images
o Described by storing descriptions of
shapes
o No reference to a pixel grid
o Resolution independent (lossless scaling)
o Must be rasterised before being displayed
o Used for drawings, text, and applications
where precision is important

}10/25/21 }Sarajevo School of Science and Technology }8


Going back to rays:
Ray approximation
o In real life
n Streams of photons
o In vacuum
n Paths are straight lines
o In computer graphics
n Rays as approximation

}10/25/21 }Sarajevo School of Science and Technology }9


Light is Bouncing Photons
o Light sources send off photons in all directions
n Model these as particles that bounce off objects in the scene
n Each photon has a wavelength and energy (color and intensity)
n When photons bounce, some energy is absorbed, some reflected,
some transmitted

o If we can model photon bounces, we can generate images

o Technique: follow each photon from the light source until:


n All of its energy is absorbed (after too many bounces)
n It departs the known universe (not just the part of the world that
is within the viewing volume!)
n It strikes the image and its contribution is added to appropriate
pixel

}10/25/21 }Sarajevo School of Science and Technology }10


Forward Ray-Tracing
o Rays are the paths of these photons
o This method of rendering by following photon paths is called
ray-tracing
o Forward
n Rays traced from the light source(s)
n Most of the rays never hit the view plane
n Highly inefficient
o Wasted computation => wasted time!!!

}10/25/21 }Sarajevo School of Science and Technology }11


Backward Ray-Tracing
o Backward
n Rays traced from the camera/eye
n No wasted rays
n Speed-up the rendering process

}10/25/21 }Sarajevo School of Science and Technology }12


Backward Ray-Tracing
o Basic idea:
n Each pixel gets light from just one direction—the line
through the image point and focal point
n Any photon contributing to that pixel’s color has to come
from this direction
n So head in that direction and see what is sending light
o If we hit a light source - done
o If we find nothing - done
o If we hit a surface - see where that surface is lit from

o At the end we’ve done forward ray-tracing, but ONLY for the
rays that contribute to the image

}10/25/21 }Sarajevo School of Science and Technology }13


The Basic Ray-Tracing
Algorithm
o Computes one pixel at a time
o Finds the object that is seen at that pixel in the image
n Ray generation
o Computes the origin and direction of each ray through a pixel
n Ray intersection
o Finds the closest object that
the ray intersects
n Shading
o Computes the pixel colour
n The calculation depends on
the rendering algorithm

}10/25/21 }Sarajevo School of Science and Technology }14


The Basic Ray-Tracing
Algorithm
o The program structure
for each pixel do
compute viewing ray
find first object hit by ray and its surface normal n
set pixel colour to value computed from hit point, light and n

}10/25/21 }15
Simple Ray Tracing
o A single ray is traced for each pixel in the image plane.

o Intersection tests are performed with all objects in the scene to


determine the object which the ray actually hits
n Keep the closest intersection

o Reflected and refracted rays are calculated/generated


o Each of these rays are traced to determine their intersections
o This recursive process is continued until the “new color”
contribution is deemed to be negligible

}10/25/21 }Sarajevo School of Science and Technology }16


Pseudo Code
Color RayTrace(Point p, Vector D, int depth){
Point pd /*intersection point*/
Boolean intersection
If (depth > MAX) return Black
Interesect (p,direction, &pd, &intersection)
If (!intersection) return Background
Ilocal=kaIa+Ip.v.(kd(n.l) +ks.(h.n)m)
Return Ilocal+kr*RayTrace(pd, R, depth+1)
}

}10/25/21 }Sarajevo School of Science and Technology }17


Computing Viewing Rays
o Using 3D parametric line
x = f(t) y = g(t) z = h(t)
for points p0 = (x0, y0, z0) and p1 = (x1, y1, z1)

é x ù é x0 + t ( x1 - x0 ) ù
ê y ú = ê y + t ( y - y )ú
ê ú ê 0 1 0 ú

êë z úû êë z0 + t ( z1 - z0 ) úû
In vector form, where p = (x, y, z)
p(t) = p0 + t(p1 – p0)
p(t) = o + t(d)

}10/25/21 }Sarajevo School of Science and Technology }18


Computing Viewing Rays
o Using 3D parametric line

p = o + t·d

e.g. o = (2,1,3) d = (7,2,-5)


for any t, we get a point p(t) on the line
for e.g. t=2 => p(t) = (16,5,-7)

for t<0 => ?


for t>1 => ?

}10/25/21 }Sarajevo School of Science and Technology }19


Computing Viewing Rays
o Using 3D parametric line

p = o + t·d

e.g. o = (2,1,3) d = (7,2,-5)


for any t, we get a point p(t) on the line
for e.g. t=2 => p(t) = (16,5,-7)

for t<0 => point before p0


for t>1 => point after p1

}10/25/21 }Sarajevo School of Science and Technology }20


Computing Viewing Rays
o For a line from the eye `e´ to a point `s´ on the
image plane
p(t) = e + t (s – e)
n We advance from point e along the vector (s-e) a fractional
distance t to find the point p

}e

}s
}s -e
} p(0) = e
} p(1) = s

}10/25/21 }Sarajevo School of Science and Technology }21


Simple ray tracing
E

R3 T2
LB
S5 S6
O2
LA
R1
O4
S1
S2
O1
S3 S4
T1

R2
}10/25/21 }Sarajevo School of Science and Technology }22
O3
Ray-Object Intersection
o Finds the closest object that the ray
intersects

}10/25/21 }Sarajevo School of Science and Technology }23


Ray-Object Intersection
o Finds the closest object that the ray intersects
n First find the plane it intersects
n Then check if the intersection point is within the polygon

}10/25/21 }Sarajevo School of Science and Technology }24


Ray definition
o Given the origin and direction vector for a ray:
Rorigin≡RO≡[XO YO ZO]
Rdirection≡Rd≡[Xd Yd Zd]

where X2d + Y2d + Z2d = 1 (normalised)


n Ray direction should be normalised, otherwise t will
represent the distance in terms of the length of the
direction vector

o The ray may be defined as a set of points on:


R(t) = RO + Rd * t, where t is the distance and t > 0

}10/25/21 }Sarajevo School of Science and Technology }25


Plane definition
o Plane is defined in terms of [A B C D]:
Plane ≡A * x + B * y + C * z + D = 0

o with the unit vector normal to the plane:


Pnormal≡Pn≡[A B C]

o D is the distance from the coordinate system origin


[0 0 0] to the plane
n The sign of D determines on which side of the plane the
system origin is located

}10/25/21 }Sarajevo School of Science and Technology }26


Ray-plane distance
o The distance from the ray’s origin to the intersection
with the plane P is
A*(XO + Xd * t) + B*(YO + Yd * t) + C*(ZO + Zd * t) + D = 0

o To find the distance we need to solve the


equation above for t:

}10/25/21 }Sarajevo School of Science and Technology }27


Ray-plane intersection
o Let:
vd = Pn · Rd = A * Xd + B * Yd + C * Zd
o If vd = 0 then no intersection occurs i.e. ray is parallel to the plane
o If vd > 0 then the normal of the plane is pointing away from the ray
o If vd < 0 then the normal is pointing back towards the ray origin

o Let:
vO = -(Pn · RO + D) = -(A * XO + B * YO + C * ZO + D)

o Then t = vO / vd
o If t < 0 then the line defined by the ray intersects the plane behind the ray’s
origin no actual intersection occurs
o Else the intersection point is:

ri = [xi yi zi] = [XO + Xd * t YO + Yd * t ZO + Zd * t]

}10/25/21 }Sarajevo School of Science and Technology }28


Example:
ray-plane intersection
o Given
n The plane [1 0 0 -7]
n A ray with an origin of [2 3 4]
n A direction of [0.577 0.577 0.577]
o Find the intersection with a two-sided plane

n Compute vd :
vd = Pn · Rd = A * Xd + B * Yd + C * Zd =
1 * 0.577 + 0 * 0.577 + 0 * 0.577 = 0.577
o In our case vd > 0 => The normal of the plane points away from
the ray
n we have a two-sided plane

}10/25/21 }Sarajevo School of Science and Technology }29


Example:
ray-plane intersection
o Compute vo and t and compare t to 0:
vo = -(Pn · RO + D) = -(A * XO + B * YO + C * ZO + D) =
-(1 * 2 + 0 * 3 + 0 * 4 + (-7)) = 5

t = 5 / 0.577 = 8.66
o t>0 the point is not behind the ray

o Compute the intersection point:


xi = 2 + 0.577 * 8.66 = 7
yi = 3 + 0.577 * 8.66 = 8 => Ri = [7 8 9]
zi = 4 + 0.577 * 8.66 = 9

}10/25/21 }Sarajevo School of Science and Technology }30


Polygon Intersection
o Calculate the ray-plane intersection
o Determine if the intersection point is inside the polygon
n Project the point and polygon vertices to the 2D plane
n Trace the rays on both sides of the point
n Count the number of intersections with the polygon
o Even – point is outside the polygon
o Odd – point is inside the polygon

}10/25/21 }Sarajevo School of Science and Technology }31


Jordan Curve Method
o Jordan Curve Method
n Given a polygon:
o Set of points Gn=[Xn Yn Zn] n=0,1,2…N-1
n The plane is defined as:
o Plane ≡A * x + B * y + C * z + D = 0
n The normal of the plane and the intersection
point:
o Pnormal≡Pn≡[A B C] Ri = [xi yi zi]

}10/25/21 }Sarajevo School of Science and Technology }32


Jordan Curve Method
(cont.)
o Project the polygon onto the plane
o All points on this plane are specified by (U, V) pair
n We have a (U, V) pair for each (X,Y,Z) coordinate
o We can do this by rotating the matrix, but it is more
efficient to eliminate one of the (X,Y,Z) coordinates
n We choose the one whose corresponding plane equation
value has the greatest magnitude
o Project the polygon onto the plane defined by the two
chosen coordinates
o Preserve the topology of the polygon, but not the area

}10/25/21 }Sarajevo School of Science and Technology }33


Test for Point Inside the
Polygon
o Translate the projected polygon so that the
intersection point is at the origin
n Subtract the intersection point’s coordinates (Ui, Vi) from
each vertex
o Trace an imaginary ray along one axis and test each
edge of the polygon against the ray
o Count whether the ray crosses the edge
o If the total number of crossing is odd the
intersection point is inside the polygon
o If the total number of crossing is even the
intersection point is outside the polygon
}10/25/21 }Sarajevo School of Science and Technology }34
Algorithm for Point in
Polygon
o Set the number of crossings NC to zero
o For each edge of the polygon, formed by
points (U‘a , V‘a) and (U‘b , V‘b)
n Set the sign holder SH as a function of the edge V‘a
o Set to -1 if V‘a is negative
o Set to +1 if V‘a is positive
n Set the next sign holder NSH as a function of the
edge V‘b
o Set to -1 if V‘b is negative
o Set to +1 if V‘b is positive

}10/25/21 }Sarajevo School of Science and Technology }35


Algorithm for Point in
Polygon
o If SH is not equal to NSH
n If both U‘a and U‘b are positive
o The line must cross +U‘
o Therefore, NC = NC+1
n Else if they are of different sign (one positive and one negative)
o Check if U‘a - V‘a · (U‘b - U‘a) / (V‘b - V‘a) > 0
n If so, the line is crossing +U‘
n Therefore, NC = NC+1
o Proceed with the next edge

o If NC is odd, the intersection point is inside the polygon

}10/25/21 }Sarajevo School of Science and Technology }36


}10/25/21 }37
Challenge:
o Given
n a triangle
G0=(-3 -3 7) G1=(3 -4 3) G2=(4 -5 4)
n And the intersection point
Ri=(-2 -2 4)
o Does the point lie within the triangle?
n The plane equation is P=(1 2 1 2)
n The dominant coordinate is Y in the plane equation
o We discharge the Y coordinates:
Guv0=(-3 7) Guv1=(3 3) Guv2=(4 4) Ruvi=(-2 4)

}10/25/21 }Sarajevo School of Science and Technology }38


Challenge (cont.)
o Translating so the intersection point is the
origin:
G’uv0 = Guv0 - Ruvi
G’uv1 = Guv1 - Ruvi
G’uv2 = Guv2 - Ruvi

G’uv0=(-1 3) G’uv1=(5 -1) G’uv2=(6 0)


o The first edge is defined by the two
vertices
(U’aV’a)=(-1 3) (U’bV’b)=(5 -1)
}10/25/21 }Sarajevo School of Science and Technology }39
Challenge (cont.)
o Sign holder (SH) is set as a function Va’ value
of the first vertex of the first edge
n -1 if V’ is negative
n +1 otherwise
n In our case is +1 since V’a is positive=3
o Next sign holder (NSH) for the edges formed
by the points (U’aV’a) (U’bV’b)
n In our case is Vb’;
n It is negative NSH=-1

}10/25/21 }Sarajevo School of Science and Technology }40


Challenge (cont.)
o If SH and NSH do not match
n If U’a and U’b are both positive then
o The line must cross +U’; this means NC=NC+1
n Else, if either U’a or U’b is positive then
o The line might cross, so we need to compute the
intersection on U’ axis
o If >0 then the line must cross

+U’: this means NC=NC+1

}10/25/21 }Sarajevo School of Science and Technology }41


Challenge (cont.)
o In our case:

=-1-3*(5-(-1))/(-1-3))=3.5>0
o This means the line must cross +U axis and
NC=NC+1=1

o Examine the next edge!


}10/25/21 }Sarajevo School of Science and Technology }42
Challenge (cont.)
o The second edge is defined by:
(U’aV’a ) = (5-1) and
(U’bV’b ) = (6 0)
a crossing takes place: NC = NC +1
o The third edge is defined by:
(U’aV’a ) = (6 0) and
(U’bV’b ) = (-1 3)
no crossing takes place
o NC = 2 ie even the intersection point is OUTSIDE
the polygon
}10/25/21 }Sarajevo School of Science and Technology }43
The Basic Ray-Tracing
Algorithm
o Computes one pixel at a time
o Finds the object that is seen at that pixel in the
image
n Ray generation
o Computes the origin and direction of each ray through a pixel
n Ray intersection
o Finds the closest object that
the ray intersects
n Shading
o Computes the pixel colour
n The calculation depends on
the rendering algorithm

}10/25/21 }Sarajevo School of Science and Technology }44

You might also like