Professional Documents
Culture Documents
cs405 08 Shading Texture
cs405 08 Shading Texture
lighting
Shading
Shading
Illumination Model
Global Illumination
object 4
object 3 object 2
object 1
Simple Local Illumination
object 4
object 3 object 2
object 1
Ambient Light Example
Ambient Light Calculation
Ambient = Ia x Ka
Diffuse Light Contribution
N
L
cos() = N.L
Specular light contribution
See no specular
Specular light example
Specular light calculation
n = 10 n = 90
n = 30 n = 270
Put it all together
Properties:
Colors / Position and type / attenuation
glLightfv(light, property, value)
1 2 3
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
mat_amb_diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_speacular);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
Global light properties
glLightModelfv(property, value)
Enable two sided lighting
property = GL_LIGHT_MODEL_TWO_SIDE
value = GL_TRUE (GL_FALSE if you don’t want two sided
lighting)
Global ambient color
Property = GL_LIGHT_MODEL_AMBIENT
Value = (red, green, blue, 1.0);
Check the red book for others
Surface Normals
v1, m1
Display
Polygon shading model
perceived intensity
n n2
n1
n3 n4
n = (n1 + n2 + n3 + n4) / 4.0
Gouraud Shading
C1
for all scanlines
Ca = lerp(C1, C2) Cb = lerp(C1, C3)
C2 C3
* lerp: linear interpolation
Lerp(Ca, Cb)
Gouraud Shading
Linear interpolation
x= a / (a+b) * v1 + b/(a+b) * v2
a b
v1 x v2
Interpolate triangle color: use y distance to interpolate
the two end points in the scanline, and use x distance
to interpolate interior pixel colors
Gouraud Shading Problem
Normal interpolation
n1
lerp(na, nb)
n2
n3
Slow – not supported by OpenGL and most graphics
hardware
Texture Mapping
1. projection
3. patch texel
2. texture lookup
3D geometry 2D projection of 3D geometry
t
2D image
S
Texture Representation
(1,1)
How about coordinates that are not
exactly at the intersection (pixel) positions?
A) Nearest neighbor
B) Linear Interpolation
C) Other filters
(1,0) (1,1)
(0,0) (1,0)
Map textures to surfaces
texture
polygon projection texture polygon projection
Magnification Minification
OpenGL texture filtering:
glTexParameteri(GL_TEXTURE_2D, glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST); GL_TEXTURE_MIN_FILTER,
GL_LINEAR)
Or GL_TEXTURE_MAX_FILTER
Texture color blending
E.g: glTexEnvf(GL_TEXTURE_ENV,
GL_TEXTURE_ENV_MODE, GL_REPLACE);
Enable (Disable) Textures
glBegin(GL_QUADS);
glTexCoord2D(0,0);
glVertex3f(-0.5, 0, 0.5);
…
glEnd();
Transform texture coordinates
…
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
…
glEnable(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB,
GL_UNSIGNED_BYTE, mytexture);