How do I make a gun alignment to camera matrix in OpenGL GLSL?












1












$begingroup$


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.










share|improve this question







New contributor




rjhwinner03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    1












    $begingroup$


    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.










    share|improve this question







    New contributor




    rjhwinner03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      1












      1








      1





      $begingroup$


      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.










      share|improve this question







      New contributor




      rjhwinner03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      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.







      opengl game-design game-mechanics






      share|improve this question







      New contributor




      rjhwinner03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      rjhwinner03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      rjhwinner03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 hours ago









      rjhwinner03rjhwinner03

      111




      111




      New contributor




      rjhwinner03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      rjhwinner03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      rjhwinner03 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          2












          $begingroup$

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


          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.






          share|improve this answer









          $endgroup$













          • $begingroup$
            The gun, however still moves away from the camera... How can I fix this?
            $endgroup$
            – rjhwinner03
            4 hours ago












          • $begingroup$
            Here is as video with the remaining issue... youtu.be/1_1XsvB12tM
            $endgroup$
            – rjhwinner03
            3 hours ago













          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "53"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          rjhwinner03 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f168614%2fhow-do-i-make-a-gun-alignment-to-camera-matrix-in-opengl-glsl%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2












          $begingroup$

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


          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.






          share|improve this answer









          $endgroup$













          • $begingroup$
            The gun, however still moves away from the camera... How can I fix this?
            $endgroup$
            – rjhwinner03
            4 hours ago












          • $begingroup$
            Here is as video with the remaining issue... youtu.be/1_1XsvB12tM
            $endgroup$
            – rjhwinner03
            3 hours ago


















          2












          $begingroup$

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


          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.






          share|improve this answer









          $endgroup$













          • $begingroup$
            The gun, however still moves away from the camera... How can I fix this?
            $endgroup$
            – rjhwinner03
            4 hours ago












          • $begingroup$
            Here is as video with the remaining issue... youtu.be/1_1XsvB12tM
            $endgroup$
            – rjhwinner03
            3 hours ago
















          2












          2








          2





          $begingroup$

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


          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.






          share|improve this answer









          $endgroup$



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


          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 4 hours ago









          BramBram

          2,2441015




          2,2441015












          • $begingroup$
            The gun, however still moves away from the camera... How can I fix this?
            $endgroup$
            – rjhwinner03
            4 hours ago












          • $begingroup$
            Here is as video with the remaining issue... youtu.be/1_1XsvB12tM
            $endgroup$
            – rjhwinner03
            3 hours ago




















          • $begingroup$
            The gun, however still moves away from the camera... How can I fix this?
            $endgroup$
            – rjhwinner03
            4 hours ago












          • $begingroup$
            Here is as video with the remaining issue... youtu.be/1_1XsvB12tM
            $endgroup$
            – rjhwinner03
            3 hours ago


















          $begingroup$
          The gun, however still moves away from the camera... How can I fix this?
          $endgroup$
          – rjhwinner03
          4 hours ago






          $begingroup$
          The gun, however still moves away from the camera... How can I fix this?
          $endgroup$
          – rjhwinner03
          4 hours ago














          $begingroup$
          Here is as video with the remaining issue... youtu.be/1_1XsvB12tM
          $endgroup$
          – rjhwinner03
          3 hours ago






          $begingroup$
          Here is as video with the remaining issue... youtu.be/1_1XsvB12tM
          $endgroup$
          – rjhwinner03
          3 hours ago












          rjhwinner03 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          rjhwinner03 is a new contributor. Be nice, and check out our Code of Conduct.













          rjhwinner03 is a new contributor. Be nice, and check out our Code of Conduct.












          rjhwinner03 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Game Development Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f168614%2fhow-do-i-make-a-gun-alignment-to-camera-matrix-in-opengl-glsl%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to label and detect the document text images

          Vallis Paradisi

          Tabula Rosettana