views:

76

answers:

3

Hello! I have the same qustion as in the title :/ I make something like:

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0f, 0, -zoom);
  glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
  glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);

  //Here model render :/

And in my app camera is rotating not model :/

A: 

You use GL_MODELVIEW when defining the transformation for your objects and GL_PROJECTION to transform your viewpoint.

andrewmu
He is already using GL_MODELVIEW. I hope you're not saying to use GL_PROJECTION to move the camera. See http://sjbaker.org/steve/omniv/projection_abuse.html
LarsH
Wow, ok. I used to do parallel projections with my own lighting, so that wouldn't have been an issue, but yes, I see why this would normally be bad.
andrewmu
@andrewmu: FYI, the -1 vote is not mine.
LarsH
+4  A: 

Presumably the reason you believe it's your camera moving and not your model is because all the objects in the scene are moving together?

After rendering your model and before rendering other objects, are you resetting the MODELVIEW matrix? In other words, are you doing another glLoadIdentity() or glPopMatrix() after you render the model you're talking about and before rendering other objects?

Because if not, whatever transformations you applied to that model will also apply to other objects rendered, and it will be as if you rotated the whole world (or the camera).

I think there may be another problem with your code though:

  glTranslatef(0.0f, 0, -zoom);
  glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
  glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
  //Here model render :/

Are you trying to rotate your model around the point (0, 0, -zoom)?

Normally, in order to rotate around a certain point (x,y,z), you do:

  1. Translate (x,y,z) to the origin (0,0,0) by translating by the vector (-x,-y,-z)
  2. Perform rotations
  3. Translate the origin back to (x,y,z) by translating by the vector (x,y,z)
  4. Draw

If you are trying to rotate around the point (0,0,zoom), you are missing step 3. So try adding this before you render your model:

glTranslatef(0.0f, 0, zoom); // translate back from origin

On the other hand if you are trying to rotate the model around the origin (0,0,0), and also move it along the z-axis, then you will want your translation to come after your rotation, as @nonVirtual said.

And don't forget to reset the MODELVIEW matrix before you draw other objects. So the whole sequence would be something like:

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

#if you want to rotate around (0, 0, -zoom):
  glTranslatef(0.0f, 0, -zoom);
  glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
  glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
  glTranslatef(0.0f, 0, zoom); // translate back from origin
#else if you want to rotate around (0, 0, 0):
  glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
  glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
  glTranslatef(0.0f, 0, -zoom);
#endif    
  //Here model render :/

  glLoadIdentity();
  // translate/rotate for other objects if necessary
  // draw other objects
LarsH
I have light on my scene. rendering looks like this: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glClearColor(0.3,0.3,0.3,1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(0.0f, 0, -zoom);glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);render objects
openglNewbie
OK... but are you doing another glLoadIdentity() or glPopMatrix() after you render the model you're talking about and before rendering other objects? Because if not, whatever transformations you applied to that model will also apply to other objects rendered, and it will be as if you rotated the whole world (or the camera).
LarsH
A: 

I believe it has to do with the order in which you are applying your transformations. Try switching the order of the call to glRotate and glTranslate. As it is now you are moving it outward from the origin, then rotating it about the origin, which will give the appearance of the object orbiting around the camera. If you instead rotate it while it is still at the origin, then move it, it should give you the desired result. Hope that helps.

nonVirtualThunk
@nonV, whether he is currently moving outward from the origin or to the origin depends on where it's starting from. I assumed the model's center was initially at (0,0,zoom). But you may be right...
LarsH