I have some code for transformation matrices and I added some functions that deal with Frustum and Perspective.
The thing is that when I call the perspective everything looks as if I have a really big field of view (ok in the middle of the screen while very stretched at the edges).
This effect is worse when I increase the near value.
void Frustum(T left, T right, T bottom, T top, T zNear, T zFar)
{
m[0][0]=2.0f*zNear/(right-left);
m[1][0]=0.0f;
m[2][0]=(right+left)/(right-left);
m[3][0]=0.0f;
m[0][1]=0.0f;
m[1][1]=2.0f*zNear/(top-bottom);
m[2][1]=(top+bottom)/(top-bottom);
m[3][1]=0.0f;
m[0][2]=0.0f;
m[1][2]=0.0f;
m[2][2]=-(zFar+zNear)/(zFar-zNear);
m[3][2]=-2.0f*zFar*zNear/(zFar-zNear);
m[0][3]=0.0f;
m[1][3]=0.0f;
m[2][3]=-1.0f;
m[3][3]=0.0f;
}
void Perspective(T fovy,T aspectRatio,T zNear,T zFar)
{
T xmin,xmax,ymin,ymax;
ymax= zNear* tan(fovy*Math<T>::PI/360.0);
ymin= -ymax;
xmin= ymin*aspectRatio;
xmax= ymax*aspectRatio;
Frustum(xmin,xmax,ymin,ymax,zNear,zFar);
}
T is a float or a double as the class is templated (for the test I only used floats).
The test values I use are fovy="60" znear="1.0" zfar="1000.0" and when I change them to fovy="60" znear="10.0" zfar="1000.0" it get’s a lot worse.
Note that the matrices are DirectX style yet I use them in OpenGL and thus I have change the order of multiplication in the shader.
Do you guys see anything wrong with my code?
Thanks
The problem was that when I started using the matrices that were transposed (DirectX style) I figured I must also transpose the frustum math which isn’t a good idea.
This is how the Frustum function should look like: