buildProjectionMatrixPerspectiveFovLH fixed

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
Rv
Posts: 10
Joined: Fri Mar 31, 2006 9:38 am

buildProjectionMatrixPerspectiveFovLH fixed

Post by Rv »

As mentioned in these other threads...

http://irrlicht.sourceforge.net/phpBB2/ ... hp?p=69746
http://irrlicht.sourceforge.net/phpBB2/ ... hp?p=68563

... The projection matrix creation in Irrlicht is buggy. Even given the suggested fixes in the other threads, it's still not accurate, and falls appart if you try to do anything clever, like dynamic view frustum re-sizing when the user changes your window size, for example.

If you'd like a more robust implementation, using the standard perspective projection matrix formula, try this:

Code: Select all

//! Builds a left-handed perspective projection matrix based on a field of view
inline void matrix4::buildProjectionMatrixPerspectiveFovLH(f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar)
{
	f32 th = (f32) tan( fieldOfViewRadians / 2.0f );
	f32 tw = th * aspectRatio;
	f32 d  = zFar - zNear;

	(*this)(0,0) = 1.0f / tw;
	(*this)(1,0) = 0;
	(*this)(2,0) = 0;
	(*this)(3,0) = 0;

	(*this)(0,1) = 0;
	(*this)(1,1) = 1.0f / th;
	(*this)(2,1) = 0;
	(*this)(3,1) = 0;

	(*this)(0,2) = 0;
	(*this)(1,2) = 0;
	(*this)(2,2) = ( zFar + zNear ) / d;
	(*this)(3,2) = 1.0f;

	(*this)(0,3) = 0;
	(*this)(1,3) = 0;
	(*this)(2,3) = ( -2.0f * zFar * zNear ) / d;
	(*this)(3,3) = 0;
}
Harvey Gilpin, code monkey
fortikur
Posts: 25
Joined: Tue Nov 15, 2005 3:59 pm
Location: Hungary

Post by fortikur »

You're absolutely right.

I spent a day with the source of the engine looking for the strange FOV settings and I found the same "source" of bug too.
Posted a question concerning the FOV problem (got no response).

Now I just pasted your code into the engine's code. It works fine.

The v1.1 and the 28/aug/2006 SVN still had the bug. :evil:

If someone says, that the setFOV with OpenGL driver is correct, just create something with 3DSMAx and try it with Irrlicht. It won't be the same.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I say it :twisted:
But I cannot afford 3dsmax so who knows :? Anyway, the FOVLH matrix in Irrlicht looks just as the one posted above :wink:
Post Reply