TriangleIndexVertexArray.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
  3. Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. This file contains a class TriangleIndexVertexArray. I tried using the class with the same name
  20. from the BulletX implementation and found it unusable for the purpose of using triangle meshes
  21. within BulletX as the implementation was painfully incomplete.
  22. The attempt to derive from the original class failed as viable members were hidden.
  23. Fiddling around with BulletX itself was not my intention.
  24. So I copied the class to the BulletX-plugin and modified it.
  25. If you want to fiddle around with it it's up to you to move all this to BulletX.
  26. If someone someday implements the missing functionality in BulletX, feel free to remove this class.
  27. It's just an ugly hack.
  28. */
  29. using System;
  30. using System.Collections.Generic;
  31. using MonoXnaCompactMaths;
  32. using XnaDevRu.BulletX;
  33. namespace OpenSim.Region.Physics.BulletXPlugin
  34. {
  35. /// <summary>
  36. /// IndexedMesh indexes into existing vertex and index arrays, in a similar way OpenGL glDrawElements
  37. /// instead of the number of indices, we pass the number of triangles
  38. /// </summary>
  39. public struct IndexedMesh
  40. {
  41. private int _numTriangles;
  42. private int[] _triangleIndexBase;
  43. private int _triangleIndexStride;
  44. private int _numVertices;
  45. private Vector3[] _vertexBase;
  46. private int _vertexStride;
  47. public IndexedMesh(int numTriangleIndices, int[] triangleIndexBase, int triangleIndexStride, int numVertices,
  48. Vector3[] vertexBase, int vertexStride)
  49. {
  50. _numTriangles = numTriangleIndices;
  51. _triangleIndexBase = triangleIndexBase;
  52. _triangleIndexStride = triangleIndexStride;
  53. _vertexBase = vertexBase;
  54. _numVertices = numVertices;
  55. _vertexStride = vertexStride;
  56. }
  57. public IndexedMesh(int[] triangleIndexBase, Vector3[] vertexBase)
  58. {
  59. _numTriangles = triangleIndexBase.Length;
  60. _triangleIndexBase = triangleIndexBase;
  61. _triangleIndexStride = 32;
  62. _vertexBase = vertexBase;
  63. _numVertices = vertexBase.Length;
  64. _vertexStride = 24;
  65. }
  66. public int TriangleCount
  67. {
  68. get { return _numTriangles; }
  69. set { _numTriangles = value; }
  70. }
  71. public int[] TriangleIndexBase
  72. {
  73. get { return _triangleIndexBase; }
  74. set { _triangleIndexBase = value; }
  75. }
  76. public int TriangleIndexStride
  77. {
  78. get { return _triangleIndexStride; }
  79. set { _triangleIndexStride = value; }
  80. }
  81. public int VertexCount
  82. {
  83. get { return _numVertices; }
  84. set { _numVertices = value; }
  85. }
  86. public Vector3[] VertexBase
  87. {
  88. get { return _vertexBase; }
  89. set { _vertexBase = value; }
  90. }
  91. public int VertexStride
  92. {
  93. get { return _vertexStride; }
  94. set { _vertexStride = value; }
  95. }
  96. }
  97. /// <summary>
  98. /// TriangleIndexVertexArray allows to use multiple meshes, by indexing into existing triangle/index arrays.
  99. /// Additional meshes can be added using addIndexedMesh
  100. /// </summary>
  101. public class TriangleIndexVertexArray : StridingMeshInterface
  102. {
  103. private List<IndexedMesh> _indexedMeshes = new List<IndexedMesh>();
  104. public TriangleIndexVertexArray()
  105. {
  106. }
  107. public TriangleIndexVertexArray(int numTriangleIndices, int[] triangleIndexBase, int triangleIndexStride,
  108. int numVertices, Vector3[] vertexBase, int vertexStride)
  109. {
  110. IndexedMesh mesh = new IndexedMesh();
  111. mesh.TriangleCount = numTriangleIndices;
  112. mesh.TriangleIndexBase = triangleIndexBase;
  113. mesh.TriangleIndexStride = triangleIndexStride;
  114. mesh.VertexBase = vertexBase;
  115. mesh.VertexCount = numVertices;
  116. mesh.VertexStride = vertexStride;
  117. AddIndexedMesh(mesh);
  118. }
  119. public TriangleIndexVertexArray(int[] triangleIndexBase, Vector3[] vertexBase)
  120. : this(triangleIndexBase.Length, triangleIndexBase, 32, vertexBase.Length, vertexBase, 24)
  121. {
  122. }
  123. public void AddIndexedMesh(IndexedMesh indexedMesh)
  124. {
  125. _indexedMeshes.Add(indexedMesh);
  126. }
  127. public override void GetLockedVertexIndexBase(out List<Vector3> verts, out List<int> indicies, out int numfaces,
  128. int subpart)
  129. {
  130. throw new Exception("The method or operation is not implemented.");
  131. }
  132. public override void GetLockedReadOnlyVertexIndexBase(out List<Vector3> verts, out List<int> indicies,
  133. out int numfaces, int subpart)
  134. {
  135. IndexedMesh m = _indexedMeshes[0];
  136. Vector3[] vertexBase = m.VertexBase;
  137. verts = new List<Vector3>();
  138. foreach (Vector3 v in vertexBase)
  139. {
  140. verts.Add(v);
  141. }
  142. int[] indexBase = m.TriangleIndexBase;
  143. indicies = new List<int>();
  144. foreach (int i in indexBase)
  145. {
  146. indicies.Add(i);
  147. }
  148. numfaces = vertexBase.GetLength(0);
  149. }
  150. public override void UnLockVertexBase(int subpart)
  151. {
  152. throw new Exception("The method or operation is not implemented.");
  153. }
  154. public override void UnLockReadOnlyVertexBase(int subpart)
  155. {
  156. }
  157. public override int SubPartsCount()
  158. {
  159. return _indexedMeshes.Count;
  160. }
  161. public override void PreallocateVertices(int numverts)
  162. {
  163. throw new Exception("The method or operation is not implemented.");
  164. }
  165. public override void PreallocateIndices(int numindices)
  166. {
  167. throw new Exception("The method or operation is not implemented.");
  168. }
  169. }
  170. }