I an new to perspective division phenomenon. I am rendering a simple square in 3D space using following code :
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. On the other hand if two vertices have -1 as z-coordinate the projection matrix works fine. 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.
gl_Position = position*MVPMatrix;
If MVPMatrix
is properly column-major, then this multiplication is backwards. The position goes on the right.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.