Strange projection matrix

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
choujs
Posts: 2
Joined: Sun Mar 05, 2006 3:48 am

Strange projection matrix

Post by choujs »

I think perspective projection matrix used in irr is strange. A standard projection matrix may like :

void matrix4::buildProjectionMatrixPerspectiveFovLH(f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar)
{
(*this)(0,0) = (cos(fieldOfViewRadians/2) * aspectRatio)/ sin(fieldOfViewRadians/2);
(*this)(1,0) = 0;
(*this)(2,0) = 0;
(*this)(3,0) = 0;

(*this)(0,1) = 0;
(*this)(1,1) = cos(fieldOfViewRadians/2) / sin(fieldOfViewRadians/2);
(*this)(2,1) = 0;
(*this)(3,1) = 0;

(*this)(0,2) = 0;
(*this)(1,2) = 0;
(*this)(2,2) = (zFar + zNear)/(zFar-zNear);
(*this)(3,2) = 1;

(*this)(0,3) = 0;
(*this)(1,3) = 0;
(*this)(2,3) = -2*zNear*zFar/(zFar-zNear);
(*this)(3,3) = 0;
}

but in irr that is:

void matrix4::buildProjectionMatrixPerspectiveFovLH(f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar)
{
f32 h = (f32)(cos(fieldOfViewRadians/2) / sin(fieldOfViewRadians/2));
f32 w = h / aspectRatio;

(*this)(0,0) = 2*zNear/w;
(*this)(1,0) = 0;
(*this)(2,0) = 0;
(*this)(3,0) = 0;

(*this)(0,1) = 0;
(*this)(1,1) = 2*zNear/h;
(*this)(2,1) = 0;
(*this)(3,1) = 0;

(*this)(0,2) = 0;
(*this)(1,2) = 0;
(*this)(2,2) = zFar/(zFar-zNear);
(*this)(3,2) = 1;

(*this)(0,3) = 0;
(*this)(1,3) = 0;
(*this)(2,3) = zNear*zFar/(zNear-zFar);
(*this)(3,3) = 0;

}
but it seem work with matrix above. Is there any one can tell me the background acknowledge of the matrix of irr?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

This has been brought up before. I think you'll find it if you do a search. Someone suggested multiplying w and h by zNear before building the matrix.
Guest

Post by Guest »

Thanks for your reply but i think i can't found the topic about it. Can you please give me a link or tell me how to search it?
hybrid

Post by hybrid »

Press the Search button in the top line directly next to FAQ and insert buildProjectionMatrixPerspectiveFovLH as search term. First hit is the mentioned thread. I think my grandma would be able to do this :roll:
Rv
Posts: 10
Joined: Fri Mar 31, 2006 9:38 am

Post by Rv »

While the other thread does suggest improvements, it's still not great. The original poster had the right idea, using the correct formula. If you want an Irrlicht friendly implementation of this, try here:

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

Cheers :D
Harvey Gilpin, code monkey
Post Reply