我正在尝试为现有引擎添加视差效果。到目前为止,引擎工作时都是正交投影。对象放置在屏幕上的像素坐标中。问题是我无法弄清楚如何使用透视投影矩阵等复制相同的投影。我可以为深度添加一个Z坐标。
我已经尝试了矩阵和z坐标的各种组合,结果始终是黑屏。
我要替换的矩阵:
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(1280.0f), static_cast<GLfloat>(720.0f), 0.0f, 0.0f, -100.0f);
顶点着色器:
// Shader code (I tested this while having identity matrices for view and model
#version 330 core
layout (location = 0) in vec2 vertex;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
void main() {
gl_Position = projection * view * model * vec4(vertex.xy, 1.0f, 1.0f);
}
我认为投影代码可能有效:
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-640, -310.0f, 0.0f));
model = glm::scale(model, glm::vec3(1.0f / 1280.0f, 1.0f / 720.0f, 1.0f));
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, -100.0f);
预期是在没有黑屏的情况下,矩形仍显示在相似的位置(我可以在工作正常后更正细节)。
答案 0 :(得分:2)
Perspective projection矩阵的规范是错误的。
glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, -100.0f);
glm::perspective
通过沿y轴的视场角,纵横比以及到近平面和远平面的距离来定义Viewing frustum。
因此, near 和 far 平面必须为正值(> 0),而 near 必须小于 far :
0 < near < far
例如:
glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
几何必须在近平面和远平面之间,否则将被裁剪。
投影区域的大小与深度的比例是线性的,可以计算。这取决于视角:
float fov_y = glm::radians(45.0f);
float ratio_size_depth = tan(fov_y / 2.0f) * 2.0f;
请注意,如果一个对象在视口中的投影中显示的大小只有原来的一半,则该对象到相机的距离(深度)必须加倍。
答案 1 :(得分:2)
因此,校正后的模型平移矩阵和着色器中需要在平面上具有坐标匹配的深度如下:
int width = 1280.0f;
int height = 720.0f;
glm::mat4 model = glm::mat4(1.0f);
model = glm::scale(model, glm::vec3(-1.0f / width, -1.0f / height, 1.0f));
model = glm::translate(model, glm::vec3(-((float)width / 2.0f), -((float)height / 2.0f), 0.0f));
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, 1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
具有Z值的着色器:
#version 330 core
layout (location = 0) in vec2 vertex;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
void main() {
gl_Position = projection * view * model * vec4(vertex.xy, 1.208f, 1.0f);
}
在该正交矩阵上将是等效的:
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(this->width), static_cast<GLfloat>(this->height), 0.0f, 0.0f, -100.0f);
也可以将矩阵相乘在一起,以使传递给着色器的投影矩阵只有一个。这将使通过网格ect传递实际模型矩阵变得更加容易。