我的相机在OpenGL中颠倒了我的问题。 如果我在没有将相机Z旋转设置为180度的情况下渲染我的对象,则对象会被颠倒渲染。 也许它与glm有关?
以下是我设置矩阵的方法:
//Model Matrix:
mat4 modelMatrix;
modelMatrix = translate(modelMatrix, vec3(entity.getPosition().x, entity.getPosition().y, entity.getPosition().z));
modelMatrix = rotate(modelMatrix, (float) (entity.getRotation().x / 180 * PI), vec3(1, 0, 0));
modelMatrix = rotate(modelMatrix, (float) (entity.getRotation().y / 180 * PI), vec3(0, 1, 0));
modelMatrix = rotate(modelMatrix, (float) (entity.getRotation().z / 180 * PI), vec3(0, 0, 1));
modelMatrix = scale(modelMatrix, entity.getScale());
return modelMatrix;
//View Matrix:
mat4 viewMatrix;
viewMatrix = rotate(viewMatrix, (float)(camera.getRotation().x / 180 * PI), vec3(1, 0, 0));
viewMatrix = rotate(viewMatrix, (float)(camera.getRotation().y / 180 * PI), vec3(0, 1, 0));
viewMatrix = rotate(viewMatrix, (float)(camera.getRotation().z / 180 * PI), vec3(0, 0, 1));
viewMatrix = translate(viewMatrix, camera.getPosition() * vec3(-1, -1, -1));
return viewMatrix;
//Projection Matrix:
return glm::perspective(camera.getFieldOfView(), Display::getWindowAspectRatio(), camera.getNearPlane(), camera.getFarPlane());
这是我的绘画方法:
for (int i = 0; i < entityMap.size(); i++)
{
map<GLuint, vector<Entity>>::iterator iterator(entityMap.begin());
advance(iterator, i);
vector<Entity> entityBatch = iterator->second;
Model entityModel = entityBatch[0].getModel();
mat4 *modelMatrices = new mat4[entityBatch.size()];
for (int j = 0; j < entityBatch.size(); j++)
{
modelMatrices[j] = Maths::createModelMatrix(entityBatch[j]);
}
glBindVertexArray(iterator->first);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glEnableVertexAttribArray(5);
GLuint vertexBufferObjectId;
glGenBuffers(1, &vertexBufferObjectId);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId);
glBufferData(GL_ARRAY_BUFFER, sizeof(mat4) * entityBatch.size(), modelMatrices, GL_STATIC_DRAW);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(vec4), (GLvoid*)(0 * sizeof(vec4)));
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(vec4), (GLvoid*)(1 * sizeof(vec4)));
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(vec4), (GLvoid*)(2 * sizeof(vec4)));
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(vec4), (GLvoid*)(3 * sizeof(vec4)));
glVertexAttribDivisor(2, 1);
glVertexAttribDivisor(3, 1);
glVertexAttribDivisor(4, 1);
glVertexAttribDivisor(5, 1);
#if ENABLE_INDEXING
glDrawElementsInstanced(GL_TRIANGLES, entityModel.getIndexCount(), GL_UNSIGNED_INT, 0, entityBatch.size());
#else
glDrawArraysInstanced(GL_TRIANGLES, 0, entityModel.getVertexCount(), entityBatch.size());
#endif
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
glDisableVertexAttribArray(4);
glDisableVertexAttribArray(5);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &vertexBufferObjectId);
glBindVertexArray(0);
}
谢谢你的帮助!
答案 0 :(得分:7)
问题在于我的投影矩阵。
我忘了glm在弧度下工作,并没有将视场从度数转换为弧度。现在一切都很好。
我还有一个问题。 你需要给glm :: perspect
感谢您的帮助!