Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Object Rotation

This topic is 5573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I need to rotate (opengl) objects by mouse motion on the screen. Do you know deep exploration software or 3D exploration ? Well, i want to move objects like this software do. When u drag mouse horizontaly, it rotates the object on the X axis, when you drag mouse verticaly, it rotates the object on the Y axis. In this code, the first called rotation function works as i want (y rotation) but the second (x rotation) doesnt rotate the teapot on a static origin because the first rotation rotate the X axis. How to use static axis for rotation ? I think i must compensate by using the Z rotation, but i dont know wich value to use. #-------- HERE IS A GLUT SAMPLE CODE float spinX = 0, // rotation values spinY = 0, spinZ = 0; int mouseX = 0, // current mouse position mouseY = 0; int oldMouseX = 0, // old mouse position oldMouseY = 0; void mouse(int x, int y) { oldMouseX = mouseX; oldMouseY = mouseY; mouseX = y; mouseY = x; spinX += mouseX-oldMouseX; spinY += mouseY-oldMouseY; //spinZ ??? } void render(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0, 0, -5); glRotatef(spinY ,0, 1, 0); glRotatef(spinX, 1, 0, 0); glColor3f(1, 0, 0); glutWireTeapot(2); glBegin(GL_LINES); glColor3f(1, 1, 1); glVertex3f(5, 0, 0); glVertex3f(0, 0, 0); glVertex3f(0, 5, 0); glVertex3f(0, 0, 0); glVertex3f(0, 0, 5); glVertex3f(0, 0, 0); glEnd(); glutSwapBuffers ( ); } void init(void) { glShadeModel(GL_SMOOTH); glClearColor(0, 0, 0, 0.5); glClearDepth(1); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (h == 0) h = 1; gluPerspective(80, (float)w/(float)h, 1, 5000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(500, 500); glutCreateWindow("OpenGL Framework"); glutDisplayFunc(render); glutReshapeFunc(reshape); glutMotionFunc(mouse); glutIdleFunc(render); glutMainLoop(); return 0; }

Share this post

Link to post
Share on other sites
If i understand you correctly... Just push a new matrix after your first rotation.

Share this post

Link to post
Share on other sites
how to do that ?


glRotated(x, 1, 0, 0);
glPushMatrix(); // <---
glRotated(y, 0, 1, 0);


glPopMatrix(); //<----

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!