View Full Version : Color problem: GL_ABMIENT and GL_DIFFUSE dont work

04-15-2011, 09:47 AM
Hi, I'm having a problem to get GL_ABMIENT and GL_DIFFUSE for the material to work; they seem to simply be set to the color which I have previously specified with glColor4f. However, GL_SPECULAR *does* work. I'm using the code below to specify some flags that OpenGL should use as well as the colors of the light and the material:

// OpegGL flags
glEnable(GL_COLOR_MATERIAL); // Enables color to work together with lighting
//glShadeModel(GL_SMOOTH); // Enable Gouraud shading
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture

// Light and materials
glColor4f(1, 1, 1, 1); // Set current color to white
GLfloat light_position[] = {1.0f, 0.0f, 0.0f, 0.0f}; // From the right
GLfloat light_color[] = {1.0f, 1.0f, 1.0f, 1.0f}; // White light
GLfloat ambient_color[] = {0.2f, 0.2f, 0.2f, 1.0f}; // Weak white light
GLfloat mat_shininess[] = {50.0};
GLfloat mat_ambientColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat mat_diffuseColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat mat_specularColor[] = {1.0f, 0.0f, 0.0f, 1.0f};

// Set current material
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambientColor);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuseColor);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specularColor);

// Set light
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_color);

As you can see, I'm trying to set all the reflecting properties of the material to black except from the specular reflectance, which I set to red. However, when I render an object (I use a sphere for trying out the settings) afterwards, mat_ambientColor and mat_diffuseColor don't have any effect - the sphere gets white, as specified by the glColor4f call (I have tried feeding this function with other colors too). mat_specularColor on the other hand, does have effect since the specular reflectance is red and is totally unaffected by the color I specify with glColor.

Why don't I get the right ambient and diffuse colors on the material?

carsten neumann
04-15-2011, 10:00 AM
Because you do: glEnable(GL_COLOR_MATERIAL);
Look at the glColorMaterial (http://www.opengl.org/sdk/docs/man/xhtml/glColorMaterial.xml) man page, it defaults to GL_AMBIENT_AND_DIFFUSE, so you are requesting to ignore your material's ambient and diffuse and use the vertex colors for those instead.

04-15-2011, 03:16 PM
It seems like that did the trick. Thanks! :) I just used glDisable(GL_COLOR_MATERIAL); instead of glEnable(GL_COLOR_MATERIAL); and that made it take every color I specified for the material's different color properties. In the man pages of glColorMaterial, it says that "If only such a subset of parameters is to be specified for each vertex, calling glColorMaterial is preferable to calling glMaterial". Why, does this optimize the performance in some way?