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.
- cpp 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;
}