How to rotate camera in ViewPort3D using mouse in WPF?

Surya KLSV picture Surya KLSV · Jan 29, 2012 · Viewed 15.6k times · Source

I was able to set the position and direction of the perspective camera placed in the viewport3d directly in XAML. But i would like to know how can i rotate the camera using the mouse input. I would prefer C# lang. I was actually stuck at the point how to rotate the camera using the input of the mouse. Please help me. It would be helpful if someone gives me a sample code....

Answer

MAXE picture MAXE · Aug 28, 2012

I think these two links can help you a lot...

Animating the Position of a 3D Camera in WPF (there's also a sample project to try!)

Rotating the Camera with the Mouse

I agree that maybe XNA would be the best solution for 3D situations, but native 3D support and hardware-accelerated rendering are also fantastic features of WPF and XAML!

As you can see, a 3D camera for XAML Viewport3D fits perfectly with the application, also using bindings:

<Viewport3D.Camera>
    <PerspectiveCamera x:Name="camera"
                       UpDirection="0,0,1"
                       LookDirection="{Binding RelativeSource={RelativeSource Self}, Path=Position, Converter={StaticResource lookBackConverter}}"
                       Position="0,0,0" />
</Viewport3D.Camera>

...and just the usual IValueConverter implementation to let the camera move:

public class LookBackConverter : IValueConverter 
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new Point3D(0,0,0) - (Point3D)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}