CS559: Computer Graphics Lecture 16: Shading and OpenGL Li Zhang Spring 2008.

Slides:



Advertisements
Similar presentations
CSPC 352: Computer Graphics
Advertisements

Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive.
1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.
Illumination and Shading
Computer Graphics - Class 10
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner Lighting/Shading III Week.
Shading in OpenGL CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
CS 4731: Computer Graphics Lecture 16: Illumination Models Part 2 Emmanuel Agu.
Shading in OpenGL Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
Hidden Surfaces and Shading CS BSP Tree T1T1 T2T2 E if (f 1 (E) < 0) then draw T 1 draw T 2 else draw T 2 draw T 1 f 1 (p) = 0 is the.
1 CSCE 641: Computer Graphics Lighting Jinxiang Chai.
Objectives Learn to shade objects so their images appear three- dimensional Learn to shade objects so their images appear three- dimensional Introduce.
1 CSC 830 Computer Graphics Lecture 5 Shading Course Note Credit: Some of slides are extracted from the course notes of prof. Mathieu Desburn (USC) and.
CS5500 Computer Graphics March 26, Shading Reference: Ed Angel’s book.
Computer Graphics (Spring 2008) COMS 4160, Lecture 14: OpenGL 3
CS425 OpenGL Materials. What Color Is It? green & blue absorbed white light Looks red.
Illumination and Shading
Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
CS 445 / 645: Introductory Computer Graphics
Computer Graphics Lighting.
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.
COMPUTER GRAPHICS CS 482 – FALL 2014 AUGUST 27, 2014 FIXED-FUNCTION 3D GRAPHICS MESH SPECIFICATION LIGHTING SPECIFICATION REFLECTION SHADING HIERARCHICAL.
Shading in OpenGL.
19/17/ :25 UML Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera Real Light Synthetic.
Shading 03/19/2003. Lighting Principles Lighting based on how objects reflect light –Surface characteristics –Light color and direction –Global lighting.
Shading and Illumination. OpenGL Shading Without ShadingWith Shading.
CSC418 Computer Graphics n Illumination n Lights n Lightinging models.
Computer Graphics I, Fall 2010 Shading in OpenGL.
Lecture 9: Lighting and Shading 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271
Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1.
08 |Lighting and Shading Eriq Muhammad Adams J |
CS559: Computer Graphics Lecture 15: Hierarchical Modeling and Shading Li Zhang Spring 2008.
Taku KomuraComputer Graphics Local Illumination and Shading Computer Graphics – Lecture 10 Taku Komura Institute for Perception, Action.
Steve Sterley. Real World Lighting Physical objects tend to interact with light in three ways: Absorption (black body) Reflection (mirror) Transmission.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Chi-Cheng Lin, Winona State University CS430 Computer Graphics Lighting and Shading Part II.
OpenGL Color and Lighting 2003 Spring Keng Shih-Ling.
OpenGL Lighting Jian-Liang Lin 2002 Hidden-Surface Removal -1 Original Code: while (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT);
11/04/04© University of Wisconsin, CS559 Fall 2004 Last Time Visibility –Z-Buffer and transparency –A-buffer –Area subdivision –BSP Trees –Exact Cell-Portal.
Illumination and Shading
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
Lecture Fall 2001 Illumination and Shading in OpenGL Light Sources Empirical Illumination Shading Transforming Normals Tong-Yee Lee.
University of Texas at Austin CS384G - Computer Graphics Fall 2010 Don Fussell Shading.
11/5/2002 (c) University of Wisconsin, CS 559 Last Time Local Shading –Diffuse term –Specular term –All together –OpenGL brief overview.
Local Illumination and Shading
Where We Stand So far we know how to: –Transform between spaces –Rasterize –Decide what’s in front Next –Deciding its intensity and color.
In the name of God Computer Graphics. Where We Stand So far we know how to: –Transform between spaces –Draw polygons Next –Deciding a pixel’s intensity.
Illumination CSE 410. Basic steps of lighting Enable smooth shading Set global ambient light glShadeModel(GL_SMOOTH); glEnable(GL_NORMALIZE); GLfloat.
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
Illumination Models and Surface-Rendering Methods CEng 477 Introduction to Computer Graphics.
1 Dr. Scott Schaefer Lighting. 2/49 Lighting/Illumination Color is a function of how light reflects from surfaces to the eye Global illumination accounts.
Graphics Graphics Korea University kucg.korea.ac.kr 1 Lights & Material 고려대학교 컴퓨터 그래픽스 연구실.
CSC Graphics Programming
© University of Wisconsin, CS559 Spring 2004
Shading To determine the correct shades of color on the surface of graphical objects.
Lecture 16: Shading and OpenGL Li Zhang Spring 2008
Advanced Graphics Algorithms Ying Zhu Georgia State University
Illumination and Shading
Open GL: Colors and Lighting
Shading in OpenGL Ed Angel
CSC461: Lecture 24 Lighting and Shading in OpenGL
Illumination and Shading
Lighting and Materials
Lighting – Light Sources
CS5500 Computer Graphics April 10, 2006.
Lighting – Material Properties
ייצוג בעולם 3D ייצוג מצולעים (פוליגונים) צלע קודקוד צלעe0 : {v1,v2}
Last Time Liang-Barsky Details Weiler-Atherton clipping algorithm
Presentation transcript:

CS559: Computer Graphics Lecture 16: Shading and OpenGL Li Zhang Spring 2008

Today Finish shading How to do shading in OpenGL Reading – Shirley, Ch 13.3 – Red book, Ch 4&5 (except color index mode)

3D Example: A robot arm Consider this robot arm with 3 degrees of freedom: – Base rotates about its vertical axis by  – Upper arm rotates in its xy-plane by  – Lower arm rotates in its xy-plane by  h1h1 h2h2 h3h3 Base Upper arm Lower arm Q: What matrix do we use to transform the base to the world? R_y(  ) Q: What matrix for the upper arm to the base? T(0,h1,0)R_z(  ) Q: What matrix for the lower arm to the upper arm? T(0,h2,0)R_z(  ) z x y x y x

3D Example: A robot arm Consider this robot arm with 3 degrees of freedom: – Base rotates about its vertical axis by  – Upper arm rotates in its xy-plane by  – Lower arm rotates in its xy-plane by  h1h1 h2h2 h3h3 Base Upper arm Lower arm Q: What matrix do we use to transform the base to the world? R_y(  ) Q: What matrix for the upper arm to the base? T(0,h1,0)R_z(  ) Q: What matrix for the lower arm to the upper arm? T(0,h2,0)R_z(  ) z x y x y x

Shading problem Given: – a point P on a surface visible through pixel p – The normal N at P – The lighting direction, L, and intensity, L ,at P – The viewing direction, V, at P – The shading coefficients at P Compute the color, I, of pixel p.

Diffuse Shading

Specular reflection Specular reflection accounts for the highlight that you see on some objects. It is particularly important for smooth, shiny surfaces, such as: – Metal, polished stone, plastics, apples, Skin

Specular Reflection Diffuse Specular

Specular reflection “derivation” For a perfect mirror reflector, light is reflected about N, so For a near-perfect reflector, you might expect the highlight to fall off quickly with increasing angle . Also known as: – “rough specular” reflection – “directional diffuse” reflection – “glossy” reflection

Derivation, cont. One way to get this effect is to take (R·V), raised to a power n s. As n s gets larger, – the dropoff becomes {more,less} gradual – gives a {larger,smaller} highlight – simulates a {more,less} mirror-like surface

“Iteration three” The next update to the Phong shading model is then: where: – k s is the specular reflection coefficient – n s is the specular exponent or shininess – R is the reflection of the light about the normal (unit vector) – V is viewing direction (unit vector)

Shininess Ns=10 Ns=20 Ns=50Ns=100

Specular vs Diffuse reflection What’s the key difference Properties: – Specular reflection depends on the viewing direction V.

Specular Reflection Improvement Compute based on normal vector and “halfway” vector, H – Always positive when the light and eye are above the tangent plane – Not quite the same result as the other formulation LV NH

Putting It Together Phong Shading Model GLfloat ke[] = { 0.1, 0.15, 0.05, 1.0 }; GLfloat ka[] = { 0.1, 0.15, 0.1, 1.0 }; GLfloat kd[] = { 0.3, 0.3, 0.2, 1.0 }; GLfloat ks[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat ns[] = { 50.0 }; glMaterialfv(GL_FRONT, GL_EMISSION, ke); glMaterialfv(GL_FRONT, GL_AMBIENT, ka); glMaterialfv(GL_FRONT, GL_DIFFUSE, kd); glMaterialfv(GL_FRONT, GL_SPECULAR, ks); glMaterialfv(GL_FRONT, GL_SHININESS, ns); GLfloat ke[] = { 0.1, 0.15, 0.05, 1.0 }; GLfloat ka[] = { 0.1, 0.15, 0.1, 1.0 }; GLfloat kd[] = { 0.3, 0.3, 0.2, 1.0 }; GLfloat ks[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat ns[] = { 50.0 }; glMaterialfv(GL_FRONT, GL_EMISSION, ke); glMaterialfv(GL_FRONT, GL_AMBIENT, ka); glMaterialfv(GL_FRONT, GL_DIFFUSE, kd); glMaterialfv(GL_FRONT, GL_SPECULAR, ks); glMaterialfv(GL_FRONT, GL_SHININESS, ns);

Lights OpenGL supports three different kinds of lights: ambient, directional, and point. Spot lights are also supported as a special form of point light. We’ve seen ambient light sources, which are not really geometric. Directional light sources have a single direction and intensity associated with them.

Point lights The direction of a point light sources is determined by the vector from the light position to the surface point. Physics tells us the intensity must drop off inversely with the square of the distance: Sometimes, this distance-squared dropoff is considered too “harsh.” A common alternative is: with user-supplied constants for a, b, and c.

OpenGL also allows one to apply a directional attenuation of a point light source, giving a spotlight effect. The spotlight intensity factor is computed in OpenGL as: where – L is the direction to the point light. – S is the center direction of the spotlight. –  is the cutoff angle for the spotlight – e is the angular falloff coefficient Spotlights

“Iteration four” Since light is additive, we can handle multiple lights by taking the sum over every light. Our equation is now: This is the Phong illumination model in OpenGL. Which quantities are spatial vectors? Which are RGB triples? Which are scalars?

Choosing the parameters Experiment with different parameter settings. To get you started, here are a few suggestions: – Try n s in the range [0,100] – Try k a + k d + k s < 1 – Use a small k a (~0.1) nsns kdkd ksks MetallargeSmall, color of metal Large, color of metal Plasticmedium Medium, color of plastic Medium, white Planet0varying0

Shading in OpenGL The OpenGL lighting model allows you to associate different lighting colors according to material properties they will influence. Thus, our original shading equation becomes: where you can have a global ambient light with intensity L a in addition to having an ambient light intensity L a j associated with each individual light.

BRDF The Phong illumination model is a function that maps light from incoming (light) directions  in to outgoing (viewing) directions  out : Here’s a plot with  in held constant: This function is called the Bi-directional Reflectance Distribution Function (BRDF). BRDF’s can be quite sophisticated…

BRDF measurement Stanford Graphics Lab

Brdf Viewer plots Diffuse written by Szymon Rusinkiewicz Torrance-Sparrow Anisotropic

More sophisticated BRDF’s Westin, Arvo, Torrance 1992 Cook and Torrance, 1982

Surface Shading Now we know how to compute the color at a point on a surface using the Phong lighting model. Does graphics hardware do this calculation at every point? Typically not (although this is changing)… Smooth surfaces are often approximated by polygonal facets. So How do we compute the shading for such a surface?

Faceted shading Assume each face has a constant normal: If we have constant material properties over the surface, how will the color of each triangle vary? Result: faceted, not smooth, appearance.

Faceted shading (cont’d)

Gouraud interpolation To get a smoother result that is easily performed in hardware, we can do Gouraud interpolation. Here’s how it works: 1.Compute normals at the vertices. 2.Shade only the vertices. 3.Interpolate the resulting vertex colors.

Facted shading vs. Gouraud interpolation

Gouraud interpolation

Gouraud interpolation artifacts Gouraud interpolation has significant limitations. – If the polygonal approximation is too coarse, we can miss specular highlights.

Phong interpolation To get an even smoother result with fewer artifacts, we can perform Phong interpolation. Here’s how it works: 1.Compute normals at the vertices. 2.Interpolate normals and normalize. 3.Shade using the interpolated normals.

Gouraud vs. Phong interpolation

How to compute vertex normals A weighted average of normals of neighboring triangles

How to compute vertex normals A weighted average of normals of neighboring triangles

Define a light in OpenGL GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); GLfloat ke[] = { 0.1, 0.15, 0.05, 1.0 }; GLfloat ka[] = { 0.1, 0.15, 0.1, 1.0 }; GLfloat kd[] = { 0.3, 0.3, 0.2, 1.0 }; GLfloat ks[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat ns[] = { 50.0 }; glMaterialfv(GL_FRONT, GL_EMISSION, ke); glMaterialfv(GL_FRONT, GL_AMBIENT, ka); glMaterialfv(GL_FRONT, GL_DIFFUSE, kd); glMaterialfv(GL_FRONT, GL_SPECULAR, ks); glMaterialfv(GL_FRONT, GL_SHININESS, ns); GLfloat ke[] = { 0.1, 0.15, 0.05, 1.0 }; GLfloat ka[] = { 0.1, 0.15, 0.1, 1.0 }; GLfloat kd[] = { 0.3, 0.3, 0.2, 1.0 }; GLfloat ks[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat ns[] = { 50.0 }; glMaterialfv(GL_FRONT, GL_EMISSION, ke); glMaterialfv(GL_FRONT, GL_AMBIENT, ka); glMaterialfv(GL_FRONT, GL_DIFFUSE, kd); glMaterialfv(GL_FRONT, GL_SPECULAR, ks); glMaterialfv(GL_FRONT, GL_SHININESS, ns);

Demo

Light.c void init(void) { GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); } void init(void) { GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); } void display(void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSolidSphere (1.0, 20, 16); glFlush (); } void display(void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSolidSphere (1.0, 20, 16); glFlush (); }

Light.c

Complex lighting How to have multiple lights? How to change lighting positions? How to change color material?

Multiple lights GLfloat light1_ambient[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light1_position[] = { -2.0, 2.0, 1.0, 1.0 }; GLfloat spot_direction[] = { -1.0, -1.0, 0.0 }; glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular); glLightfv(GL_LIGHT1, GL_POSITION, light1_position); glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5); glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5); glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2); glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction); glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0); glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0); glEnable(GL_LIGHT1); GLfloat light1_ambient[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light1_position[] = { -2.0, 2.0, 1.0, 1.0 }; GLfloat spot_direction[] = { -1.0, -1.0, 0.0 }; glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular); glLightfv(GL_LIGHT1, GL_POSITION, light1_position); glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5); glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5); glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2); glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction); glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0); glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0); glEnable(GL_LIGHT1);

Moving light source Method 1: – Use glLightfv(GL_LIGHT1, GL_POSITION, light1_position); Method 2: – Use transformation

Moving a light source Use glLightfv(GL_LIGHT1, GL_POSITION, light1_position);

Moving light source Use transformation static GLdouble spin; void display(void) { GLfloat light_position[] = { 0.0, 0.0, 1.5, 1.0 }; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix(); glRotated(spin, 1.0, 0.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glPopMatrix(); glutSolidTorus (0.275, 0.85, 8, 15); glPopMatrix(); glFlush(); } static GLdouble spin; void display(void) { GLfloat light_position[] = { 0.0, 0.0, 1.5, 1.0 }; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix(); glRotated(spin, 1.0, 0.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glPopMatrix(); glutSolidTorus (0.275, 0.85, 8, 15); glPopMatrix(); glFlush(); }

Demo Rotating a light source demo.

glColorMaterial glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_DIFFUSE); /* now glColor* changes diffuse reflection */ glColor3f(0.2, 0.5, 0.8); /* draw some objects here */ glColorMaterial(GL_FRONT, GL_SPECULAR); /* glColor* no longer changes diffuse reflection */ /* now glColor* changes specular reflection */ glColor3f(0.9, 0.0, 0.2); /* draw other objects here */ glDisable(GL_COLOR_MATERIAL); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_DIFFUSE); /* now glColor* changes diffuse reflection */ glColor3f(0.2, 0.5, 0.8); /* draw some objects here */ glColorMaterial(GL_FRONT, GL_SPECULAR); /* glColor* no longer changes diffuse reflection */ /* now glColor* changes specular reflection */ glColor3f(0.9, 0.0, 0.2); /* draw other objects here */ glDisable(GL_COLOR_MATERIAL);

glColorMaterial Demo

glColorMaterial void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { /* change red */ diffuseMaterial[0] += 0.1; if (diffuseMaterial[0] > 1.0) diffuseMaterial[0] = 0.0; glColor4fv(diffuseMaterial); glutPostRedisplay(); } break; case GLUT_MIDDLE_BUTTON: if (state == GLUT_DOWN) { /* change green */ diffuseMaterial[1] += 0.1; if (diffuseMaterial[1] > 1.0) diffuseMaterial[1] = 0.0; glColor4fv(diffuseMaterial); glutPostRedisplay(); } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { /* change blue */ diffuseMaterial[2] += 0.1; if (diffuseMaterial[2] > 1.0) diffuseMaterial[2] = 0.0; glColor4fv(diffuseMaterial); glutPostRedisplay(); } break; default: break; } void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { /* change red */ diffuseMaterial[0] += 0.1; if (diffuseMaterial[0] > 1.0) diffuseMaterial[0] = 0.0; glColor4fv(diffuseMaterial); glutPostRedisplay(); } break; case GLUT_MIDDLE_BUTTON: if (state == GLUT_DOWN) { /* change green */ diffuseMaterial[1] += 0.1; if (diffuseMaterial[1] > 1.0) diffuseMaterial[1] = 0.0; glColor4fv(diffuseMaterial); glutPostRedisplay(); } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { /* change blue */ diffuseMaterial[2] += 0.1; if (diffuseMaterial[2] > 1.0) diffuseMaterial[2] = 0.0; glColor4fv(diffuseMaterial); glutPostRedisplay(); } break; default: break; } }

Shading in OpenGL, cont’d Notes: You can have as many as GL_MAX_LIGHTS lights in a scene. This number is system-dependent. For directional lights, you specify a light direction, not position, and the attenuation and spotlight terms are ignored. The directions of directional lights and spotlights are specified in the world coordinate systems, not the object coordinate system.