How do I make a gun alignment to camera matrix in OpenGL GLSL?Panning with the OpenGL Camera / View...

Avoiding morning and evening handshakes

It took me a lot of time to make this, pls like. (YouTube Comments #1)

Talents during the time of Achasverous

How to get Region text from address id (Magento 2)

What is the difference between ashamed and shamed?

A Wacky, Wacky Chessboard (That Makes No Sense)

How can I get the count of how many times a string appears in my list?

g++ and clang++ different behaviour with recursive initialization of a static member

What can I substitute for soda pop in a sweet pork recipe?

Compare four integers, return word based on maximum

Where was Karl Mordo in Infinity War?

How to get the sitecore field updated date instead of item updated date?

How to define a macro with multiple optional parameters?

Why do members of Congress in committee hearings ask witnesses the same question multiple times?

If all harmonics are generated by plucking, how does a guitar string produce a pure frequency sound?

What are these green text/line displays shown during the livestream of Crew Dragon's approach to dock with the ISS?

What do the pedals on grand pianos do?

Can chords be played on the flute?

Why can I easily sing or whistle a tune I've just heard, but not as easily reproduce it on an instrument?

Could quantum mechanics be necessary to analyze some biology scenarios?

Auto Insert date into Notepad

If I delete my router's history can my ISP still provide it to my parents?

Is Draco portrayed in the canon books as good-looking?

Dilemma of explaining to interviewer that he is the reason for declining second interview



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


Panning with the OpenGL Camera / View Matrixrotate opengl mesh relative to cameraOpenGL - Understanding the relationship between Model, View and World MatrixGLSL does weird things to my view matrixOpenGL - Calculating camera view matrixOpenGL/GLM CameraDoing a turntable camera animation on a world matrix rather than view matrixHow to find the “up” direction of the view matrix, with GLMOpenGL camera movement with mouse buttonsOpenGL 2D camera













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 17 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
            15 hours ago












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












          • $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
            11 hours ago










          • $begingroup$
            I am moving the camera and the model at the same rate
            $endgroup$
            – rjhwinner03
            11 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
            15 hours ago












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












          • $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
            11 hours ago










          • $begingroup$
            I am moving the camera and the model at the same rate
            $endgroup$
            – rjhwinner03
            11 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
            15 hours ago












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












          • $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
            11 hours ago










          • $begingroup$
            I am moving the camera and the model at the same rate
            $endgroup$
            – rjhwinner03
            11 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 16 hours ago









          BramBram

          2,2441015




          2,2441015












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












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












          • $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
            11 hours ago










          • $begingroup$
            I am moving the camera and the model at the same rate
            $endgroup$
            – rjhwinner03
            11 hours ago


















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












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












          • $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
            11 hours ago










          • $begingroup$
            I am moving the camera and the model at the same rate
            $endgroup$
            – rjhwinner03
            11 hours ago
















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






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














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






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














          $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
          11 hours ago




          $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
          11 hours ago












          $begingroup$
          I am moving the camera and the model at the same rate
          $endgroup$
          – rjhwinner03
          11 hours ago




          $begingroup$
          I am moving the camera and the model at the same rate
          $endgroup$
          – rjhwinner03
          11 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

          El tren de la libertad Índice Antecedentes "Porque yo decido" Desarrollo de la...

          Castillo d'Acher Características Menú de navegación

          Connecting two nodes from the same mother node horizontallyTikZ: What EXACTLY does the the |- notation for...