Page 1 of 1

buildProjectionMatrixPerspectiveFovLH fixed

Posted: Mon Apr 03, 2006 12:34 pm
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;
}

Posted: Wed Aug 30, 2006 1:25 pm
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.

Posted: Wed Aug 30, 2006 1:57 pm
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: