Can someone give me the code that draws a cube in OpenGL with each face a different colour because I did it with rectangles not polygons and the faces go through each other. Thanks alot!
This is a discussion on OpenGL cube's faces mess up within the Game Programming forums, part of the General Programming Boards category; Can someone give me the code that draws a cube in OpenGL with each face a different colour because I ...
Can someone give me the code that draws a cube in OpenGL with each face a different colour because I did it with rectangles not polygons and the faces go through each other. Thanks alot!
Post the code you have and we may correct it. Could be the z-buffer, is it enabled?
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
That's my cube. If you add some rotatef's then you'll see what I mean. And about the z-buffer, could you expand, I'm learning OpenGL and the more explaining the better!Code:glPushMatrix(); glColor3f(1.0f, 0.0f, 0.0f); glBegin(GL_TRIANGLE_STRIP); //back glVertex3f(-1.0f, 0.0f, -1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f(0.0f, 0.0f, -1.0f); glVertex3f(0.0f, -1.0f, -1.0f); glEnd(); glColor3f(1.0f, 1.0f, 0.0f); glBegin(GL_TRIANGLE_STRIP); //left glVertex3f(-1.0f, 0.0f, -1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f(-1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glEnd(); glColor3f(1.0f, 0.0f, 1.0f); glBegin(GL_TRIANGLE_STRIP); //right glVertex3f(0.0f, 0.0f, -1.0f); glVertex3f(0.0f, -1.0f, -1.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(0.0f, -1.0f, 0.0f); glEnd(); glColor3f(0.0f, 1.0f, 1.0f); glBegin(GL_TRIANGLE_STRIP); //bottom glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f(0.0f, -1.0f, -1.0f); glVertex3f(0.0f, -1.0f, 0.0f); glEnd(); glColor3f(0.0f, 0.0f, 1.0f); glBegin(GL_TRIANGLE_STRIP); //top glVertex3f(-1.0f, 0.0f, -1.0f); glVertex3f(-1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, -1.0f); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); glColor3f(1.0f, 0.5f, 0.5f); glBegin(GL_TRIANGLE_STRIP); //front glVertex3f(-1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(0.0f, -1.0f, 0.0f); glEnd(); glPopMatrix();
Add the following to your initialization routine.
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
It might not matter that much at this point, but for future reference, try calling you're glBegin once at the beginning of ALL the triangles, and glEnd and the end of ALL the triangles. Calling them for each face will eventually slow you down.
-psychopath
Thanks guys, I'll try the commands at initialization. I'm not at my computer right now but I'll post back if it works or not.