I am trying to program a FPS game with OpenGL.

I am using 3D eight the OpenGL 3.3 programmable pipeline.

I have a gun and a camera that I loaded.

When I load all of the assets for the game, I put everything int a 4x4 matrix, as it is compatible with the view matrix.

I set the corresponding matrix for the model equal to all the needed variables, then I use another variable that I defined in a header file to draw the actual model...

I did some searching because I do not have the code with me, and the closest I could find is learnopengl’s model loading tutorial as that is the most similar.

What I have tried; I have tried multiplying the matrix for the gun’s world info, which I will call the model view matrix (such as position and scale) times the view matrix. I have also tried doing the same but multiplying the model view times the inverse of the normal view matrix.

I already set the guns position equal to the camera’a position, but when I run the application and try to move around, the gun still moves away from the camera.

I knew a way of how to fix this in OpenGL 1.1 by using glLoadIdentity, which is now deprecated with the core version of OpenGL 3.*.

When i try to set the two matrices equal, I get different results because the gun doesn’t move at all.

When I move the camera with the inverse of view times gun, it moves away from the camera...

I learned about the inverse matrix thing from here: https://forums.khronos.org/showthread.php/81932

My matrixes are also set up the same. The gun model view * inverse(view matrix) has worked the best... this link is where I found it.


1 Answer 1


You need to transform the vertices of the gun with the Model • View • Projection matrix.

The thing to know is, that the View matrix, is just the inverse of the Camera transformation matrix.

So if you have a matrix that positions and orients your camera in the world, its inverse will be your view matrix.

You also have a matrix that positions and orients your gun.

In your FPS game, you will want these matrices to be quite similar: looking into the same direction, and maybe the camera slightly above and behind the gun.

Once you have multiplied the gun matrix, with the inverse of the camera matrix and then with the projection matrix, you have the resulting matrix that you want to feed into your shader using the glUniformMatrix4fv() call.

In pseudo code:

Mat44 viewmat = cammat.invert();
Mat44 mvp = projmat * viewmat * gunmat;
glUniformMatrix4fv( mvpUniform, 1, false, mvp );

NOTE: Depending on your math library, you may have to post-multiply or pre-multiply your matrices. In my code I have multiply them as proj * view * model. If it doesn't work for you, reverse the order.

Then, in your vertex shader, you multiply the vertex position with this matrix.

#version 150
in mediump vec4 position;
uniform highp mat4 modelviewprojmat;
void main()
    gl_Position = modelviewprojmat * position;

Lastly, if you need to know how to build a projection matrix, look at the gluPerspective() call.

  • \$\begingroup\$ The gun, however still moves away from the camera... How can I fix this? \$\endgroup\$ Mar 3, 2019 at 21:02
  • \$\begingroup\$ Here is as video with the remaining issue... youtu.be/1_1XsvB12tM \$\endgroup\$ Mar 3, 2019 at 21:08
  • \$\begingroup\$ How do you move? If you move the camera and the gun forward, by the same amount. Then they'll stay together. \$\endgroup\$
    – Bram
    Mar 4, 2019 at 1:08
  • \$\begingroup\$ I am moving the camera and the model at the same rate \$\endgroup\$ Mar 4, 2019 at 1:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .