in this code from the collisionmanager, i try to get a ray that goes from the camera, through the screen and picks nodes.
as i said, it doesnt work if the render window is not centered on the screen, but I cannot for the life of me figure out how to fix it.
any help would be appreciated.
to render to any portion of the screen, i set the viewport to the desired window and then render.
Code: Select all
//! Returns a 3d ray which would go through the 2d screen coodinates.
core::line3d<f32> CSLevel::getRayFromScreenCoordinates(const core::position2d<s32> & pos, ICameraSceneNode* camera)
{
core::line3d<f32> ln(0, 0, 0, 0, 0, 0);
if (!getSmgr()) return ln;
if (!camera)
camera = getSmgr()->getActiveCamera();
if (!camera)
return ln;
const scene::SViewFrustum* f = camera->getViewFrustum();
core::vector3df farLeftUp = f->getFarLeftUp();
core::vector3df lefttoright = f->getFarRightUp() - farLeftUp;
core::vector3df uptodown = f->getFarLeftDown() - farLeftUp;
const core::rect<s32>& viewPort = getDriver()->getViewPort();
core::dimension2d<u32> screenSize(viewPort.getWidth(), viewPort.getHeight());
f32 dx = pos.X / (f32)screenSize.Width;
f32 dy = pos.Y / (f32)screenSize.Height;
if (camera->isOrthogonal())
ln.start = f->cameraPosition + (lefttoright * (dx - 0.5f)) + (uptodown * (dy - 0.5f));
else
ln.start = f->cameraPosition;
ln.end = farLeftUp + (lefttoright * dx) + (uptodown * dy);
return ln;
}