[C++ & OpenGL] Cubes messed up

Started by
3 comments, last by epicar 15 years, 1 month ago
The code below draws two cubes, with box2 being below box. When I run the program, however, there is a lot of weird twitching (z-fighting?) between the bottom of box and the top of box2:
void RendererOpenGL::Render()
{	
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // clear buffers from last frame
	glLoadIdentity(); // clear coordinate transformation - (0,0,0) is the screen centre
	
    static double a = 0; a += 0.1;
    glTranslated(0, 0, -20);
    glRotated(a, .5, -1, 0);
      

    Box box(Point_3d(0, 0, 0), Orient_3d(0, 0, 0),
        Size_3d(1, 1, 1), Colour::RED);
    Box box2(Point_3d(0, -2, 0), Orient_3d(0, 0, 0),
        Size_3d(1, 1, 1), Colour::BLUE);

    box.Render();
    box2.Render();

	SwapBuffers(hDC);
}

Box::Render and the vertices used are defined like this:
const double Box::CUBE_VERTICES[QTY_CUBE_VERTICES] =
{
    1, 1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, // TOP
    1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, // BOTTOM
    1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, // FRONT
    1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, // BACK
    -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, // LEFT
    1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1 // RIGHT
};

void Box::Render()
{
    glPushMatrix();

    glColor3d(colour.r, colour.g, colour.b);
    glTranslated(position.x, position.y, position.z);
    if(orient.x) glRotated(orient.x, 1, 0, 0);
    if(orient.y) glRotated(orient.y, 0, 1, 0);
    if(orient.z) glRotated(orient.z, 0, 0, 1);
    glScaled(size.w, size.h, size.d);

    glBegin(GL_QUADS);

    for(unsigned short i = 3; i < QTY_CUBE_VERTICES; i += 3)
    {
        glVertex3d(CUBE_VERTICES, CUBE_VERTICES, CUBE_VERTICES);
    }

    glEnd();

    glPopMatrix();
}

</pre></div><!–ENDSCRIPT–>

Anyone know why this is happening?

Edit: I distanced the cubes further apart (by a larger y distance) and the twitching stops.. still don't know why it's happening though.

Cheers.

Advertisement
Do you have both depth testing and depth writing turned on?
I'm just adding to a framework provided by our lecturer, so this is what he intitialises OpenGL with:
glClearColor(0.0,0.0,0.0,0.0);						// select what colour the background is (here set to black)	glClearDepth(1.0f);									// Depth Buffer Setup	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing (using Z-buffer)	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// give OpenGL a hint on which perspective calculation to use (here suggesting the nicest)	SwapBuffers(hDC);	// draw the screen (will draw a black blank screen as no drawing has been done yet)	glLineWidth(4);


Cheers.

What precision is your depth buffer? When you create your opengl context, how many bits did you request your depth buffer be created as?

Cheers,

Bob

[size="3"]Halfway down the trail to Hell...
Maybe try glDepthFunc( GL_LESS )? That's what is set by default.

This topic is closed to new replies.

Advertisement