/// <summary> /// Helper Method to get the vertex and index List from the model. /// </summary> /// <param name="vertices"></param> /// <param name="indices"></param> /// <param name="model"></param> public void ExtractData(List<Vector3> vertices, List<TriangleVertexIndices> indices,Model model) { Matrix[] bones_ = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(bones_); foreach (ModelMesh mm in model.Meshes) { Matrix xform = bones_[mm.ParentBone.Index]; foreach (ModelMeshPart mmp in mm.MeshParts) { int offset = vertices.Count; Vector3[] a = new Vector3[mmp.NumVertices]; mm.VertexBuffer.GetData<Vector3>(mmp.StreamOffset + mmp.BaseVertex * mmp.VertexStride, a, 0, mmp.NumVertices, mmp.VertexStride); for (int i = 0; i != a.Length; ++i) Vector3.Transform(ref a[i], ref xform, out a[i]); vertices.AddRange(a); if (mm.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits) throw new Exception( String.Format("Model uses 32-bit indices, which are not supported.")); short[] s = new short[mmp.PrimitiveCount * 3]; mm.IndexBuffer.GetData<short>(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3); JigLibX.Geometry.TriangleVertexIndices[] tvi = new JigLibX.Geometry.TriangleVertexIndices[mmp.PrimitiveCount]; for (int i = 0; i != tvi.Length; ++i) { tvi[i].I0 = s[i * 3 + 2] + offset; tvi[i].I1 = s[i * 3 + 1] + offset; tvi[i].I2 = s[i * 3 + 0] + offset; } indices.AddRange(tvi); } } }
// Extracts the neccesary information from a model to // simulate physics on it public void ExtractData(List <Vector3> vertices, List <TriangleVertexIndices> indices, Model model) { Matrix[] bones_ = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(bones_); foreach (ModelMesh mm in model.Meshes) { int offset = vertices.Count; Matrix xform = bones_[mm.ParentBone.Index]; foreach (ModelMeshPart mmp in mm.MeshParts) { Vector3[] a = new Vector3[mmp.NumVertices]; int stride = mmp.VertexBuffer.VertexDeclaration.VertexStride; byte[] bRaw = new byte[mmp.VertexBuffer.VertexCount * mmp.VertexBuffer.VertexDeclaration.VertexStride]; mmp.VertexBuffer.GetData <byte>(bRaw); for (int i = 0; i < mmp.NumVertices; i++) { int iStart = (i + mmp.VertexOffset) * stride; a[i] = new Vector3(BitConverter.ToSingle(bRaw, iStart), BitConverter.ToSingle(bRaw, iStart + 4), BitConverter.ToSingle(bRaw, iStart + 8)); } for (int i = 0; i != a.Length; ++i) { Vector3.Transform(ref a[i], ref xform, out a[i]); } vertices.AddRange(a); if (mmp.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits) { throw new Exception(String.Format("Model uses 32-bit indices, which are not supported.")); } short[] s = new short[mmp.PrimitiveCount * 3]; mmp.IndexBuffer.GetData(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3); JigLibX.Geometry.TriangleVertexIndices[] tvi = new JigLibX.Geometry.TriangleVertexIndices[mmp.PrimitiveCount]; for (int i = 0; i != tvi.Length; ++i) { tvi[i].I0 = s[i * 3 + 2] + offset; tvi[i].I1 = s[i * 3 + 1] + offset; tvi[i].I2 = s[i * 3 + 0] + offset; } indices.AddRange(tvi); } } }
// Extracts the neccesary information from a model to // simulate physics on it public void ExtractData(List<Vector3> vertices, List<TriangleVertexIndices> indices, Model model) { Matrix[] bones_ = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(bones_); foreach (ModelMesh mm in model.Meshes) { int offset = vertices.Count; Matrix xform = bones_[mm.ParentBone.Index]; foreach (ModelMeshPart mmp in mm.MeshParts) { Vector3[] a = new Vector3[mmp.NumVertices]; int stride = mmp.VertexBuffer.VertexDeclaration.VertexStride; byte[] bRaw = new byte[mmp.VertexBuffer.VertexCount * mmp.VertexBuffer.VertexDeclaration.VertexStride]; mmp.VertexBuffer.GetData<byte>(bRaw); for (int i = 0; i < mmp.NumVertices; i++) { int iStart = (i + mmp.VertexOffset) * stride; a[i] = new Vector3(BitConverter.ToSingle(bRaw, iStart), BitConverter.ToSingle(bRaw, iStart + 4), BitConverter.ToSingle(bRaw, iStart + 8)); } for (int i = 0; i != a.Length; ++i) Vector3.Transform(ref a[i], ref xform, out a[i]); vertices.AddRange(a); if (mmp.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits) throw new Exception(String.Format("Model uses 32-bit indices, which are not supported.")); short[] s = new short[mmp.PrimitiveCount * 3]; mmp.IndexBuffer.GetData(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3); JigLibX.Geometry.TriangleVertexIndices[] tvi = new JigLibX.Geometry.TriangleVertexIndices[mmp.PrimitiveCount]; for (int i = 0; i != tvi.Length; ++i) { tvi[i].I0 = s[i * 3 + 2] + offset; tvi[i].I1 = s[i * 3 + 1] + offset; tvi[i].I2 = s[i * 3 + 0] + offset; } indices.AddRange(tvi); } } }
/// <summary> /// Helper Method to get the vertex and index List from the model. /// </summary> /// <param name="vertices"></param> /// <param name="indices"></param> /// <param name="model"></param> public void ExtractData(List <Vector3> vertices, List <TriangleVertexIndices> indices, Model model) { Matrix[] bones_ = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(bones_); foreach (ModelMesh mm in model.Meshes) { int offset = vertices.Count; Matrix xform = bones_[mm.ParentBone.Index]; foreach (ModelMeshPart mmp in mm.MeshParts) { Vector3[] a = new Vector3[mmp.NumVertices]; int stride = mmp.VertexBuffer.VertexDeclaration.VertexStride; //mm.VertexBuffer.GetData<Vector3>(mmp.StreamOffset + mmp.BaseVertex * mmp.VertexStride, a, 0, mmp.NumVertices, mmp.VertexStride); //XNA 4.0 change mmp.VertexBuffer.GetData(mmp.VertexOffset * stride, a, 0, mmp.NumVertices, stride); for (int i = 0; i != a.Length; ++i) { Vector3.Transform(ref a[i], ref xform, out a[i]); } vertices.AddRange(a); //if (mm.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits) //XNA 4.0 change if (mmp.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits) { throw new Exception(String.Format("Model uses 32-bit indices, which are not supported.")); } short[] s = new short[mmp.PrimitiveCount * 3]; //mm.IndexBuffer.GetData<short>(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3); //XNA 4.0 change mmp.IndexBuffer.GetData(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3); JigLibX.Geometry.TriangleVertexIndices[] tvi = new JigLibX.Geometry.TriangleVertexIndices[mmp.PrimitiveCount]; for (int i = 0; i != tvi.Length; ++i) { tvi[i].I0 = s[i * 3 + 2] + offset; tvi[i].I1 = s[i * 3 + 1] + offset; tvi[i].I2 = s[i * 3 + 0] + offset; } indices.AddRange(tvi); } } }
void GenerateCollisionMesh(VoxelGeometry geometry) { List<Vector3> vertices = new List<Vector3>(geometry.verts.Length); List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>(geometry.ib.Length / 3); Matrix transform = this.Transformation.GetTransform(); for (int i = 0; i < geometry.verts.Length; i++) vertices.Add(Vector3.Transform(geometry.verts[i].Position, transform)); for (int i = 0; i < geometry.ib.Length; i += 3) { TriangleVertexIndices tri = new TriangleVertexIndices(geometry.ib[i + 2], geometry.ib[i + 1], geometry.ib[i]); indices.Add(tri); } TriangleMesh collisionMesh = new TriangleMesh(vertices, indices); CollisionSkin collision = new CollisionSkin(null); collision.AddPrimitive(collisionMesh, (int)MaterialTable.MaterialID.NotBouncyRough); CollisionSkin collision2 = new CollisionSkin(null); collision2.AddPrimitive(new JigLibX.Geometry.Plane(Vector3.Up, Transformation.GetBounds().Min.Y-3f), (int)MaterialTable.MaterialID.NotBouncyRough); scene.GetPhysicsEngine().CollisionSystem.AddCollisionSkin(collision); //scene.GetPhysicsEngine().CollisionSystem.AddCollisionSkin(collision2); }
public void CreateMesh(Model model) { var vertices = new List<Vector3>(); var indices = new List<TriangleVertexIndices>(); var boneTransforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(boneTransforms); foreach (var mesh in model.Meshes) { var transform = boneTransforms[mesh.ParentBone.Index]; foreach (var part in mesh.MeshParts) { if (part.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits) { throw new InvalidOperationException( "Model uses 32-bit indices, which are not supported."); } var declaration = part.VertexBuffer.VertexDeclaration; var vertexPosition = new VertexElement(); foreach (var element in declaration.GetVertexElements()) { if (element.VertexElementUsage == VertexElementUsage.Position && element.VertexElementFormat == VertexElementFormat.Vector3) { vertexPosition = element; break; } } if (vertexPosition.VertexElementUsage != VertexElementUsage.Position || vertexPosition.VertexElementFormat != VertexElementFormat.Vector3) { throw new InvalidOperationException( "Model has no vertex element with Vector3 position."); } int offset = vertices.Count; var vData = new Vector3[part.NumVertices]; part.VertexBuffer.GetData<Vector3>( part.VertexOffset * declaration.VertexStride + vertexPosition.Offset, vData, 0, part.NumVertices, declaration.VertexStride); for (int i = 0; i < vData.Length; i++) { Vector3.Transform( ref vData[i], ref transform, out vData[i]); } vertices.AddRange(vData); var iData = new short[part.PrimitiveCount * 3]; part.IndexBuffer.GetData<short>( part.StartIndex * 2, iData, 0, part.PrimitiveCount * 3); for (int i = 0; i < part.PrimitiveCount; i++) { var tvi = new TriangleVertexIndices() { I0 = iData[i * 3 + 2] + offset, I1 = iData[i * 3 + 1] + offset, I2 = iData[i * 3 + 0] + offset }; indices.Add(tvi); } } } CreateMesh(vertices, indices); }
/// <summary> /// Helper method to get the vertex and index lists from the model. /// </summary> /// <param name="vertices"> /// List which is to be filled with the vertices of the model. /// </param> /// <param name="indices"> /// List which is to be filled with the indices of the model. /// </param> /// <param name="model"> /// Model from which the vertices and indices are to be exctracted. /// </param> /// <param name="scale">Scale factors which are to be applied to each vertex.</param> protected void ExtractDataFromModel(List <Vector3> vertices, List <TriangleVertexIndices> indices, Model model, Vector3 scale) { Matrix[] bones = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(bones); foreach (ModelMesh mm in model.Meshes) { Matrix xform = bones[mm.ParentBone.Index]; foreach (ModelMeshPart mmp in mm.MeshParts) { //Get the vertices from the vertex buffer. int offset = vertices.Count; Vector3[] a = new Vector3[mmp.NumVertices]; int stride = mmp.VertexBuffer.VertexDeclaration.VertexStride; mmp.VertexBuffer.GetData <Vector3>(mmp.VertexOffset * stride, a, 0, mmp.NumVertices, stride); for (int i = 0; i != a.Length; ++i) { Vector3.Transform(ref a[i], ref xform, out a[i]); } vertices.AddRange(a); if (mmp.IndexBuffer.IndexElementSize == IndexElementSize.SixteenBits) { //Get the indices from the index buffer. short[] rawIndices = new short[mmp.PrimitiveCount * 3]; mmp.IndexBuffer.GetData <short>(mmp.StartIndex * 2, rawIndices, 0, mmp.PrimitiveCount * 3); JigLibX.Geometry.TriangleVertexIndices[] tvi = new JigLibX.Geometry.TriangleVertexIndices[mmp.PrimitiveCount]; for (int i = 0; i < tvi.Length; ++i) { tvi[i].I0 = rawIndices[i * 3 + 0] + offset; tvi[i].I1 = rawIndices[i * 3 + 1] + offset; tvi[i].I2 = rawIndices[i * 3 + 2] + offset; } indices.AddRange(tvi); } else { //Get the indices from the index buffer. int[] rawIndices = new int[mmp.PrimitiveCount * 3]; mmp.IndexBuffer.GetData <int>(mmp.StartIndex * 4, rawIndices, 0, mmp.PrimitiveCount * 3); JigLibX.Geometry.TriangleVertexIndices[] tvi = new JigLibX.Geometry.TriangleVertexIndices[mmp.PrimitiveCount]; for (int i = 0; i != tvi.Length; ++i) { tvi[i].I0 = rawIndices[i * 3 + 2] + offset; tvi[i].I1 = rawIndices[i * 3 + 1] + offset; tvi[i].I2 = rawIndices[i * 3 + 0] + offset; } indices.AddRange(tvi); } } } //Scale all the vertices. if (!scale.Equals(Vector3.One)) { int count = vertices.Count; for (int i = 0; i < count; ++i) { vertices[i] = vertices[i] * scale; } } }
public void AddTriangles(Vector3[] _positions, TriangleVertexIndices[] _tris) { // copy the position data into a array //positions = new Vector3[_positions.Count]; positions = _positions; // copy the triangles //tris = new TriangleVertexIndices[_tris.Count]; tris = _tris; }
void GenerateCollisionMesh() { List<Vector3> vertColl = new List<Vector3>(); for (int i = 0; i < geometry.verts.Length; i++) { vertColl.Add(Vector3.Transform(new Vector3(geometry.verts[i].Position.X, geometry.verts[i].Position.Y, geometry.verts[i].Position.Z), transformation.GetTransform())); //vertColl[i] = Vector3.Transform(new Vector3(geometry.verts[i].Position.X, geometry.verts[i].Position.Y, geometry.verts[i].Position.Z), transformation.GetTransform()); } int triCount = 0; TriangleVertexIndices triIdx = new TriangleVertexIndices(0, 0, 0); List<TriangleVertexIndices> triColl = new List<TriangleVertexIndices>(); for (int i = 0; i < geometry.ib.Length; i++) { //int index = geometry.ib[i]; //vertColl.Add(Vector3.Transform(new Vector3(geometry.verts[index].Position.X, geometry.verts[index].Position.Y, geometry.verts[index].Position.Z), transformation.GetTransform())); switch (triCount) { case 0: triIdx.I2 = geometry.ib[i]; break; case 1: triIdx.I1 = geometry.ib[i]; break; case 2: triIdx.I0 = geometry.ib[i]; triCount = -1; triColl.Add(triIdx); triIdx = new TriangleVertexIndices(0, 0, 0); break; } triCount++; } CollisionMesh = new TriangleMesh(); CollisionMesh.CreateMesh(vertColl.ToArray(), triColl.ToArray(), 1500, 0.01f); Collision = new CollisionSkin(null); Collision.AddPrimitive(CollisionMesh, (int)MaterialTable.MaterialID.NotBouncyRough); PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(Collision); }