OpenGL。将顶点乘以投影矩阵
OpenGL. Multiplying a vertex by a projection matrix
我正在尝试绘制一个模型,这就是我 运行 所做的,下面的代码像 2d 一样工作,尽管应该有一个透视图
Code # 1
mat4 Proj = glFrustum(left, right, bottom, top, zNear, zFar);
mat4 View = gluLookAt(position, direction, up);
mat4 World = mat4(1);
glBegin(GL_TRIANGLES);
for (auto& t : mesh)
{
norm = Proj * View * World * vec4(t.norm, 0);
dot1 = Proj * View * World * vec4(t.dot1, 1);
dot2 = Proj * View * World * vec4(t.dot2, 1);
dot3 = Proj * View * World * vec4(t.dot3, 1);
glNormal3f(norm);
glVertex3f(dot1.x, dot1.y, dot1.z);
glVertex3f(dot2.x, dot2.y, dot3.z);
glVertex3f(dot3.x, dot3.y, dot3.z);
}
glEnd();
于是透视图出现:
Code # 2
mat4 Proj = glFrustum(left, right, bottom, top, zNear, zFar);
mat4 View = gluLookAt(position, direction, up);
mat4 World = mat4(1);
glMatrixMode(GL_PROJECTION);
glLoadMatrix(Proj);
glBegin(GL_TRIANGLES);
for (auto& t : mesh)
{
norm = View * World * vec4(t.norm, 0);
dot1 = View * World * vec4(t.dot1, 1);
dot2 = View * World * vec4(t.dot2, 1);
dot3 = View * World * vec4(t.dot3, 1);
glNormal3f(norm);
glVertex3f(dot1.x, dot1.y, dot1.z);
glVertex3f(dot2.x, dot2.y, dot3.z);
glVertex3f(dot3.x, dot3.y, dot3.z);
}
glEnd();
由此产生的问题:为什么代码2和代码3得到的结果一样,而代码1和代码2却不同?
Code # 3
mat4 Proj = glFrustum(left, right, bottom, top, zNear, zFar);
mat4 View = gluLookAt(position, direction, up);
mat4 World = mat4(1);
glMatrixMode(GL_PROJECTION);
glLoadMatrix(Proj);
*glMatrixMode(GL_MODELVIEW);
glLoadMatrix(View);*
glBegin(GL_TRIANGLES);
for (auto& t : mesh)
{
norm = World * vec4(t.norm, 0);
dot1 = World * vec4(t.dot1, 1);
dot2 = World * vec4(t.dot2, 1);
dot3 = World * vec4(t.dot3, 1);
glNormal3f(norm);
glVertex3f(dot1.x, dot1.y, dot1.z);
glVertex3f(dot2.x, dot2.y, dot3.z);
glVertex3f(dot3.x, dot3.y, dot3.z);
}
glEnd();
告诉我我做错了什么?如何正确计算顶点乘以投影矩阵?
投影矩阵变换生成Homogeneous coordinates。齐次坐标有 4 个分量,但您只需将 3 个分量传递给顶点坐标。使用所有 4 个组件来指定顶点坐标。例如:
glVertex3f(dot1.x, dot1.y, dot1.z);
glVertex4f(dot1.x, dot1.y, dot1.z, dot1.w);
或
glVertex4fv(glm::value_ptr(dot1));
我正在尝试绘制一个模型,这就是我 运行 所做的,下面的代码像 2d 一样工作,尽管应该有一个透视图
Code # 1
mat4 Proj = glFrustum(left, right, bottom, top, zNear, zFar);
mat4 View = gluLookAt(position, direction, up);
mat4 World = mat4(1);
glBegin(GL_TRIANGLES);
for (auto& t : mesh)
{
norm = Proj * View * World * vec4(t.norm, 0);
dot1 = Proj * View * World * vec4(t.dot1, 1);
dot2 = Proj * View * World * vec4(t.dot2, 1);
dot3 = Proj * View * World * vec4(t.dot3, 1);
glNormal3f(norm);
glVertex3f(dot1.x, dot1.y, dot1.z);
glVertex3f(dot2.x, dot2.y, dot3.z);
glVertex3f(dot3.x, dot3.y, dot3.z);
}
glEnd();
于是透视图出现:
Code # 2
mat4 Proj = glFrustum(left, right, bottom, top, zNear, zFar);
mat4 View = gluLookAt(position, direction, up);
mat4 World = mat4(1);
glMatrixMode(GL_PROJECTION);
glLoadMatrix(Proj);
glBegin(GL_TRIANGLES);
for (auto& t : mesh)
{
norm = View * World * vec4(t.norm, 0);
dot1 = View * World * vec4(t.dot1, 1);
dot2 = View * World * vec4(t.dot2, 1);
dot3 = View * World * vec4(t.dot3, 1);
glNormal3f(norm);
glVertex3f(dot1.x, dot1.y, dot1.z);
glVertex3f(dot2.x, dot2.y, dot3.z);
glVertex3f(dot3.x, dot3.y, dot3.z);
}
glEnd();
由此产生的问题:为什么代码2和代码3得到的结果一样,而代码1和代码2却不同?
Code # 3
mat4 Proj = glFrustum(left, right, bottom, top, zNear, zFar);
mat4 View = gluLookAt(position, direction, up);
mat4 World = mat4(1);
glMatrixMode(GL_PROJECTION);
glLoadMatrix(Proj);
*glMatrixMode(GL_MODELVIEW);
glLoadMatrix(View);*
glBegin(GL_TRIANGLES);
for (auto& t : mesh)
{
norm = World * vec4(t.norm, 0);
dot1 = World * vec4(t.dot1, 1);
dot2 = World * vec4(t.dot2, 1);
dot3 = World * vec4(t.dot3, 1);
glNormal3f(norm);
glVertex3f(dot1.x, dot1.y, dot1.z);
glVertex3f(dot2.x, dot2.y, dot3.z);
glVertex3f(dot3.x, dot3.y, dot3.z);
}
glEnd();
告诉我我做错了什么?如何正确计算顶点乘以投影矩阵?
投影矩阵变换生成Homogeneous coordinates。齐次坐标有 4 个分量,但您只需将 3 个分量传递给顶点坐标。使用所有 4 个组件来指定顶点坐标。例如:
glVertex3f(dot1.x, dot1.y, dot1.z);
glVertex4f(dot1.x, dot1.y, dot1.z, dot1.w);
或
glVertex4fv(glm::value_ptr(dot1));