Archived

This topic is now archived and is closed to further replies.

Penance

Can someone explain glColorMaterial

Recommended Posts

I''m not sure how to use it or even what it does. I''ve read the technical description but it still isn''t clicking. When should I use glEnable(GL_COLOR_MATERIAL) and for what purpose? What is the correct (efficient) way to use multiple materials? Examples appreciated!

Share this post


Link to post
Share on other sites
With "glEnable (GL_COLOR_MATERIAL)" you simply tell OpenGL that material properties are defined by glColor*() function.

Here are 2 examples - in one you specify material properties with "glMaterial*()" and in one you use "glColorMaterial":

///////////////// Example 1
...
// grey diffuse light
float diffuse_light [] = {0.4f, 0.4f, 0.4f, 1.0f};

// yellow specular light
float specular_light [] = {1.0f, 1.0f, 0.0f, 1.0f};

// diffuse material
float diffuse_material [] = {1.0f, 1.0f, 1.0f, 1.0f};

// specular material
float specular_material [] = {1.0f, 1.0f, 1.0f, 1.0f};

// lighting
void Lighting ()
{
...
// grey diffuse light
glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light);

// yellow specular light
glLightfv (GL_LIGHT0, GL_SPECULAR, specular_light);

// diffuse material properties
glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse_material);

// specular material properties
glMaterialfv (GL_FRONT, GL_SPECULAR, specular_material);
...
glEnable (GL_LIGHT0);
...
}

void DrawScene ()
{
...
auxSolidCube (5.0f);
...
}
...
/////////////////

And now example how to do the same thing with "glColorMaterial()":

///////////////// Example 2
...
// grey diffuse light
float diffuse_light [] = {0.4f, 0.4f, 0.4f, 1.0f};

// yellow specular light
float specular_light [] = {1.0f, 1.0f, 0.0f, 1.0f};

/* no need to define materials, you will use "glColor*()" and "glColorMaterial()"! */

// lighting
void Lighting ()
{
...
// grey diffuse light
glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light);

// yellow specular light
glLightfv (GL_LIGHT0, GL_SPECULAR, specular_light);

// specify diffuse and specular material properties
glEnable (GL_COLOR_MATERIAL);
glColorMaterial (GL_FRONT, GL_DIFFUSE);
glColorMaterial (GL_FRONT, GL_SPECULAR);

glEnable (GL_LIGHT0);
...
}

void DrawScene ()
{
...
/* both, specular and diffuse material, have white color thus
they reflect all light */
glColor3f (1.0f, 1.0f, 1.0f);
auxSolidCube (5.0f);
...
}
...
/////////////////

You'll mostly use that when you want that every object reflects different light. So in first example all objects drawn later would have same material properties for specified lights while in the second case you can have lots of different objects and every would have own material properties for each light component.

Hope you understand now. If you still have problems, ask, that's why we're here for.

"multiple materials" - how do you mean that? Describe a little...



[edited by - Wingman on November 9, 2002 6:12:22 AM]

Share this post


Link to post
Share on other sites
Thanks your reply was very helpful and got me on the road to understanding.

What i meant by multiple materials was defining different material parameters so I could have different object have different materials (like a gold cup and a silver knife, etc).

Sorry so late in coming, but the board wouldn''t take new messages last time I had a chance to try

Share this post


Link to post
Share on other sites