Jump to content
  • Advertisement

Archived

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

encom

Object Rotation

This topic is 5653 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
Advertisement
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 ?

draw()
{
glLoadIdentity();

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

glutWireTeapot(2);

glPopMatrix(); //<----
swapBuffers();
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!