/// <summary> /// Creates a BoundingSphere from a given model and world matrix. /// </summary> /// <param name="model">Model to create BoundingSphere from.</param> /// <param name="world">Matrix to place BoundingSphere with.</param> /// <returns></returns> public static BoundingSphere CreateBoundingSphere(Model model, Matrix world) { Matrix[] boneTransforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(boneTransforms); BoundingSphere meshSphere, boundingSphere = new BoundingSphere(); for (int i = 0; i < model.Meshes.Count; i++) { meshSphere = TransformBoundingSphere(boneTransforms[i], model.Meshes[i].BoundingSphere); boundingSphere = BoundingSphere.Merge(boundingSphere, meshSphere); } return TransformBoundingSphere(world, boundingSphere); }
public Windmill(VisionContent vContent, Vector3 location) : base(vContent.LoadEffect("effects/SimpleTextureEffect")) { World = Matrix.RotationY(-MathUtil.PiOverTwo)*Matrix.Scaling(0.005f)*Matrix.Translation(location); _model = vContent.Load<Model>("models/windmill"); _texture = vContent.Load<Texture2D>("textures/windmill_diffuse"); _bones = new Matrix[_model.Bones.Count]; _model.CopyAbsoluteBoneTransformsTo(_bones); //foreach (var mesh in _model.Meshes) // foreach (var part in mesh.MeshParts) // part.Effect = Effect.Effect; _animation = new ObjectAnimation(new Vector3(0, 875, 0), new Vector3(0, 875, 0), Vector3.Zero, new Vector3(0, 0, MathUtil.TwoPi), TimeSpan.FromSeconds(10), true); }
public static Octree BuildOctree(Model model) { List<JVector> vertices = new List<JVector>(); List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>(); Matrix[] bones = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(bones); foreach (ModelMesh modelMesh in model.Meshes) { JMatrix boneTransform = PhysicsSystem.toJMatrix(bones[modelMesh.ParentBone.Index]); foreach (ModelMeshPart meshPart in modelMesh.MeshParts) { int offset = vertices.Count; var meshVertices = meshPart.VertexBuffer.Resource.Buffer.GetData<JVector>(); for (int i = 0; i < meshVertices.Length; ++i) { JVector.Transform(ref meshVertices[i], ref boneTransform, out meshVertices[i]); } vertices.AddRange(meshVertices); // append transformed vertices // there should DEFINITELY be a check here to ensure that the indices used in the model // don't exceed 65535 (max short int). If the model doesn't use shorts, then they get cast all weird. var indexElements = meshPart.IndexBuffer.Resource.GetData<short>(); // Each TriangleVertexIndices holds the indices that constitute a triangle primitive TriangleVertexIndices[] tvi = new TriangleVertexIndices[indexElements.Length]; for (int i = 0; i <= tvi.Length - 2; i += 3) { tvi[i].I0 = indexElements[i + 0] + offset; tvi[i].I1 = indexElements[i + 1] + offset; tvi[i].I2 = indexElements[i + 2] + offset; } indices.AddRange(tvi); // append triangles } } Octree ot = new Octree(vertices, indices); //ot.BuildOctree(); // (already happens in Octree constructor) return ot; }
private static List<JVector> ExtractVertices(Model model) { List<JVector> vertices = new List<JVector>(); Matrix[] bones = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(bones); foreach (ModelMesh modelMesh in model.Meshes) { JMatrix boneTransform = PhysicsSystem.toJMatrix(bones[modelMesh.ParentBone.Index]); foreach (ModelMeshPart meshPart in modelMesh.MeshParts) { int offset = vertices.Count; var meshVertices = meshPart.VertexBuffer.Resource.Buffer.GetData<JVector>(); for (int i = 0; i < meshVertices.Length; ++i) { JVector.Transform(ref meshVertices[i], ref boneTransform, out meshVertices[i]); } vertices.AddRange(meshVertices); // append transformed vertices } } return vertices; }