Translate objects relative to the camera view

Viewed 51

I'm trying to translate objects via matrices in OpenGL and I know how to rotate/translate/scale objects in model coordinates.

enter image description here

However, I want to make a callback to translate objects relative to the camera view (so, the z-axis always looks at the camera):

enter image description here

I'm operating with objects using the MVP matrix. Does somebody know how to translate objects on the screen coordinates, not world ones?

1 Answers

So you have this sequence of matrices: project * view * model * <vertices>.

Going right-to-left:

  • model converts the model from model space to world space.
  • view converts it from world space to camera space.
  • project converts it from camera space to clip space.

You have your offset in camera space, and want to convert it to model space, so you need to apply:

  • view.inverse() to transform from camera space to world space.
  • model.inverse() to transform from world space to model space.

Those are too ordered right-to-left, so:

modelspace_offset = model.inverse() * view.inverse() * cameraspace_offset
Related