WPF 3D to 2D projection appears wrong?

WoodManEXP 1 Reputation point
2023-09-11T14:55:48.5633333+00:00

Have a case where a point on the PerspectiveCamera’s LookDirection vector does not project to the center of the Viewport3D. Any thoughts as to why?

Here are details of the case (all the values are rounded for brevity): The Viewport3D is 778, 405. Any visible Point3D the camera is looking directly at would project to (389, 202.5). The PerspectiveCamera is located at (1379.4, -577.7, 0.7).

In the first case the center of the White sphere is (13,080.3,-6,491.9,-407.9) which is 13,117 units from the camera. Camera’s LookDirection is (0.89, -0.45, -0.03). The White sphere projects to the visual center of the Viewport3D. MathUtils.TryWorldToViewportTransform reports the sphere’s center point projects to (389, 202.5), as expected. The black lines are added to show the center of the Viewport3D.

White sphere

In the second case the center of the Blue sphere is (1,383.0,-579.6, 0.3) which is 4.1 units from the camera. Camera’s LookDirection is (0.88,-0.46,-0.11). The Blue sphere does NOT render to the visual center of the Viewport3D. MathUtils.TryWorldToViewportTransform reports the center point being projected to (389, 202.5), as expected. Visually though the blue sphere is not drawing at center, it is at more like (295, 303).

Blue sphere

Placing the camera at the 4.1 distance from the White sphere it projects correctly to the center of the Viewport3D. The difference is that the White sphere is further from the (0, 0, 0) origin than the Blue sphere.

The Blue sphere will project more correctly to the center of the Viewport3D if the camera is “far away” from it, like out near the White sphere.

The Blue sphere is mathematically positioned to be at the center of the Viewport3D. Any thoughts as to why the Blue sphere is not rendering at the center of the Viewport3D in the second case above?

Thank you!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,647 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 47,341 Reputation points Microsoft Vendor
    2023-09-13T02:05:27.4866667+00:00

    Hi,@WoodManEXP. To calculate a 3D-to-2D projection that includes the perspective distortion, you could try to perform a perspective projection transformation.

    You could try to see if the solutions here and projection-3d-2d are helpful to you.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. WoodManEXP 1 Reputation point
    2023-11-26T22:43:22.4666667+00:00

    After lots of research into WPF’s 3D capabilities (using the Viewport3D control to render 3D) including reverse engineering parts of the WPF Viewport3D source code, the best way forward has been to give up using WPF’s 3D subsystem. It does not offer a usable means of dealing with or controlling, among others, the 3D to 2D projection problems baked into it.

    Fortunately, a WPF OpenGL C# binding is available, it is called OpenTK and can be nicely woven into the WPF framework. Can be found at http://opentk.net.

    OpenGL is somewhat of a paradigm shift from WPF’s 3D. It, at first, feels more tedious to learn and use. Once getting into the Zen of OpenGL though it is, IMHO, a nice improvement.  It provides excellent control of the many things that are hard to control in WPF 3D, including the 3D to 2D projection issues.

    OpenGL seems to offer better performance in terms of both processing and memory utilization.

    WPF 3D’s hit testing will be missed though. It is baked more into WPF 3D’s object model, not the case with OpenGL…

    0 comments No comments