// Make the ground mesh. private void MakeGround(double groundY) { MeshGeometry3D groundMesh = new MeshGeometry3D(); const double dx = 15; const double dy = 1; const double dz = dx; Point3D corner = new Point3D(-dx / 2, groundY - dy, -dz / 2); groundMesh.AddBoxWrapped(corner, D3.XVector(dx), D3.YVector(dy), D3.ZVector(dz)); Point[] topCoords = { new Point(0.1, 0.1), new Point(0.1, 0.9), new Point(0.9, 0.9), new Point(0.9, 0.1), }; Point[] frontCoords = { new Point(0.0, 0.1), new Point(0.0, 0.9), new Point(0.1, 0.9), new Point(0.1, 0.1), }; Point[] leftCoords = { new Point(0.9, 0.0), new Point(0.1, 0.0), new Point(0.1, 0.1), new Point(0.9, 0.1), }; Point[] rightCoords = { new Point(0.1, 1.0), new Point(0.9, 1.0), new Point(0.9, 0.9), new Point(0.1, 0.9), }; Point[] backCoords = { new Point(1.0, 0.9), new Point(1.0, 0.1), new Point(0.9, 0.1), new Point(0.9, 0.9), }; Point[] bottomCoords = { new Point(0.9, 0.1), new Point(0.9, 0.9), new Point(0.1, 0.9), new Point(0.1, 0.1), }; groundMesh.AddBox(corner, D3.XVector(dx), D3.YVector(dy), D3.ZVector(dz), frontCoords, leftCoords, rightCoords, backCoords, topCoords, bottomCoords); MainGroup.Children.Add(groundMesh.MakeModel("rock.jpg")); }
// Process the models. private void ProcessModels(bool invertTextures, bool zIsUp) { // Make the dictionary of materials. foreach (ObjMaterial material in AllMaterials) { // Make the material's MaterialGroup. material.MatGroup = new MaterialGroup(); // Transparency. (Not used.) byte alpha = (byte)(material.Alpha * 255); // Diffuse. byte diffR = (byte)(material.Kd.X * 255); byte diffG = (byte)(material.Kd.Y * 255); byte diffB = (byte)(material.Kd.Z * 255); Color diffColor = Color.FromArgb(255, diffR, diffG, diffB); SolidColorBrush diffBrush = new SolidColorBrush(diffColor); DiffuseMaterial diffMat = new DiffuseMaterial(diffBrush); material.MatGroup.Children.Add(diffMat); // If it has a file, use it. if (material.Filename != null) { // Use the file. string filename = material.Filename; ImageBrush imgBrush = new ImageBrush(); imgBrush.ViewportUnits = BrushMappingMode.Absolute; imgBrush.TileMode = TileMode.Tile; // Invert the texture if necessary. if (invertTextures) { TransformGroup trans = new TransformGroup(); trans.Children.Add(new ScaleTransform(1, -1)); trans.Children.Add(new TranslateTransform(0, 1)); imgBrush.Transform = trans; } imgBrush.ImageSource = new BitmapImage(new Uri(filename, UriKind.Relative)); DiffuseMaterial imgMat = new DiffuseMaterial(imgBrush); material.MatGroup.Children.Add(imgMat); } // Specular. byte specR = (byte)(material.Ks.X * 255); byte specG = (byte)(material.Ks.Y * 255); byte specB = (byte)(material.Ks.Z * 255); Color specColor = Color.FromArgb(255, specR, specG, specB); SolidColorBrush specBrush = new SolidColorBrush(specColor); SpecularMaterial specMat = new SpecularMaterial(specBrush, material.Ns); material.MatGroup.Children.Add(specMat); // We ignore Ka and Tr. // Add it to the materials dictionary. MtlMaterials.Add(material.Name, material); } // Convert the object models into meshes. foreach (ObjModel model in AllObjectModels) { // Make the mesh. MeshGeometry3D mesh = new MeshGeometry3D(); Meshes.Add(mesh); MeshNames.Add(model.Name); MaterialNames.Add(model.MaterialName); // Make a new list of smoothing groups. Dictionary <int, Dictionary <Point3D, int> > smoothingGroups = new Dictionary <int, Dictionary <Point3D, int> >(); // Entry 0 is null (no smoothing). smoothingGroups.Add(0, null); // Make the faces. foreach (ObjFace face in model.Faces) { // Make the face's vertices. int numPoints = face.Vertices.Count; Point3D[] points = new Point3D[numPoints]; for (int i = 0; i < numPoints; i++) { points[i] = AllVertices[face.Vertices[i] - 1]; } // Get texture coordinates if present. Point[] textureCoords = null; if (face.TextureCoords.Count > 0) { textureCoords = new Point[numPoints]; for (int i = 0; i < numPoints; i++) { textureCoords[i] = AllTextureCoordinates[face.TextureCoords[i] - 1]; } } // Get normals if present. Vector3D[] normals = null; if (face.Normals.Count > 0) { normals = new Vector3D[numPoints]; for (int i = 0; i < numPoints; i++) { normals[i] = AllNormals[face.Normals[i] - 1]; } } // Get the point dictionary for this smoothing group. // Add new groups if needed. if (!smoothingGroups.ContainsKey(face.SmoothingGroup)) { smoothingGroups.Add(face.SmoothingGroup, new Dictionary <Point3D, int>()); } Dictionary <Point3D, int> pointDict = smoothingGroups[face.SmoothingGroup]; // Make the polygon. mesh.AddPolygon(pointDict: pointDict, textureCoords: textureCoords, normals: normals, points: points); } // If Z is up, rotate the model. if (zIsUp) { mesh.ApplyTransformation(D3.Rotate(D3.XVector(), D3.Origin, -90)); } } }
// Define the model. private void DefineModel() { // Axes. //MainGroup.Children.Add(MeshExtensions.XAxisModel(5, 0.1)); //MainGroup.Children.Add(MeshExtensions.YAxisModel(12, 0.1)); //MainGroup.Children.Add(MeshExtensions.ZAxisModel(5, 0.1)); //MainGroup.Children.Add(MeshExtensions.OriginModel(0.12)); // Make the ground. const double groundY = -5; MakeGround(groundY); // This group represents the whole robot. RobotGroup = new Model3DGroup(); MainGroup.Children.Add(RobotGroup); // Various robot dimensions. const double headR = 1.5; // Head radius. const double neckLen = headR; // Neck length. const double backLen = 3 * headR; // Back length. const double shouW = 3 * headR; // Shoulder width. const double uaLen = 2 * headR; // Upper arm length. const double laLen = 2 * headR; // Lower arm length const double hipsW = 2 * headR; // Hip width. const double ulLen = 2 * headR; // Upper leg length. const double llLen = 2 * headR; // Lower leg length. const double boneR = 0.3; // Bone radius. const double jointR = 0.4; // Joint radius. const double height = 2 * headR + neckLen + backLen + ulLen + llLen; const double headY = height - headR; // Distance from center of head to ground. Brush boneBrush = Brushes.PowderBlue; // This group represents the whole robot. RobotGroup = new Model3DGroup(); MainGroup.Children.Add(RobotGroup); RobotGroup.Transform = new TranslateTransform3D(0, headY + groundY, 0); // Head. // Skull. MeshGeometry3D skullMesh = new MeshGeometry3D(); skullMesh.AddSphere(D3.Origin, headR, 20, 10, true); GeometryModel3D skullModel = skullMesh.MakeModel(boneBrush); // Nose. MeshGeometry3D noseMesh = new MeshGeometry3D(); Point3D noseCenter = new Point3D(0, 0, headR); Point3D[] nosePoints = G3.MakePolygonPoints(10, noseCenter, D3.XVector(headR * 0.2), D3.YVector(headR * 0.2)); Vector3D noseAxis = new Vector3D(0, 0, headR); noseMesh.AddConeFrustum(noseCenter, nosePoints, noseAxis, headR * 0.5); GeometryModel3D noseModel = noseMesh.MakeModel(Brushes.Orange); // Eyes and smile. MeshGeometry3D eyeMesh = new MeshGeometry3D(); Point3D eyeCenter = SphericalToCartesian(headR, -Math.PI * 0.2, Math.PI * 0.4); eyeMesh.AddSphere(eyeCenter, headR * 0.2, 10, 5, false); eyeCenter = SphericalToCartesian(headR, Math.PI * 0.2, Math.PI * 0.4); eyeMesh.AddSphere(eyeCenter, headR * 0.2, 10, 5, false); eyeCenter = SphericalToCartesian(headR, Math.PI * 0, Math.PI * 0.7); eyeMesh.AddSphere(eyeCenter, headR * 0.1, 10, 5, false); eyeCenter = SphericalToCartesian(headR, Math.PI * 0.1, Math.PI * 0.67); eyeMesh.AddSphere(eyeCenter, headR * 0.1, 10, 5, false); eyeCenter = SphericalToCartesian(headR, -Math.PI * 0.1, Math.PI * 0.67); eyeMesh.AddSphere(eyeCenter, headR * 0.1, 10, 5, false); eyeCenter = SphericalToCartesian(headR, Math.PI * 0.15, Math.PI * 0.6); eyeMesh.AddSphere(eyeCenter, headR * 0.1, 10, 5, false); eyeCenter = SphericalToCartesian(headR, -Math.PI * 0.15, Math.PI * 0.6); eyeMesh.AddSphere(eyeCenter, headR * 0.1, 10, 5, false); GeometryModel3D eyeModel = eyeMesh.MakeModel(Brushes.Black); // Hat. MeshGeometry3D hatMesh = new MeshGeometry3D(); Point3D hatCenter = new Point3D(0, headR * 0.75, 0); hatMesh.AddSphere(hatCenter, headR * 0.75, 20, 10, true); const double hatR = headR * 1.2; Point3D[] hatPgon = G3.MakePolygonPoints(20, hatCenter, D3.XVector(hatR), D3.ZVector(hatR)); hatMesh.AddCylinder(hatPgon, D3.YVector(-0.2), true); GeometryModel3D hatModel = hatMesh.MakeModel(Brushes.SaddleBrown); // Head groups. HeadGroup = JoinBones(RobotGroup, null); HeadGroup.Children.Add(skullModel); HeadGroup.Children.Add(noseModel); HeadGroup.Children.Add(eyeModel); HeadGroup.Children.Add(hatModel); // Neck. MeshGeometry3D neckMesh = new MeshGeometry3D(); Point3D[] neckPgon = G3.MakePolygonPoints(10, D3.Origin, D3.XVector(boneR), D3.ZVector(boneR)); neckMesh.AddCylinder(neckPgon, D3.YVector(-neckLen), true); GeometryModel3D neckModel = neckMesh.MakeModel(boneBrush); NeckGroup = JoinBones(HeadGroup, new TranslateTransform3D(0, -headR, 0)); NeckGroup.Children.Add(neckModel); // Shoulders. MeshGeometry3D shoulderMesh = new MeshGeometry3D(); Point3D[] shouldersPgon = G3.MakePolygonPoints(10, new Point3D(-shouW / 2, 0, 0), D3.ZVector(boneR), D3.YVector(-boneR)); shoulderMesh.AddCylinder(shouldersPgon, D3.XVector(shouW), true); GeometryModel3D shoulderModel = shoulderMesh.MakeModel(boneBrush); ShoulderGroup = JoinBones(NeckGroup, new TranslateTransform3D(0, -neckLen, 0)); ShoulderGroup.Children.Add(shoulderModel); // Left upper arm. MeshGeometry3D luArmMesh = new MeshGeometry3D(); luArmMesh.AddCylinder(neckPgon, D3.YVector(-uaLen), true); luArmMesh.AddSphere(D3.Origin, jointR, 10, 5, true); GeometryModel3D luArmModel = luArmMesh.MakeModel(boneBrush); LuArmGroup = JoinBones(ShoulderGroup, new TranslateTransform3D(shouW / 2, 0, 0)); LuArmGroup.Children.Add(luArmModel); // Right upper arm. MeshGeometry3D ruArmMesh = new MeshGeometry3D(); ruArmMesh.AddCylinder(neckPgon, D3.YVector(-uaLen), true); ruArmMesh.AddSphere(D3.Origin, jointR, 10, 5, true); GeometryModel3D ruArmModel = ruArmMesh.MakeModel(boneBrush); RuArmGroup = JoinBones(ShoulderGroup, new TranslateTransform3D(-shouW / 2, 0, 0)); RuArmGroup.Children.Add(ruArmModel); // Left lower arm. MeshGeometry3D llArmMesh = new MeshGeometry3D(); llArmMesh.AddCylinder(neckPgon, D3.YVector(-laLen), true); llArmMesh.AddSphere(D3.Origin, jointR, 10, 5, true); GeometryModel3D llArmModel = llArmMesh.MakeModel(boneBrush); LlArmGroup = JoinBones(LuArmGroup, new TranslateTransform3D(0, -uaLen, 0)); LlArmGroup.Children.Add(llArmModel); // Right lower arm. MeshGeometry3D rlArmMesh = new MeshGeometry3D(); rlArmMesh.AddCylinder(neckPgon, D3.YVector(-laLen), true); rlArmMesh.AddSphere(D3.Origin, jointR, 10, 5, true); GeometryModel3D rlArmModel = rlArmMesh.MakeModel(boneBrush); RlArmGroup = JoinBones(RuArmGroup, new TranslateTransform3D(0, -uaLen, 0)); RlArmGroup.Children.Add(rlArmModel); // Back and hips. MeshGeometry3D backMesh = new MeshGeometry3D(); backMesh.AddCylinder(neckPgon, D3.YVector(-backLen), true); GeometryModel3D backModel = backMesh.MakeModel(boneBrush); MeshGeometry3D hipsMesh = new MeshGeometry3D(); Point3D[] hipsPgon = G3.MakePolygonPoints(10, new Point3D(-hipsW / 2, -backLen, 0), D3.ZVector(boneR), D3.YVector(-boneR)); hipsMesh.AddCylinder(hipsPgon, D3.XVector(hipsW), true); GeometryModel3D hipsModel = hipsMesh.MakeModel(boneBrush); BackGroup = JoinBones(NeckGroup, new TranslateTransform3D(0, -neckLen, 0)); BackGroup.Children.Add(backModel); BackGroup.Children.Add(hipsModel); // Left upper leg. MeshGeometry3D luLegMesh = new MeshGeometry3D(); luLegMesh.AddCylinder(neckPgon, D3.YVector(-ulLen), true); luLegMesh.AddSphere(D3.Origin, jointR, 10, 5, true); GeometryModel3D luLegModel = luLegMesh.MakeModel(boneBrush); LuLegGroup = JoinBones(BackGroup, new TranslateTransform3D(-hipsW / 2, -backLen, 0)); LuLegGroup.Children.Add(luLegModel); // Right upper leg. MeshGeometry3D ruLegMesh = new MeshGeometry3D(); ruLegMesh.AddCylinder(neckPgon, D3.YVector(-ulLen), true); ruLegMesh.AddSphere(D3.Origin, jointR, 10, 5, true); GeometryModel3D ruLegModel = ruLegMesh.MakeModel(boneBrush); RuLegGroup = JoinBones(BackGroup, new TranslateTransform3D(hipsW / 2, -backLen, 0)); RuLegGroup.Children.Add(ruLegModel); // Left lower leg. MeshGeometry3D llLegMesh = new MeshGeometry3D(); llLegMesh.AddCylinder(neckPgon, D3.YVector(-llLen), true); llLegMesh.AddSphere(D3.Origin, jointR, 10, 5, true); GeometryModel3D llLegModel = llLegMesh.MakeModel(boneBrush); LlLegGroup = JoinBones(LuLegGroup, new TranslateTransform3D(0, -ulLen, 0)); LlLegGroup.Children.Add(llLegModel); // Right lower leg. MeshGeometry3D rlLegMesh = new MeshGeometry3D(); rlLegMesh.AddCylinder(neckPgon, D3.YVector(-llLen), true); rlLegMesh.AddSphere(D3.Origin, jointR, 10, 5, true); GeometryModel3D rlLegModel = rlLegMesh.MakeModel(boneBrush); RlLegGroup = JoinBones(RuLegGroup, new TranslateTransform3D(0, -ulLen, 0)); RlLegGroup.Children.Add(rlLegModel); }
private void rightKneeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e) { RlLegGroup.Transform = D3.Rotate(-D3.XVector(), D3.Origin, rightKneeSlider.Value); }
private void leftElbowSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e) { LlArmGroup.Transform = D3.Rotate(-D3.XVector(), D3.Origin, leftElbowSlider.Value); }
private void neckSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e) { NeckGroup.Transform = D3.Rotate(D3.YVector(), D3.Origin, neckSlider.Value); }