PDA

View Full Version : how to draw in color with lighting



geckosenator
11-15-2005, 11:34 AM
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?

thanks.

Aeluned
11-15-2005, 11:54 AM
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 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_7cq4.asp)

geckosenator
11-16-2005, 07:52 AM
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..

thanks

songho
11-16-2005, 09:02 AM
geckosenator,

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
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
...

// 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
glColor3fv(diffuseColor);
...It renders reddish body with white spot for specular reflection.

==song==

geckosenator
11-16-2005, 06:32 PM
I realized I had not called:
glLightfv with GL_SPECULAR!! I didn't realize the light had a specular attribute.

Thank you