PDA

View Full Version : camera manipulation: how to rotate my mesh on x, y, z axis



beachboy1976
05-21-2002, 07:22 AM
Hi,
I tried to implement camera manipulation for my mesh by using mouse movement. I used the following code to handle camera. However, when I clicked on mouse button, my mesh disappered from scene. I really have no idea what was wrong with my code. Please help me out with the problem and show me how to fix it.

switch (m_camera)
{
case TILT:
glMatrixMode(GL_PROJECTION);
glRotatef( 0.0, // angle
1.0, // x
0.0, // y
0.0 // z
);
break;

case PAN:
glMatrixMode(GL_PROJECTION);
glRotatef( 0.0, // angle
0.0, // x
1.0, // y
0.0 // z
);
break;

case ROLL:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glRotatef( 0.0, // angle 0.0, // x
0.0, // y
0.2 // z
);

break;

}

coredump
05-21-2002, 07:28 AM
you may want to use Gl_MODELVIEW instaed of GL_PROJECTION.

b

beachboy1976
05-21-2002, 07:34 AM
Thank you for your help.
I switch to GL_MODELVIEW and I can see my mesh now when I click on my mouse button. So How can I make my mesh rotating. I was clicking on my mouse button, nothing was happening at all. How to fix that?


Originally posted by coredump:
you may want to use Gl_MODELVIEW instaed of GL_PROJECTION.

b

05-21-2002, 07:52 AM
case ROLL:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef( 0.0, // angle 0.0, // x
0.0, // y
0.2 // z
);

Ok, look at your code up there.
Why don't you change the angle with a variable that retains it's value whenever it's changed and not constant?

beachboy1976
05-21-2002, 08:15 AM
I made the following changes for my z axis rotation and it works now.

case ROLL:
glMatrixMode(GL_MODELVIEW);
glTranslatef((float)(x - m_lStatex), (float)(m_lStatey - y), 0.0f);
m_lStatex = x;
m_lStatey = y;

glMatrixMode(GL_PROJECTION);
glRotatef( 30.0,0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
break;

Here m_lStatey, m_lStatex are CPoint.

I tried to use the same way to hanfle x, y axis rotation. It won't work right. Mesh is rotating but it is out of scene.

Please help me out here.

x axis rotation
case TILT:
glMatrixMode(GL_MODELVIEW);
glTranslatef((float)(x - m_lStatex), (float)(m_lStatey - y), 0.0f);
m_lStatex = x;
m_lStatey = y;

glMatrixMode(GL_PROJECTION);
glRotatef( 30.0, 1.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
break;


Originally posted by SPL_BINUS:
case ROLL:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef( 0.0, // angle 0.0, // x
0.0, // y
0.2 // z
);

Ok, look at your code up there.
Why don't you change the angle with a variable that retains it's value whenever it's changed and not constant?

05-21-2002, 05:22 PM
case ROLL:
glMatrixMode(GL_MODELVIEW);
// glLoadIdentity(); -->use this to clear the matrix
glTranslatef((float)(x - m_lStatex), (float)(m_lStatey - y), 0.0f);
m_lStatex = x;
m_lStatey = y;
glMatrixMode(GL_PROJECTION); // --> erase this line
glRotatef( 30.0,0.0, 0.0, 1.0); // --> what is this line doing here anyway?
glMatrixMode(GL_MODELVIEW);-->this too !
break;

Seems you don't understand the projection and modelview matrix very well. The point is you can't use the modeling transformations (glRotate*,glTranslate*,glScale*) with projection matrix ! it'll screw up your scene. You can only use it with glOrtho,glOrtho2d,glLookAt,and glFrustum. And remember that you have to choose the transformation based on local coordinate or fixed one. Look again at chapter 3 of the OpenGL red book okay.

Bob
05-22-2002, 08:14 AM
The point is you can't use the modeling transformations (glRotate*,glTranslate*,glScale*) with projection matrix ! it'll screw up your scene. You can only use it with glOrtho,glOrtho2d,glLookAt,and glFrustum.

Sure you CAN call glRotate/glTranslate/glScale on the projection matrix. OpenGL won't stop you. As long as you know what you do, there's nothing wrong doing it. I know what you mean though, you usually shouldn't transform your objects with the projection matrix. But if that's really what you mean, then gluLookAt in the projection matrix is as much no-no as calling glRotate in the projection matrix.

AsylumX
05-22-2002, 11:01 AM
To me, it looks like you're missing any control over the amount of rotation. your glRotatef() has a rotation parameter of 0.0, which means no rotation. What I recommend is to keep a few static vars xRot, yRot and zRot perhaps. Now, when your input says you should rotate along the X axis, just increment or decriment xRot. Same thing for y axis and z axis. then put those variables into the first parameter for glRotatef()

ie..

glRotatef(xRot, 1.0, 0.0, 0.0);
glRotatef(yRot, 0.0, 1.0, 0.0);
glRotatef(zRot, 0.0, 0.0, 1.0);

and boom you've got the right rotations.

This may not be the most efficient way in the world, but it will get you up and running.

This will work in the Modelview matrix.

Just to comment... you could do roughly the same with glTranslatef() and add some extra functionality.

-AsylumX

Feel free to correct me if I'm mistaken!