程序代写 COSC363

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

Ambient Reflections

Copyright By PowCoder代写 加微信 powcoder

 Ambient reflections correspond to shadow regions
 Since it is a constant, low intensity illumination, it gives a flat appearance of objects.
Material Color (0, 1, 0)
Ambient Reflection (0, 0.2, 0)
La: Light’s ambient color. Usually, (0.2, 0.2, 0.2).
Ma: Material’s ambient color. Same as material color.
float gray[3] = {0.2, 0.2, 0.2}; glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
(0.2, 0.2, 0.2) * (0, 1, 0)
Ambient reflection Ia = La *Ma
float green[3] = {0, 1, 0}; glMaterialfv(GL_FRONT, GL_AMBIENT, green);
R. Mukundan, CSSE, University of Canterbury

Diffuse Reflections
 The brightness of diffuse reflections from a surface varies
with the cosine of the angle (cos) between the light source
vector and the normal vector.
Note: l, n must be normalized to unit vectors.
Light source
Ld: Light’s diffuse color. Usually, (1, 1, 1).
Md: Material’s diffuse color. Same as material color.
Diffuse reflection Id = Ld*Md max(l•n , 0)
cos  = l.n
float white[3] = {1, 1, 1}; glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
float green[3] = {0, 1, 0}; glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
R. Mukundan, CSSE, University of Canterbury

Specular Reflections
Consider a highly polished (mirror-like) surface S. In the following figure, l is the light source vector and n the normal vector.
A viewer along the direction of reflection r, where the angle of reflection is the same as the angle of incidence () of light, will observe maximum specular reflection from S.
The intensity of specular highlight reduces as the viewer moves away from r.
Light source
R. Mukundan, CSSE, University of Canterbury

Specular Reflections
 The brightness of specular reflections from a surface varies
with the cosine of the angle between the reflection vector
and the view vector.
Direction of max. specular reflection
Light source
Light’s specular color. Usually, (1, 1, 1).
Ms: Material’s specular color.
Usually, (1, 1, 1).
Note: r, v are unit vectors.
float white[3] = {1, 1, 1}; glLightfv(GL_LIGHT0, GL_SPECULAR, white);
Specular reflection Is = Ls*Ms max(r • v , 0)
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
R. Mukundan, CSSE, University of Canterbury

Specular Reflections
An exponent f known as the Phong’s constant (or shininess term) is used to control the diameter of the specular highlight. Large values of the exponent f gives highly concentrated specular highlights.
Is = Ls*Ms {max(r•v , 0)}f
R. Mukundan, CSSE, University of Canterbury

Illumination Model
Vertex Colour
VC = La*Ma + Ld*Md max(l•n , 0) + Ls*Ms{max(r•v , 0)}f Ambient Diffuse Specular
Term Term Term
R. Mukundan, CSSE, University of Canterbury

Computation of Reflection Vector
The computation of the specular component of lighting requires the reflection vector r. It has the following properties:
 Vectors l and r make equal angles with the normal vector n  Vectors l, r and n are on the same plane.
Let k be the length of projection of the unit vector l on vector n.
k=cos =l•n
The projection of r on the unit vector n also has the same length k.
Therefore,
r = 2 (l•n) n − l

Half-way Vector
Consider the vector h = (l + v) normalized. This vector is called the “half-way vector” Let  be the angle between h and n.
We observe the following facts:
When v is moved away from r, the vector h moves away from n.
i.e.,  increases with  .
When  becomes 0,  also becomes 0.
R. Mukundan, CSSE, University of Canterbury
 varies with  , and could be used as a substitute for .

Phong-Blinn Model
 OpenGL uses an approximation of (r•v) by the term (h•n) in the computation of specular reflections, where h is the Half-way Vector, computed as h = (l + v) normalized.
 We can now rewrite the formula for specular reflection: Is = LsMs {max(h•n), 0}f
 The lighting equation with the above approximation is called the Phong-Blinn model.
 The advantages of using the half-way vector:
Easier to compute ‘h’ compared to ‘r’.
If l is a directional source, and the view direction is constant (viewer at infinity), then h needs to be computed only once for the whole scene.
R. Mukundan, CSSE, University of Canterbury

Illumination Model: Phong-Blinn Equation
Vertex Colour
VC = La*Ma + Ld*Md max(l•n , 0) + Ls*Ms{max(h•n , 0)}f
Specular Term
R. Mukundan, CSSE, University of Canterbury

Emissivity
 The emissive property of a material is used to simulate light emitted by the surface. This is a constant emissive intensity that is added to the lighting equation. Unlike other terms in the equation, the emissive term is not modulated with incoming light.
+ (0.4, 0.4, 0.4) =
Emissive Color = Me
float emission[3] = {0.4, 0.4, 0.4}; glMaterialfv(GL_FRONT, GL_EMISSION, emission);
R. Mukundan, CSSE, University of Canterbury

Illumination Model: Phong-Blinn Equation
Vertex Colour
VC = La*Ma + Ld*Md max(l•n , 0) + Ls*Ms{max(h•n , 0)}f + Me
Ambient Term
Diffuse Specular Term Term
Emissivity Term
R. Mukundan, CSSE, University of Canterbury

Camera View and Projection
 Camera View
 Depends on the camera’s position and orientation
 Specified by a view transformation matrix V generated by the function gluLookAt(ex, ey, ez, lx, ly, lz, ux, uy, uz);
 Note: gluLookAt() represents a view matrix, not just the camera’s position.
 Camera Projection
 Depends on the field of view and focal length.
 Specified by a projection matrix P generated by
glFrustum(L, R, B, T, N, F);
or, gluPerspective(fov, ar, N, F)
R. Mukundan, CSSE, University of Canterbury

Camera/Eye Position
(e , e , e ) x y z
OpenGL “Camera”
Look-At Point
View axis (-Ze)
(l , l , l ) xyz
gluLookAt(ex,ey,ez, lx,ly,lz, 0,1,0);
glFrustum(L, R, B, T, N, F);
R. Mukundan, CSSE, University of GL “Camera”
gluLookAt(14,5,0, 0,0,0, 0,1,0);
Default Camera
gluLookAt(0,0,0, 0,0,-1, 0,1,0);
R. Mukundan, CSSE, University of Canterbury

View Transformation
The view transformation matrix generated by the function gluLookAt(ex,ey,ez,lx,ly,lz,0,1,0) is given below:
Eye xu u u −eu−eu−eux up
Coordinates
ex y z xx yy zz YZ y vvv−ev−ev−evy ev e
e=xyzxxyyzz ze  wx wy wz −exwx −eywy −ezwz z
to the eye-coordinate space (see example on next slide) • Obtaining the view matrix:
1000 1 1 • u, v, w are unit vectors along camera axes X , Y , Z
respectively. • The view matrix transforms points from world-coordinate space
float mat[16];
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(ex, ey, ez, lx, ly, lz, 0.,1.,0.); glGetRF.lMouakutnvda(nG, CLS_SEM, OUnDivEeLrsVityIoEf WCa_nMteArbTurRyIX, mat);

View Transformation
View axis d
(e , e , e )
(l , l , l ) xyz
World Coordinates
Eye Coordinates
(e , e , e , 1 ) xyz
(0, 0, 0, 1)
(l , l , l , 1 ) xyz
(0, 0, −d, 1)
(u , u , u , 0 ) xyz
(1, 0, 0, 0)
(v , v , v , 0 ) xyz
(0, 1, 0, 0)
(w , w , w , 0 ) xyz
(0, 0, 1, 0)
R. Mukundan, CSSE, University of Canterbury

View Transformation Example
The camera is placed at (10, 10, 0), looking at the origin. The point P on the cube has coordinates (1, 1, 0). This point is along the view axis of the camera.
The view of the scene from the camera is given below. In this view, the point P can be seen along the view axis. Therefore, for this point, xe = 0, ye = 0. ze will have a negative value, giving the distance of the point P from E. Every point in the field of view of the camera will have a negative value for ze.
View Matrix:
The above matrix converts the coordinates of P from to
Camera View
(World coordinates)
(Eye coordinates)
R. Mukundan, CSSE, University of Canterbury
gluLookAt(10,10,0, 0,0,0, 0,1,0);

Camera Modes
A camera can be placed in a scene and transformed in different ways:
 Free-camera: The user can control the position and orientation of the camera irrespective of other object transformations in the scene.
 Fly-by camera: The camera transformed along a predefined path, usually without any user interaction.
 First Person View: The view of the scene from the primary object/character being controlled. In a game, it is the view from the player’s eye level.
 Third Person View: A view of the scene from a different perspective (eg. a general observer). This camera mode could either be a “free-camera” or dependent on other transformations.
R. Mukundan, CSSE, University of Canterbury

Creating First Person Views (Method 1)
Keep track of the object’s position and orientation in world coordinate space, and update the camera position and the look vector.
 Compute object’s eye position (e , e , e ) xyz
 Compute the “look at” point (l , l , l ) using the eye position xyz
and the object’s view direction
L (l , l , l ) E (e , e , e ) xyz xyz
R. Mukundan, CSSE, University of Canterbury

Creating First Person Views (Method 1)
void display()
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
… // compute camera parameters here … // by using character transformation
gluLookAt (ex,ey,ez, lx,ly,lz, 0,1,0); … //common transforms glPushMatrix();
//character transform
drawCharacter(); //user defined glPopMatrix();
glPushMatrix(); //Teapot transform glutSolidTeapot(1);
glPopMatrix();
R. Mukundan, CSSE, University of Canterbury

Creating First Person Views (Method 2)
 This method does not use gluLookAt(…) which requires the coordinates of the view position on the character.
 Instead, the enitre scene is inverse-transformed so that the character goes back to the origin, looking towards the –z axis. Here we assume that the character is initially created at the origin, facing –z direction.
R. Mukundan, CSSE, University of Canterbury

Creating First Person Views (Method 2)
void display()
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
… // Inverse of character transformation
… //common scene transforms
glPushMatrix();
//character transformation
drawCharacter(); //user defined glPopMatrix();
glPushMatrix(); //Teapot transform glutSolidTeapot(1);
glPopMatrix();
R. Mukundan, CSSE, University of Canterbury

Character Transformation Example
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
glRotatef(-theta, 0, 1, 0);
glTranslatef(-tx, -ty, -tz);
Inverse of
… //common scene transforms glPushMatrix();
glTranslatef(tx, ty, tz); glRotatef(theta, 0, 1, 0); drawCharacter(); //user defined
glPopMatrix();
… //other objects in the scene
If A and B are matrices, (AB)−1 = B−1 A−1 R. Mukundan, CSSE, University of Canterbury

View Volumes
 The view transformation only transforms the world coordinates of points into the camera’s coordinate frame.
 We need to specify “how much” the camera actually sees. That is, we require a view volume that contains the part of the scene that is visible to the camera. In other words, the view volume acts as a clipping volume.
 We further require a projection model to simulate the way one would “see” the 3D scene through the camera.
 The projection matrix is defined using the frustum parameters. It transforms eye coordinates to clip coordinates. The clip coordinates of a point will have values in the range [-1, +1] if the point is inside the view frustum.
R. Mukundan, CSSE, University of Canterbury

Perspective View Volume
 The perspective view volume is defined by a frustum that has its vertex at the eye position. The near-plane acts as the
plane of projection.
 OpenGL function: positive
glFrustum(left, right, bottom, top, near, far);
E.g: glFrustum(-10, 10, -8, 8, 10, 100);
ye = top xe = left
Both values are
R. Mukundan, CSSE, University of Canterbury
Near Plane
xe = right ye = bottom
View Volume

View Volume: Eye Coordinates
(L, T, −N)
Plane (0, 0, −N)
(R, B, −N)
(0, 0, −F)
R. Mukundan, CSSE, University of Canterbury
glFrustum(L, R, B, T, N, F);
Width of the near plane: w = R−L
Height of the near plane:

Perspective View
 The field of view of the view frustum is a useful parameter that can be conveniently adjusted to cover a region in front of the camera.
Near plane
Ze N Camera 
tan= h COSC363
 Field of view along the y-axis of the eye-coordinate space
h = T−B R. Mukundan, CSSE, University of Canterbury
glFrustum(L, R, B, T, N, F)

gluPerspective
 The GLU library provides another function for perspective transformation in the form
gluPerspective(fov, aspect, near, far);
 In this case, the view axis passes through the centre of the near plane.
AspectRatio a=w/h
(Note: w, h are not specified!)
R. Mukundan, CSSE, University of Canterbury

gluPerspective vs. glFrustum
 (, a, N, F) → (L,R,B,T,N,F):
h = 2N tan   2 
(Slide 21)
w = a. h L = −w/2 B = −h/2
R = w/2 Ze T = h/2,
Note: L+R=0, B+T=0
 (L,R,B,T,N,F) → (, a, N, F) :
w = R−L , h = T−B a = w/h
=2tan−1 h   2 N 
R. Mukundan, CSSE, University of Canterbury

The Canonical View Volume
 All view volumes are mapped to a canonical view volume which is an axis-aligned cube with sides at a distance of 1 unit from the centre.
 The canonical view volume facilitates clipping of the primitives with its sides.
 The coordinates of a point inside the canonical view volume are called clip coordinates.
 A point is visible only if it has clip coordinates between -1
e 1Zc Camera
−1 Clip Coordinate Axes
R. Mukundan, CSSE, University of Canterbury
X −1−1 1 e
Left handed system

glFrustum(L,R,B,T,N,F)
 The function glFrustum(…) transforms points inside the perspective view volume into points inside the
canonical view volume, where the coordinates have the range [-1, 1].
Clip coordinates
Eye coordinates
R. Mukundan, CSSE, University of Canterbury

View Volume: Clip Coordinates
(0, 0, +1)
Near Plane
(− 1, 1, −1)
R. Mukundan, CSSE, University of
(0, 0, −1)
(1, − 1, −1)
−1 Clip coordinates of a few points inside
the camera’s view volume

Clip Coordinates
Suppose a point has clip coordinates (xc, yc, zc).
 The zc value is called the point’s pseudo-depth.
It has a value between -1 and +1.
 The pseudo-depth is converted into a depth buffer value in the range [0, 1] using the equation
zdepth = (zc + 1)/2
 If the point passes the depth test, then its clip coordinates (xc, yc) are mapped to the display viewport.
ZC Pseudo Depth
𝑥𝑐 +1=𝑥𝑝 −𝑥𝑚𝑖𝑛 2𝑊
(xmin , ymin )
+1 Viewport
Viewport Transformation
Clip CoordinateR.sMukundan, CSSE, University of OSC363

An Overview of Transformations
Eye coordinates
Clip coordinates
Vertex Data
glOrtho(…)
glFrustum(..)
gluPerspective(..)
R. Mukundan, CSSE, University of Canterbury
Transformations in
World Space
glPushMatrix(…);
glTranslatef(…);
glPopMatrix();
Transformations to Camera Space
gluLookAt(…)
Pixel coordinates
Projection Transformation
Viewport Transformation
glViewport(…)

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com