显示正交的 OpenGL 投影矩阵

OpenGL Projection Matrix showing Orthographic

我有一个正交相机,但我想尝试实现一个透视相机,这样我以后就可以做一些视差效果了。我在尝试实施时遇到了一些问题。深度似乎无法正常工作。我正在沿 x 轴旋转 2d 图像以模拟它稍微向下放置,这样我就可以看到投影矩阵在工作。不过它仍然显示为正交透视图。

这是我的一些代码:

CameraPersp::CameraPersp() :
    _camPos(0.0f,0.0f,0.0f), _modelMatrix(1.0f), _viewMatrix(1.0f), _projectionMatrix(1.0f)

函数调用 init 来设置矩阵变量:

void CameraPersp::init(int screenWidth, int screenHeight)
{
    _screenHeight = screenHeight;
    _screenWidth = screenWidth;

    _modelMatrix = glm::translate(_modelMatrix, glm::vec3(0.0f, 0.0f, 0.0f));
    _modelMatrix = glm::rotate(_modelMatrix, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
    _viewMatrix = glm::translate(_viewMatrix, glm::vec3(0.0f, 0.0f, -3.0f));
    _projectionMatrix = glm::perspective(glm::radians(45.0f), static_cast<float>(_screenWidth) / _screenHeight, 0.1f, 100.0f);
}

正在使用 x,y,z,width,height,src 初始化要加载的纹理

_sprites.back()->init(-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, "src/content/sprites/DungeonCrawlStoneSoupFull/monster/deep_elf_death_mage.png");

将矩阵发送到顶点着色器:

    GLint mLocation = _colorProgram.getUniformLocation("M");
    glm::mat4 mMatrix = _camera.getMMatrix();
    //glUniformMatrix4fv(mLocation, 1, GL_FALSE, &(mMatrix[0][0]));
    glUniformMatrix4fv(mLocation, 1, GL_FALSE, glm::value_ptr(mMatrix));

    GLint vLocation = _colorProgram.getUniformLocation("V");
    glm::mat4 vMatrix = _camera.getVMatrix();
    //glUniformMatrix4fv(vLocation, 1, GL_FALSE, &(vMatrix[0][0]));
    glUniformMatrix4fv(vLocation, 1, GL_FALSE, glm::value_ptr(vMatrix));

    GLint pLocation = _colorProgram.getUniformLocation("P");
    glm::mat4 pMatrix = _camera.getPMatrix();
    //glUniformMatrix4fv(pLocation, 1, GL_FALSE, &(pMatrix[0][0]));
    glUniformMatrix4fv(pLocation, 1, GL_FALSE, glm::value_ptr(pMatrix));

这是我的顶点着色器:

#version 460
//The vertex shader operates on each vertex

//input data from VBO. Each vertex is 2 floats
in vec3 vertexPosition;
in vec4 vertexColor;
in vec2 vertexUV;

out vec3 fragPosition;
out vec4 fragColor;
out vec2 fragUV;

//uniform mat4 MVP;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;

void main() {
    //Set the x,y position on the screen
    //gl_Position.xy = vertexPosition;
    gl_Position = M * V * P * vec4(vertexPosition, 1.0);

    //the z position is zero since we are 2d
    //gl_Position.z = 0.0;

    //indicate that the coordinates are nomalized
    gl_Position.w = 1.0;

   fragPosition = vertexPosition;
   fragColor = vertexColor;
   // opengl needs to flip the coordinates
   fragUV = vec2(vertexUV.x, 1.0 - vertexUV.y);
}

我可以稍微看到图像 "squish" 因为它仍在将透视渲染为正交。如果我移除 x 轴上的旋转,它就不再被压扁,因为它根本就没有放下。对我做错了什么有什么想法吗?我可以根据要求提供更多信息,但我想我已经提供了大部分内容。

图片:

你不应该修改 gl_Position.w

    gl_Position = M * V * P * vec4(vertexPosition, 1.0); // gl_Position is good

    //indicate that the coordinates are nomalized < not true
    gl_Position.w = 1.0; // Now perspective divisor is lost, projection isn't correct