Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting separate material properties for different objects in OpenGL

Tags:

c++

opengl

How can I have separate material properties for different objects drawn in OpenGL? I did the following code, which apparently only shows the later colour:

//************** Object 1 **************
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColor4f(149.0/255.0, 78.0/255.0, 22.0/255.0, 1.0);
float mat_specular[] = {0.992157, 0.941176, 0.807843, 1.0};
float shininess = 10;

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);

glPushMatrix();
glTranslatef(0, 3.0, 0);
drawSphere(0.1, 0.1, 0.1);
glRotatef(10, 1, 0, 0);

glDisable(GL_COLOR_MATERIAL);


//************** Object 2 *****************
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(48.0/255.0, 48.0/255.0, 48.0/255.0, 1.0);
float mat_specular_2[] = {0.992157, 0.941176, 0.807843, 1.0};
float shininess_2 = 10;

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular_2);
glMaterialf(GL_FRONT, GL_SHININESS, shininess_2);

glPushMatrix();
glTranslatef(-0.6, 0.2, 1.6/2.0);
drawSphere(0.1, 0.1, 0.1);
glPopMatrix();

glDisable(GL_COLOR_MATERIAL);

When rendered, the colour set for the Object 2 is used for the entire scene. So the Object 1 is also rendered in Object 2's colour despite having its own colour set already.

How can I have the 2 objects to have separate material properties so that they can be displayed as different colours instead of just one colours in the whole scene?

like image 770
Carven Avatar asked Oct 12 '11 04:10

Carven


2 Answers

You should put:

glEnable(GL_COLOR_MATERIAL);

As the first thing in your render function, then set the light parameters:

glDisable(GL_COLOR_MATERIAL);
glPushMatrix();

Then set the properties of the material and call the object. All the objects from now on will have this property, if you want to use another material on another object just type:

glDisable(GL_COLOR_MATERIAL);

again, before modeling the second object and so on. If you still have questions, just ask.

like image 52
mohab emad mohammed Avatar answered Oct 23 '22 05:10

mohab emad mohammed


First, your example code looks reasonable and your objects should indeed have different materials.

But keep in mind that you only change the diffuse material color for your second object, as you set exactly the same specular colors and shininess values for both objects. And the ambient of the second object is also the same like for the first, as you only enable color material for the diffuse channel, so the ambient is unchanged from the first object, as OpenGL is a state machine.

So the only material difference between the objects is their diffuse color and this difference is (101, 30, 26). So can it be, that this difference is just outweighted by the ambient and specular terms that are completely equal and is therefore just too small for you to notice? Try completely different materials and see if there is really no difference.

like image 45
Christian Rau Avatar answered Oct 23 '22 05:10

Christian Rau