private void MakeObjects(out MeshGeometry3D cameraMesh, out MeshGeometry3D boxMesh, out MeshGeometry3D edgeMesh, out Point3D cameraCenter) { // Make a "camera." cameraMesh = new MeshGeometry3D(); cameraMesh.AddBox(new Point3D(-0.6, -0.5, -0.2), D3.XVector(1.2), D3.YVector(1), D3.ZVector(0.4)); Point3D[] points = G3.MakePolygonPoints(20, new Point3D(0, 0, -0.7), D3.XVector(0.3), D3.YVector(0.3)); cameraMesh.AddCylinder(points, D3.ZVector(0.7), true); points = G3.MakePolygonPoints(20, new Point3D(0, 0, -0.8), D3.XVector(0.4), D3.YVector(0.4)); cameraMesh.AddCylinder(points, D3.ZVector(0.2), true); cameraMesh.AddBox(new Point3D(0.3, 0.5, -0.1), D3.XVector(0.2), D3.YVector(0.2), D3.ZVector(0.2)); MainGroup.Children.Add(cameraMesh.MakeModel(Brushes.LightBlue)); // Transform the camera and vector. Transform3DGroup trans = new Transform3DGroup(); RotateTransform3D r1 = D3.Rotate(D3.XVector(), D3.Origin, -45); trans.Children.Add(r1); RotateTransform3D r2 = D3.Rotate(D3.YVector(), D3.Origin, -45); trans.Children.Add(r2); // See where we need to translate to make the camera point at the origin. Point3D lookAtPoint = new Point3D(0, 0, -3.5); lookAtPoint = trans.Transform(lookAtPoint); TranslateTransform3D t1 = new TranslateTransform3D( -lookAtPoint.X, -lookAtPoint.Y, -lookAtPoint.Z); trans.Children.Add(t1); cameraMesh.ApplyTransformation(trans); cameraCenter = trans.Transform(D3.Origin); // Make a target box. boxMesh = new MeshGeometry3D(); boxMesh.AddBox(new Point3D(-0.75, -0.75, -0.75), D3.XVector(1.5), D3.YVector(1.5), D3.ZVector(1.5)); MainGroup.Children.Add(boxMesh.MakeModel(Brushes.LightGreen)); // Make the box's edges. edgeMesh = new MeshGeometry3D(); HashSet <Edge> edges = new HashSet <Edge>(); edgeMesh.AddBox(new Point3D(-0.75, -0.75, -0.75), D3.XVector(1.5), D3.YVector(1.5), D3.ZVector(1.5), edges: edges); MainGroup.Children.Add(edgeMesh.MakeModel(Brushes.Black)); }
// Define the model. private void DefineModel() { // Axes. MeshExtensions.AddAxes(MainGroup); // Make the objects. MeshGeometry3D cameraMesh, boxMesh, edgeMesh; Point3D cameraCenter; MakeObjects(out cameraMesh, out boxMesh, out edgeMesh, out cameraCenter); // Get the view transform. Matrix3D viewMat = Cameras.GetViewMatrix( new Point3D(cameraCenter.X, cameraCenter.Y, cameraCenter.Z), new Vector3D(-cameraCenter.X, -cameraCenter.Y, -cameraCenter.Z), D3.YVector()); // Apply the view transform. cameraMesh.ApplyTransformation(viewMat); boxMesh.ApplyTransformation(viewMat); edgeMesh.ApplyTransformation(viewMat); // Get the projection transformation. Matrix3D projMat = Cameras.GetPerspectiveProjectionMatrix( 60, 0.125, double.PositiveInfinity, mainViewport.ActualWidth / mainViewport.ActualHeight); boxMesh.ApplyTransformation(projMat); edgeMesh.ApplyTransformation(projMat); // Rotate the camera so it makes sense. RotateTransform3D rot = D3.Rotate(D3.YVector(), D3.Origin, 180); cameraMesh.ApplyTransformation(rot); // Move the objects a bit so they're not so close to the camera. TranslateTransform3D trans = new TranslateTransform3D(0, 0, 1f); boxMesh.ApplyTransformation(trans); edgeMesh.ApplyTransformation(trans); }