在屏幕上转换对象可编程管道

Translating an object across the screen programmable-pipeline

本文关键字:可编程 管道 对象 转换 屏幕      更新时间:2023-10-16

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

这是我正在做的计算视图矩阵的工作

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;

所以我最终在 @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;