// Now material lighting lets you color certain objects. // Using the color command, we are still setting the color // of the object, but it does not show when we turn on the // lights, why? This is because the color of the object // does not matter. What matters is the color of the Material (skin) // of the object. // So respectively we change the: // glColor3f(...); // to: // glMaterialfv (GL_FRONT, GL_DIFFUSE, redDiffuseMaterial); // Now this new command sets the Material Values, to make it // red we choose the color of the DIFFUSE light, or direct lighting // if I am not mistaken. We are also setting the FRONT of the object // to do this only. As there is no point if there is a light behind it. // We would just see black anyway if that was the case. #include #include GLfloat angle = 0.0; GLfloat redDiffuseMaterial[] = {1.0, 0.0, 0.0}; //set the material to red void cube (void) { glMaterialfv (GL_FRONT, GL_DIFFUSE, redDiffuseMaterial); //replaces glColor3f(...); glRotatef(angle, 1.0, 0.0, 0.0); glRotatef(angle, 0.0, 1.0, 0.0); glRotatef(angle, 0.0, 0.0, 1.0); //glColor3f(1.0, 0.0, 0.0); has now been replaced glutSolidCube(2); } void init (void) { glEnable (GL_DEPTH_TEST); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); } void display (void) { glClearColor (0.0,0.0,0.0,1.0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); cube(); glutSwapBuffers(); angle ++; } void reshape (int w, int h) { glViewport (0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); glMatrixMode (GL_MODELVIEW); } int main (int argc, char **argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow ("A basic OpenGL Window"); init (); glutDisplayFunc (display); glutIdleFunc (display); glutReshapeFunc (reshape); glutMainLoop (); return 0; }