CGIllumination Rendering Texture
CGIllumination Rendering Texture
ka I a
Ambient Light
k d I l cos
Combining Ambient and Diffuse
k a I a k d I l (N L), if N L 0
I l ,diff
ka I a , if N L 0
Psource Psurface
L
Psource Psurface
Combining Ambient and Diffuse
k s I l ( V R ) n s , if V R 0 and N L 0
I l , spec
0.0, if V R 0 and N L 0
Specular Reflection
k s k sR , k sG , k sB
RGB Colour Considerations
• Each component of the surface colour is then
calculated with a separate expression
• For example:
I lR,diff k dR I lR ( N L)
I lG,diff k dG I lG ( N L)
I lB,diff k dB I lB ( N L)
Lighting in OpenGL
Create/Enable lights
• To use lighting in OpenGL, first you must enable
lighting, through a call to glEnable(GL _LIGHTING).
• OpenGL allows the user to create at least 8 light
sources, named GL LIGHT0 through GL LIGHT7.
• The properties of each light source is set by the
command glLight*(name, property, propertyValue)
• This command takes three arguments,
– the name of the light,
– the property of the light to set, and
– the value of this property.
Create/Enable lights
• Let us consider a light source 0, whose
position is (2.0, 4.0, 5.0, 1.0) in homogenous
coordinate, and has
– Red ambient intensity : (0.9, 0.0, 0.0)
– White diffuse and specular intensities, given as the
RGB triple (1.0, 1.0,1.0).
Create/Enable lights
GLfloat ambIntensity[] = {0.9, 0.0, 0.0, 1.0};
GLfloat diffIntensity[] = {1.0, 1.0, 1.0, 1.0};
GLfloat speIntensity[] = {1.0, 1.0, 1.0, 1.0};
GLfloat position[] = {2.0, 4.0, 5.0, 1.0};
• Ray Tracing
• Radiosity
Ray Casting
• Simplest Shading Approach
– Perform independent lighting calculation for every
pixel
I I E K A I AL i ( K D N Li I i K S V R i I i )
n
Polygon Shading
• Can Take Advantage of Spatial Coherence
– Illumination calculations for pixels covered by
same primitive are related to each other
I I E K A I AL i ( K D N Li I i K S V R i I i )
n
Polygon Shading Algorithms
• Flat Shading
• Gouraud Shading
• Phong Shading
Flat Shading
• Illuminated only by directional light sources
– Diffuse or viewed from infinitely far away
Flat Shading
• One Illumination Calculation per Polygon
– Assign all pixels inside each polygon the same
color
Flat Shading
• Objects Look Like They are Composed of
Polygons
– Not so good for ones with smooth surfaces
Gouraud Shading
• Smooth Surface
– Represented by polygonal mesh with a normal at
each vertex
Gouraud Shading
• One Lighting Calculation per Vertex
– Assign pixels inside polygon by interpolating colors
computed at vertices N1
Viewer V1 L1 Light
N2
N3
Polygon
Gouraud Shading
• Bilinearly Interpolate Colors at Vertices Down
and Across Scan Lines
A I1 1 I 2 B I1 1 I 3
I1
I 1 A B
I2
I3
Gouraud Shading
• Produces Smoothly Shaded Polygonal Mesh
– Piecewise linear approximation
– Need fine mesh to capture subtle lighting effects
N2 N3
Polygon
Phong Shading
• One Lighting Calculation per Pixel
– Approximate surface normals for points inside polygons by
bilinear interpolation of normals from vertices
N1
Viewer Light
N
V L
N2
N3
Polygon
Phong Shading
• Bilinearly Interpolate Normals at Vertices
Down and Across Scan Lines
A N1 1 N 2 N1 B N1 1 N 3
α β
γ
N2
N3
N 1 A B
Phong Shading
Flat
Gouraud Phong
Texture Mapping
What is texture mapping?
• Application of textures to surfaces of 3d
objects to produce photo-realistic images.
• glTexImage2D(GL_TEXTURE_2D, level,
internalFormat, width, height, border, format,
type, image);
– level: specifies level resolution (if 0 it is the highest)
– internalFormat/format: specifies format of the data
eg. GL_RGB
– type: type of each element such as: GL_UNSIGNED_BYTE
– Width and height must be a power of 2.
What if the texture is the wrong size?
• glTexImage2D() requires a texture whose pixel
dimensions are powers of two
• If necessary, use the OpenGL utility routine
gluScaleImage() to scale the image
• Pass the scaled image to glTexImage2D()
Scaling texture images
glBegin( GL_QUADS );
glTexCoord2fv( t0 );
glVertex3fv( v0 );
glTexCoord2fv( t1 );
glVertex3fv( v1 );
glTexCoord2fv( t2 );
glVertex3fv( v2 );
glTexCoord2fv( t3 );
glVertex3fv( v3 );
glEnd();
Which Texel Goes With Which Pixel?