














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















Computer Graphics Shading
n Define colors and position a light
GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0}; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_position[] = {0.0, 0.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position);
colors
Position What if I set Position to (0,0,1,0)?
n Illumination from a light: Illum = ambient + diffuse + specular = Ka x I + Kd x I x (cos θ ) + Ks x I x cos (φ) n If there are N lights Total illumination for a point P = Σ (Illum) n Sometimes light or surfaces are colored n Treat R,G and B components separately n i.e. can specify different RGB values for either light or material To: Illum_r = Kar x Ir + Kdr x Ir x (cos θ ) + Ksr x Ir x cos (φ) Illum_g = Kag x Ig + Kdg x Ig x (cos θ ) + Ksg x Ig x cos (φ) Illum_b = Kab x Ib + Kdb x Ib x (cos θ ) + Ksb x Ib x cos (φ)
n
n
n n
Illum = ambient + diffuse + specular = Ka x I + Kd x I x (cos θ ) + Ks x I x cos (φ) n (cos θ ) and cos (φ) are calculated as dot products of Light vector L, Normal N, and Mirror direction vector R
Illum = Ka x I + Kd x I x (N.L) + Ks x I x (R.V)
n
θ p
φ
n
N (^) V L R
n Light calculation so far is at vertices n Pixel may not fall right on vertex n Shading: calculates color to set interior pixel to n Where are lighting/shading performed in the pipeline?
modeling and viewing
v1, m
v2, m2 v3, m
per vertex lighting projection
interpolate clipping vertex colors
viewport mapping
Rasterization texturing shading
Display
for(int y = ybott; y < ytop; y++) { find xleft and xright for(int x = xleft; x < xright; x++) { find the color c for this pixel put c into the pixel at (x, y) } }
nScans pixels, row by row, calculating color for each pixel
color color
color
color
xleft xright
ybott
ys
y
ytop
n Only use one vertex normaland material property to compute the color for the polygon n Benefit: fast to compute n Used when: n Polygon is small enough n Light source is far away (why?) n Eye is very far away (why?) n OpenGL command: glShadeModel(GL_FLAT)
n Flat shading suffers from “mach band effect” n Mach band effect – human eyes accentuate the discontinuity at the boundary
Side view of a polygonal surface
perceived intensity
n Two popular methods: n Gouraud shading (used by OpenGL) n Phong shading (better specular highlight, not in OpenGL)
n The smooth shading algorithm used in OpenGL glShadeModel(GL_SMOOTH) n Lighting is calculated for each of the polygon vertices n Colors are interpolated for interior pixels
n Compute vertex illumination (color) before the projection transformation n Shade interior pixels: color interpolation (normals are not needed)
C
C2 C
Ca = lerp(C1, C2) Cb = lerp(C1, C3)
Lerp(Ca, Cb)
for all scanlines
n Linear interpolation
n Interpolate triangle color: use y distance to interpolate the two end points in the scanline, and use x distance to interpolate interior pixel colors
a b v1 x v
x = b / (a+b) * v1 + a/(a+b) * v
n Lighting in the polygon interior can be inaccurate
n Instead of interpolation, we calculate lighting for each pixel inside the polygon (per pixel lighting) n Need normals for all the pixels – not provided by user n Phong shading algorithm interpolates the normals and compute lighting during rasterization (need to map the normal back to world or eye space though)