PageRenderTime 123ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 73ms app.codeStats 1ms

/PhysicsEngines/JigLib/Helpers.cs

#
C# | 130 lines | 91 code | 15 blank | 24 comment | 6 complexity | 88db17e4d291adf95033a5d4666383b5 MD5 | raw file
  1using System.Collections.Generic;
  2using Delta.Rendering.Models;
  3using Delta.Utilities.Datatypes;
  4using JigLibX.Geometry;
  5using Microsoft.Xna.Framework;
  6using Color = Delta.Utilities.Datatypes.Color;
  7using Line = Delta.Rendering.Basics.Drawing.Line;
  8using Matrix = Delta.Utilities.Datatypes.Matrix;
  9
 10namespace Delta.PhysicsEngines.JigLib
 11{
 12	/// <summary>
 13	/// Helper class using for comunicate between DeltaEngine and JigLibX.
 14	/// </summary>
 15	public static class Helpers
 16	{
 17		#region CreateFrom (Static)
 18		/// <summary>
 19		/// Creates from.
 20		/// </summary>
 21		/// <param name="mesh">The mesh.</param>
 22		/// <param name="localSpaceMatrix">The local space matrix.</param>
 23		/// <param name="invertTriangles">if set to <c>true</c> [invert triangles].</param>
 24		/// <returns>
 25		/// New created JigLib TriangleMesh shape created from Delta engine mesh.
 26		/// </returns>
 27		public static TriangleMesh CreateFrom(Mesh mesh,
 28			Matrix localSpaceMatrix, bool invertTriangles)
 29		{
 30			// extract the data from the physics mesh
 31			List<Vector3> triangleVertex =
 32				new List<Vector3>();
 33			List<TriangleVertexIndices> triangleVertexIndices =
 34				new List<TriangleVertexIndices>();
 35
 36			// apply the local transform of the mesh so we get correct coordinates
 37			Matrix TransMat = localSpaceMatrix;
 38
 39			Vector transformed;
 40			Vector3 xnaVector;
 41			for (int num = 0; num < mesh.Geometry.Data.NumberOfUsedVertices; num++)
 42			{
 43				transformed = Vector.Transform(
 44					mesh.Geometry.Data.GetPosition(num), TransMat);
 45				JigLibDatatypesMapping.Convert(ref transformed, out xnaVector);
 46				triangleVertex.Add(xnaVector);
 47			}
 48
 49			// set inverted tris if needed
 50			ushort[] Indices = mesh.Geometry.Data.Indices;
 51			if (invertTriangles)
 52			{
 53				for (int i = 0; i < Indices.Length; i = i + 3)
 54				{
 55					triangleVertexIndices.Add(new TriangleVertexIndices(
 56						Indices[i + 2],
 57						Indices[i + 1],
 58						Indices[i]));
 59				}
 60			} // if
 61			else
 62			{
 63				for (int i = 0; i < Indices.Length; i = i + 3)
 64				{
 65					triangleVertexIndices.Add(new TriangleVertexIndices(
 66						Indices[i],
 67						Indices[i + 1],
 68						Indices[i + 2]));
 69				}
 70			} // else
 71
 72			// create the triangle mesh of the level and add it to the physics world
 73			TriangleMesh triangleMesh = new TriangleMesh();
 74			triangleMesh.CreateMesh(triangleVertex, triangleVertexIndices, 1, 1.0f);
 75
 76			return triangleMesh;
 77		}
 78		#endregion
 79
 80		#region DrawDebug (Static)
 81		/// <summary>
 82		/// Draws the debug.
 83		/// </summary>
 84		/// <param name="primitive">The primitive.</param>
 85		public static void DrawDebug(Primitive primitive)
 86		{
 87			if (primitive is TriangleMesh)
 88			{
 89				DrawTriangleMesh(primitive as TriangleMesh);
 90			}
 91		}
 92		#endregion
 93
 94		#region Methods (Private)
 95
 96		#region DrawTriangleMesh
 97		/// <summary>
 98		/// Performs debug drawing of JigLibX triangle mesh.
 99		/// </summary>
100		/// <param name="triangleMesh">The triangle mesh to draw.</param>
101		private static void DrawTriangleMesh(TriangleMesh triangleMesh)
102		{
103			Vector3 tempVector;
104			Vector v0;
105			Vector v1;
106			Vector v2;
107
108			for (int i = 0; i < triangleMesh.GetNumTriangles(); ++i)
109			{
110				IndexedTriangle tri = triangleMesh.GetTriangle(i);
111
112				tempVector = triangleMesh.GetVertex(tri.GetVertexIndex(0));
113				JigLibDatatypesMapping.Convert(ref tempVector, out v0);
114
115				tempVector = triangleMesh.GetVertex(tri.GetVertexIndex(1));
116				JigLibDatatypesMapping.Convert(ref tempVector, out v1);
117
118				tempVector = triangleMesh.GetVertex(tri.GetVertexIndex(2));
119				JigLibDatatypesMapping.Convert(ref tempVector, out v2);
120
121				Line.Draw(v0, v1, Color.Black);
122				Line.Draw(v1, v2, Color.Black);
123				Line.Draw(v2, v0, Color.Black);
124			}
125		}
126		#endregion
127
128		#endregion
129	}
130}