Sign in to follow this  
reducer

Camera rotate and translate

Recommended Posts

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:

[code]
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);
[/code]

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:

[code]
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);
[/code]

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.
[code]
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();
}
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='larspensjo' timestamp='1343300524' post='4963248']
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.
[/quote]

Why not?
[code]
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
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this