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.