Search code examples
wpf3dperspectivecamera

PerspectiveCamera WPF looking downwards


I'm really getting frustrated of the perspectivecamera in WPF it does all kinds of strange things and all the solutions that I can find on the internet are not working for me.

Currently I have this code:

<Window x:Class="projectv1.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Name="MainWin">
<Grid HorizontalAlignment="Stretch" >
    <Viewport3D Name="viewport3D1" >
        <Viewport3D.Camera>

            <PerspectiveCamera x:Name="camMain"  Position="-500 -10 300" LookDirection="0 -0.2 -1" UpDirection="0 1 0" FarPlaneDistance="10000" NearPlaneDistance="1" />

        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <DirectionalLight x:Name="dirLightMain" Direction="-1,-1,-1">
                </DirectionalLight>
            </ModelVisual3D.Content>
        </ModelVisual3D>

        <ModelVisual3D>
            <ModelVisual3D.Content>
                <GeometryModel3D>
                    <GeometryModel3D.Geometry>
                        <MeshGeometry3D x:Name="meshMain"
                            Positions="-800,0,-1000 -200,0,-1000 -500,0,-100"
                            TriangleIndices="0,1,2" >
                        </MeshGeometry3D>
                    </GeometryModel3D.Geometry>
                    <GeometryModel3D.Material>
                        <DiffuseMaterial x:Name="matDiffuseMain">
                            <DiffuseMaterial.Brush>
                                <SolidColorBrush Color="Red"/>
                            </DiffuseMaterial.Brush>
                        </DiffuseMaterial>
                    </GeometryModel3D.Material>
                </GeometryModel3D>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>

</Grid>

So I have a triangle and I look at it from below... but now I want to look at it from the top so I changed the position of the Y parameter of the camera to any a value greater than 0 but then suddenly I cannot see anything. e.g. Position ="-500 -1 300" works but Position="-500 1 300" does not work anymore. I played a lot with the updirection variable but I was not able to fix this....


Solution

  • You need to change the LookDirection as well as the position.

    What you have done is move the camera from one side of the object to the other, but kept it looking in the same direction:

    ----- <-- object
    
      ^
      |
      Camera
    

    Like this:

      ^
      |
      Camera
    
    ----- <-- object
    

    If you flip the camera direction as well you'll get the right result:

      Camera
      |
      V
    
    ----- <-- object