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....
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;
}
}
Following extension methods implement free flight in any directions & rotations of a projection camera into Euclid 3D space.
using System.Windows.Input;
using System.Windows.Media.Media3D;
using static System.Windows.Input.Key;
using static System.Windows.Input.ModifierKeys;
public static class ProjectionCameraExtensions
{
public static TCamera Move<TCamera>(this TCamera camera, Vector3D axis, double step)
where TCamera : ProjectionCamera
{
camera.Position += axis * step;
return camera;
}
public static TCamera Rotate<TCamera>(this TCamera camera, Vector3D axis, double angle)
where TCamera : ProjectionCamera
{
Matrix3D matrix3D = new();
matrix3D.RotateAt(new(axis, angle), camera.Position);
camera.LookDirection *= matrix3D;
return camera;
}
public static Vector3D GetYawAxis(this ProjectionCamera camera) => camera.UpDirection;
public static Vector3D GetRollAxis(this ProjectionCamera camera) => camera.LookDirection;
public static Vector3D GetPitchAxis(this ProjectionCamera camera) => Vector3D.CrossProduct(camera.UpDirection, camera.LookDirection);
public static PerspectiveCamera MoveBy(this PerspectiveCamera camera, Key key) => camera.MoveBy(key, camera.FieldOfView / 180d);
public static PerspectiveCamera RotateBy(this PerspectiveCamera camera, Key key) => camera.RotateBy(key, camera.FieldOfView / 45d);
public static TCamera MoveBy<TCamera>(this TCamera camera, Key key, double step) where TCamera : ProjectionCamera => key switch
{
W => camera.Move(Keyboard.Modifiers.HasFlag(Shift) ? camera.GetYawAxis() : camera.GetRollAxis(), +step),
S => camera.Move(Keyboard.Modifiers.HasFlag(Shift) ? camera.GetYawAxis() : camera.GetRollAxis(), -step),
A => camera.Move(camera.GetPitchAxis(), +step),
D => camera.Move(camera.GetPitchAxis(), -step),
_ => camera
};
public static TCamera RotateBy<TCamera>(this TCamera camera, Key key, double angle) where TCamera : ProjectionCamera => key switch
{
Left => camera.Rotate(camera.GetYawAxis(), +angle),
Right => camera.Rotate(camera.GetYawAxis(), -angle),
Down => camera.Rotate(camera.GetPitchAxis(), +angle),
Up => camera.Rotate(camera.GetPitchAxis(), -angle),
_ => camera
};
}
Handlers for keyboard and mouse input
private void Window_PreviewKeyDown(object sender, KeyEventArgs e) =>
Camera.MoveBy(e.Key).RotateBy(e.Key);
Point from;
private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
{
var till = e.GetPosition(sender as IInputElement);
double dx = till.X - from.X;
double dy = till.Y - from.Y;
from = till;
var distance = dx * dx + dy * dy;
if (distance <= 0)
return;
if (e.MouseDevice.LeftButton is MouseButtonState.Pressed)
{
var angle = (distance / Camera.FieldOfView) % 45;
Camera.Rotate(new(dy, -dx, 0d), angle);
}
}
The image source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With