Download presentation
Presentation is loading. Please wait.
Published byNoel Booker Modified over 9 years ago
1
Chi-Cheng Lin, Winona State University CS430 Computer Graphics Lighting and Shading Part II
2
2 Topics l Lighting and Shading in OpenGL l Hidden Surface Removal
3
3 Lighting and Shading in OpenGL l Steps zSpecify normal for each vertex zDefine and enable light source(s) zDefine and enable lighting model zDefine material properties zMoving light source(s) zSpotlight and attenuation
4
4 Specify Normal for Vertex l Each vertex of a mesh is sent down the graphics pipeline along with its normal l OpenGL zglNormal3f(nx, ny, nz); glVertex3f(x, y, z); l The last glNormal() called determines the state used for subsequent vertices
5
5 Examples: Specify Normal for a Triangle l Same normal for vertices zglBegin(GL_TRIANGLES) glNormal3f(nx, ny, nz); for (i=0; i<3; i++) glVertex3f(pt[i].x, pt[i].y, pt[i].z); glEnd(); l Different normals for vertices zglBegin(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();
6
6 Light Source l Up to eight light sources can be defined zGL_LIGHT0, GL_LIGHT1, …, GL_LIGHT7 l Location of light source (example w/ light0) zGlfloat position0[]={3.0, 6.0, 5.0, 1.0} glLightfv(GL_LIGHT0, GL_POSITION, position0); l Positional (local) light source z(x, y, z, 1) zDesk lamp l Directional (infinitely remote) light source z(x, y, z, 0) zSun
7
7 Light Source l Light source properties zGLfloat amb0[4], diff0[4], spec0[4]; glLightfv(GL_LIGHT0, GL_AMBIENT, amb0); glLightfv(GL_LIGHT0, GL_DIFFUSE, diff0); glLightfv(GL_LIGHT0, GL_SPECULAR, spec0); zThere are default values for light sources l Turn on the power … zglEnable(GL_LIGHTING); l Switch on the light! zglEnable(GL_LIGHT0);
8
8 Lighting Model l Global ambient light zGLfloat amb[4]; glLightModelfv(GL_LIGHT_MODEL_AMBIENT,amb); zDefault = (0.2, 0.2, 0.2, 1.0) always present l Viewpoint: remote or local? zWhy is it useful to set the eye remote? h = s + v zDefault value of v = (0, 0, 1) zTo make it local: glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); yDefault is GL_FALSE
9
9 Lighting Model l Shading: which side(s)? zNo inside and outside, only front and back sides zFront side: if vertices listed in CCW order as seen by the eye zTo shade both sides glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); yDefault is GL_FALSE
10
10 Define Material Properties l 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); zface = GL_FRONT, GL_BACK, or GL_FRONAT_AND_BACK l Phong exponent? zGLfloat shininess; glMaterialf(face, GL_SHININESS, shininess);
11
11 Moving Light Source(s) l Moving light source independent of camera zEmbed transformation(s) and light-position command in a push-pop pair l Moving light source with camera zPosition light source at (0, 0, 0) zDo not embed light-position command in a push-pop pair l Code segments on page 429
12
12 Spotlight l Spotlight zDirection: d zCutoff angle: zspot = I s cos ( ) spot = I s max(, 0) l Example zGLfloat 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); l Default: d = (0, 0, -1), =180, =0 spot = 1 (omnidirectional point source) d p
13
13 Attenuation l Light intensity decreased with distance D zCoefficients: k c : GL_CONSTANT_ATTENUATION k l : GL_LINEAR_ATTENUATION k q : GL_QUADRATIC_ATTENUATION zatten = 1/(k c + k l D + k q D 2 ) l glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0); l Default (k c, k l, k q ) = (1, 0, 0) atten = 1
14
14 Putting Everything Together l The total red component I r = e r + I mr ar + atten k spot k (I k ar ar + I k dr dr lambert k + I k spr sr phong k f ) where e r is the red component of emissive light I mr is the red component of global ambient light l Green and blue components can be calculated similarly l If I r > 1.0, OpenGL sets it to 1.0. l Flat shading: glShadeModel(GL_FLAT); l Smooth shading: glShadeModel(GL_SMOOTH); k
15
15 Hidden Surface Removal l z-buffer (depth buffer) algorithm is the dominant algorithm in 3D graphics l Store current depth of each pixel in a buffer l Pseudo-depth computed after perspective projection is linearly interpolated across polygon faces l z-buffer test is then performed at each pixel l Usually special z-buffer hardware is used
16
16 Hidden Surface Removal l Algorithm to draw an object zInitialize zbuf[i, j] = infinity for all i, j zfor y = ymin to ymax for x = xmin y to xmax y if z[x, y] < zbuf[x, y] zbuf[x, y] = z(x, y) pixel[x, y] = shade(x, y) xmin y xmax y ymin ymax scanline y
17
17 Hidden Surface Removal l Drawbacks zLarge amount of memory required zSome computations are wasted l Simple l Fast l Good performance in general
18
18 Hidden Surface Removal in OpenGL l Use z-buffer l Create depth buffer zglutInitDisplayMode(GLUT_DEPTH | GLUT_RGB); l Enable depth testing zglutEnable(GL_DEPTH_TEST); l When a new picture is created zglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.