Hi, I have what seems a very simple problem, and am a total OpenGL beginner, so be gentle :-)
To learn the basics, I have a class (C++) which draws a cube. I then create multiple instances of this class (currently < 10) to position cubes around the 3D space with different colors, lighting properties and rotations.
My problem is with rotation - as soon as I add what I think is the proper rotation code, I get random/intermittent rendering bugs (most often in that some cubes aren't displayed at all).
My setup code is:
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
//allow the native colour of polygons to be shown under lighting
glEnable (GL_COLOR_MATERIAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
And my code looks like this for each render pass:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1 * aspectRatio, 1 * aspectRatio, -1, 1, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
glLoadIdentity();
//translate everything "into" the screen...
glTranslatef(0.0f,0.0f,-1.5f);
//iterate through the cube instances, calling their render routines (see below)...
SwapBuffers (*hDC);
A cube's rendering process looks like this:
glPushMatrix();
//so it rotates our cube around the cube's centre, not the screen centre, translate to our drawing position, then draw the cube centred
glTranslatef(xPos,yPos,zPos);
//3 rotation factors, for x, y and z
glRotatef(xRotation, 1.0f, 0.0f, 0.0f);
glRotatef(yRotation, 0.0f, 1.0f, 0.0f);
glRotatef(zRotation, 0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glColor3f(redAmount, greenAmount, blueAmount);
//draw cube...
glEnd();
glPopMatrix();
But, as I said, as soon as I add the above glRotatef rotation code, I get intermittent and seemingly random rendering errors, like things disappearing entirely, or even the background ("clear color") changing color. Comment it out and everything's fine again. I figure it's not glRotatef on its own which is causing this, so am I doing something hugely wrong here?
PS: The rotation does actually "work", when it's possible to see it, so the cubes are rotated to the correct angles.