Camera rotate and translate

Started by
1 comment, last by reducer 11 years, 8 months ago
I am having trouble properly understanding about rotating a camera. I just can't get it to work properly. I am trying to achieve camera behaviour similar to what you see in most 3d modelling packages. I would like the camera to rotate around a given point at a distance of zoom factor (mZoom in the code below). So for e.g if i pan the camera on the x axis 1 unit and the y axis 1 unit the camera rotates around Vec3(1,1,0) at a distance of mZoom while looking at that point.

Case a:


glLoadIdentity();
glTranslatef(0,0,-mZoom);
glRotatef(mRotation.x,1.0,0.0,0.0);
glRotatef(mRotation.y,0.0,1.0,0.0);
glTranslatef(-mPosition.x,-mPosition.y,-mPosition.z);


With case a, it works to a certain degree. What goes wrong is when i try to pan left/right after rotating it feels like the scene itself is panning. For e.g if i rotate around the Y axis by 180 degrees the panning is completely reversed. However, the camera rotates exactly as i wanted.

Case b:


glLoadIdentity();
glTranslatef(0,0,-mZoom);
glTranslatef(-mPosition.x,-mPosition.y,-mPosition.z);
glRotatef(mRotation.x,1.0,0.0,0.0);
glRotatef(mRotation.y,0.0,1.0,0.0);


With Case b: Now the panning works 100% correct but the rotating no longer rotates around the point i want it too.

What do i have to do to get it right? There is something i am just not understanding correctly.

If it helps this is the mouse press event. It's Qt but should be readable to most. The emit signals are just for the gui to display the position/rotations.

void RenderWidget::mouseMoveEvent(QMouseEvent *e)
{
const QPointF delta = e->posF() - mLastPos;
if (e->buttons() & Qt::LeftButton)
{
mRotation +=Vec3(delta.y(), delta.x(), 0) * 0.3f;
emit cameraRotationXChanged(mRotation.x);
emit cameraRotationYChanged(mRotation.y);
}
else if (e->buttons() & Qt::RightButton)
{
mPosition.x -= delta.x()/100.0f;
mPosition.y += delta.y()/100.0f;
emit cameraPositionXChanged(mPosition.x);
emit cameraPositionYChanged(mPosition.y);
}
mLastPos = e->posF();
}
Advertisement
You do realize that you are using deprecated functions of legacy OpenGL?

You can't use most of this experience and knowledge in a "real" application.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

You do realize that you are using deprecated functions of legacy OpenGL?

You can't use most of this experience and knowledge in a "real" application.


Why not?

glm::mat4 view = glm::translate(glm::vec3(0,0,-mZoom));
view = glm::rotate(view,mRotation.x,X_VECTOR);
view = glm::rotate(view,mRotation.y,Y_VECTOR);
view = glm::translate(view,glm::vec3(-mPosition.x,-mPosition.y,-mPosition.z));
glLoadMatrixf(&view[0][0]); //or send to shader if not using fixed function

This topic is closed to new replies.

Advertisement