Unformatted text preview:

Shading in OpenGLEd AngelProfessor of Computer Science,Electrical and ComputerEngineering, and Media ArtsUniversity of New Mexico2Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Objectives• Introduce the OpenGL shading functions• Discuss polygonal shading- Flat- Smooth- Gouraud3Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Steps in OpenGL shading1. Enable shading and select model2. Specify normals3. Specify material properties4. Specify lights4Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Normals• In OpenGL the normal vector is part of the state• Set by glNormal*()- glNormal3f(x, y, z);- glNormal3fv(p);• Usually we want to set the normal to have unitlength so cosine calculations are correct- Length can be affected by transformations- Note that scaling does not preserved length- glEnable(GL_NORMALIZE) allows forautonormalization at a performance penalty5Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Normal for Trianglep0p1p2nplane n ·(p - p0 ) = 0n = (p2 - p0 ) ×(p1 - p0 )normalize n ← n/ |n|pNote that right-hand rule determines outward face6Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Enabling Shading• Shading calculations are enabled by- glEnable(GL_LIGHTING)- Once lighting is enabled, glColor() ignored• Must enable each light source individually- glEnable(GL_LIGHTi) i=0,1…..• Can choose light model parameters- glLightModeli(parameter, GL_TRUE)• GL_LIGHT_MODEL_LOCAL_VIEWER do not usesimplifying distant viewer assumption in calculation• GL_LIGHT_MODEL_TWO_SIDED shades both sides ofpolygons independently7Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Defining a Point Light Source• For each light source, we can set an RGBA for thediffuse, specular, and ambient components, andfor the positionGL float diffuse0[]={1.0, 0.0, 0.0, 1.0};GL float ambient0[]={1.0, 0.0, 0.0, 1.0};GL float specular0[]={1.0, 0.0, 0.0, 1.0};Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glLightv(GL_LIGHT0, GL_POSITION, light0_pos);glLightv(GL_LIGHT0, GL_AMBIENT, ambient0);glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0);glLightv(GL_LIGHT0, GL_SPECULAR, specular0);8Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Distance and Direction• The source colors are specified in RGBA• The position is given in homogeneouscoordinates- If w =1.0, we are specifying a finite location- If w =0.0, we are specifying a parallel sourcewith the given direction vector• The coefficients in the distance terms are bydefault a=1.0 (constant terms), b=c=0.0 (linearand quadratic terms). Change bya= 0.80;glLightf(GL_LIGHT0, GLCONSTANT_ATTENUATION, a);9Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Spotlights• Use glLightv to set- Direction GL_SPOT_DIRECTION- Cutoff GL_SPOT_CUTOFF- Attenuation GL_SPOT_EXPONENT• Proportional to cosαφθ−θφ10Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Global Ambient Light• Ambient light depends on color of lightsources- A red light in a white room will cause a redambient term that disappears when the light isturned off• OpenGL also allows a global ambientterm that is often helpful for testing- glLightModelfv(GL_LIGHT_MODEL_AMBIENT,global_ambient)11Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Moving Light Sources• Light sources are geometric objects whosepositions or directions are affected by themodel-view matrix• Depending on where we place the position(direction) setting function, we can- Move the light source(s) with the object(s)- Fix the object(s) and move the light source(s)- Fix the light source(s) and move the object(s)- Move the light source(s) and object(s) independently12Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Material Properties• Material properties are also part of the OpenGLstate and match the terms in the modified Phongmodel• Set by glMaterialv()GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0};GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};GLfloat shine = 100.0glMaterialf(GL_FRONT, GL_AMBIENT, ambient);glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse);glMaterialf(GL_FRONT, GL_SPECULAR, specular);glMaterialf(GL_FRONT, GL_SHININESS, shine);13Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Front and Back Faces• The default is shade only front faces whichworks correctly for convex objects• If we set two sided lighting, OpenGL will shadeboth sides of a surface• Each side can have its own properties which areset by using GL_FRONT, GL_BACK, orGL_FRONT_AND_BACK in glMaterialfback faces not visible back faces visible14Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Emissive Term• We can simulate a light source in OpenGLby giving a material an emissivecomponent• This component is unaffected by anysources or transformationsGLfloat emission[] = 0.0, 0.3, 0.3, 1.0);glMaterialf(GL_FRONT, GL_EMISSION, emission);15Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Transparency• Material properties are specified as RGBAvalues• The A value can be used to make thesurface translucent• The default is that all surfaces are opaqueregardless of A• Later we will enable blending and use thisfeature16Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Efficiency• Because material properties are part of thestate, if we change materials for many surfaces,we can affect performance• We can make the code cleaner by defining amaterial structure and setting all materials duringinitialization• We can then select a material by a pointertypedef struct materialStruct { GLfloat ambient[4]; GLfloat diffuse[4]; GLfloat specular[4]; GLfloat shineness;} MaterialStruct;17Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Polygonal Shading• Shading calculations are done for eachvertex- Vertex colors become vertex shades• By default, vertex shades are interpolatedacross the polygon-glShadeModel(GL_SMOOTH);• If we use glShadeModel(GL_FLAT); thecolor at the first vertex will determine theshade of the whole polygon18Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005Polygon Normals• Polygons have a single normal- Shades at the vertices as computed by thePhong model can be almost same- Identical for a distant viewer (default) or if thereis no specular component• Consider model of sphere• Want different normals ateach vertex even thoughthis


View Full Document

UNM CS 433 - CS 433 Shading in OpenGL

Download CS 433 Shading in OpenGL
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 433 Shading in OpenGL and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 433 Shading in OpenGL 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?