Questions of glRotate

Started by
1 comment, last by jackylx 22 years, 6 months ago
I want to rotate a object around the center(0.0,0.0,0.0).When right or left button down,it circumgyrate in horizontal direction,and when up or down button down,it circumgyrate in apeak direction.At begining,I wrote the code as below. #include #include #include #include static int Xcoordinate=0,Ycoordinate=0; void CALLBACK XcoordinateAdd(void) { Xcoordinate=(Xcoordinate+5)%360; } void CALLBACK XcoordinateSubtract(void) { Xcoordinate=(Xcoordinate-5)%360; } void CALLBACK YcoordinateAdd(void) { Ycoordinate=(Ycoordinate+5)%360; } void CALLBACK YcoordinateSubtract(void) { Ycoordinate=(Ycoordinate-5)%360; } void CALLBACK display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glBegin(GL_LINES); glColor3f(1.0,1.0,1.0); glVertex3i(0,0,0); glVertex3i(1,0,0); glColor3f(1.0,1.0,1.0); glVertex3i(0,0,0); glVertex3i(0,1,0); glEnd(); glPushMatrix(); glRotated( (GLfloat)Xcoordinate,1.0,0.0,0.0);//Here is the problem glRotated( (GLfloat)Ycoordinate,0.0,1.0,0.0); glBegin(GL_LINES); glColor3f(1.0,0.0,0.0); glVertex3f(0,0,0); glVertex3f(0.5,0,0); glColor3f(0.0,1.0,0.0); glVertex3f(0,0,0); glVertex3f(0,0.5,0); glColor3f(0.0,0.0,1.0); glVertex3f(0,0,0); glVertex3f(0,0.0,1.5); glEnd(); glTranslated(0.0,0.0,1.5); glColor3f(0.0,1.0,1.0); auxWireCube(0.1); glPopMatrix(); glFlush(); } void myinit(void) { glShadeModel(GL_FLAT); } void CALLBACK myReshape(GLsizei w,GLsizei h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(65.0,(GLfloat)w/(GLfloat)h,1.0,20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0,0.0,-5.0); } void main(int argc,char**argv) { auxInitDisplayMode(AUX_SINGLE|AUX_RGBA); auxInitPosition(0,0,800,800); auxInitWindow(argv[0]); myinit(); auxKeyFunc(AUX_LEFT,XcoordinateSubtract); auxKeyFunc(AUX_RIGHT,XcoordinateAdd); auxKeyFunc(AUX_UP,YcoordinateAdd); auxKeyFunc(AUX_DOWN,YcoordinateSubtract); auxReshapeFunc(myReshape); auxMainLoop(display); } I know that it works incorrect and I know why it is.But how can I do to solve the problem?
Advertisement
Well, you are not very about what the problem is...

One guess could be that you should translate before you rotate.
Little Tip:
Read something about Matrix computations, it will help you figure out your problem :-)

cause: you are transforming the "coordinate system", the matrix by which the vertices of the object are multiplied.
so you first got to move it to the position then rotate ist about its own axis then draw the cube at the origin of the transformed "coordinate system".

if you first rotate, you will move your coordinate sys along the rotated axis (wherever they point to)

Hope you´ll figure this out.

This topic is closed to new replies.

Advertisement