OpenGL Camera Issues

Started by
1 comment, last by stein 15 years, 3 months ago
I've written a couple other openGL camera's before, but for some reason this one is giving me trouble and I just can't see where the problem is.

class Camera  {
public:
   Camera(const Camera & cam)  {
      _transform = cam._transform;
   }

   Camera(Vector & position, Vector & lookAtPoint, Vector & up)  {
      Vector look = lookAtPoint - position;
      VectorMath::Normalize(look);

      Vector right = VectorMath::Cross(up, look);

      _transform.GetColumn(0) = right;
      _transform.GetColumn(1) = VectorMath::Cross(look, right);
      _transform.GetColumn(2) = look;
      _transform.GetColumn(3) = position;
      _normalize();

      _transform.InvertAffine();

   }

   void SetView() {
      glLoadMatrixf(_transform.ToGLMatrix());
   }

   void SetPosition(Vector & position)  {
      _transform[3] = position * -1.0f;
   }

   void Rotate(float amount, Vector & axis)  {
      float temp[16];
      glMatrixMode(GL_MODELVIEW);
	   glPushMatrix();

	   glLoadMatrixf(_transform.ToGLMatrix());
	   glRotatef(-amount, axis.x, axis.y, axis.z);

	   glGetFloatv(GL_MODELVIEW_MATRIX, temp);
      _transform.FromGLMatrix(temp);

	   glPopMatrix();
   }

   void Move(Vector & amount)  {
      _transform[3].x += -amount.x;
      _transform[3].y += -amount.y;
      _transform[3].z += -amount.z;
   }

   inline Vector GetPosition() {
      return _transform[3] * -1.0f;
   }

   inline Vector GetLook() {
      return _transform.GetRow(2);
   }

private:

   void _normalize()  {
      VectorMath::Normalize(_transform[0]);
      VectorMath::Normalize(_transform[1]);
      VectorMath::Normalize(_transform[2]);
   }

   Matrix _transform;
};


And in my rendering loop:

mainCam.SetView();

// For brevity the input handling code is omitted but certain 
// input calls the following functions
mainCam.Rotate(len * 0.5f, axis);
mainCam.Move(Vector(dx * 0.05f, -dy * 0.05f));


The problem is that rotations don't properly rotate the camera. When I call Rotate() it just rotates the objects around the origin rather than rotating the camera around it's local coordinates. I'm sure I just am having a mental block but I've been playing with it for a few hours and haven't made any progress.
Advertisement
Quote:Original post by BlodBath
The problem is that rotations don't properly rotate the camera. When I call Rotate() it just rotates the objects around the origin rather than rotating the camera around it's local coordinates.


Well, when you say rotate about the origin, do you mean the origin of the scene (which you might be looking at), or do you mean the origin of the camera. Because the last thing is the exact same thing as rotating the camera around itself (in the opposite direction).
Sounds to me like you need to change the order of translation and rotation.
| Stein Nygård - http://steinware.dk |

This topic is closed to new replies.

Advertisement