aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/StridingMeshInterface.cs
blob: 20baccb5d522606855c49a40c0ff2cd2a22b4c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
  Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
  Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

using System;
using System.Collections.Generic;
using System.Text;
using MonoXnaCompactMaths;

namespace XnaDevRu.BulletX
{
	/// <summary>
	/// PHY_ScalarType enumerates possible scalar types.
	/// See the StridingMeshInterface for its use
	/// </summary>
	public enum PHY_ScalarType
	{
		PHY_FLOAT,
		PHY_DOUBLE,
		PHY_INTEGER,
		PHY_SHORT,
		PHY_FIXEDPOINT88
	}

	/// <summary>
	/// StridingMeshInterface is the interface class for high performance access to triangle meshes
	/// It allows for sharing graphics and collision meshes. Also it provides locking/unlocking of graphics meshes that are in gpu memory.
	/// </summary>
	public abstract class StridingMeshInterface
	{
		protected Vector3 _scaling;

		public StridingMeshInterface()
		{
			_scaling = new Vector3(1f,1f,1f);
		}

		public void InternalProcessAllTriangles(ITriangleIndexCallback callback, Vector3 aabbMin, Vector3 aabbMax)
		{
			int numtotalphysicsverts = 0;
            int numtriangles, gfxindex;
			int part, graphicssubparts = SubPartsCount();
            Vector3[] triangle = new Vector3[3];
            List<Vector3> verts;
            List<int> indicies;

			Vector3 meshScaling = Scaling;

			//if the number of parts is big, the performance might drop due to the innerloop switch on indextype
			for (part = 0; part < graphicssubparts; part++)
			{
                GetLockedReadOnlyVertexIndexBase(out verts, out indicies, out numtriangles, part);
			    numtotalphysicsverts += numtriangles * 3; //upper bound

                for (gfxindex = 0; gfxindex < numtriangles; gfxindex++)
                {
                    triangle[0] = verts[indicies[gfxindex * 3 + 0]];
                    triangle[1] = verts[indicies[gfxindex * 3 + 1]];
                    triangle[2] = verts[indicies[gfxindex * 3 + 2]];

                    callback.ProcessTriangleIndex(triangle, part, gfxindex);
                }

			    UnLockReadOnlyVertexBase(part);
			}
		}


		// get read and write access to a subpart of a triangle mesh
		// this subpart has a continuous array of vertices and indices
		// in this way the mesh can be handled as chunks of memory with striding
		// very similar to OpenGL vertexarray support
		// make a call to unLockVertexBase when the read and write access is finished	
		public abstract void GetLockedVertexIndexBase(out List<Vector3> verts, out List<int> indicies, out int numfaces, int subpart);

        public abstract void GetLockedReadOnlyVertexIndexBase(out List<Vector3> verts, out List<int> indicies, out int numfaces, int subpart);
	
		// unLockVertexBase finishes the access to a subpart of the triangle mesh
		// make a call to unLockVertexBase when the read and write access (using getLockedVertexIndexBase) is finished
		public abstract void UnLockVertexBase(int subpart);

		public abstract void UnLockReadOnlyVertexBase(int subpart);


		// getNumSubParts returns the number of seperate subparts
		// each subpart has a continuous array of vertices and indices
        public abstract int SubPartsCount();

		public abstract void PreallocateVertices(int numverts);
		public abstract void PreallocateIndices(int numindices);

		public Vector3 Scaling
        {           
			get { return _scaling; }
            set { _scaling = value; }
		}
	}
}