Sign in to follow this  
GrayedOut

[OpenGL] glRotatef causing rendering errors?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
I just started having this issue as well. Oddly enough, the same code compiles and runs fine on my OS X machine- i get funky behavior only when using VC++ 2005. Also, if i switch from the Release to Debug configuration in VC++ the behavior goes away, but my objects do not rotate properly anymore.

Share this post


Link to post
Share on other sites
Are you defining any lights at all? If not, you shouldn't enable lighting.

Apart from that, your code looks fine... I don't know if lighting could cause such bugs.

Share this post


Link to post
Share on other sites
I did indeed define one light - apologies for not including that code, but since the errors only started once I introduced the rotation, I assumed that was not the problem. I've tried removing all the lighting code, and it still does weird things....

If I was getting this kind of behaviour in non-OpenGL program logic, I'd be looking for some kind of memory issue like a bad pointer, but since it only affects the OpenGL display, I'm confused.

Detroit's comment made me wonder if my compiler was doing something screwy, so I checked that out, and I'm fairly sure it's not to blame.

Anyway, it's helpful to know that my code appears correct to someone with more knowledge on this than me [grin] I'll go for some more violent debugging on it...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this