0

Let's say I have the following vertex shader code below:

attribute vec4 vPos;
uniform mat4 MVP;
uniform mat4 LookAt;

void main{
     gl_Position = MVP * vPos;
}

How do I use the LookAt matrix in this shader to position the eye of the camera? I have tried LookAt * MVP * vPos but that didn't seem to work as my triangle just disappeared off screen!

4

2 回答 2

1

矩阵LookAt通常称为View矩阵,并与模型到世界变换矩阵连接以形成WorldView矩阵。然后将其乘以通常是正交或透视的投影矩阵。模型空间中的顶点位置与生成的矩阵相乘,以便转换为剪辑空间(有点……我在这里跳过了几个您不必做的步骤,由硬件/驱动程序执行)。

在您的情况下,请确保您使用正确的“惯用手”进行转换。您也可以尝试以相反的顺序将位置与转换矩阵的转置相乘,如下所示vPos*T_MVP*T_LookAt

于 2015-06-01T14:02:59.257 回答
1

我建议将 LookAt 移到着色器之外,以防止对每个顶点进行不必要的计算。着色器仍然可以

gl_Position = MVP * vPos;

并且您使用 glm 在应用程序中操作 MVP。例如:

projection = glm::perspective(fov, aspect, 0.1f, 10000.0f);
view = glm::lookAt(eye, center, up);
model = matrix of the model, with all the dynamic transforms.

MVP = projection * view * model;
于 2015-06-01T14:48:10.583 回答