XML Shrink ▲ <Window x:Class="WPFChart.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF 3D Chart" Height="500" Width="600"> <Grid> <Viewport3D Name="mainViewport" > <Viewport3D.Camera> <OrthographicCamera x:Name="camera" FarPlaneDistance="10" NearPlaneDistance="1" LookDirection="0,0,-1" UpDirection="0,1,0" Position="0,0,2" /> </Viewport3D.Camera> <Viewport3D.Children> <ModelVisual3D x:Name="Light1"> <ModelVisual3D.Content> <DirectionalLight Color="White" Direction="1,1,-1"/> </ModelVisual3D.Content> </ModelVisual3D> </Viewport3D.Children> </Viewport3D> </Grid> </Window>
System.Windows.Media.Media3D.Point3D point0 = new Point3D(-0.5, 0, 0); System.Windows.Media.Media3D.Point3D point1 = new Point3D(0.5, 0.5, 0.3); System.Windows.Media.Media3D.Point3D point2 = new Point3D(0, 0.5, 0);
System.Windows.Media.Media3D.MeshGeometry3D triangleMesh = new MeshGeometry3D(); triangleMesh.Positions.Add(point0); triangleMesh.Positions.Add(point1); triangleMesh.Positions.Add(point2);
int n0 = 0; int n1 = 1; int n2 = 2;
triangleMesh.TriangleIndices.Add(n0); triangleMesh.TriangleIndices.Add(n1); triangleMesh.TriangleIndices.Add(n2);
System.Windows.Media.Media3D.Vector3D norm = new Vector3D(0, 0, 1); triangleMesh.Normals.Add(norm); triangleMesh.Normals.Add(norm); triangleMesh.Normals.Add(norm);
System.Windows.Media.Media3D.Material frontMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));
System.Windows.Media.Media3D.GeometryModel3D triangleModel = new GeometryModel3D(triangleMesh, frontMaterial);
triangleModel.Transform = new Transform3DGroup();
System.Windows.Media.Media3D.ModelVisual3D visualModel = new ModelVisual3D(); visualModel.Content = triangleModel;
this.mainViewport.Children.Add(visualModel);
public class TransformMatrix { public Matrix3D m_viewMatrix = new Matrix3D(); private Point m_movePoint; }
public class TransformMatrix { public void OnMouseMove(Point pt, System.Windows.Controls.Viewport3D viewPort) { double width = viewPort.ActualWidth; double height = viewPort.ActualHeight; if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) { } else { double aY = 180 * (pt.X - m_movePoint.X) / width; double aX = 180 * (pt.Y - m_movePoint.Y) / height; m_viewMatrix.Rotate(new Quaternion(new Vector3D(1, 0, 0), aX)); m_viewMatrix.Rotate(new Quaternion(new Vector3D(0, 1, 0), aY)); m_movePoint = pt; } } }
public partial class Window1 : Window { public WPFChart.TransformMatrix m_transformMatrix = new WPFChart.TransformMatrix(); public void OnViewportMouseMove(object sender, System.Windows.Input.MouseEventArgs args) { Point pt = args.GetPosition(mainViewport); if (args.LeftButton == MouseButtonState.Pressed) { m_transformMatrix.OnMouseMove(pt, mainViewport); Transform3DGroup group1 = triangleModel.Transform as Transform3DGroup; group1.Children.Clear(); group1.Children.Add(new MatrixTransform3D(transformMatrix.m_ m_viewMatrix)); } } }
public class TransformMatrix { private Matrix3D m_viewMatrix = new Matrix3D(); private Matrix3D m_projMatrix = new Matrix3D(); public Matrix3D m_totalMatrix = new Matrix3D(); }
public class TransformMatrix { public void CalculateProjectionMatrix(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax, double k) { double xC = (xMin + xMax) / 2; double yC = (yMin + yMax) / 2; double zC = (zMin + zMax) / 2; m_projMatrix.SetIdentity(); m_projMatrix.Translate(new Vector3D(-xC, -yC, -zC)); double sX = k*2 / (xMax - xMin); m_projMatrix.Scale(new Vector3D(sX, sX, sX)); m_totalMatrix = Matrix3D.Multiply(m_projMatrix, m_viewMatrix); } }
public class TransformMatrix { public Point VertexToScreenPt(Point3D point, System.Windows.Controls.Viewport3D viewPort) { Point3D pt2 = m_totalMatrix.Transform(point); double width = viewPort.ActualWidth; double height = viewPort.ActualHeight; double x3 = width / 2 + (pt2.X) * width / 2; double y3 = height / 2 - (pt2.Y) * width / 2; return new Point(x3, y3); } }
public partial class Window1 : Window { public void OnViewportMouseMove(object sender, System.Windows.Input.MouseEventArgs args) { Point pt = args.GetPosition(mainViewport); if (args.LeftButton == MouseButtonState.Pressed) { } else { String s1; Point pt2 = m_transformMatrix.VertexToScreenPt (new Point3D(0.5, 0.5, 0.3), mainViewport); s1 = string.Format("Screen:({0:d},{1:d}), Predicated:({2:d}, H:{3:d})", (int)pt.X, (int)pt.Y, (int)pt2.X, (int)pt2.Y); this.statusPane.Text = s1; } } }
public class TransformMatrix { public void OnMouseMove(Point pt, System.Windows.Controls.Viewport3D viewPort) { double width = viewPort.ActualWidth; double height = viewPort.ActualHeight; if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) { double shiftX = 2 *(pt.X - m_movePoint.X) /( width); double shiftY = -2 *(pt.Y - m_movePoint.Y)/( width); m_viewMatrix.Translate(new Vector3D(shiftX, shiftY, 0)); m_movePoint = pt; } m_totalMatrix = Matrix3D.Multiply(m_projMatrix, m_viewMatrix); } }
public class Mesh3D { private Point3D [] m_points; // x, y, z coordinate private Triangle3D [] m_tris; // triangle information private Color m_color; // mesh color public double m_xMin, m_xMax, m_yMin, m_yMax, m_zMin, m_zMax; }
public class Triangle3D { public int n0, n1, n2; }
public class TextureMapping { public DiffuseMaterial m_material; private void SetRGBMaping() { WriteableBitmap writeableBitmap = new WriteableBitmap(64, 64, 96, 96, PixelFormats.Bgr24, null); writeableBitmap.Lock();
unsafe { byte* pStart = (byte*)(void*)writeableBitmap.BackBuffer; int nL = writeableBitmap.BackBufferStride; for (int r = 0; r < 16; r++) { for (int g = 0; g < 16; g++) { for (int b = 0; b < 16; b++) { int nX = (g % 4) * 16 + b; int nY = r*4 + (int)(g/4); *(pStart + nY*nL + nX*3 + 0) = (byte)(b * 17); *(pStart + nY*nL + nX*3 + 1) = (byte)(g * 17); *(pStart + nY*nL + nX*3 + 2) = (byte)(r * 17); } } } }
writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, 64, 64)); writeableBitmap.Unlock(); ImageBrush imageBrush = new ImageBrush(writeableBitmap); imageBrush.ViewportUnits = BrushMappingMode.Absolute; m_material = new DiffuseMaterial(); m_material.Brush = imageBrush; } }
public class TextureMapping { public Point GetMappingPosition(Color color) { int r = (color.R) / 17; int g = (color.G) / 17; int b = (color.B) / 17; int nX = (g % 4) * 16 + b; int nY = r * 4 + (int)(g / 4); return new Point((double)nX /63, (double)nY /63); } }