Introduction to Computer GraphicsFarhana Bandukwala, PhDLecture 15: Illumination and Shading in OpenGLOutline• Specifying light sources• Material properties• Transparency• Texture mappingLight sources• Supported light types: ambient, point, spot, directional• Upto 8 different lights can be specified• Parameters to be defined for each source1. Ambient intensity (r,g,b,a)2. Diffuse intensity (r,g,b,a)3. Specular color (r,g,b,a)4. Position (x,y,z,w)5. Spotlights: spot direction, exponent & cutoffDefining a light source• Enable lights: – glEnable(GL_LIGHTING);– glEnable(GL_LIGHT0);• General form: – glLightfv(source, parameter,<pointer to array>) – glLight(source, parameter, value)• Position: point: w=1.0, directional: w=0.0– GLfloat light_pos[]={1.0,2.0,3.0,1.0}– glLightfv(GL_LIGHT0,GL_POSITION,light_pos);• Ambient component:– GLfloat global_ambient[]={.1,.1,.1,1.0};– GLfloat ambient[]={1.0,0.0,0.0,1.0};– glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);– glLightfv(GL_LIGHT_MODEL_AMBIENT,global_ambient);• Diffuse component– GLfloat diffuse[]={0.0,1.0,0.0,1.0};– glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);• Specular component– GLfloat specular[] = { 0.0,1.0,1.0,1.0};– glLightfv(GL_LIGHT0,GL_SPECULAR,specular);• Spotlight parameters: – GL_SPOT_DIRECTION, GL_SPOT_EXPONENT,GL_SPOT_CUTOFFLight sources as objects• Affected by model-view transformations• Define light position just as any other object in a world model• Define light sources at proper points (before or after model view transformations)• Possible to have – lights stationary while objects move– lights move while objects stationary– lights which move with objectsMaterials in OpenGL• Specify materials for front and back surfaces separately• General form: – glMaterialfv(face, type, <pointer to array>);– glMaterialf(face, type, value);• Ambient component– GLfloat ambient[] ={.2,.2,.2,1.0}– glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient);• Diffuse component– GLfloat diffuse[]={1.0,0.8,0.0,1.0};– glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse);• Specular component– Glfloat specular[]={0.0,0.0,1.0,1.0};– glMaterialfv(GL_FRONT,GL_SPECULAR,specular);• Shininess: glMaterialf(GL_FRONT,GL_SHININESS,80.0);• Emission: – Glfloat emission[] = { .5,0.0,0.0,1.0}– glMaterialfv(GL_BACK,GL_EMISSION,emission);Shading a polygon• Specify filling for front/back: – glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);• Specify a shading model– Flat: glShadeModel(GL_FLAT);– Gouraud: glShadeModel(GL_SMOOTH)• Putting it together:1. Specify lights2. Specify shading model3. Specify material properties for surface4. Render polygons:glBegin(GL_POLYGON)glNormal3fv(n1);glVertex3fv(v1);glNormal3fv(n2);glVertex3fv(v2);glNormal3fv(n3);glVertex3fv(v3);…glEnd();Setting up Lights (an example)Void renderScene(){GLfloat light_position[]={ 0.0,0.0,0.0,1.0};glMatrixMode(GL_PROJECTION);glLoadIdentity();glPerspective(…);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(…);glLightfv(GL_LIGHT0,GL_POSITION,light_position); // positioned in world coords and is fixed.glRotate /glTranslate() / glScale()… (draw objects)}void initState() {GLfloat light_diffuse[]={1.0,1.0,1.0,1.0};GLfloat light_specular[]={0.0,0.5,1.0,1.0};GLfloat light_model_ambient[]={.5,.5,.5,1.0};glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.5f);glLightModelfv(GL_LIGHT_MODEL_AMBIENT,light_model_ambient);glEnable(GL_LIGHTING); …glEnable(GL_LIGHT0);}main(){initState();…glutMainLoop();}Setting up Material Properties & NormalsVoid drawObject() {float mat_specular[4]={0.0,.3f,.8f,1.0f};float mat_diffuse[4]={0.0,1.0f,0.0f,1.0f};float mat_ambient[4]={0.1,0.1f,0.1f,1.0f};float mat_shininess=50.0;glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);glBegin(GL_QUAD_STRIP);glNormal3f((GLfloat)0.0,(GLfloat)0.0,(GLfloat)-1.0);glVertex3f((GLfloat)-.5,(GLfloat)0.5,(GLfloat)-.25);glVertex3f((GLfloat)-.5,(GLfloat)-.25,(GLfloat)-.25);glVertex3f((GLfloat)0.0,(GLfloat)0.5,(GLfloat)-.25);glVertex3f((GLfloat)0.0,(GLfloat)-.25,(GLfloat)-.25);glNormal3f((GLfloat)1.0,(GLfloat)0.0,(GLfloat)0.0);glVertex3f((GLfloat)0.0,(GLfloat)0.5,(GLfloat).25);glVertex3f((GLfloat)0.0,(GLfloat)-.25,(GLfloat).25);glNormal3f((GLfloat)0.0,(GLfloat)0.0,(GLfloat)1.0);glVertex3f((GLfloat)-.5,(GLfloat)0.5,(GLfloat).25);glVertex3f((GLfloat)-.5,(GLfloat)-.25,(GLfloat).25);glNormal3f((GLfloat)-1.0,(GLfloat)0.0,(GLfloat)0.0);glVertex3f((GLfloat)-.5,(GLfloat)0.5,(GLfloat)-.25);glVertex3f((GLfloat)-.5,(GLfloat)-.25,(GLfloat)-.25);glEnd();}Void initState(){glShadeModel(GL_FLAT);glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);glEnable(GL_NORMALIZE);}Transparent surfacesTransparency• Opacity: how much light is blocked by surface (0-1.0)• Alpha (α) channel in color specification controls opacity• Transparency: 1-α• Alpha blending: when polygon rasterized, alpha component determines contribution of polygon material to pixel vs background color contribution• Enable blending: glEnable(GL_BLEND);• Specify type of blending:glBlendFunc(source,destination)– GL_ONE, GL_ZERO– GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA– GL_DST_ALPHA, GL_ONE_MINUS_DEST_ALPHA• Order in which polygons rendered matters when blendingTexture mapping• Textures: patterns, images of complex scenes• Texture array: 2-D array of texels • Texture coordinates: coordinate of texture array• Texture map: associates each geometrical object point with a texture point• Surface: S(x,y,z,w) where – X=x(s,t), Y=y(s,t), Z=z(s,t)– s,t can map to texture coordinates– Similar to u,v parametric coordinatesDiagram of texture mapvutsyzxsTexture mapping in OpenGL• Define texture:– Glubyte my_texture[128*128];– glTexImage2D(GL_TEXTURE_2D,level,components,width,height,border,format,type,<pointer to array>);– glTexImage2D(GL_TEXTURE_2D,0,3,128,128,0,GL_RGB,GL_UNSIGNED_BYTE, my_texture);– glEnable(GL_TEXTURE_2D);• Specify behavior of texture parameters (repeat or clamp):– glTexParameteri(GL_TEXTURE_WRAP_S,GL_REPEAT);– glTexParameteri(GL_TEXTURE_WRAP_T,GL_CLAMP);• Specify texture coordinates
View Full Document