更改 lookatmatrix 后 OpenGL 消失的元素
OpenGL disappearing element after changing lookatmatrix
我的立方体的顶点定义为
float vertices[180] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
其中每行的最后两个坐标是纹理坐标。
在顶点着色器中,我只是将顶点位置乘以 viewMatrix
,目前还没有考虑应用任何 modelMatrix
或 projectionMatrix
。
这是我的顶点着色器:
layout (location = 0) in vec3 aPos;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
void main()
{
gl_Position =viewMatrix * vec4(aPos, 1.0f);
}
现在,当我像这样更改 viewMatrix
时:
viewMatrix_ = glm::lookAt(glm::vec3(0, 0.0f, 2.0f), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
我的对象完全消失了。
这是我的渲染循环:
while (!glfwWindowShouldClose(window_)) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // also clear the depth buffer now!
auto elapsedTime = GetElapsedTime();
shaderProgram_.Use();
// change the color
HSV::RGB timeColor = HSV::HSVtoRGB(static_cast<int>(elapsedTime* 100) % 360, 100, 100);
shaderProgram_.SetUniformVec3("ResColor", { timeColor.r / 255, timeColor.g / 255, timeColor.b / 255 });
viewMatrix_ = glm::lookAt(glm::vec3(0, 0.0f, 2.0f), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
shaderProgram_.SetUniformMat4("viewMatrix", viewMatrix_);
glBindVertexArray(VAO_);
glDrawArrays(GL_TRIANGLES, 0, 36);
//glDrawElements(GL_TRIANGLES, 30, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window_);
glfwPollEvents();
}
投影矩阵是单位矩阵。因此近平面为-1,远平面为1。相机与立方体中心的距离为2。因此立方体被远平面裁剪。
改变相机的位置
viewMatrix_ = glm::lookAt(glm::vec3(0, 0.0f, 2.0f), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
viewMatrix_ = glm::lookAt(glm::vec3(0, 0, 0), glm::vec3(0, 0, -2.0f), glm::vec3(0, 0, 0));
或者,您可以使用正交投影矩阵,在其中增加到远平面的距离(参见 glm::ortho
)
glm::mat4 projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 10.0f);
或者透视投影(glm::perspective
)
glm::mat4 projection = glm::perspective(glm::radians(60.0f), 1.0f, 0.1f, 10.0f);
我的立方体的顶点定义为
float vertices[180] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
其中每行的最后两个坐标是纹理坐标。
在顶点着色器中,我只是将顶点位置乘以 viewMatrix
,目前还没有考虑应用任何 modelMatrix
或 projectionMatrix
。
这是我的顶点着色器:
layout (location = 0) in vec3 aPos;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
void main()
{
gl_Position =viewMatrix * vec4(aPos, 1.0f);
}
现在,当我像这样更改 viewMatrix
时:
viewMatrix_ = glm::lookAt(glm::vec3(0, 0.0f, 2.0f), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
我的对象完全消失了。
这是我的渲染循环:
while (!glfwWindowShouldClose(window_)) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // also clear the depth buffer now!
auto elapsedTime = GetElapsedTime();
shaderProgram_.Use();
// change the color
HSV::RGB timeColor = HSV::HSVtoRGB(static_cast<int>(elapsedTime* 100) % 360, 100, 100);
shaderProgram_.SetUniformVec3("ResColor", { timeColor.r / 255, timeColor.g / 255, timeColor.b / 255 });
viewMatrix_ = glm::lookAt(glm::vec3(0, 0.0f, 2.0f), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
shaderProgram_.SetUniformMat4("viewMatrix", viewMatrix_);
glBindVertexArray(VAO_);
glDrawArrays(GL_TRIANGLES, 0, 36);
//glDrawElements(GL_TRIANGLES, 30, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window_);
glfwPollEvents();
}
投影矩阵是单位矩阵。因此近平面为-1,远平面为1。相机与立方体中心的距离为2。因此立方体被远平面裁剪。
改变相机的位置
viewMatrix_ = glm::lookAt(glm::vec3(0, 0.0f, 2.0f), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
viewMatrix_ = glm::lookAt(glm::vec3(0, 0, 0), glm::vec3(0, 0, -2.0f), glm::vec3(0, 0, 0));
或者,您可以使用正交投影矩阵,在其中增加到远平面的距离(参见 glm::ortho
)
glm::mat4 projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 10.0f);
或者透视投影(glm::perspective
)
glm::mat4 projection = glm::perspective(glm::radians(60.0f), 1.0f, 0.1f, 10.0f);