#### Archived

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

# 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.

## 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 on other sites
If i understand you correctly... Just push a new matrix after your first rotation.

##### Share on other sites
how to do that ?

draw()
{

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

glutWireTeapot(2);

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

##### Share on other sites
Yeah, that do what you were looking to do?

##### Share on other sites
Hmmmm, your solution doesnt work...

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013165
×