透视投影的Opengl顶点坐标



我需要使用透视转换,但我不明白如何定义精灵的模型坐标。如果我使用正交投影,我可以将每个顶点的坐标定义为屏幕上的数字像素。但是有了透视投影,我就不能了。

正交投影:

glm::ortho<GLfloat>(0.0f, screen_width, screen_height, 0.0f, 1.0f, -1.0f));

:

glm::perspective(glm::radians(45.f), (float)screen_width / (float)screen_height, 0.1f, 100.f);

顶点着色器:

#version 330 core
layout (std140) uniform Matrices
{
mat4 ProjectionMatrix;
mat4 ViewMatrix;
mat4 ModelMatrix;
};
layout (location = 0) in vec2 position;
layout (location = 1) in vec2 inTexCoords;
out vec2 TextureCoords;
void main()
{
TextureCoords = inTexCoords;
gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vec4(position, 1.f, 1.0);
}
例如

vertices[1] = 0.f;
vertices[8] = 0.f;
vertices[12] = 0.f;
vertices[13] = 0.f;
for (GLuint i = 0; i < m_totSprites; ++i) {
// Vertex pos
vertices[0] = m_clips[i].w;
vertices[4] = vertices[0];
vertices[5] = m_clips[i].h;
vertices[9] = vertices[5];
// Texture pos
vertices[2] = (m_clips[i].x + m_clips[i].w) / tw;
vertices[3] = (m_clips[i].y + m_clips[i].h) / th;
vertices[6] = (m_clips[i].x + m_clips[i].w) / tw;
vertices[7] = m_clips[i].y / th;
vertices[10] = m_clips[i].x / tw;
vertices[11] = m_clips[i].y / th;
vertices[14] = m_clips[i].x / tw;
vertices[15] = (m_clips[i].y + m_clips[i].h) / th;

对于正交投影效果很好。如何定义透视的顶点坐标?

在正交投影和透视中与模型坐标有什么不同?为什么在第一种情况下,很容易将顶点的坐标设置为像素大小,但在所有透视的例子中,它们在-0.5到0.5之间归一化?它是必要的吗?

最初我误解了正交投影和透视投影的区别。正如我现在所理解的那样,所有的顶点最初都映射在NDC中进行透视投影。然后他们移动,缩放等与模型矩阵。像素完美渲染只能通过一定的深度或正交来实现。这对3D透视投影很有用。

如果你有投影矩阵,你也需要一个视图矩阵。
glm::lookAt()for ex
我通常使用这个组合

glm::lookAt(glm::vec3(-1.2484,0.483,1.84384), glm::vec3(-0.3801, -0.4183,-3.15),glm::vec3( 0., 0.2,-00.))
glm::perspective(45., 1., 1.2, 300.) 
glm::mat4(1.)

最新更新