Aparent 2D position of a directional light.

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Aparent 2D position of a directional light.

Post by Mel »

This code projects the aparent 2D position of a directional light, so you can know the center of a lens flare simulating the sun, for instance. You need the light direction, and it will provide the result as a vector 2D in which the visible space is in the range (0,1), so, you can, after, multiply it by the screen resolution to find the proper place in screen space.

Code: Select all

 
core::vector2df projectVectorTo2DCoordinates(core::vector3df v,scene::ICameraSceneNode* cam){
core::matrix viewProjection;
core::vector3df ligthDir,viewDir;
core::vector2df screenPos;
f32 vect[4];
 
viewDir = cam->getTarget()-cam->getAbsolutePosition();
viewDir.normalize();
 
lightDir = v;
lightDir.normalize();
 
viewProjection = cam->getProjectionMatrix();
viewProjection *= core::matrix4().buildCameraLookAtMatrixLH(core::vector3df(0,0,0),viewDir,cam->getUpVector()); 
//use RH if needed, instead, it must match the projection matrix of the camera, though...
 
viewProjection.transformVect(vect,lightDir);
screenPos.X = vect[0]/vect[3];
screenPos.Y = -vect[1]/vect[3];
 
screenPos = 2*screenPos - core::vector2df(1,1);
 
return screenPos;
}
 
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Post Reply