[英]Perspective projection matrix in OpenGL
I an new to perspective division phenomenon. 我是一个新的视角分裂现象。 I am rendering a simple square in 3D space using following code : 我使用以下代码在3D空间中渲染一个简单的正方形:
void MatrixPersp(Mat4& matrix,const float fovy, const float aspect, const float zNear, const float zFar)
{
float sine, cotangent, deltaZ;
float radians;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(i==j)
matrix[i][j]=1.0;
else
matrix[i][j]=0.0;
}
radians = fovy / 2 * GLES_Pi / 180;
deltaZ = zFar - zNear;
sine = (float) sin(radians);
cotangent = (float) cos(radians) / sine;
matrix[0][0] = cotangent / aspect;
matrix[1][1] = cotangent;
matrix[2][2] = -(zFar + zNear) / deltaZ;
matrix[2][3] = -1;
matrix[3][2] = -2 * zNear * zFar / deltaZ;
matrix[3][3] = 0;
return;
}
void Render()
{
GLfloat vertices[] =
{
-0.8,0.6,1.0,1.0,
-0.8,0.2,1.0,1.0,
0.2,0.2,1.0,1.0,
0.2,0.6,1.0,1.0
};
MatrixPersp(perspective,90.0,aspect,2.0,100.0);
glUniformMatrix4fv(glGetUniformLocation(program_object,"MVPMatrix"), 1, GL_FALSE, (GLfloat*)perspective);
glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDrawElements();
} }
Vertex Shader is : 顶点着色器是:
#version 150
in vec4 position;
uniform mat4 MVPMatrix;
void main()
{
gl_Position = position*MVPMatrix;
}
The problem here is that , when all the four vertices have same z-value nothing is rendered at all. 这里的问题是,当所有四个顶点具有相同的z值时,根本不渲染。 On the other hand if two vertices have -1 as z-coordinate the projection matrix works fine. 另一方面,如果两个顶点具有-1作为z坐标,则投影矩阵工作正常。 I am not sure what is going wrong here. 我不确定这里出了什么问题。
glUniformMatrix4fv(glGetUniformLocation(program_object,"MVPMatrix"), 1, GL_FALSE, (GLfloat*)perspective);
This line suggests that your matrix is in column-major order. 此行表明您的矩阵按列主要顺序排列。 This is confirmed by the way your MatrixPersp
function computes its matrix, depending on exactly how Mat4
is defined. 这可以通过MatrixPersp
函数计算其矩阵的方式得到证实,具体取决于Mat4
的定义方式。
gl_Position = position*MVPMatrix;
If MVPMatrix
is properly column-major, then this multiplication is backwards. 如果MVPMatrix
是正确的列主要,那么这个乘法是向后的。 The position goes on the right. 位置在右边。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.