No announcement yet.

Getting the 2D screen position of a 3d point in Worldspace

  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting the 2D screen position of a 3d point in Worldspace

    Does the framework have a function that helps getting the 2d position on the screen of a 3d point in worldspace (with its view and projection matrix)? So like a reverse TRay that tells you where a 3d point ends up on the screen and not where a 2d screen position ends up in 3d space to select objects.

    I want to create some lensflare effects which is why I need to know where my sun sprite currently is on the screen. I could fiddle around with the camera axis numbers until I get something useful. (The skydome and the sunsprite don't move, only rotate with the camera, so it's not so hard)
    But I thought I check if there is a direct and exact way. Could be also useful for other stuff, like placing a textlabel next to an object without making it part of the 3D scene, just render it on canvas where the object is on screen.

  • #2
    The framework has facilities for that. The simplest one that you are mentioning, suppose you have a 3D position and you need to know where it'll be located in 2D. You could do the following:
      LPosition: TVector3f;
      LView, LProjection: TMatrix4f;
      LScreenPos: TPoint2f;
      // This is the position that you have in 3D space.
      LPosition := Vector3f(-10.0, 15.0, 25.0);
      // These are the matrices that you have for camera and screen projection.
      LView := TMatrix4f.LookAt(Vector3f(0.0, 50.0, -400.0), ZeroVector3f, AxisYVector3f);
      LProjection := TMatrix4f.PerspectiveFOVY(Pi * 0.25, FDisplaySize.X / FDisplaySize.Y, 10.0, 1800.0);
      // Get 2D position on the screen. This should be the answer to your question.
      LScreenPos := (LView * LProjection).Project(LPosition, FDisplaySize);
    When dealing with 3D objects, the framework also provides functions for calculating their visible 2D rectangles on the screen. For that, you would need object's OOBB (Object-Oriented Bounding Box):
      LObjectOBB, LWorld: TMatrix4f;
      LScreenRect: TFloatRect;
      // .. other declarations like in previous code snippet
      // Object is a box 100 by 80 by 60, with origin in the bottom middle.
      LObjectOBB := TMatrix4f.Scale(100.0, 80.0, 60.0) * TMatrix4f.Translate(0.0, 40.0, 0.0);
      // Object is rotated and placed somewhere...
      LWorld := TMatrix4f.RotateY(Pi * 0.5) * TMatrix4f.Translate(-10.0, 15.0, 25.0);
      // Calculate 2D rectangle that object occupies on the screen.
      LScreenRect := TVolume.VisibleFrame(LObjectOBB * LWorld * LView * LProjection, FDisplaySize);
    If you are loading your mesh from disk, you can use its min/max bounds to create an OOBB (alternatively, TMeshBuffer can calculate min/max bounds for you too):
      LMeshBuffer: TMeshBuffer;
      LObjectOBB: TMatrix4f;
      LMinBounds, LMaxBounds: TVector3f;
      // Calculate min/max bounds for an existing mesh buffer.
      LMeshBuffer.CalculateBounds(LMinBounds, LMaxBounds);
      // Create an OOBB for the given min/max bounds.
      LObjectOBB := TVolume.BoundsToMatrixVolume(LMinBounds, LMaxBounds);
      // ...
    Please note that framework also facilitates mouse-picking objects in 3D space accurately either via OOBB and/or with their voxel representations (generated via Voxelize tool). However, this would be a separate topic.


    • #3
      Perfect, thanks once again.

      And the additional boundingbox examples can be used to see if something blocks the lightsource (and so the lensflare effect)

      Once I some basic gravity and jumping done I would like to post my test project as a 3D Game example here and maybe on other forums like PascalGameDevopement and the Lazarus Forum. That is... if you are ok with that, since (even though probably not many) this might attract some more hobby game enthusiasts. I mean since the main focus of the Framework is on buisness apps now.


      • #4


        • #5
          The lens flares effect looks great!
          And sure, please feel free to post the project both here and on any other forums you like.