在屏幕可编程管道上翻译对象

时间:2014-04-08 21:03:54

标签: c++ opengl pipeline opengl-3 glm-math

我很难确定如何在给定箭头键输入的情况下在屏幕上翻译对象。目前我没有问题移动相机,但我似乎无法绕着对象移动而不是相机。

这是我在计算查看矩阵

时所做的工作
ViewMatrix = glm::lookAt(
    position,   //camera position
    position+direction, //look at origin
    up  //head up
);

其中位置方向glm::vec3


所以要改变对象的位置,我会修改模型矩阵吗?或者我会用mvp做什么?

模型矩阵目前保持在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;

1 个答案:

答案 0 :(得分:0)

所以我最终在@ j-p的帮助下解决了这个问题。我想要做的是移动对象,所以我将glm函数translate()应用于模型矩阵。为此,我转到我的控件文件并创建了一个名为

的函数
glm::mat4 getModelMatrix();

返回了我在头文件中声明的变量glm::mat4 ModelMatrix。代码的实际部分和移动对象是这样的:

//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..

然后传回我的主循环,最终代码如下:

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;