OpenGL Lighting Functions
The necessary sequence of operations to perform shading with the OpenGL lighting model are:
glNormal3{sifd}(nx, ny, nz); /* specify normal at vertex */ glVertex3{sifd}(x, y, z); /* specify vertex */ ...
float ambient[4] glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); /* ambient RGB */Shading can be performed on both sides of the surface (ignoring the sign of the normal vector), or only on one side (the side with the positive normal vector).
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
float ambient[4], diffuse[4], specular[4]; float position[4]; float kc, kl, kq; /* define the characteristics of light source 0 */ glLightf(GL_CONSTANT, GL_LIGHT0, Kc); /* constant attenutation */ glLightf(GL_LINEAR, GL_LIGHT0, kl); /* linear attenuation */ glLightf(GL_QUADRATIC, GL_LIGHT0, kq); /* quadratic attenuation */ glLightfv(GL_AMBIENT, GL_LIGHT0, ambient); /* ambient RGB */ glLightfv(GL_DIFFUSE, GL_LIGHT0, diffuse); /* diffuse RGB */ glLightfv(GL_SPECULAR, GL_LIGHT0, specular); /* specular RGB */ glLightfv(GL_POSITION, GL_LIGHT0, position); /* define the characteristics of light source i */ ... glEnable(GL_LIGHT0); /* enable light source 0 */ ... /* enable light source i */
Light source i can be turned off with:
glDisable(GL_LIGHTi); /* disable light source i */
float ambient[4], diffuse[4], emissive[4], specular[4]; float shininess; glMaterialfv(face, GL_EMISSION, emission); /* emissive RGB */ glMaterialfv(face, GL_AMBIENT, ambient); /* ambient RGB */ glMaterialfv(face, GL_DIFFUSE, diffuse); /* diffuse RGB */ glMaterialfv(face, GL_SPECULAR, specular); /* specular RGB */ glMaterialf(face, GL_SHININESS, shininess); /* specular exponent */
where face can be either GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK.
glEnable(GL_LIGHTING); /* enable the lighting model */
The lighting model can be disabled with:
glDisable(GL_LIGHTING); /* disable the lighting model */
On to the lighting model defaults...
Back to the lighting model...
Back to the 13.016 overview...