Jump to content
  • Advertisement
Sign in to follow this  
reducer

Camera rotate and translate

This topic is 2187 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:


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();
}

Share this post


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

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.


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!