Search code examples
c++openglpipelineopengl-3glm-math

Translating an object across the screen programmable-pipeline


I'm having difficulty figuring out how to translate an object across the screen given arrow key inputs. Currently I have no issue moving the camera around, but I can't seem to wrap my head around making the object move instead of the camera.

Here's what I'm doing to compute the View Matrix

ViewMatrix = glm::lookAt(
    position,   //camera position
    position+direction, //look at origin
    up  //head up
);

where position and direction are glm::vec3


So to instead change the position of the object, would I modify the Model Matrix? or would I do something with mvp?

The model matrix currently remains at glm::mat4(1.0)

computeMatricesFromInputs(window,time); //function that handles input and computes viewMatrix
        glm::mat4 projectionMatrix = glm::perspective(45.0f, 4.0f/3.0f, 0.1f, 100.0f);
        glm::mat4 viewMatrix = getViewMatrix();
        glm::mat4 modelMatrix = glm::mat4(1.0);
        glm::mat4 MVP = projectionMatrix * viewMatrix * modelMatrix;

Solution

  • So I ended up figuring the problem with with help from @j-p. What I wanted to do was move the object, so I applied the glm function translate() to the model matrix. To do this I went over to my controls file and created a function called

    glm::mat4 getModelMatrix();
    

    which returned the variable glm::mat4 ModelMatrix that I had also declared in the header file. The actual portion of code and moved the object was like so:

    //If the the corresponding key is pressed...
        ModelMatrix = glm::translate(ModelMatrix, glm::vec3(0.0f, 1.0f, 0.0f); //move y 1.0f
    //else If..etc..
    

    Then passing back towards my main loop the final code would look like:

    computeMatricesFromInputs(window,time); //function that handles input and computes viewMatrix
            glm::mat4 projectionMatrix = glm::perspective(45.0f, 4.0f/3.0f, 0.1f, 100.0f);
            glm::mat4 viewMatrix = getViewMatrix();
            glm::mat4 modelMatrix = getModelMatrix();
            glm::mat4 MVP = projectionMatrix * viewMatrix * modelMatrix;