My Xml Code is here:
<Window x:Class="WpfApplication6.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" WindowState="Maximized">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Grid x:Name="background" removed="LightGray" MouseMove="background_MouseMove" MouseDown="background_MouseDown" MouseUp="background_MouseUp" >
<!--
<Viewport3D x:Name="viewport" ClipToBounds="False" >
<Viewport3D.Camera>
<OrthographicCamera x:Name="myCameraMatrix"></OrthographicCamera>
</Viewport3D.Camera>
<ModelVisual3D x:Name="model">
<ModelVisual3D.Content>
<Model3DGroup x:Name="group">
<AmbientLight Color="White" />
<DirectionalLight x:Name="directionlight" Color="White" Direction="-5,-5,-7"/>
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
<!--
</Grid>
</Grid>
</Window>
My mouseutilities code is here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Diagnostics;
using System.Windows.Input;
namespace WpfCustomControlLibrary1
{
public class MouseUtilities
{
public static double propangle;
private FrameworkElement _eventSource;
private Point _previousPosition2D;
private Vector3D _previousPosition3D = new Vector3D(0, 0, 1);
private Transform3DGroup _transform;
public RotateTransform3D _rotateTransform;
public ScaleTransform3D _scale = new ScaleTransform3D();
private AxisAngleRotation3D _rotation = new AxisAngleRotation3D();
private TranslateTransform3D _translate = new TranslateTransform3D();
private double _rotationFactor;
public MouseUtilities(double rotationFactor = 1.0)
{
_rotationFactor = rotationFactor;
_transform = new Transform3DGroup();
_transform.Children.Add(_scale);
_rotateTransform = new RotateTransform3D(_rotation);
_transform.Children.Add(_rotateTransform);
_transform.Children.Add(_translate);
}
public Transform3D Transform
{
get { return _transform; }
}
public Transform3D RotateTransform
{
get { return _rotateTransform; }
}
#region Event Handling
public FrameworkElement EventSource
{
get { return _eventSource; }
set
{
if (_eventSource != null)
{
_eventSource.PreviewMouseDown -= this.OnMouseDown;
_eventSource.PreviewMouseUp -= this.OnMouseUp;
_eventSource.PreviewMouseMove -= this.OnMouseMove;
_eventSource.PreviewMouseWheel -= this.OnMouseWheel;
}
_eventSource = value;
_eventSource.PreviewMouseDown += this.OnMouseDown;
_eventSource.PreviewMouseUp += this.OnMouseUp;
_eventSource.PreviewMouseMove += this.OnMouseMove;
_eventSource.PreviewMouseWheel += this.OnMouseWheel;
}
}
private void OnMouseUp(object sender, MouseEventArgs e)
{
Mouse.Capture(EventSource, CaptureMode.None);
}
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
Mouse.Capture(EventSource, CaptureMode.None);
double scale = 1;
if (e.Delta > 0 && _scale.ScaleX < 1.5)
{
scale = scale + 0.10;
_scale.ScaleX *= scale;
_scale.ScaleY *= scale;
_scale.ScaleZ *= scale;
}
else if (e.Delta < 0 && _scale.ScaleX > 0.15)
{
scale = scale - 0.10;
_scale.ScaleX *= scale;
_scale.ScaleY *= scale;
_scale.ScaleZ *= scale;
}
}
private void OnMouseDown(object sender, MouseEventArgs e)
{
Mouse.Capture(EventSource, CaptureMode.SubTree);
_previousPosition2D = e.GetPosition(EventSource);
_previousPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, _previousPosition2D);
mouseLastPosition = new Point(_previousPosition2D.X - EventSource.ActualWidth / 2, EventSource.ActualHeight / 2 - _previousPosition2D.Y);
}
Point mouseLastPosition;
private void OnMouseMove(object sender, MouseEventArgs e)
{
Point currentPosition = e.GetPosition(EventSource);
_eventSource.RenderTransformOrigin = currentPosition;
if (e.RightButton == MouseButtonState.Pressed)
{
Pan(currentPosition);
}
if (e.LeftButton == MouseButtonState.Pressed )
{
Look(currentPosition);
}
_previousPosition2D = currentPosition;
}
#endregion Event Handling
double rotationangle;
public static bool brotation;
public static double xxang = 0;
public static double xang=0;
private void Look(Point currentPosition)
{
Vector3D currentPosition3D = ProjectToTrackball(EventSource.ActualWidth, EventSource.ActualHeight, currentPosition);
if (_previousPosition3D.Equals(currentPosition3D)) return;
Vector3D axis = Vector3D.CrossProduct(_previousPosition3D, currentPosition3D);
rotationangle = _rotationFactor * Vector3D.AngleBetween(_previousPosition3D, currentPosition3D);
Quaternion delta = new Quaternion(axis, -rotationangle);
Quaternion q = new Quaternion(_rotation.Axis, _rotation.Angle);
q *= delta;
_rotation.Axis = q.Axis;
_rotation.Angle = q.Angle;
_previousPosition3D = currentPosition3D;
}
private void Pan(Point currentPosition)
{
Vector3D currentPosition3D = ProjectToTrackball(
EventSource.ActualWidth, EventSource.ActualHeight, currentPosition);
Vector change = Point.Subtract(_previousPosition2D, currentPosition);
Vector3D changeVector = new Vector3D(change.X, change.Y, 0);
_translate.OffsetX -= changeVector.X * .01;
_translate.OffsetY += changeVector.Y * .01;
_translate.OffsetZ -= changeVector.Z * .01;
_previousPosition3D = currentPosition3D;
}
private Vector3D ProjectToTrackball(double width, double height, Point point)
{
double x = point.X / (width / 2);
double y = point.Y / (height / 2);
x = x - 1;
y = 1 - y;
double z2 = 1 - x * x - y * y;
double z = z2 > 0 ? Math.Sqrt(z2) : 0;
return new Vector3D(x, y, z);
}
}
}
my xaml.cs code is here
<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using _3DTools;
using WpfCustomControlLibrary1;
namespace WpfApplication6
{
public partial class MainWindow : Window
{
MouseUtilities trackball = new MouseUtilities();
ScreenSpaceLines3D line = new ScreenSpaceLines3D();
public MainWindow()
{
InitializeComponent();
SetMatrixCamera();
trackball.EventSource = background;
Point3D p0 = new Point3D(-0.5, 0.5, 0.5);
Point3D p1 = new Point3D(0.5, 0.5, 0.5);
Point3D p2 = new Point3D(-0.5, -0.5, 0.5);
Point3D p3 = new Point3D(0.5, -0.5, 0.5);
Point3D p4 = new Point3D(-0.5, 0.5, -0.5);
Point3D p5 = new Point3D(0.5, 0.5, -0.5);
Point3D p6 = new Point3D(-0.5, -0.5, -0.5);
Point3D p7 = new Point3D(0.5, -0.5, -0.5);
line.Points.Add(p0);
line.Points.Add(p1);
line.Points.Add(p2);
line.Points.Add(p3);
line.Points.Add(p0);
line.Points.Add(p2);
line.Points.Add(p3);
line.Points.Add(p1);
line.Points.Add(p4);
line.Points.Add(p5);
line.Points.Add(p6);
line.Points.Add(p7);
line.Points.Add(p4);
line.Points.Add(p6);
line.Points.Add(p7);
line.Points.Add(p5);
line.Points.Add(p0);
line.Points.Add(p4);
line.Points.Add(p1);
line.Points.Add(p5);
line.Points.Add(p2);
line.Points.Add(p6);
line.Points.Add(p3);
line.Points.Add(p7);
line.Color = Colors.Red;
line.Thickness = 1.0;
line.Transform = trackball.Transform;
viewport.Children.Add(line);
}
private void SetMatrixCamera()
{
Point3D cameraPosition = new Point3D(0, 0, 20);
Vector3D lookDirection = new Vector3D(0, 0, -20);
Vector3D upDirection = new Vector3D(0, 1, 0);
double w = 6;
myCameraMatrix.Position = cameraPosition;
myCameraMatrix.LookDirection = lookDirection;
myCameraMatrix.Width = w;
myCameraMatrix.UpDirection = upDirection;
double zn = 1;
double zf = 100;
myCameraMatrix.NearPlaneDistance = zn;
myCameraMatrix.FarPlaneDistance = zf;
}
private void background_MouseMove(object sender, MouseEventArgs e)
{
}
bool isMouseDown;
Point mouseLastPosition;
private void background_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton != MouseButtonState.Pressed) return;
isMouseDown = true;
Point currentPosistion = Mouse.GetPosition(viewport);
mouseLastPosition = new Point(currentPosistion.X - viewport.ActualWidth / 2, viewport.ActualHeight / 2 - currentPosistion.Y);
}
private void background_MouseUp(object sender, MouseButtonEventArgs e)
{
isMouseDown = false;
}
}
}