摄像头颠倒了OpenGL

3
我在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);
}

感谢您的帮助!

1
如果不展示你是如何绘制和设置矩阵的,这个问题就无法回答。 - BDL
旋转的值是什么?如果您根本不应用任何旋转,是否会发生相同的事情?我可能知道发生了什么,但这并不能解释为什么会翻转,除非涉及旋转。 - Reto Koradi
我尝试不应用旋转,但它没有起作用。顺便说一下,只有相机被翻转了。世界坐标被反转,物体看起来是倒立的。 - Linas
1个回答

9

我的问题出在投影矩阵上。

我忘记了glm使用弧度制,没有将视野从角度转换为弧度。现在一切都正常了。

我还有一个问题。 你需要给出glm::perspect吗?

感谢你的所有帮助!


当我尝试在Blender中模拟相机视图时,遇到了这个问题。将FOV从45设置为49.134(适用于35mm胶片),Y轴翻转了。在所有这些时间里,我一直使用45“度”作为我的FOV,实际上我一直在使用45弧度,它约等于58度。哈! - Chris Watts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接