编程辅导 COSC363: The ray tracer iteratively traverses the pixel grid, drawing each

R. Mukundan Department of Computer Science and Software Engineering University of Canterbury, .

Local Illumination Model

Copyright By PowCoder代写 加微信 powcoder

 A local illumination model considers only light travelling from a light source to a surface and then reflected off the surface to the eye.
 Requires only the light source coordinates, local surface geometry and the material characteristics at a vertex.
 Suitable for the hardware pipeline (OpenGL, Direct3D)
 Does not consider occlusions and transmittance of light.
R. Mukundan, CSSE, University of Canterbury

Global Illumination
 The illumination at a given point is a combination of the amount of light received from a source and the light reflected from other surfaces in the scene.
• The point P on the sphere is in shadow
• If the sphere is reflective, the viewer will see a part of the red sphere at Q. • If the sphere transmits light (e.g. a glass sphere or a hollow sphere) , the
viewer will see a part of the blue cylinder at R.
R. Mukundan, CSSE, University of Canterbury

Students’ Work!
R. Mukundan, CSSE, University of Canterbury

(Backward )
 Uses a two-dimensional grid of pixels and computes the colour value of each pixel using rays generated from the eye position through the centres of each pixel.
 Generates a 2D image, pixel by pixel. Ray tracing is an image synthesis algorithm.
COSC363: The ray tracer iteratively traverses the pixel grid, drawing each “pixel” (quad) using the computed colour value. OpenGL is used only for drawing the quads.
R. Mukundan, CSSE, University of Canterbury
Pixel grid

(Backward )
 Rays generated from the eye position towards each pixel are called primary rays.
 Along each ray, we compute the closest point of intersection with objects in the scene.
 If the ray does not intersect any object, the pixel is assigned the background colour.
 If the closest point of intersection exists, secondary rays may be generated from this point to model shadows, reflections and refractions.
 Colour contributions from secondary rays are combined and returned by each ray
Secondary rays
R. Mukundan, CSSE, University of Canterbury
Primary rays
Pixel grid

 Trace a primary ray from the view point through each “pixel” on the image plane
 Test each surface to determine if it is intersected by the ray.
 Compute the closest point of intersection Q on each primary ray.
 Use the material colour of the object on which Q lies as pixel colour.
 A ray casting method does not use secondary rays.
R. Mukundan, CSSE, University of Canterbury

What is a “ray”?
A ray is specified using
 A point (the source of the ray): p0 = (x0, y0, z0)
 A unit vector (the direction of the ray): d = (l, m, n)
Any point on the ray can be represented using a single parameter t. The value of t denotes the distance from the source to that point.
Ray parameter
Ray’s Equation
direction source
p = p0 + t d d = (l, m, n)
p0 = (x0, y0, z0)
glm::vec3 p0(x0, y0, z0); glm::vec3 dir(l, m, n); Ray ray = Ray(p0, dir);
R. Mukundan, CSSE, University of Canterbury

and Function
ray.closestPt(sceneObjects); Finds the closest point of intersection with objects in the scene.
ray.p0: Source point of the ray (glm::vec3) ray.dir: Direction of the ray (glm::vec3)
ray.hit: Closest point of intersection (glm::vec3)
ray.index: Index of the object on which the closest point of
intersection was found (int)
ray.dist: Distance to the closest point of intersection
p ray.hit R. Mukundan, CSSE, University of Canterbury
p0 COSC363

EDIST = Eye distance in world units. XMIN, XMAX, YMIN, YMAX = min, max values of the display window
NUMDIV = Number of subdivisions along x, y directions.
Eye (0,0,0) z
cellX cellY
R. Mukundan, CSSE, University of Canterbury
cellX = cell width = (XMAX-XMIN)/NUMDIV cellY = cell height = (YMAX-YMIN)/NUMDIV cell indices: i, j = 0, …, NUMDIV-1.

Cell (i, j) cellX
( xp = XMIN + (i+0.5) * cellX,
yp = YMIN + (j+0.5) * cellY, zp = −EDIST )
 Ray position (source): (0, 0, 0)  Ray direction d: ( xp, yp, zp)
for(int i = 0; i < NUMDIV; i++) xp = XMIN + i * cellX; for(int j = 0; j < NUMDIV; j++) Primary Ray yp = YMIN + j * cellY; glm::vec3 dir(xp+0.5*cellX, yp+0.5*cellY, -EDIST); Ray ray = Ray(eye, dir); R. Mukundan, CSSE, University of Canterbury  A ray originating on a surface will output the source point itself as the closest point of intersection with objects in the scene. The source of the ray is therefore offset by a small distance along the direction of the ray. p0 p0 COSC363 R. Mukundan, CSSE, University of Canterbury The Ray Class class Ray { glm::vec3 p0 = glm::vec3(0); glm::vec3 dir = glm::vec3(0,0,-1); glm::vec3 hit = glm::vec3(0); int index = -1; float dist = 0; Ray(glm::vec3 source, glm::vec3 direction) { const float RSTEP = 0.005f; p0 = source; dir = glm::normalize(direction); p0 = p0 + RSTEP * dir; R. Mukundan, CSSE, University of Canterbury void closestPt(std::vector& sceneObjects);

 At the closest point of intersection Q on Q each primary ray, compute the colour value
using an illumination model.
 Simplified Phong Illumination: Assumptions:
Light’s ambient color A = (0.2, 0.2, 0.2) Light’s diffuse and specular color = (1,1,1) M = Material Color (ambient and diffuse) Material’s specular color = (1, 1, 1)
Col = A.M + (l•n) M + Ambient Diffuse
(r•v)f (1,1,1) Specular
R. Mukundan, CSSE, University of Canterbury

Shadow Ray L
 Trace a ray from the point of intersection Q towards the light source L (2)
 If the shadow ray hits an object, and if the point of intersection R is between the light source and the object A (i.e., RQ < LQ), then the point Q is in shadow. If Q is in shadow IA = A.M Else IA =A.M+ M(l•n) + (1,1,1)(r•v)f R. Mukundan, CSSE, University of Canterbury Reflections  If the surface is reflective, then a secondary ray along the direction of reflection is traced.  If this secondary ray meets a surface at a point with intensity IB , then r IB is added to the pixel color  r is a scale factor (< 1), called the coefficent of reflection. It represents how much of colour IB is reflected on the surface A.  The colour of the pixel is: I = IA + r IB  = 0.2 R. Mukundan, CSSE, University of Canterbury Transmission of Light  If the surface is transparent or refractive, a secondary ray along the direction of transmission of light is traced.  If this secondary ray meets a surface at a point with intensity IC , then t IC is added to the pixel color.  t is a scale factor (<1), called the coeff. of refraction (for a solid object) or coeff. of transparency (for a hollow object)  The colour of the pixel is now I = IA + t Ic Hollow Transparency Refraction R. Mukundan, CSSE, University of Ray-tracing Tree IA + r(IB+r ID) +t(IC+r IE) A IC + r IE E IB + r ID ID • Left branches represent reflections • Right branches represent transmission paths R. Mukundan, CSSE, University of Canterbury Shadow ray Reflection ray Secondary Rays Shadow ray: A ray that originates at a point of intersection with an object, and directed towards a light source. Position = q (point of intersection) Direction = L− q normalized. Incident ray Reflection ray: A ray from the intersection point towards the direction of reflection of the incident ray (Used only for reflective surfaces) Position = q Direction: r = − 2(d•n)n + d normalized. (We will use a GLM function to compute r) R. Mukundan, CSSE, University of Canterbury Reflections Computation of reflected ray using GLM: r = glm::reflect(d, n); if(obj->isReflective() && step < MAX_STEPS) { rho = obj->getReflectionCoeff();
glm::vec3 normalVec = obj->normal(ray.hit);
glm::vec3 reflDir = glm::reflect(ray.dir, normalVec); Ray reflectedRay(ray.hit, reflDir);
glm::vec3 reflColor = trace(reflectedRay, step+1); color = color + (rho * reflColor);
R. Mukundan, CSSE, University of Canterbury

Index of Refraction
 Light travels at speed c/ in a medium with index of refraction .
 Common values of index of refraction: Air 1.
Water 1.33 Glass 1.5 Diamond 2.4
 Snell’s Law of Refraction:  1 sin1 = 2 sin2
R. Mukundan, CSSE, University of Canterbury

Refracted Ray
usini −ncosi =d usint −ncost =g
 g= d− (d.n)+costn
 2  1
() cost = 1−  1−(d.n)2 

Sphere Refraction
refrRay.dir
Incident ray
refrRay.hit
Incident ray
n = obj->normal(ray.hit);
g = glm::refract(d, n, eta);
Ray refrRay(ray.hit, g) refrRay.closestPt(sceneObjects); m = obj->normal(refrRay.hit);
h = glm::refract(g, −m, 1.0f/eta);
2 //eta = 1/2
R. Mukundan, CSSE, University of Canterbury

GLM Functions for
 Reflection:
r = glm::reflect(d, n); d d: Unit incident vector
n: Unit normal vector
The reflection vector also is a unit vector.
 Refraction:
g = glm::refract(d, n, eta);
d: Unit incident vector
n: Unit normal vector
The refraction vector also is a unit vector. eta = Ratio of refractive indices = 1/2
R. Mukundan, CSSE, University of Canterbury

R. Mukundan, CSSE, University of Canterbury
Sphere Refraction
eta = 1/1.5

R. Mukundan, CSSE, University of Canterbury
Sphere Refractions
eta = 1/1.01
eta = 1/1.003

Transparency and Refractions
 = 1.01 Better
The spheres are also reflective
With anti-aliasing
R. Mukundan, CSSE, University of Canterbury

Note: A concentration of light following refraction through a surface is known as a caustic. Caustics cannot be generated using simple shadow rays.
R. Mukundan, CSSE, University of Canterbury

Ray-plane intersection
 Given a point a on a plane, and the normal vector n of the plane, the plane’s equation can be written as
(p−a)•n =0
 A ray is given by the equation
 At the point of intersection, both equations are true.
Therefore, (p0 + t d − a)• n = 0  From the above equation, we get
t = (a − p0 ) • n d•n
R. Mukundan, CSSE, University of Canterbury

Ray-sphere intersection
 Equation of a sphere centred at C with radius r is ( p − C ) • ( p − C ) = r2
 Consider a ray given by the equation p=p0 +td.
 At the point of intersection, both equations are true. Therefore, ( p0 + t d − C ) • ( p0 + t d − C ) = r2
( s + t d ) • ( s + t d ) = r2, where s = p0 − C (d•d)t2 +2(s•d)t+(s•s)−r2 =0.
Since d is a unit vector, we get
t=−(s•d) (s•d)2 −(s•s)+r2
R. Mukundan, CSSE, University of Canterbury

Ray intersection with polygonal objects
 Use the plane equation of each triangle to get the point of intersection q with the ray.
 Check if the point of intersection q lies within the triangle. This is done using a simple point inclusion test.
Point inclusion test : B
Intersection : (A− p )n t=0
If the cross products
(uAvA).n, (uBvB).n, (uCvC).n have the same sign, then the point q is inside the triangle.
R. Mukundan, CSSE, University of Canterbury
dn q = p0 + t d

Ray intersection with polygonal objects
 Complex polygonal objects will require a large amount of ray-triangle intersection tests.
 Bounding volume hierarchies and spatial subdivision methods (kd-Trees, Octrees) are used to reduce the number of ray-primitive intersection tests.
Stanford Bunny
69451 Polygons!
Bounding Sphere
R. Mukundan, CSSE, University of Canterbury
360,000 Rays!

Texture Mapping
 2D texturing:
 Similar to OpenGL texturing, but does not use texture
coordinates or texture memory.
 Map the coordinates of the point of intersection (x, y, z) to image coordinates, and assign the colour of the pixel to that point.
(x, y, z) q
 Procedural texturing:
 Define functions to map (x, y, z) coordinates to (r, g, b) colour
values. r= r(x,y,z); g= g(x,y,z); b= b(x,y,z);
R. Mukundan, CSSE, University of Canterbury

Texture Mapping
#include “Ray.h” #include “TextureBMP.h” using namespace std; …
TextureBMP texture; …
void initialize()
texture = TextureBMP(“myPicture.bmp”);
glm::vec3 trace(Ray ray, int step)
col = texture.getColorAt(s, t);
TextureBMP.h
TextureBMP.cpp
R. Mukundan, CSSE, University of Canterbury
Note: 0s1 0t1

Texturing a Plane
(a1, b2, z)
(a1, b1, z)
(a2, b1, z)
if(ray.index == 3) //Assuming the plane to be textured has index 3
texcoords = (ray.hit.x – a1)/(a2-a1); texcoordt = (ray.hit.y – b1)/(b2-b1);
col = texture.getColorAt(texcoords, texcoordt);
R. Mukundan, CSSE, University of Canterbury

Anti-Aliasing
The ray tracing algorithm samples the light field using a finite set of rays generated through a discretized image space. This results in distortion artefacts such as jaggedness along edges of polygons and shadows.
Supersampling: Generateseveralraysthrougheachsquare pixel (eg. divide the pixel into four equal segments) and compute the average of the colour values.
R. Mukundan, CSSE, University of Canterbury

Anti-Aliasing
 Adaptive Sampling: As shown on the previous slide, each pixel is divided into four “sub-pixels”. Primary rays are generated through the centres of each sub-pixel. If the colour value along any ray varies significantly from the other three, that sub-pixel is split further into four sub- pixels, and more rays are generated through them.
R. Mukundan, CSSE, University of Canterbury

Anti-Aliasing Example
R. Mukundan, CSSE, University of Canterbury

 A cylinder at the origin with axis along the y- axis, radius R and height h is given by
x2 +z2 =R2 0yh
 Normal vector at (x, y, z) (un-normalized) n = (x, 0, z)
Normalized n = (x/R, 0, z/R)
R. Mukundan, CSSE, University of Canterbury

Ray – Cylinder Intersection
 A cylinder at (xc, yc, zc), with axis parallel to the y-axis, radius R and height h is given by
(x−xc)2 +(z−zc)2 =R2 0  (y− yc)  h
(x, y, z) d
(normalized)n=((x−xc)/R, 0, (z−zc )/R) Ray equation:
x=x0 +dx t; y=y0 +dy t; z=z0 +dz t;  Intersection equation:
t2(d2 +d2)+2td (x −x )+d (z −z ) xzx0cz0c
 Normal vector at (x, y, z) (un-normalized) n = (x − xc, 0, z − zc)
(xc, yc, zc)
+ ( x − x ) + ( z − z ) − R  = 0 .
R. Mukundan, CSSE, University of Canterbury

t2(d2 +d2)+2td (x −x )+d (z −z ) xzx0cz0c
+ ( x − x ) + ( z − z ) − R  = 0 .
0c0c Using only the above equation to
find the closest point of intersection gives cylinders of infinite height.
p = p0 + t d t1
R. Mukundan, CSSE, University of Canterbury
(xc, yc, zc) 41

Broken Cylinders
Should the closest point of intersection with the cylinder be discarded if it has a y-coordinate value greater than yc+h ?
p = p0 + t d t1 yc + h
R. Mukundan, CSSE, University of Canterbury
(xc, yc, zc)

If the closest point of intersection is outside the cylinder, we should check if the second point of intersection is within the cylinder, and if so, return that value.
p = p0 + t d
(xc, yc, zc)
R. Mukundan, CSSE, University of Canterbury

Cylinders with reflections
Back facing surfaces are also reflective
Compute reflected colour only for front facing surfaces
A surface with normal vector n is front facing with respect to a ray if dot(ray.dir, n) < 0 R. Mukundan, CSSE, University of Canterbury Cylinders with cap If the closest point of intersection is above the cylinder and the next point of intersection within the cylinder, we could compute the point on the ray which lies on the cap and return the corresponding t value. p = p0 + t d yc + h R. Mukundan, CSSE, University of Canterbury (xc, yc, zc)  Consider a cone with centre of base at (xc, yc, zc), axis parallel to the y-axis, radius R, and height h:  Important equations: tan() = R/h ( = half cone angle) For any point (x, y, z) on the cone, (x−xc)2 +(z−zc)2 =r2 (xc, yc, zc)  r =  R (h − y + y )  h  c  Surface normal vector (normalized): n = ( sin cos , sin, cos cos ) x−x  where  = tan−1 c  COSC363 R. Mukundan, CSSE, University of Canterbury  Equation of a cone with base at (xc, yc, zc), axis parallel to the y-axis, radius R, and height h: (x−xc) +(z−zc) =h (h−y+yc)  Ray equation: p0 d x=x0 +dx t; y=y0 +dy t; z=z0 +dz t;  The points of intersection are obtained by substituting the ray equation in the cone’s equation and solving for t. (xc, yc, zc) R. Mukundan, CSSE, University of Canterbury 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com