Advertisement

Generating a ray

Started by June 22, 2002 02:49 PM
0 comments, last by LoneFox 22 years, 7 months ago
Hello, I need to generate a ray starting at the views orgin to a place the user selects with the mouse. I have the Perspective matrix and the view matrix (this contains the position of the viewer.) To generate a ray I need orgin (have this) and the other point the mouse makes. This will be used for object selection in case anyone was wondering. Thks in advace.
Ray_Struct Pos2DTORay(D3DVECTOR Pos2D, Camera_Struct Cam)
{
D3DMATRIX InvertedMat;
D3DVECTOR Pos3D = D3DVECTOR(0,0,0);
D3DVECTOR MouseNormal;
Ray_Struct Ray;

MatrixInvert(InvertedMat,Cam.MatrixView);

//center of viewport is (0,0).. left and right are -1 and 1
MouseNormal.x=(Pos2D.x-Engine.Config.ScreenWidth/2)/(Engine.Config.ScreenWidth/2)/Cam.AspectRatio;
MouseNormal.y=(-Pos2D.y+Engine.Config.ScreenHeight/2)/(Engine.Config.ScreenHeight/2);
MouseNormal.z=1/(tan(Cam.FOV/2));

Pos3D=SetMatrixVector(InvertedMat,MouseNormal);

Ray.Vec[0] = Cam.Position+Normalize(Pos3D-Cam.Position)*Cam.ViewDistance;
Ray.Vec[1] = Cam.Position;


return Ray;
}


Enjoy this very usefull function
Fantasio

This topic is closed to new replies.

Advertisement