I am having trouble with my scene in OpenGL. Objects that are supposed to be further away are drawn closer etc AND front facing triangles are being culled instead of back facing ones. They are drawn in the correct orientation as it is a package I have used before. I am convinced that it is something to do with my projection or veiwModel matrix. I can not see anything wrong with these though!
AV4X4FLOAT formProjMatrix(float FOVangle,float aspect,float nearz,float farz)
{
AV4X4FLOAT A;
A.m[0] = 1/(aspect*tanf(FOVangle/2));
A.m[5] = 1/tanf(FOVangle/2);
A.m[10] = farz/(farz-nearz);
A.m[11] = -nearz*farz/(farz-nearz);
A.m[14] = 1;
return A;
}
AV4X4FLOAT formViewModelMatrix(AV4FLOAT pos,AV4FLOAT target,AV4FLOAT up)
{
AV4X4FLOAT M;
AV4X4FLOAT R;
AV4FLOAT u;
AV4FLOAT v;
AV4FLOAT W;
W.x = -pos.x + target.x;
W.y = -pos.y + target.y;
W.z = -pos.z + target.z;
W.w = 0;
W.normalize();
u.x = up.y*W.z-W.y*up.z;
u.y = -up.x*W.z+W.x*up.z;
u.z = up.x*W.y-W.x*up.y;
u.w = 0;
u.normalize();
v.x = W.y*u.z-u.y*W.z;
v.y = -W.x*u.z+u.x*W.z;
v.z = W.x*u.y-u.x*W.y;
v.w = 0;
M.m[0] = u.x; M.m[1] = u.y; M.m[2] = u.z; M.m[3] = 0;
M.m[4] = v.x; M.m[5] = v.y; M.m[6] = v.z; M.m[7] = 0;
M.m[8] = -W.x; M.m[9] = -W.y; M.m[10] = -W.z; M.m[11] = 0;
M.m[12] = 0; M.m[13] = 0; M.m[14] = 0; M.m[15] = 1;
R.m[0] = 1;
R.m[5] = 1;
R.m[10] = 1;
R.m[15] = 1;
R.m[12] = -pos.x;
R.m[13] = -pos.y;
R.m[14] = -pos.z;
//the opposite of what you expect because of the way we overload mult operator!
M.display ();
R.display ();
return M*R;
}
This is what I call in my drawing routine.
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(projMatrix.m);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(viewModelMatrix.m);
Some otherinfo,
Yes, I have enabled depth testing!
There are some issues with in the calculation of the projection matrix. You have to adapt your code like this:
The Perspective Projection Matrix looks like this:
it follows:
The following function will calculate the same projection matrix as
gluPerspective
orglm::perspective
does:On the viewport the X-axis points to the left, the Y-axis up and the Z-axis out of the view (Note in a right hand system the Z-Axis is the cross product of the X-Axis and the Y-Axis).
The following code does the same as
gluLookAt
orglm::lookAt
does:Adapt your code like this:
See further the answers to the following question: