problems rendering with a perspective matrix

hi people :). i have set up a system for passing a model through multiple lights. everything works fine until i try to multiply the vertices passed to the vertex shader when finally rendering the image by a 4x4 perspective matrix. i get nothing on the window. if i remove the perspective matrix i get a result that looks fairly good, but obviously this is not what i want. i have tried setting the perspective matrix as a uniform and hard coding it in and i get nothing in both cases. i am wondering if any of you could help me fix this.
here is my vertex shader:

const char *vshaderdraw = 
"#version 430 core
"
"uniform mat4 perspective;
"
"uniform mat3 uvn;
"
"layout (location = 4) in vec3 verticestfin;
"
"layout (location = 6) in vec4 colorstfin;
"
"out vec4 color;
"
"mat4 correctaxes = mat4(1, 0, 0, 0,
"
"0, 0, 1, 0,
"
"0, 1, 0, 0,
"
"0, 0, 0, 1);
"
"void main()
"
"{
"
"gl_Position =  perspective * correctaxes * vec4(verticestfin, 1);
"
"color = colorstfin;
"
"}
";

here is my rendering code:

glGenVertexArrays(1, &vao);
		glBindVertexArray(vao);
		
		glUseProgram(progdraw);
		
		GLuint perspectiveloc = glGetUniformLocation(progdraw, "perspective");
		GLuint uvnloc = glGetUniformLocation(progdraw, "uvn");
		
		glUniformMatrix4fv(perspectiveloc, 1, GL_FALSE, cam->ProjectionMat.marray);
		glUniformMatrix3fv(uvnloc, 1, GL_FALSE, cam->UVNMat.marray);
		
		glBindBuffer(GL_ARRAY_BUFFER, vertexbufferout);
		glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, 0);
		glEnableVertexAttribArray(4);
		
		glBindBuffer(GL_ARRAY_BUFFER, colorbufferout);
		glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, 0, 0);
		glEnableVertexAttribArray(6);
		
		glDrawArrays(GL_TRIANGLES, 0, mesh->MeshIndexListSize * 3);
		
		glBindVertexArray(0);
		glDeleteVertexArrays(1, &vao);

thanks alot

You don’t show the construction or contents of the “perspective” matrix you’re using.

First, check that using an identity matrix for the perspective matrix has the same results as not using a perspective matrix.

If it does, then it should just a matter of getting the correct matrix (unless you’re doing something weird in the fragment shader, but that’s relatively far down the list of likely reasons for failure).

One thing to bear in mind is that conventional OpenGL projection matrices (and the work-alikes provided by GLM) flip the Z direction (eye space has the +Z axis pointing toward the viewer, while clip-space has +Z pointing away). If your code is currently working without any projection matrix, you’ll need to allow for this in the construction of the projection matrix.

Another likely reason for problems is constructing a projection which results in the projected scene being outside of the clip volume. Trying to use a conventional matrix (as per glFrustum() or gluPerspective() or the GLM equivalents) with an inverted Z axis will definitely cause this. A conventional matrix will result in a view frustum where visible points have a negative eye-space Z coordinate. Any point with a positive eye-space Z coordinate will automatically be on the wrong side of the near clipping plane.

With no projection transformation, the near and far planes will be at Z=-1 and Z=1 respectively. A perspective transformation requires that both planes have the same sign (i.e. Z=0 must lie outside of the view frustum). If your scene is centred around the origin, it will need to be offset in the Z direction in order to avoid at least half of it being clipped away.

omgggg this is getting annoying. ive spent about 2 hours now trying to fix this. i have tried calculating a new perpective projection matrix (instead of using a function i wrote to build it) and hardcoding it in the shader and passing it as a uniform. i also tried switching from drawing the object to drawing a triangle where all the data was in the drawing function, and still nothing when i use a perspective matrix in my shader. Can anyone think of any reason for this? any ideas at all would be greatly appreciated. you can look at my shader in my first post and here is my perspective matrix:


GLfloat pers[] = {1, 0, 0, 0,
		        0, 1, 0, 0,
			0, 0, 1.222, 2.222,
			0, 0, -1, 0};

thank you greatly XD

EDIT:
Here is all the info (it might help)
Okay i figured i should tell you all that is going on in my little driver program for pushing an object through a bunch of lights and then drawing it. the drawing part is done as a separate function from the main function. first an object is loaded into a structure that stores the normals, world coords, vertices, indices etc. then i load lights into a single light object with 3 different linked lists for ambient, directional and point (the three im testing atm). I also create a camera structure that, by functions, is filled with the perspective projection matrix and a matrix to tell the camera which direction to point. then i push the vertices and normals through a “transform” program which positions the model in world coords, renormalizes the normals and passes those out to separate buffers through transform feedback. i then use the results to calculate the colors (object color and light are taken into account) of the object. and then i try to finish the drawing by just passing the colors and vertices through glDrawArrays. i get a result that looks close to my desired result when i dont use a perspective matrix but when i do i get nothing.

sorry if thats alot to read, just really need help thanks

  1. The above matrix is in row-major order, but OpenGL uses column-major order by default (unless you set the transpose parameter to GL_TRUE for glUniformMatrix4fv(), or use the row_major qualifier on a uniform variable).

  2. Your near and far planes are swapped. The above matrix is what you would get with nearVal=10 and farVal=1, e.g. from gluPerspective(90,1,10,1) or glFrustum(-10,10,-10,10,10,1). Normally, the entries which are 1.222 and 2.222 would be negative.

If you’re using a coordinate system where eye-space +Z points away from the viewer, then the matrix should be


1 0 0      0
0 1 0      0
0 0 1.222 -2.222
0 0 1      0

i.e. the values in the third column should be positive to account for Z having the opposite sign to usual.

hi gclements can you give me a matrix for a near plane of 1 and a far plane of 10. i don’t really know whats going on because im using projection matrices that have worked fine before in other programs but to no success. i would love to just try out one that you came up with to see if that is 100% the problem. thanks :slight_smile:

The matrix generated by glPerspective(90,1,1,10) or the equivalent glm::perspective() call is


[ 1. 0. 0.     0.    ]
[ 0. 1. 0.     0.    ]
[ 0. 0.-1.222 -2.222 ]
[ 0. 0.-1.     0.    ]

which would be defined as:


GLfloat pers[] = {
    1,  0,  0    ,  0,
    0,  1,  0    ,  0,
    0,  0, -1.222, -1,
    0,  0, -2.222,  0
};

in C or as


mat4 pers = mat4(
    1,  0,  0    ,  0,
    0,  1,  0    ,  0,
    0,  0, -1.222, -1,
    0,  0, -2.222,  0
);

in GLSL.

If your Z axis is flipped, then the matrix would instead be


[ 1. 0. 0.     0.    ]
[ 0. 1. 0.     0.    ]
[ 0. 0. 1.222 -2.222 ]
[ 0. 0. 1.     0.    ]

k thanks alot. i tried those :(. i guess ill just rewrite it and check it as often as possible. sigh…