how to draw in color with lighting

Hi, I’m learning to use opengl, and I can’t figure out how to have polygons that are colored and have lighting. As soon as I enable a light source everything becomes white.

The only thing I can think of is texture mapping a plain color… but isn’t this inefficient?


You most likely haven’t set up your materials.

you need to:

  1. glEnable(GL_COLOR_MATERIAL).
  2. set up material properties; have a looksy: click

enabling materials seemed to make the colors work… although I do not see a difference when I change the values sent to glMaterialf or even if I do not call it. I wanted to render a sphere and have that little shiny part…



After enabling lightinng and GL_COLOR_MATERIAL, apply specular colour and shininess on your model.

For example;

// track material ambient and diffuse from vertex color

// specify material color of an object
GLfloat shininess = 50; // range 0 ~ 128
GLfloat specularColor[4] = {1,1,1,1}; // white
GLfloat diffuseColor[3] = {1,0,0};    // red

// set specular and shiniess
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularColor);

// set ambient and diffuse color

It renders reddish body with white spot for specular reflection.


I realized I had not called:
glLightfv with GL_SPECULAR!! I didn’t realize the light had a specular attribute.

Thank you