PDA

View Full Version : GL_POLYGON and colors



Zath
06-12-2006, 08:05 PM
Ok, as I stated before, I'm very new to openGL but not to programming.

I'm trying to add a polygon to the window that already contains a sphere and the teapot.

I know the poly is there because I placed it over the sphere, but the poly is completely dark.

The code for the poly works ok from previous code I did.

But adding it to the new project is not as easy as I thought it would be.

Why is no color coming through?

Here is some code....

void paint () {

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClear (GL_COLOR_BUFFER_BIT);


glPushMatrix();
glTranslatef (0, 0, -5);
//glRotatef (degree, 0, 1, 0);
brass();
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glBindTexture (GL_TEXTURE_2D, check_id);
//glBindTexture (GL_TEXTURE_2D, static_id);
glutSolidTeapot (1.0);
//glDisable(GL_DEPTH_TEST);

glPushMatrix();
silver();
glDisable(GL_TEXTURE_2D); // no textures
//glDisable(GL_DEPTH_TEST); // visible through walls
//glBindTexture (GL_TEXTURE_2D, static_id);
GLUquadricObj *ball;
glColor3f(0.0f,0.0f,1.0f); // Set Ballhack Color
glTranslatef(4.5,0.0,-2.0);
ball=gluNewQuadric();
gluQuadricNormals(ball, GLU_SMOOTH);
gluSphere(ball,1.0f,30,30);
gluDeleteQuadric(ball);
glDisable(GL_DEPTH_TEST);


glPushMatrix();
//glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClear(GL_COLOR_BUFFER_BIT);
//glEnable(GL_DEPTH_TEST);
//glEnable(GL_TEXTURE_2D);
//glBindTexture (GL_TEXTURE_2D, check_id);

glBegin(GL_POLYGON);

glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5, -0.5);

glColor3f(1.0f, 0.0f, 1.0f);
glVertex2f(-0.5, 0.5);

glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.5, 0.5);

glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.5, -0.5);

glEnd( );

glPopMatrix();


glPopMatrix();

glPopMatrix();

glFlush();

glutSwapBuffers();
degree += 0.1f;
glutPostRedisplay();

}

int main(int argc, char** argv){
glutInit(&argc, argv);

glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow ("Assignment 2");

setupTextures();
//glEnable(GL_TEXTURE_2D);
//glEnable (GL_DEPTH_TEST);
doLights();
doCamera();
glutDisplayFunc (paint);

glutMainLoop();
}

I didn't post some code because I know it works.

Suggestions?

Thanks all,

Zath

Relic
06-13-2006, 02:09 AM
If doLights() enabled lighting, the color is determined by lighting calculations.
These use the lights, the current material and current normals per vertex to calculate the color.
Assuming lighting is on:
1.) Your polygon has no own normal vector assigned. Add glNormal(0.0f, 0.0f, 1.0f) for your case.
2.) Your polygon is clockwise oriented. glFrontFace normally is GL_CCW in OpenGL. Use counter-clockwise ordering of your vertices.
3.) The glColor calls only have an effect on the current material if GL_COLOR_MATERIAL is enabled.

Two ways to fix:
If you want to use the lights, do this:

glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); // Default
glEnable(GL_COLOR_MATERIAL); // Mind OpenGL pitfall #14. http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/
glNormal3f(0.0f, 0.0f, 1.0f)
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5, -0.5);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.5, -0.5);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.5, 0.5);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex2f(-0.5, 0.5);
glEnd( );
glDisable(GL_COLOR_MATERIAL);

If you don't, just glDisable(GL_LIGHTING) for the quad.
Change the winding anyway to not run into trouble with face culling.

Zath
06-13-2006, 09:57 AM
Thanks for the help!

It was the glNormal3f I was missing.
Luckily I don't have to ask what normals are :D

It became visible and not just black - looked white with no color though.

Just a small step and I'll get the color going then add some sides to it for a cube.

Fun stuff - but then again, programming is fun to me.

Zath

Relic
06-13-2006, 10:30 AM
"It became visible and not just black - looked white with no color though."

If there is just a standard directional light enabled, that is shining down the negative z-axis in eye-coordinates and an unrotated normal (0,0,1) will hit it perfectly. The result is maximum ambient+diffuse+specular material color. These are normally default at different levels of grey, it adds up to white under these conditions. The last call which probably set a material was silver() in your code, your polygon would also be silver then.
Try adding an own routine with different glMaterial calls for the diffuse and specular color and call that before rendering your polygon.

Zath
06-13-2006, 06:47 PM
Of course you are right.

I removed the brass and silver and it turned the color of all the rest.

Here is the doLights and camers calls...

void doLights() {
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light);
}

void doCamera() {
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective (60.0f, 1.5, 0.1, 1000);
glMatrixMode (GL_MODELVIEW);
}
And the lines under the includes...

GLfloat light_position[] = {10.0f, 10.0f, 10.0f, 1.0f};
GLfloat diffuse_light[] = {0.9f, 0.9, 0.9f, 0.0f};

So, what you are saying is change the light_position? And the normal for the poly?

Something like this made it change...

glBegin(GL_POLYGON);
glDisable(GL_LIGHTING);
glNormal3f(0.5f, 1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5, -0.5);
.
.
.

After the silver and brass was commented out.

Hmmmm, I think I need to do a little more reading :D

Zath

Relic
06-14-2006, 04:17 AM
This can't work:

glBegin(GL_POLYGON);
glDisable(GL_LIGHTING); // Not allowed inside begin-end, will throw an invalid operation. Add glGetError() after the glEnd and you'll see.
glNormal3f(0.5f, 1.0f, 1.0f); // This is not a normalized vector, you should always try to use normalized normals or glEnable(GL_NORMALIZE) which is required if you use scaling matrices.
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5, -0.5);
...
What about cut-'n'-paste of the code I gave before?

Zath
06-15-2006, 08:16 PM
I understand what you are saying and fixed the problem...

glBegin(GL_POLYGON);
glVertex3f (-50.0f, 0, -50.0f);
glVertex3f (50.0f, 0, -50.0f);
glVertex3f (50.0f, 0, 50.0f);
glVertex3f (-50.0f, 0, 50.0f);
glEnd();

That is the effect I was looking for, a plane for the ground.

Now, just to bind a bmp texture to it.

Thanks,

Zath