1. ## Emergency Help

Hi All,
I met a very odd problem. I using glRotatef() to rotate a cube. My problem is the cube rotates according to one axis, after a while, it rotates according to another axis. I do not change anything at this time. Ater a while, it changes back. Does someone know how to deal with this problem? Many thanks!
This is my code:
#include <iostream.h>
#include <stdio.h>
#include <GL/glut.h>

GLfloat vertices[][3]={
{-1.0,-1.0,1.0},{-1.0,1.0,1.0},{1.0,1.0,1.0},
{1.0,-1.0,1.0},{-1.0,-1.0,-1.0},{-1.0,1.0,-1.0},
{1.0,1.0,-1.0},{1.0,-1.0,-1.0}};

GLfloat colors[][3]={
{1.0,0.0,0.0},{0.0,1.0,1.0},{1.0,1.0,0.0},
{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0}};

char key;
bool RotateSign=false, EyeSign=true;
GLfloat Theta=0.0,dx=0.0,dy=0.0,dz=1.0,Eye_x=0.0,Eye_y=0.0 ,Eye_z=1.0;

void polygon(int a, int b, int c, int d)
{
glColor3fv(colors[a]);
glBegin(GL_POLYGON);
glVertex3fv(vertices[a]);
glVertex3fv(vertices[B]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glEnd();
}

void cube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(3,0,4,7);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(5,4,0,1);
}

void idle()
{
}

void myidle()
{
Theta=Theta+2.0;
if(Theta>=360) Theta=Theta-360;
glutPostRedisplay();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
gluLookAt(Eye_x,Eye_y,Eye_z,0.0,0.0,0.0,0.0,1.0,0. 0);
glRotatef(Theta,dx,dy,dz);
cube();
glFlush();
glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
glMatrixMode(GL_MODELVIEW);
}

void mymouse(int button,int state, int x, int y)
{

if(state==GLUT_DOWN && button==GLUT_LEFT_BUTTON)
{
RotateSign=!RotateSign;
if(RotateSign==true)
glutIdleFunc(myidle);
else glutIdleFunc(idle);
}

if(state==GLUT_DOWN && button==GLUT_RIGHT_BUTTON)
{
EyeSign=!EyeSign;
if(EyeSign==true)
{
Eye_x=0.0;
Eye_y=0.0;
Eye_z=1.0;

}
else
{
Eye_x=1.0;
Eye_y=1.0;
Eye_z=1.0;
}
myidle();
}
}

void mykey(unsigned char key, int x, int y)
{
if(key=='x'&#0124; &#0124;key=='X')
{
dx=1.0;
dy=0.0;
dz=0.0;
}

if(key=='y'&#0124; &#0124;key=='Y')
{
dx=0.0;
dy=1.0;
dz=0.0;
}

if(key=='z'&#0124; &#0124; key=='Z')
{
dx=0.0;
dy=0.0;
dz=1.0;
}
myidle();
}

int main(int argc, char** argv)
{
//intro();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(600,600);
glutCreateWindow("Assignment6");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(mykey);
glutMouseFunc(mymouse);
glutMainLoop();
return 0;
}

2. ## Re: Emergency Help

Pretty simple: You have no depth buffer.
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPT H);
and this in display
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

[This message has been edited by Relic (edited 12-05-2002).]

3. ## Re: Emergency Help

Hi Relic:
Thank you very much for your help! You save my life!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•