Menu

Mouse Position in cs Orthogonal CameraStyle

Help
2019-01-04
2019-01-18
  • Damjan Mikuž

    Damjan Mikuž - 2019-01-04

    Hello to all!

    Here is the function that I use to determine the mouse position in csPerspective CameraStyle:

    function Tmain.MouseWorldPos(const X, Y: Integer; isy: boolean = false): TVector;
    var
    v: TVector;
    InvertedY: Integer;
    begin

    InvertedY := Viewer.Height - Y;

    SetVector(v, X, InvertedY, 0);
    if not isy then
    Viewer.Buffer.ScreenVectorIntersectWithPlaneXZ
    (v,TargetCamera.AbsolutePosition.y, Result)
    else
    Viewer.Buffer.ScreenVectorIntersectWithPlaneXY
    (v,TargetCamera.AbsolutePosition.z, Result)
    end;

    I am also to able determine the intersection on FreeForm with procedure like this on csOrthogonal CameraStyle:

    procedure TForm1.GLSceneViewer1MouseDown(Sender: TObject;
    Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    var
    rayStart, rayVector, iPoint, iNormal : TVector;
    begin
    // retrieve raycasting data:
    // rayStart is obtained for camera and screen position
    // rayVector is the camera direction (i.e direction to target since our camera is targeted)
    // (note that (0, 0) is lower left for the Screen function, whereas Delphi
    // uses top-left as origin, hence the Y inversion)
    SetVector(rayStart, GLSceneViewer1.Buffer.OrthoScreenToWorld(x, GLSceneViewer1.Height-y));
    SetVector(rayVector, GLCamera1.AbsoluteVectorToTarget);
    NormalizeVector(rayVector);
    // Here we require RayCast intersection
    if FreeForm1.RayCastIntersect(rayStart, rayVector, @iPoint, @iNormal) then begin
    // got one, move the sphere there and orient it appropriately
    Sphere1.Position.AsVector:=iPoint;
    Sphere1.Direction.AsVector:=VectorNormalize(iNormal);
    // make it visible
    Sphere1.Visible:=True;
    end else begin
    // hide it if we did not hit
    Sphere1.Visible:=False;
    end;
    end;

    Now here is my problem: how to determine intersection WithPlaneXY or WithPlaneX in csOrthogonal CameraStyle like in case 1:
    Here is some example how i try, but with no sucsess!
    Procedure TForm1.ViewerMouseMove(Sender: TObject; Shift: TShiftState; X,
    Y: Integer);
    var
    raystart,rayvector,ipoint,inormal:TVector;
    raytarget,v:TAffineVector ;

    cp : TPoint;
    

    begin

    v := viewer.Buffer.OrthoScreenToWorld(x, y);
    
         //glcube2.AbsoluteToLocal(v);
     setVector(raystart, viewer.Buffer.OrthoScreenToWorld(x, viewer.Height-y),0);
    // edit4.Text:=MouseWorldPos(x,Y).X.ToString();
    
       setVector(rayvector, glcamera1.AbsoluteVectorToTarget);
    

    Viewer.Buffer.ScreenVectorIntersectWithPlaneyz(raystart,rayvector.y,ipoint);

    // GLGetCursorPos(cp);
    glcube1.Position.AsVector:=(ipoint);
    // glcube1.Position.Y:=(rayvector.z);
    // glcube1.Position.Z:=(raystart.y);
    edit1.Text:=(ipoint.x).ToString();
    edit2.Text:=(ipoint.Y).ToString();
    edit3.Text:=(ipoint.z).ToString;

     end;
    
     
  • Jerome.D (BeanzMaster)

    Hi can you provide a full sample, If i'm remember well your problem come from ScreenVectorIntersectWithPlaneyz. I don't rember with csOrthogonal CameraStyle what the right planes to test. I must do some research in some old codes

     
  • Damjan Mikuž

    Damjan Mikuž - 2019-01-18

    Hello,
    here is my code(be aware that is quite a mess).
    I partly solved with adding a TGLPlane to Glscene and using Viewer.Buffer.OrthoScreenToWorld(x, viewer.Height-y)) +TGLPlane.RayCastIntersect(rayStart, rayVector, @iPoint, @iNormal) . But if I remember correctly in csPerspective CameraStyle adding GLPlane it is not necessary. The code bellow absolutely needs a fix!
    I will dig a little deeper!
    Ps. tray press radio button rbYZ, Whay is TGLCube does not intersect corectly!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.