Setting separate material properties for different objects in OpenGL - c++

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?

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.

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.

It seems you have missed a glPopMatrix after drawing the first object. I don't know how that would change things.

Related

GL_POSITION does not work

I want to add a Light source to my OpenGl code.
I have added following code to my init function...
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightf(GL_LIGHT0, GL_POSITION,(50.0,0.0,0.0,1.0));
Moreover I have drawn a quad:
glBegin(GL_QUADS);
glNormal3f(0.0,0.0,1.0);
glVertex3f(0.0,0.0,20.0);
glVertex3f(0.0,10.0,20.0);
glVertex3f(10.0,10.0,20.0);
glVertex3f(10.0,0.0,20.0);
glEnd();
But no matter what value I use for the Position,
the lighting doesn't change at all...
glLightf(GL_LIGHT0, GL_POSITION,(50.0,0.0,0.0,1.0));
^^^^^^^^^^^^^^^^^^
(50.0,0.0,0.0,1.0) in this context will evaluate to the last expression in the list (1.0). Probably not what you want.
You need to use a real array and glLightfv() to specify light positions:
GLfloat pos[] = { 50.0, 0.0, 0.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, pos );

opengl object movements/switch statements

anyone can teach me how to draw 2 objects, for example a cube and a sphere, in origin (0, 0, 0) and when i move the cube, the sphere just remains in the origin. same with moving the sphere, the cube remains..
using keyboardfunc.
if (!LightSwitch)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPushMatrix();
glTranslatef(CubeX, CubeY, CubeZ);
glColor3f(1.0, 0.0, 0.0);
glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Green);
glutSolidCube(2.0);
glPopAttrib();
glPopMatrix();
glEnd();
}
if (!LightSwitch1)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
glPushMatrix();
glTranslatef(AxisX, AxisY, AxisZ);
glColor3f(1.0, 0.0, 0.0);
glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Blue);
glutSolidSphere(2.0, 10.0, 5.0);
glPopAttrib();
glPopMatrix();
}
glEnd();
I'm no expert, but I believe that by having separate glPushMatrix() / glPopMatrix() blocks for the sphere and the cube, you are giving each object its own local coordinate system. This is why they transform independent of each other.
If you want to move them together, you need to specify glTranslate() outside your glPushMatrix() / glPopMatrix() first:
glTranslatef(translation....); // Translates the whole scene
glPushMatrix();
glTranslatef(the sphere); // Sphere & cube locations changeable via keyb.
glTranslatef(the cube);
glPopMatrix();
If you want to, say, rotate the cube with respect to the sphere (ie., make the center of the sphere the origin of the cube's coordinate system), make a new glPushMatrix()/glPopMatrix() block nested within the first block:
glPushMatrix();
glTranslatef(the sphere);
glPushMatrix();
glRotatef(the cube); // Rotate about specific axis of the sphere
glPopMatrix();
glPopMatrix();
I believe your code is correct for drawing the sphere at location Axis and the cube at location Cube. You say you are using a keyboard func and it is moving them both together when you want to move just one. I believe you are updating both sets of variables in your keyboard function. Try printing out CubeXYZ and AxisXYZ and make sure you're getting the right thing.

glDisable(GL_COLOR_MATERIAL) does not work properly

I need to draw an object with triangles and a sphere. My object has its own material properties that I define them with glMaterialfv. Sphere is just a sphere with a color. However, some part of my object(it is one of its polygons) is colored with sphere's color. How can I solve this problem?
my object function
void drawObject()
{glDisable(GL_COLOR_MATERIAL);
for(int i=0;i<j;i++)
{ glBegin(GL_TRIANGLES);
glNormal3f(..);
glVertex3f(..);
glNormal3f(..);
glVertex3f(..);
glNormal3f(..);
glVertex3f(..);
GLfloat ambientValues[]={..};
GLfloat specularValues[]={..};
GLfloat diffuseValuse[]={..};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,ambientValues);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,diffuseValues);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,specularValues);
glEnd();
}
}
Two mistakes here: Materials muse be set before the drawing operation. And glMaterialfv is not valid within a glBegin/glEnd block. BTW, you shouldn't use immediate mode (glBegin/glEnd) in the first place. Its use has been discouraged for well over 15 years now.

opengl light problem

I have loaded an object, and when I draw the object, I set the color to green..
After drawing the object, I draw lines in red.
It all worked out fine. The problem arise when I input lighting properties.
When I create a light source, everything where the light projects becomes white.
Why does the lighting over writes my color? And how do I solve this problem?
Thanks in advance..
The code you have would help in diagnosing the problem. This sounds like a problem with setting up the materials for the items (which define how they interact with the lights).
You may want to look into the glColorMaterial function. The following snippet of code will set this up:
GLfloat mat_specular[] = {0.3, 0.3, 0.3, 1.0};
GLfloat mat_shininess[] = { 10.0 };
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);

How to draw a filled envelop like a cone on OpenGL (using GLUT)?

I am using freeglut for opengl rendering...
I need to draw an envelop looking like a cone (2D) that has to be filled with some color and some transparency applied.
Is the freeglut toolkit equipped with such an inbuilt functionality to draw filled geometries(or some trick)?
or is there some other api that has an inbuilt support for filled up geometries..
Edit1:
just to clarify the 2D cone thing... the envelop is the graphical interpretation of the coverage area of an aircraft during interception(of an enemy aircraft)...that resembles a sector of a circle..i should have mentioned sector instead..
and glutSolidCone doesnot help me as i want to draw a filled sector of a circle...which i have already done...what remains to do is to fill it with some color...
how to fill geometries with color in opengl?
Edit2:
All the answers posted to this questions can work for my problem in a way..
But i would definitely would want to know a way how to fill a geometry with some color.
Say if i want to draw an envelop which is a parabola...in that case there would be no default glut function to actually draw a filled parabola(or is there any?)..
So to generalise this question...how to draw a custom geometry in some solid color?
Edit3:
The answer that mstrobl posted works for GL_TRIANGLES but for such a code:
glBegin(GL_LINE_STRIP);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(200.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(200.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glEnd();
which draws a square...only a wired square is drawn...i need to fill it with blue color.
anyway to do it?
if i put some drawing commands for a closed curve..like a pie..and i need to fill it with a color is there a way to make it possible...
i dont know how its possible for GL_TRIANGLES... but how to do it for any closed curve?
On Edit3: The way I understand your question is that you want to have OpenGL draw borders and anything between them should be filled with colors.
The idea you had was right, but a line strip is just that - a strip of lines, and it does not have any area.
You can, however, have the lines connect to each other to define a polygon. That will fill out the area of the polygon on a per-vertex basis. Adapting your code:
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(200.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(200.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glEnd();
Please note however, that drawing a polygon this way has two limitations:
The polygon must be convex.
This is a slow operation.
But I assume you just want to get the job done, and this will do it. For the future you might consider just triangulating your polygon.
I'm not sure what you mean by "an envelop", but a cone is a primitive that glut has:
glutSolidCone(radius, height, number_of_slices, number_of_stacks)
The easiest way to fill it with color is to draw it with color. Since you want to make it somewhat transparent, you need an alpha value too:
glColor4f(float red, float green, float blue, float alpha)
// rgb and alpha run from 0.0f to 1.0f; in the example here alpha of 1.0 will
// mean no transparency, 0.0 total transparency. Call before drawing.
To render translucently, blending has to be enabled. And you must set the blending function to use. What you want to do will probably be achieved with the following. If you want to learn more, drop me a comment and I will look for some good pointers. But here goes your setup:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Call that before doing any drawing operations, possibly at program initialization. :)
Since you reclarified your question to ask for a pie: there's an easy way to draw that too using opengl primitives:
You'd draw a solid sphere using gluSolidSphere(). However, since you only want to draw part of it, you just clip the unwanted parts away:
void glClipPlane(GLenum plane, const GLdouble * equation);
With plane being GL_CLIPPLANE0 to GL_CLIPPLANEn and equation being a plane equation in normal form (ax + by + c*z + d = 0 would mean equation would hold the values { a, b, c, d }. Please note that those are doubles and not floats.
I remember there was a subroutine for that. But it's neither too hard to do by yourself.
But I don't understand the 2D -thing. Cone in 2D? Isn't it just a triangle?
Anyway, here's an algorithm to drawing a cone in opengl
First take a circle, subdivision it evenly so that you get a nice amount of edges.
Now pick the center of the circle, make triangles from the edges to the center of the circle. Then select a point over the circle and make triangles from the edges to that point.
The size shape and orientation depends about the values you use to generate the circle and two points. Every step is rather simple and shouldn't cause trouble for you.
First just subdivision a scalar value. Start from [0-2] -range. Take the midpoint ((start+end)/2) and split the range with it. Store the values as pairs. For instance, subdividing once should give you: [(0,1), (1,2)] Do this recursively couple of times, then calculate what those points are on the circle. Simple trigonometry, just remember to multiply the values with π before proceeding. After this you have a certain amount of edges. 2^n where n is the amount of subdivisions. Then you can simply turn them into triangles by giving them one vertex point more. Amount of triangles ends up being therefore: 2^(n+1). (The amounts are useful to know if you are doing it with fixed size arrays.
Edit: What you really want is a pie. (Sorry the pun)
It's equally simple to render. You can again use just triangles. Just select scalar range [-0.25 - 0.25], subdivide, project to circle, and generate one set of triangles.
The scalar - circle projection is simple as: x=cos(v*pi)r, y=sin(vpi)*r where (x,y) is the resulting vertex point, r is a radius, and trigonometric functions work on radiances, not degrees. (if they work with degrees, replace pi with 180)
Use vertex buffers or lists to render it yourself.
Edit: About the coloring question. glColor4f, if you want some parts of the geometry to be different by its color, you can assign a color for each vertex in vertex buffer itself. I don't right now know all the API calls to do it, but API reference in opengl is quite understandable.
On the edit on colors:
OpenGL is actually a state machine. This means that the current material and/or color position is used when drawing. Since you probably won't be using materials, ignore that for now. You want colors.
glColor3f(float r, float g, float b) // draw with r/g/b color and alpha of 1
glColor4f(float r, float g, float b, float alpha)
This will affect the colors of any vertices you draw, of any geometry you render - be it glu's or your own - after the glColorXX call has been executed. If you draw a face with vertices and change the color inbetween the glVertex3f/glVertex2f calls, the colors are interpolated.
Try this:
glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-3.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 3.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(3.0, 0.0, 0.0);
glEnd();
But I pointed at glColor4f already, so I assume you want to set the colors on a per-vertex basis. And you want to render using display lists.
Just like you can display lists of vertices, you can also make them have a list of colors: all you need to do is enable the color lists and tell opengl where the list resides. Of course, they need to have the same outfit as the vertex list (same order).
If you had
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices_);
glDisableClientState(GL_VERTEX_ARRAY);
you should add colors this way. They need not be float; in fact, you tell it what format it should be. For a color list with 1 byte per channel and 4 channels (R, G, B and A) use this:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices_);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors_);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
EDIT: Forgot to add that you then have to tell OpenGL which elements to draw by calling glDrawElements.