我的透视矩阵做得很奇怪,它使我的3d金字塔看起来像梯形。
如果我旋转金字塔,则该效果是一致的,因此我认为它与我的顶点没有任何关系。是什么导致这种影响发生?
动画循环:
//enables depth testing
glEnable(GL_DEPTH_TEST);
//creates aspect ratio variable
float aspect = 500.0f / 500.0f;
//enables projection matrix
glm::mat4 pmat = glm::perspective(70.0f, aspect, 0.01f, 1000.0f);
//enables view matrix
glm::vec3 eye(0, -1, 2);
glm::vec3 center(0, 0, 0);
glm::vec3 up(0, 1, 0);
glm::mat4 vmat = glm::lookAt(eye, center, up);
while (!glfwWindowShouldClose(mWindow)) //animation loop
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 mvp = pmat * vmat * tmat;
glUniformMatrix4fv(uTransform, 1, GL_FALSE, glm::value_ptr(mvp));
for (int i = 0; i <= numberofshapes; i++)
{
glBindVertexArray(shapes[i].vao);
glDrawArrays(shapes[i].drawtype, 0, shapes[i].numOfvertices);
}
if (w == true) {
tmat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.001f, 0.0f)) * tmat;
}if (a == true) {
tmat = glm::translate(glm::mat4(1.0f), glm::vec3(-0.001f, 0.0f, 0.0f)) * tmat;
}if (s == true) {
tmat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -0.001f, 0.0f)) * tmat;
}if (d == true) {
tmat = glm::translate(glm::mat4(1.0f), glm::vec3(0.001f, 0.0f, 0.0f)) * tmat;
}if (r == true) {
tmat = glm::rotate(glm::mat4(1.0f), glm::radians(0.1f), glm::vec3(0.0f, 1.0f, 0.0f)) * tmat;
}
glfwSwapBuffers(mWindow);
counter += 1;
glfwPollEvents();
}
顶点数组:
vertex vertices[] = {
mkVert(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f),
mkVert(-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f),
mkVert(0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f)
};
//SECOND OBJECT
vertex vertices2[] = {
mkVert(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f),
mkVert(-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f),
mkVert(-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f)
};
//THIRD OBJECT
vertex vertices3[] = {
mkVert(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f),
mkVert(0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f),
mkVert(0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f)
};
vertex vertices4[] = {
mkVert(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f),
mkVert(0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f),
mkVert(-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f)
};
仅供参考,我的窗口是500x500,我不知道这是否会导致这种效果。