110 likes | 166 Views
Lighting Model (Ch 8). 8.1 - 8.4 three kinds of light affect the appearance of an object ambient, diffuse, and specular light source has an RGB intensity for each kind of light the color of an object is determined by several factors including its own color
E N D
Lighting Model (Ch 8) • 8.1 - 8.4 • three kinds of light affect the appearance of an object • ambient, diffuse, and specular • light source has an RGB intensity for each kind of light • the color of an object is determined by several factors including its own color • color = ambient + diffuse + specular
diffuse • light bounces off in all directions • light intensity is determine by the angle of approach (l * n) • full intensity at zero degrees and none beyond 90 • light directly overhead is brighter than others • Ld - light intensity • kd - reflection coefficient (0 - 1) fraction of light that is reflected • Id = kd Ld (ul * un) • Intensity = reflection * light intensity n l O
specular • specular surfaces are smooth or shiny • light that bounces of is the color of the light source (not the object) • light only bounces of in the reflective direction • Is = ks Ls (ur * uv)a • a - shininess coefficient • determines the narrow cone of vision • > 1000 a perfect mirror • 100 - 500 metallic surfaces • < 100 show broad highlights v l r O
ambient • this lighting model would create rather sever images. Items would be either dark or light • ambient light is a technique to ‘fake’ the complex interactions that go on in the real world • it simulates a constant background light • Ia = ka La • Final computation for a surface • for each color • Red Ir = Ira + Ird + Irs • Red intensity = ambient + diffuse + specular • Green intensity = • Blue intensity =
Lighting Properties 8.2.8 • Defining Light Source properties for up to 8 sources • Define Position (x, y, z, w) • Positional Light - a desk lamp w = 1 • Directional Light - the sun w = 0 • The position is transformed through the Modelling matrix! • Setup the light first • float position[4] = {x, y, z, w}; • float ambient[4] = {r, g, b, a}; • float diffuse[4] = {r, g, b, a}; • float specular[4] = {r, g, b, a}; • Define the light source(probably inside read file) • glLightfv (GL_LIGHT0, GL_POSITION, position); • glLightfv (GL_LIGHT0, GL_AMBIENT, ambient); • glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse); • glLightfv (GL_LIGHT0, GL_SPECULAR, specular); • Enable Light Source AFTER defining everything • glEnable(GL_LIGHTING); • glEnable(GL_LIGHT0); • Disable Light Source if necessary • glDisable (GL_LIGHT0); • glDisable (GL_LIGHTING);
Material Properties (8.2.9) • Material properties are modal, they remain valid until you change them. You will generally change them before each object rendered. It takes trial and error to adjust all of the values for the desired effect. • Fill each object with the parameters from the scene file. • uisObject obj; • obj.setAmbient (R, G, B, A); // don’t make this too bright A=1 • obj.setDiffuse (R, G, B, A); //generally same as ambient • obj.setSpecular (R, G, B, A); // general set a white • obj.setShininess (S); // a float between 1 and 128 • When rendering an object using one of the shading modes you set the material properties just before drawing. • glMaterialfv (GL_FRONT, GL_AMBIENT, obj.getAmbient() ); • glMaterialfv (GL_FRONT, GL_DIFFUSE, obj.getDiffuse() ); • glMaterialfv (GL_FRONT, GL_SPECULAR, obj.getSpecular() ); • glMaterialfv (GL_FRONT, GL_SHININESS, obj.getShininess() ); • Rule of thumb • ambient and diffuse should be identical
uisGL Extended Example // look at ~grissom/367uisGL.h uisObject cube; uisMatrix M, S, T; // initialize structure from external object file infile.open(“cube.obj”); infile >> cube; // set object color from scene description file cube.setAmbient(r, g, b, a); cube.setDiffuse(r, g, b, a); cube.setSpecular(r, g, b, a); cube.setShininess(float); // set transformation matrix from scene description file S.setScaling(x,y,z); T.setTranslation(x,y,z); M= T * S; cube.setTransformation(M); // transform all points at the beginning cube = M * cube; cube.renderWireframe(); // your overloaded function // alternative: modify OpenGL ModelView matrix each time M = cube.getTransformation(); glPushMatrix() glLoadMatrixf(M); cube.renderMyself(WIREFRAME); // your overloaded function glPopMatrix();
Flat Shading • Flat shading • use a single normal vector to determine the color for the entire face. This works fine for cubes but gives a faceted appearance for curved surfaces. • Pseudo code to render an object with flat shading: • uisVector N; • uisFace F; • uisVertex P; • For each face • N = calculate the normal vector for this face • glNormal3fv (N); • glBegin (GL_POLYGON); • For each vertex on the face • P = F.getVertex(j); • glVertex3fv (P); • glEnd();
Gouraud Smooth Shading • Gouraud Smooth Shading • a more realistic approach is to gradual change the color of the face as it moves from one side to the other. • the trick is to calculate correct colors at each vertex based on the average normal of each adjoining face • the color is then interpolated as each pixel is drawn across the face • Pseudo code to render an object with gouraud shading: • uisVector N; • uisFace F; • uisVertex P; • For each face • glBegin (GL_POLYGON); • For each vertex on the face • N = calculate the normal vector for this vertex • glNormal3fv (N); • P = F.getVertex(j); • glVertex3fv (P); • glEnd(); • Calculating a normal vector for a vertex is relatively simple. You sum the normal vectors for each adjacent face like so. • for (i = 1; i <= P.getNumFaces(); i++){ • F = P.getFace(i); • N = calculate the normal vector for this Face • Sum = Sum + N; • } Sum.normalize(); // normalize the vector
Phong Smooth Shading • More realistic than Gouraud but very calculation intensive. • Vetex normals are calculated in the same way but as the face is rendered the normal is interpolated and the color is recalulated for each pixel • This makes specular reflection have a tighter hot spot • OpenGL does not support this option
Transparency • Objects can appear transparent or opaque • The alpha bit must be set in RGBA color mode • a = 1 is opaque • a = 0 is completely transparent • Blending factors are identified for the source and destination • source is the new pixel to be rendered • destination is what is already in the buffer • glotInitDisplayMode (GLUT_SINGLE, GLUT_RGBA, GLUT_DEPTH) • glEnable(GL_BLEND)