OpenGL 3.x: perspective projection problem

Lately, I’ve been writing a matrix implementation of my own to replace the built-in matrix stack that is deprecated as of OpenGL 3.0 and finally removed in core 3.3 / 4.0. The exact profile I’m developing for is core OpenGL 3.2 / GLSL 150.

Referring to the detailed instructions provided here, I’ve set up my perspective projection matrix as follows (symmetric viewing frustum):

I figured the only way to clearly display matrices on the forums would be to typeset them with LaTeX ;). The values -2.22 and -1.22 (obviously) suffer from truncation.

Anyway, however, the rendering result doesn’t quite match the one I’m expecting to see (the primitive is deformed beyond recognition; vertices at the boundaries of the viewing volume seem to be approaching infinity). More often than not I would initially see an completely black scene, and with a simple keyboard control mechanism was, on occasion, able to get at least something to show up.

Now, please take a moment to look at the actual code I’m working with. Here are the relevant bits (btw, I’m using the Eigen library, which by default uses column-major storage order):

uni_modelview_loc = glGetUniformLocation(programHandle, "ModelView");
uni_projection_loc = glGetUniformLocation(programHandle, "Projection");


//The matrices projection, model and view have been initialized as shown above

//premultiply these two
Eigen::Matrix4f modelview = model * view;
glUniformMatrix4fv(uni_modelview_loc, 1, GL_FALSE,;
glUniformMatrix4fv(uni_projection_loc, 1, GL_FALSE,;

/* set up attribpointers and draw scene */


#version 150

in vec3 in_position;
in vec3 in_normal;

uniform mat4 ModelView;
uniform mat4 Projection;

out vec3 vvertex;
out vec4 vnormal;

void main(void)
	gl_Position = Projection * ModelView * vec4(in_position, 1.0); 
	vnormal = vec4(in_normal, 1.0);

Without providing any matrix uniforms to the shader, the primitive is rendered correctly. Weirdly enough, reversing the order in which the matrices ModelView and Projection are multiplied (again, in the shader) sometimes yields a result one could consider a perspective projection of sorts, but still not the one I would have expected (i.e. one similar to the default projection).

Please point out the error(s) I may be failing to see here. If required, images demonstrating this issue can also be arranged.

Here are the relevant bits

So where are the other relevant bits? Like how you create your matrices?

Eigen::Matrix4f modelview = model * view;

That’s the wrong order. The world-to-camera comes after the model-to-world matrix, so it goes on the left.

I wanted to keep the original post as math library-independent as I possibly could, so I chose not to give too much specific info on that. Also, I was very much inclined to think the problem lay not in my actual code, but rather in my algebra. However, if there are - other than the model * view order, thanks! - no obvious errors to be found, I will gladly go ahead and elaborate.

All matrices used in this program are globally declared, and in a typical OpenGL init function, the following is done:

view = Eigen::Matrix4f::Identity();

model = Eigen::Matrix4f::Identity();
model(3,2) = 4.0; 

projection << r/n, 0, 0, 0,
              0, r/t, 0, 0,
              0, 0, -(n+f)/(f-n), -(2*f*n)/(f-n),
	      0, 0, -1.0, 0.0;

When the contents of e.g. are printed in a loop, the following sequence is given:

for (int i = 0; i < 16; i++)
	printf("%f ",[i]);

1.6 0.0 0.0 0.0 0.0 1.6 0.0 0.0 0.0 0.0 -1.22 -1.0 0.0 0.0 -2.2 0.0

which appears to be both column-major and correct (figures truncated for clarity).

After extensive testing, I have come to the (fortunate) conclusion that I had, in fact, had a correct perspective projection all along; only the znear/zfar planes were too close to each other for the program to render anything other than merely a slice or a cross-section of the primitive (and for me to recognize the shape). Scaling down the mesh fixed this issue. :slight_smile:


check this website to understand the concept of prepective projection

check this website to understand the prespective projection