180 likes | 330 Views
CS430 Computer Graphics. Lighting and Shading Part II. Topics. Lighting and Shading in OpenGL Hidden Surface Removal. Lighting and Shading in OpenGL. Steps Specify normal for each vertex Define and enable light source(s) Define and enable lighting model Define material properties
E N D
CS430 Computer Graphics Lighting and Shading Part II Chi-Cheng Lin, Winona State University
Topics • Lighting and Shading in OpenGL • Hidden Surface Removal
Lighting and Shading in OpenGL • Steps • Specify normal for each vertex • Define and enable light source(s) • Define and enable lighting model • Define material properties • Moving light source(s) • Spotlight and attenuation
Specify Normal for Vertex • Each vertex of a mesh is sent down the graphics pipeline along with its normal • OpenGL • glNormal3f(nx, ny, nz); glVertex3f(x, y, z); • The last glNormal() called determines the state used for subsequent vertices
Examples: Specify Normal for a Triangle • Same normal for vertices • glBegin(GL_TRIANGLES) glNormal3f(nx, ny, nz); for (i=0; i<3; i++) glVertex3f(pt[i].x, pt[i].y, pt[i].z); glEnd(); • Different normals for vertices • glBegin(GL_TRIANGLES) for (i=0; i<3; i++) glNormal3f(n[i].x, n[i].y, n[i].z); glVertex3f(pt[i].x, pt[i].y, pt[i].z); glEnd();
Light Source • Up to eight light sources can be defined • GL_LIGHT0, GL_LIGHT1, …, GL_LIGHT7 • Location of light source (example w/ light0) • Glfloat position0[]={3.0, 6.0, 5.0, 1.0} glLightfv(GL_LIGHT0, GL_POSITION, position0); • Positional (local) light source • (x, y, z, 1) • Desk lamp • Directional (infinitely remote) light source • (x, y, z, 0) • Sun
Light Source • Light source properties • GLfloat amb0[4], diff0[4], spec0[4]; glLightfv(GL_LIGHT0, GL_AMBIENT, amb0); glLightfv(GL_LIGHT0, GL_DIFFUSE, diff0); glLightfv(GL_LIGHT0, GL_SPECULAR, spec0); • There are default values for light sources • Turn on the power … • glEnable(GL_LIGHTING); • Switch on the light! • glEnable(GL_LIGHT0);
Lighting Model • Global ambient light • GLfloat amb[4]; glLightModelfv(GL_LIGHT_MODEL_AMBIENT,amb); • Default = (0.2, 0.2, 0.2, 1.0) always present • Viewpoint: remote or local? • Why is it useful to set the eye remote? h = s + v • Default value of v = (0, 0, 1) • To make it local: glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); • Default is GL_FALSE
Lighting Model • Shading: which side(s)? • No inside and outside, only front and back sides • Front side: if vertices listed in CCW order as seen by the eye • To shade both sides glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); • Default is GL_FALSE
Define Material Properties • GLfloat amb[4], diff[4], emi[4], spec[4]; glMaterialfv(face, GL_AMBIENT, amb); glMaterialfv(face, GL_DIFFUSE, diff); glMaterialfv(face, GL_AMBIENT_AND_DIFFUSE, diff); glMaterialfv(face, GL_ GL_SPECULAR, spec); glMaterialfv(face, GL_EMISSION, emi); • face = GL_FRONT, GL_BACK, or GL_FRONAT_AND_BACK • Phong exponent? • GLfloat shininess; glMaterialf(face, GL_SHININESS, shininess);
Moving Light Source(s) • Moving light source independent of camera • Embed transformation(s) and light-position command in a push-pop pair • Moving light source with camera • Position light source at (0, 0, 0) • Do not embed light-position command in a push-pop pair • Code segments on page 429
Spotlight d • Spotlight • Direction: d • Cutoff angle: • spot = Iscos() spot = Is max( , 0) • Example • GLfloat dir[]= {2.0, 1.0, -4.0}; glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0); glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 4.0); glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir); • Default: d = (0, 0, -1), =180, =0 spot = 1 (omnidirectional point source) p
Attenuation • Light intensity decreased with distance D • Coefficients: kc : GL_CONSTANT_ATTENUATION kl : GL_LINEAR_ATTENUATION kq: GL_QUADRATIC_ATTENUATION • atten = 1/(kc + klD + kqD2) • glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0); • Default (kc , kl , kq) = (1, 0, 0) atten = 1
Putting Everything Together • The total red component Ir = er + Imr ar + attenk spotk(Ikar ar + Ikdr dr lambertk+ Ikspr sr phongkf ) where er is the red component of emissive light Imr is the red component of global ambient light • Green and blue components can be calculated similarly • If Ir > 1.0, OpenGL sets it to 1.0. • Flat shading: glShadeModel(GL_FLAT); • Smooth shading: glShadeModel(GL_SMOOTH); k
Hidden Surface Removal • z-buffer (depth buffer) algorithm is the dominant algorithm in 3D graphics • Store current depth of each pixel in a buffer • Pseudo-depth computed after perspective projection is linearly interpolated across polygon faces • z-buffer test is then performed at each pixel • Usually special z-buffer hardware is used
Hidden Surface Removal • Algorithm to draw an object • Initialize zbuf[i, j] = infinity for all i, j • for y = ymin to ymax for x = xminy to xmaxy if z[x, y] < zbuf[x, y] zbuf[x, y] = z(x, y) pixel[x, y] = shade(x, y) ymax scanliney xminy xmaxy ymin
Hidden Surface Removal • Drawbacks • Large amount of memory required • Some computations are wasted • Simple • Fast • Good performance in general
Hidden Surface Removal in OpenGL • Use z-buffer • Create depth buffer • glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB); • Enable depth testing • glutEnable(GL_DEPTH_TEST); • When a new picture is created • glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);