diff options
Diffstat (limited to 'OpenSim/Region/Physics/OdePlugin/Meshing/Meshmerizer.cs')
-rw-r--r-- | OpenSim/Region/Physics/OdePlugin/Meshing/Meshmerizer.cs | 1120 |
1 files changed, 560 insertions, 560 deletions
diff --git a/OpenSim/Region/Physics/OdePlugin/Meshing/Meshmerizer.cs b/OpenSim/Region/Physics/OdePlugin/Meshing/Meshmerizer.cs index 28dca41..ce3ba5c 100644 --- a/OpenSim/Region/Physics/OdePlugin/Meshing/Meshmerizer.cs +++ b/OpenSim/Region/Physics/OdePlugin/Meshing/Meshmerizer.cs | |||
@@ -1,560 +1,560 @@ | |||
1 | using System; | 1 | using System; |
2 | using System.Globalization; | 2 | using System.Globalization; |
3 | using System.Diagnostics; | 3 | using System.Diagnostics; |
4 | using System.Collections.Generic; | 4 | using System.Collections.Generic; |
5 | using System.Text; | 5 | using System.Text; |
6 | using System.Runtime.InteropServices; | 6 | using System.Runtime.InteropServices; |
7 | 7 | ||
8 | using OpenSim.Framework.Types; | 8 | using OpenSim.Framework.Types; |
9 | using OpenSim.Region.Physics.Manager; | 9 | using OpenSim.Region.Physics.Manager; |
10 | 10 | ||
11 | namespace OpenSim.Region.Physics.OdePlugin | 11 | namespace OpenSim.Region.Physics.OdePlugin |
12 | { | 12 | { |
13 | public class Mesh | 13 | public class Mesh |
14 | { | 14 | { |
15 | public List<Vertex> vertices; | 15 | public List<Vertex> vertices; |
16 | public List<Triangle> triangles; | 16 | public List<Triangle> triangles; |
17 | 17 | ||
18 | public float[] normals; | 18 | public float[] normals; |
19 | 19 | ||
20 | public Mesh() | 20 | public Mesh() |
21 | { | 21 | { |
22 | vertices = new List<Vertex>(); | 22 | vertices = new List<Vertex>(); |
23 | triangles = new List<Triangle>(); | 23 | triangles = new List<Triangle>(); |
24 | } | 24 | } |
25 | 25 | ||
26 | public void Add(Triangle triangle) | 26 | public void Add(Triangle triangle) |
27 | { | 27 | { |
28 | int i; | 28 | int i; |
29 | i = vertices.IndexOf(triangle.v1); | 29 | i = vertices.IndexOf(triangle.v1); |
30 | if (i < 0) | 30 | if (i < 0) |
31 | throw new ArgumentException("Vertex v1 not known to mesh"); | 31 | throw new ArgumentException("Vertex v1 not known to mesh"); |
32 | i = vertices.IndexOf(triangle.v2); | 32 | i = vertices.IndexOf(triangle.v2); |
33 | if (i < 0) | 33 | if (i < 0) |
34 | throw new ArgumentException("Vertex v2 not known to mesh"); | 34 | throw new ArgumentException("Vertex v2 not known to mesh"); |
35 | i = vertices.IndexOf(triangle.v3); | 35 | i = vertices.IndexOf(triangle.v3); |
36 | if (i < 0) | 36 | if (i < 0) |
37 | throw new ArgumentException("Vertex v3 not known to mesh"); | 37 | throw new ArgumentException("Vertex v3 not known to mesh"); |
38 | 38 | ||
39 | triangles.Add(triangle); | 39 | triangles.Add(triangle); |
40 | } | 40 | } |
41 | 41 | ||
42 | public void Add(Vertex v) | 42 | public void Add(Vertex v) |
43 | { | 43 | { |
44 | vertices.Add(v); | 44 | vertices.Add(v); |
45 | } | 45 | } |
46 | 46 | ||
47 | 47 | ||
48 | public float[] getVertexListAsFloat() | 48 | public float[] getVertexListAsFloat() |
49 | { | 49 | { |
50 | float[] result = new float[vertices.Count * 3]; | 50 | float[] result = new float[vertices.Count * 3]; |
51 | for (int i = 0; i < vertices.Count; i++) | 51 | for (int i = 0; i < vertices.Count; i++) |
52 | { | 52 | { |
53 | Vertex v = vertices[i]; | 53 | Vertex v = vertices[i]; |
54 | PhysicsVector point = v.point; | 54 | PhysicsVector point = v.point; |
55 | result[3 * i + 0] = point.X; | 55 | result[3 * i + 0] = point.X; |
56 | result[3 * i + 1] = point.Y; | 56 | result[3 * i + 1] = point.Y; |
57 | result[3 * i + 2] = point.Z; | 57 | result[3 * i + 2] = point.Z; |
58 | } | 58 | } |
59 | GCHandle.Alloc(result, GCHandleType.Pinned); | 59 | GCHandle.Alloc(result, GCHandleType.Pinned); |
60 | return result; | 60 | return result; |
61 | } | 61 | } |
62 | 62 | ||
63 | public int[] getIndexListAsInt() | 63 | public int[] getIndexListAsInt() |
64 | { | 64 | { |
65 | int[] result = new int[triangles.Count * 3]; | 65 | int[] result = new int[triangles.Count * 3]; |
66 | for (int i = 0; i < triangles.Count; i++) | 66 | for (int i = 0; i < triangles.Count; i++) |
67 | { | 67 | { |
68 | Triangle t = triangles[i]; | 68 | Triangle t = triangles[i]; |
69 | result[3 * i + 0] = vertices.IndexOf(t.v1); | 69 | result[3 * i + 0] = vertices.IndexOf(t.v1); |
70 | result[3 * i + 1] = vertices.IndexOf(t.v2); | 70 | result[3 * i + 1] = vertices.IndexOf(t.v2); |
71 | result[3 * i + 2] = vertices.IndexOf(t.v3); | 71 | result[3 * i + 2] = vertices.IndexOf(t.v3); |
72 | } | 72 | } |
73 | GCHandle.Alloc(result, GCHandleType.Pinned); | 73 | GCHandle.Alloc(result, GCHandleType.Pinned); |
74 | return result; | 74 | return result; |
75 | } | 75 | } |
76 | 76 | ||
77 | 77 | ||
78 | public void Append(Mesh newMesh) | 78 | public void Append(Mesh newMesh) |
79 | { | 79 | { |
80 | foreach (Vertex v in newMesh.vertices) | 80 | foreach (Vertex v in newMesh.vertices) |
81 | vertices.Add(v); | 81 | vertices.Add(v); |
82 | 82 | ||
83 | foreach (Triangle t in newMesh.triangles) | 83 | foreach (Triangle t in newMesh.triangles) |
84 | Add(t); | 84 | Add(t); |
85 | 85 | ||
86 | } | 86 | } |
87 | } | 87 | } |
88 | 88 | ||
89 | 89 | ||
90 | 90 | ||
91 | public class Meshmerizer | 91 | public class Meshmerizer |
92 | { | 92 | { |
93 | 93 | ||
94 | static List<Triangle> FindInfluencedTriangles(List<Triangle> triangles, Vertex v) | 94 | static List<Triangle> FindInfluencedTriangles(List<Triangle> triangles, Vertex v) |
95 | { | 95 | { |
96 | List<Triangle> influenced = new List<Triangle>(); | 96 | List<Triangle> influenced = new List<Triangle>(); |
97 | foreach (Triangle t in triangles) | 97 | foreach (Triangle t in triangles) |
98 | { | 98 | { |
99 | float dx, dy; | 99 | float dx, dy; |
100 | 100 | ||
101 | if (t.isInCircle(v.point.X, v.point.Y)) | 101 | if (t.isInCircle(v.point.X, v.point.Y)) |
102 | { | 102 | { |
103 | influenced.Add(t); | 103 | influenced.Add(t); |
104 | } | 104 | } |
105 | } | 105 | } |
106 | return influenced; | 106 | return influenced; |
107 | } | 107 | } |
108 | 108 | ||
109 | 109 | ||
110 | static void InsertVertices(List<Vertex> vertices, int usedForSeed, List<Triangle> triangles, List<int> innerBorders) | 110 | static void InsertVertices(List<Vertex> vertices, int usedForSeed, List<Triangle> triangles, List<int> innerBorders) |
111 | { | 111 | { |
112 | // This is a variant of the delaunay algorithm | 112 | // This is a variant of the delaunay algorithm |
113 | // each time a new vertex is inserted, all triangles that are influenced by it are deleted | 113 | // each time a new vertex is inserted, all triangles that are influenced by it are deleted |
114 | // and replaced by new ones including the new vertex | 114 | // and replaced by new ones including the new vertex |
115 | // It is not very time efficient but easy to implement. | 115 | // It is not very time efficient but easy to implement. |
116 | 116 | ||
117 | int iCurrentVertex; | 117 | int iCurrentVertex; |
118 | int iMaxVertex=vertices.Count; | 118 | int iMaxVertex=vertices.Count; |
119 | for (iCurrentVertex = usedForSeed; iCurrentVertex < iMaxVertex; iCurrentVertex++) | 119 | for (iCurrentVertex = usedForSeed; iCurrentVertex < iMaxVertex; iCurrentVertex++) |
120 | { | 120 | { |
121 | // Background: A triangle mesh fulfills the delaunay condition if (iff!) | 121 | // Background: A triangle mesh fulfills the delaunay condition if (iff!) |
122 | // each circumlocutory circle (i.e. the circle that touches all three corners) | 122 | // each circumlocutory circle (i.e. the circle that touches all three corners) |
123 | // of each triangle is empty of other vertices. | 123 | // of each triangle is empty of other vertices. |
124 | // Obviously a single (seeding) triangle fulfills this condition. | 124 | // Obviously a single (seeding) triangle fulfills this condition. |
125 | // If we now add one vertex, we need to reconstruct all triangles, that | 125 | // If we now add one vertex, we need to reconstruct all triangles, that |
126 | // do not fulfill this condition with respect to the new triangle | 126 | // do not fulfill this condition with respect to the new triangle |
127 | 127 | ||
128 | // Find the triangles that are influenced by the new vertex | 128 | // Find the triangles that are influenced by the new vertex |
129 | Vertex v=vertices[iCurrentVertex]; | 129 | Vertex v=vertices[iCurrentVertex]; |
130 | List<Triangle> influencedTriangles=FindInfluencedTriangles(triangles, v); | 130 | List<Triangle> influencedTriangles=FindInfluencedTriangles(triangles, v); |
131 | 131 | ||
132 | List<Simplex> simplices = new List<Simplex>(); | 132 | List<Simplex> simplices = new List<Simplex>(); |
133 | 133 | ||
134 | // Reconstruction phase. First step, dissolve each triangle into it's simplices, | 134 | // Reconstruction phase. First step, dissolve each triangle into it's simplices, |
135 | // i.e. it's "border lines" | 135 | // i.e. it's "border lines" |
136 | // Goal is to find "inner" borders and delete them, while the hull gets conserved. | 136 | // Goal is to find "inner" borders and delete them, while the hull gets conserved. |
137 | // Inner borders are special in the way that they always come twice, which is how we detect them | 137 | // Inner borders are special in the way that they always come twice, which is how we detect them |
138 | foreach (Triangle t in influencedTriangles) | 138 | foreach (Triangle t in influencedTriangles) |
139 | { | 139 | { |
140 | List<Simplex> newSimplices = t.GetSimplices(); | 140 | List<Simplex> newSimplices = t.GetSimplices(); |
141 | simplices.AddRange(newSimplices); | 141 | simplices.AddRange(newSimplices); |
142 | triangles.Remove(t); | 142 | triangles.Remove(t); |
143 | } | 143 | } |
144 | // Now sort the simplices. That will make identical ones side by side in the list | 144 | // Now sort the simplices. That will make identical ones side by side in the list |
145 | simplices.Sort(); | 145 | simplices.Sort(); |
146 | 146 | ||
147 | // Look for duplicate simplices here. | 147 | // Look for duplicate simplices here. |
148 | // Remember, they are directly side by side in the list right now | 148 | // Remember, they are directly side by side in the list right now |
149 | int iSimplex; | 149 | int iSimplex; |
150 | List<Simplex> innerSimplices=new List<Simplex>(); | 150 | List<Simplex> innerSimplices=new List<Simplex>(); |
151 | for (iSimplex = 1; iSimplex < simplices.Count; iSimplex++) // Startindex=1, so we can refer backwards | 151 | for (iSimplex = 1; iSimplex < simplices.Count; iSimplex++) // Startindex=1, so we can refer backwards |
152 | { | 152 | { |
153 | if (simplices[iSimplex - 1].CompareTo(simplices[iSimplex])==0) | 153 | if (simplices[iSimplex - 1].CompareTo(simplices[iSimplex])==0) |
154 | { | 154 | { |
155 | innerSimplices.Add(simplices[iSimplex - 1]); | 155 | innerSimplices.Add(simplices[iSimplex - 1]); |
156 | innerSimplices.Add(simplices[iSimplex]); | 156 | innerSimplices.Add(simplices[iSimplex]); |
157 | } | 157 | } |
158 | } | 158 | } |
159 | 159 | ||
160 | foreach (Simplex s in innerSimplices) | 160 | foreach (Simplex s in innerSimplices) |
161 | { | 161 | { |
162 | simplices.Remove(s); | 162 | simplices.Remove(s); |
163 | } | 163 | } |
164 | 164 | ||
165 | // each simplex still in the list belongs to the hull of the region in question | 165 | // each simplex still in the list belongs to the hull of the region in question |
166 | // The new vertex (yes, we still deal with verices here :-) ) forms a triangle | 166 | // The new vertex (yes, we still deal with verices here :-) ) forms a triangle |
167 | // With each of these simplices. Build the new triangles and add them to the list | 167 | // With each of these simplices. Build the new triangles and add them to the list |
168 | foreach (Simplex s in simplices) | 168 | foreach (Simplex s in simplices) |
169 | { | 169 | { |
170 | Triangle t = new Triangle(s.v1, s.v2, vertices[iCurrentVertex]); | 170 | Triangle t = new Triangle(s.v1, s.v2, vertices[iCurrentVertex]); |
171 | triangles.Add(t); | 171 | triangles.Add(t); |
172 | } | 172 | } |
173 | } | 173 | } |
174 | 174 | ||
175 | // At this point all vertices should be inserted into the mesh | 175 | // At this point all vertices should be inserted into the mesh |
176 | // But the areas, that should be kept free still are filled with triangles | 176 | // But the areas, that should be kept free still are filled with triangles |
177 | // We have to remove them. For this we have a list of indices to vertices. | 177 | // We have to remove them. For this we have a list of indices to vertices. |
178 | // Each triangle that solemnly constists of vertices from the inner border | 178 | // Each triangle that solemnly constists of vertices from the inner border |
179 | // are deleted | 179 | // are deleted |
180 | 180 | ||
181 | List<Triangle> innerTriangles = new List<Triangle>(); | 181 | List<Triangle> innerTriangles = new List<Triangle>(); |
182 | foreach (Triangle t in triangles) | 182 | foreach (Triangle t in triangles) |
183 | { | 183 | { |
184 | if ( | 184 | if ( |
185 | innerBorders.Contains(vertices.IndexOf(t.v1)) | 185 | innerBorders.Contains(vertices.IndexOf(t.v1)) |
186 | && innerBorders.Contains(vertices.IndexOf(t.v2)) | 186 | && innerBorders.Contains(vertices.IndexOf(t.v2)) |
187 | && innerBorders.Contains(vertices.IndexOf(t.v3)) | 187 | && innerBorders.Contains(vertices.IndexOf(t.v3)) |
188 | ) | 188 | ) |
189 | innerTriangles.Add(t); | 189 | innerTriangles.Add(t); |
190 | } | 190 | } |
191 | foreach (Triangle t in innerTriangles) | 191 | foreach (Triangle t in innerTriangles) |
192 | { | 192 | { |
193 | triangles.Remove(t); | 193 | triangles.Remove(t); |
194 | } | 194 | } |
195 | } | 195 | } |
196 | 196 | ||
197 | 197 | ||
198 | static Mesh CreateBoxMeshX(PrimitiveBaseShape primShape, PhysicsVector size) | 198 | static Mesh CreateBoxMeshX(PrimitiveBaseShape primShape, PhysicsVector size) |
199 | // Builds the x (+ and -) surfaces of a box shaped prim | 199 | // Builds the x (+ and -) surfaces of a box shaped prim |
200 | { | 200 | { |
201 | UInt16 hollowFactor = primShape.ProfileHollow; | 201 | UInt16 hollowFactor = primShape.ProfileHollow; |
202 | Mesh meshMX = new Mesh(); | 202 | Mesh meshMX = new Mesh(); |
203 | 203 | ||
204 | 204 | ||
205 | // Surface 0, -X | 205 | // Surface 0, -X |
206 | meshMX.Add(new Vertex("-X-Y-Z", -size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); | 206 | meshMX.Add(new Vertex("-X-Y-Z", -size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); |
207 | meshMX.Add(new Vertex("-X+Y-Z", -size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); | 207 | meshMX.Add(new Vertex("-X+Y-Z", -size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); |
208 | meshMX.Add(new Vertex("-X-Y+Z", -size.X / 2.0f, -size.Y / 2.0f, +size.Z / 2.0f)); | 208 | meshMX.Add(new Vertex("-X-Y+Z", -size.X / 2.0f, -size.Y / 2.0f, +size.Z / 2.0f)); |
209 | meshMX.Add(new Vertex("-X+Y+Z", -size.X / 2.0f, +size.Y / 2.0f, +size.Z / 2.0f)); | 209 | meshMX.Add(new Vertex("-X+Y+Z", -size.X / 2.0f, +size.Y / 2.0f, +size.Z / 2.0f)); |
210 | 210 | ||
211 | meshMX.Add(new Triangle(meshMX.vertices[0], meshMX.vertices[2], meshMX.vertices[1])); | 211 | meshMX.Add(new Triangle(meshMX.vertices[0], meshMX.vertices[2], meshMX.vertices[1])); |
212 | meshMX.Add(new Triangle(meshMX.vertices[1], meshMX.vertices[2], meshMX.vertices[3])); | 212 | meshMX.Add(new Triangle(meshMX.vertices[1], meshMX.vertices[2], meshMX.vertices[3])); |
213 | 213 | ||
214 | 214 | ||
215 | Mesh meshPX = new Mesh(); | 215 | Mesh meshPX = new Mesh(); |
216 | // Surface 1, +X | 216 | // Surface 1, +X |
217 | meshPX.Add(new Vertex("+X-Y-Z", +size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); | 217 | meshPX.Add(new Vertex("+X-Y-Z", +size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); |
218 | meshPX.Add(new Vertex("+X+Y-Z", +size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); | 218 | meshPX.Add(new Vertex("+X+Y-Z", +size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); |
219 | meshPX.Add(new Vertex("+X-Y+Z", +size.X / 2.0f, -size.Y / 2.0f, +size.Z / 2.0f)); | 219 | meshPX.Add(new Vertex("+X-Y+Z", +size.X / 2.0f, -size.Y / 2.0f, +size.Z / 2.0f)); |
220 | meshPX.Add(new Vertex("+X+Y+Z", +size.X / 2.0f, +size.Y / 2.0f, +size.Z / 2.0f)); | 220 | meshPX.Add(new Vertex("+X+Y+Z", +size.X / 2.0f, +size.Y / 2.0f, +size.Z / 2.0f)); |
221 | 221 | ||
222 | 222 | ||
223 | meshPX.Add(new Triangle(meshPX.vertices[0], meshPX.vertices[1], meshPX.vertices[2])); | 223 | meshPX.Add(new Triangle(meshPX.vertices[0], meshPX.vertices[1], meshPX.vertices[2])); |
224 | meshPX.Add(new Triangle(meshPX.vertices[2], meshPX.vertices[1], meshPX.vertices[3])); | 224 | meshPX.Add(new Triangle(meshPX.vertices[2], meshPX.vertices[1], meshPX.vertices[3])); |
225 | 225 | ||
226 | 226 | ||
227 | if (hollowFactor > 0) | 227 | if (hollowFactor > 0) |
228 | { | 228 | { |
229 | float hollowFactorF = (float)hollowFactor / (float)50000; | 229 | float hollowFactorF = (float)hollowFactor / (float)50000; |
230 | 230 | ||
231 | Vertex IPP; | 231 | Vertex IPP; |
232 | Vertex IPM; | 232 | Vertex IPM; |
233 | Vertex IMP; | 233 | Vertex IMP; |
234 | Vertex IMM; | 234 | Vertex IMM; |
235 | 235 | ||
236 | IPP = new Vertex("Inner-X+Y+Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); | 236 | IPP = new Vertex("Inner-X+Y+Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); |
237 | IPM = new Vertex("Inner-X+Y-Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); | 237 | IPM = new Vertex("Inner-X+Y-Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); |
238 | IMP = new Vertex("Inner-X-Y+Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); | 238 | IMP = new Vertex("Inner-X-Y+Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); |
239 | IMM = new Vertex("Inner-X-Y-Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); | 239 | IMM = new Vertex("Inner-X-Y-Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); |
240 | 240 | ||
241 | meshMX.Add(IPP); | 241 | meshMX.Add(IPP); |
242 | meshMX.Add(IPM); | 242 | meshMX.Add(IPM); |
243 | meshMX.Add(IMP); | 243 | meshMX.Add(IMP); |
244 | meshMX.Add(IMM); | 244 | meshMX.Add(IMM); |
245 | 245 | ||
246 | meshMX.Add(new Triangle(IPP, IMP, IPM)); | 246 | meshMX.Add(new Triangle(IPP, IMP, IPM)); |
247 | meshMX.Add(new Triangle(IPM, IMP, IMM)); | 247 | meshMX.Add(new Triangle(IPM, IMP, IMM)); |
248 | 248 | ||
249 | foreach (Triangle t in meshMX.triangles) | 249 | foreach (Triangle t in meshMX.triangles) |
250 | { | 250 | { |
251 | PhysicsVector n = t.getNormal(); | 251 | PhysicsVector n = t.getNormal(); |
252 | } | 252 | } |
253 | 253 | ||
254 | 254 | ||
255 | 255 | ||
256 | IPP = new Vertex("Inner+X+Y+Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); | 256 | IPP = new Vertex("Inner+X+Y+Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); |
257 | IPM = new Vertex("Inner+X+Y-Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); | 257 | IPM = new Vertex("Inner+X+Y-Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); |
258 | IMP = new Vertex("Inner+X-Y+Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); | 258 | IMP = new Vertex("Inner+X-Y+Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); |
259 | IMM = new Vertex("Inner+X-Y-Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); | 259 | IMM = new Vertex("Inner+X-Y-Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); |
260 | 260 | ||
261 | meshPX.Add(IPP); | 261 | meshPX.Add(IPP); |
262 | meshPX.Add(IPM); | 262 | meshPX.Add(IPM); |
263 | meshPX.Add(IMP); | 263 | meshPX.Add(IMP); |
264 | meshPX.Add(IMM); | 264 | meshPX.Add(IMM); |
265 | 265 | ||
266 | meshPX.Add(new Triangle(IPP, IPM, IMP)); | 266 | meshPX.Add(new Triangle(IPP, IPM, IMP)); |
267 | meshPX.Add(new Triangle(IMP, IPM, IMM)); | 267 | meshPX.Add(new Triangle(IMP, IPM, IMM)); |
268 | 268 | ||
269 | foreach (Triangle t in meshPX.triangles) | 269 | foreach (Triangle t in meshPX.triangles) |
270 | { | 270 | { |
271 | PhysicsVector n = t.getNormal(); | 271 | PhysicsVector n = t.getNormal(); |
272 | } | 272 | } |
273 | } | 273 | } |
274 | 274 | ||
275 | Mesh result = new Mesh(); | 275 | Mesh result = new Mesh(); |
276 | result.Append(meshMX); | 276 | result.Append(meshMX); |
277 | result.Append(meshPX); | 277 | result.Append(meshPX); |
278 | 278 | ||
279 | return result; | 279 | return result; |
280 | } | 280 | } |
281 | 281 | ||
282 | 282 | ||
283 | 283 | ||
284 | static Mesh CreateBoxMeshY(PrimitiveBaseShape primShape, PhysicsVector size) | 284 | static Mesh CreateBoxMeshY(PrimitiveBaseShape primShape, PhysicsVector size) |
285 | // Builds the y (+ and -) surfaces of a box shaped prim | 285 | // Builds the y (+ and -) surfaces of a box shaped prim |
286 | { | 286 | { |
287 | UInt16 hollowFactor = primShape.ProfileHollow; | 287 | UInt16 hollowFactor = primShape.ProfileHollow; |
288 | 288 | ||
289 | // (M)inus Y | 289 | // (M)inus Y |
290 | Mesh MeshMY = new Mesh(); | 290 | Mesh MeshMY = new Mesh(); |
291 | MeshMY.Add(new Vertex("-X-Y-Z", -size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); | 291 | MeshMY.Add(new Vertex("-X-Y-Z", -size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); |
292 | MeshMY.Add(new Vertex("+X-Y-Z", +size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); | 292 | MeshMY.Add(new Vertex("+X-Y-Z", +size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); |
293 | MeshMY.Add(new Vertex("-X-Y+Z", -size.X / 2.0f, -size.Y / 2.0f, +size.Z / 2.0f)); | 293 | MeshMY.Add(new Vertex("-X-Y+Z", -size.X / 2.0f, -size.Y / 2.0f, +size.Z / 2.0f)); |
294 | MeshMY.Add(new Vertex("+X-Y+Z", +size.X / 2.0f, -size.Y / 2.0f, +size.Z / 2.0f)); | 294 | MeshMY.Add(new Vertex("+X-Y+Z", +size.X / 2.0f, -size.Y / 2.0f, +size.Z / 2.0f)); |
295 | 295 | ||
296 | MeshMY.Add(new Triangle(MeshMY.vertices[0], MeshMY.vertices[1], MeshMY.vertices[2])); | 296 | MeshMY.Add(new Triangle(MeshMY.vertices[0], MeshMY.vertices[1], MeshMY.vertices[2])); |
297 | MeshMY.Add(new Triangle(MeshMY.vertices[2], MeshMY.vertices[1], MeshMY.vertices[3])); | 297 | MeshMY.Add(new Triangle(MeshMY.vertices[2], MeshMY.vertices[1], MeshMY.vertices[3])); |
298 | 298 | ||
299 | // (P)lus Y | 299 | // (P)lus Y |
300 | Mesh MeshPY = new Mesh(); | 300 | Mesh MeshPY = new Mesh(); |
301 | 301 | ||
302 | MeshPY.Add(new Vertex("-X+Y-Z", -size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); | 302 | MeshPY.Add(new Vertex("-X+Y-Z", -size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); |
303 | MeshPY.Add(new Vertex("+X+Y-Z", +size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); | 303 | MeshPY.Add(new Vertex("+X+Y-Z", +size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); |
304 | MeshPY.Add(new Vertex("-X+Y+Z", -size.X / 2.0f, +size.Y / 2.0f, +size.Z / 2.0f)); | 304 | MeshPY.Add(new Vertex("-X+Y+Z", -size.X / 2.0f, +size.Y / 2.0f, +size.Z / 2.0f)); |
305 | MeshPY.Add(new Vertex("+X+Y+Z", +size.X / 2.0f, +size.Y / 2.0f, +size.Z / 2.0f)); | 305 | MeshPY.Add(new Vertex("+X+Y+Z", +size.X / 2.0f, +size.Y / 2.0f, +size.Z / 2.0f)); |
306 | 306 | ||
307 | MeshPY.Add(new Triangle(MeshPY.vertices[1], MeshPY.vertices[0], MeshPY.vertices[2])); | 307 | MeshPY.Add(new Triangle(MeshPY.vertices[1], MeshPY.vertices[0], MeshPY.vertices[2])); |
308 | MeshPY.Add(new Triangle(MeshPY.vertices[1], MeshPY.vertices[2], MeshPY.vertices[3])); | 308 | MeshPY.Add(new Triangle(MeshPY.vertices[1], MeshPY.vertices[2], MeshPY.vertices[3])); |
309 | 309 | ||
310 | if (hollowFactor > 0) | 310 | if (hollowFactor > 0) |
311 | { | 311 | { |
312 | float hollowFactorF = (float)hollowFactor / (float)50000; | 312 | float hollowFactorF = (float)hollowFactor / (float)50000; |
313 | 313 | ||
314 | Vertex IPP; | 314 | Vertex IPP; |
315 | Vertex IPM; | 315 | Vertex IPM; |
316 | Vertex IMP; | 316 | Vertex IMP; |
317 | Vertex IMM; | 317 | Vertex IMM; |
318 | 318 | ||
319 | IPP = new Vertex("Inner+X-Y+Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); | 319 | IPP = new Vertex("Inner+X-Y+Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); |
320 | IPM = new Vertex("Inner+X-Y-Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); | 320 | IPM = new Vertex("Inner+X-Y-Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); |
321 | IMP = new Vertex("Inner-X-Y+Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); | 321 | IMP = new Vertex("Inner-X-Y+Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); |
322 | IMM = new Vertex("Inner-X-Y-Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); | 322 | IMM = new Vertex("Inner-X-Y-Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); |
323 | 323 | ||
324 | MeshMY.Add(IPP); | 324 | MeshMY.Add(IPP); |
325 | MeshMY.Add(IPM); | 325 | MeshMY.Add(IPM); |
326 | MeshMY.Add(IMP); | 326 | MeshMY.Add(IMP); |
327 | MeshMY.Add(IMM); | 327 | MeshMY.Add(IMM); |
328 | 328 | ||
329 | MeshMY.Add(new Triangle(IPP, IPM, IMP)); | 329 | MeshMY.Add(new Triangle(IPP, IPM, IMP)); |
330 | MeshMY.Add(new Triangle(IMP, IPM, IMM)); | 330 | MeshMY.Add(new Triangle(IMP, IPM, IMM)); |
331 | 331 | ||
332 | foreach (Triangle t in MeshMY.triangles) | 332 | foreach (Triangle t in MeshMY.triangles) |
333 | { | 333 | { |
334 | PhysicsVector n = t.getNormal(); | 334 | PhysicsVector n = t.getNormal(); |
335 | } | 335 | } |
336 | 336 | ||
337 | 337 | ||
338 | 338 | ||
339 | IPP = new Vertex("Inner+X+Y+Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); | 339 | IPP = new Vertex("Inner+X+Y+Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); |
340 | IPM=new Vertex("Inner+X+Y-Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); | 340 | IPM=new Vertex("Inner+X+Y-Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); |
341 | IMP=new Vertex("Inner-X+Y+Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); | 341 | IMP=new Vertex("Inner-X+Y+Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, +size.Z / 2.0f); |
342 | IMM=new Vertex("Inner-X+Y-Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); | 342 | IMM=new Vertex("Inner-X+Y-Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, -size.Z / 2.0f); |
343 | 343 | ||
344 | MeshPY.Add(IPP); | 344 | MeshPY.Add(IPP); |
345 | MeshPY.Add(IPM); | 345 | MeshPY.Add(IPM); |
346 | MeshPY.Add(IMP); | 346 | MeshPY.Add(IMP); |
347 | MeshPY.Add(IMM); | 347 | MeshPY.Add(IMM); |
348 | 348 | ||
349 | MeshPY.Add(new Triangle(IPM, IPP, IMP)); | 349 | MeshPY.Add(new Triangle(IPM, IPP, IMP)); |
350 | MeshPY.Add(new Triangle(IMP, IMM, IPM)); | 350 | MeshPY.Add(new Triangle(IMP, IMM, IPM)); |
351 | 351 | ||
352 | foreach (Triangle t in MeshPY.triangles) | 352 | foreach (Triangle t in MeshPY.triangles) |
353 | { | 353 | { |
354 | PhysicsVector n = t.getNormal(); | 354 | PhysicsVector n = t.getNormal(); |
355 | } | 355 | } |
356 | 356 | ||
357 | 357 | ||
358 | 358 | ||
359 | } | 359 | } |
360 | 360 | ||
361 | 361 | ||
362 | Mesh result = new Mesh(); | 362 | Mesh result = new Mesh(); |
363 | result.Append(MeshMY); | 363 | result.Append(MeshMY); |
364 | result.Append(MeshPY); | 364 | result.Append(MeshPY); |
365 | 365 | ||
366 | return result; | 366 | return result; |
367 | } | 367 | } |
368 | 368 | ||
369 | static Mesh CreateBoxMeshZ(PrimitiveBaseShape primShape, PhysicsVector size) | 369 | static Mesh CreateBoxMeshZ(PrimitiveBaseShape primShape, PhysicsVector size) |
370 | // Builds the z (+ and -) surfaces of a box shaped prim | 370 | // Builds the z (+ and -) surfaces of a box shaped prim |
371 | { | 371 | { |
372 | UInt16 hollowFactor = primShape.ProfileHollow; | 372 | UInt16 hollowFactor = primShape.ProfileHollow; |
373 | 373 | ||
374 | // Base, i.e. outer shape | 374 | // Base, i.e. outer shape |
375 | // (M)inus Z | 375 | // (M)inus Z |
376 | Mesh MZ = new Mesh(); | 376 | Mesh MZ = new Mesh(); |
377 | 377 | ||
378 | MZ.Add(new Vertex("-X-Y-Z", -size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); | 378 | MZ.Add(new Vertex("-X-Y-Z", -size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); |
379 | MZ.Add(new Vertex("+X-Y-Z", +size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); | 379 | MZ.Add(new Vertex("+X-Y-Z", +size.X / 2.0f, -size.Y / 2.0f, -size.Z / 2.0f)); |
380 | MZ.Add(new Vertex("-X+Y-Z", -size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); | 380 | MZ.Add(new Vertex("-X+Y-Z", -size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); |
381 | MZ.Add(new Vertex("+X+Y-Z", +size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); | 381 | MZ.Add(new Vertex("+X+Y-Z", +size.X / 2.0f, +size.Y / 2.0f, -size.Z / 2.0f)); |
382 | 382 | ||
383 | 383 | ||
384 | MZ.Add(new Triangle(MZ.vertices[1], MZ.vertices[0], MZ.vertices[2])); | 384 | MZ.Add(new Triangle(MZ.vertices[1], MZ.vertices[0], MZ.vertices[2])); |
385 | MZ.Add(new Triangle(MZ.vertices[1], MZ.vertices[2], MZ.vertices[3])); | 385 | MZ.Add(new Triangle(MZ.vertices[1], MZ.vertices[2], MZ.vertices[3])); |
386 | 386 | ||
387 | // (P)lus Z | 387 | // (P)lus Z |
388 | Mesh PZ = new Mesh(); | 388 | Mesh PZ = new Mesh(); |
389 | 389 | ||
390 | PZ.Add(new Vertex("-X-Y+Z", -size.X / 2.0f, -size.Y / 2.0f, 0.0f)); | 390 | PZ.Add(new Vertex("-X-Y+Z", -size.X / 2.0f, -size.Y / 2.0f, 0.0f)); |
391 | PZ.Add(new Vertex("+X-Y+Z", +size.X / 2.0f, -size.Y / 2.0f, 0.0f)); | 391 | PZ.Add(new Vertex("+X-Y+Z", +size.X / 2.0f, -size.Y / 2.0f, 0.0f)); |
392 | PZ.Add(new Vertex("-X+Y+Z", -size.X / 2.0f, +size.Y / 2.0f, 0.0f)); | 392 | PZ.Add(new Vertex("-X+Y+Z", -size.X / 2.0f, +size.Y / 2.0f, 0.0f)); |
393 | PZ.Add(new Vertex("+X+Y+Z", +size.X / 2.0f, +size.Y / 2.0f, 0.0f)); | 393 | PZ.Add(new Vertex("+X+Y+Z", +size.X / 2.0f, +size.Y / 2.0f, 0.0f)); |
394 | 394 | ||
395 | // Surface 5, +Z | 395 | // Surface 5, +Z |
396 | PZ.Add(new Triangle(PZ.vertices[0], PZ.vertices[1], PZ.vertices[2])); | 396 | PZ.Add(new Triangle(PZ.vertices[0], PZ.vertices[1], PZ.vertices[2])); |
397 | PZ.Add(new Triangle(PZ.vertices[2], PZ.vertices[1], PZ.vertices[3])); | 397 | PZ.Add(new Triangle(PZ.vertices[2], PZ.vertices[1], PZ.vertices[3])); |
398 | 398 | ||
399 | if (hollowFactor > 0) | 399 | if (hollowFactor > 0) |
400 | { | 400 | { |
401 | float hollowFactorF = (float)hollowFactor / (float)50000; | 401 | float hollowFactorF = (float)hollowFactor / (float)50000; |
402 | 402 | ||
403 | MZ.Add(new Vertex("-X-Y-Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, 0.0f)); | 403 | MZ.Add(new Vertex("-X-Y-Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, 0.0f)); |
404 | MZ.Add(new Vertex("-X+Y-Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, 0.0f)); | 404 | MZ.Add(new Vertex("-X+Y-Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, 0.0f)); |
405 | MZ.Add(new Vertex("-X-Y+Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, 0.0f)); | 405 | MZ.Add(new Vertex("-X-Y+Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, 0.0f)); |
406 | MZ.Add(new Vertex("-X+Y+Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, 0.0f)); | 406 | MZ.Add(new Vertex("-X+Y+Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, 0.0f)); |
407 | 407 | ||
408 | List<int> innerBorders = new List<int>(); | 408 | List<int> innerBorders = new List<int>(); |
409 | innerBorders.Add(4); | 409 | innerBorders.Add(4); |
410 | innerBorders.Add(5); | 410 | innerBorders.Add(5); |
411 | innerBorders.Add(6); | 411 | innerBorders.Add(6); |
412 | innerBorders.Add(7); | 412 | innerBorders.Add(7); |
413 | 413 | ||
414 | InsertVertices(MZ.vertices, 4, MZ.triangles, innerBorders); | 414 | InsertVertices(MZ.vertices, 4, MZ.triangles, innerBorders); |
415 | 415 | ||
416 | PZ.Add(new Vertex("-X-Y-Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, 0.0f)); | 416 | PZ.Add(new Vertex("-X-Y-Z", -size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, 0.0f)); |
417 | PZ.Add(new Vertex("-X+Y-Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, 0.0f)); | 417 | PZ.Add(new Vertex("-X+Y-Z", +size.X * hollowFactorF / 2.0f, -size.Y * hollowFactorF / 2.0f, 0.0f)); |
418 | PZ.Add(new Vertex("-X-Y+Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, 0.0f)); | 418 | PZ.Add(new Vertex("-X-Y+Z", -size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, 0.0f)); |
419 | PZ.Add(new Vertex("-X+Y+Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, 0.0f)); | 419 | PZ.Add(new Vertex("-X+Y+Z", +size.X * hollowFactorF / 2.0f, +size.Y * hollowFactorF / 2.0f, 0.0f)); |
420 | 420 | ||
421 | innerBorders = new List<int>(); | 421 | innerBorders = new List<int>(); |
422 | innerBorders.Add(4); | 422 | innerBorders.Add(4); |
423 | innerBorders.Add(5); | 423 | innerBorders.Add(5); |
424 | innerBorders.Add(6); | 424 | innerBorders.Add(6); |
425 | innerBorders.Add(7); | 425 | innerBorders.Add(7); |
426 | 426 | ||
427 | InsertVertices(PZ.vertices, 4, PZ.triangles, innerBorders); | 427 | InsertVertices(PZ.vertices, 4, PZ.triangles, innerBorders); |
428 | 428 | ||
429 | } | 429 | } |
430 | 430 | ||
431 | foreach (Vertex v in PZ.vertices) | 431 | foreach (Vertex v in PZ.vertices) |
432 | { | 432 | { |
433 | v.point.Z = size.Z / 2.0f; | 433 | v.point.Z = size.Z / 2.0f; |
434 | } | 434 | } |
435 | foreach (Vertex v in MZ.vertices) | 435 | foreach (Vertex v in MZ.vertices) |
436 | { | 436 | { |
437 | v.point.Z = -size.Z / 2.0f; | 437 | v.point.Z = -size.Z / 2.0f; |
438 | } | 438 | } |
439 | 439 | ||
440 | foreach (Triangle t in MZ.triangles) | 440 | foreach (Triangle t in MZ.triangles) |
441 | { | 441 | { |
442 | PhysicsVector n = t.getNormal(); | 442 | PhysicsVector n = t.getNormal(); |
443 | if (n.Z > 0.0) | 443 | if (n.Z > 0.0) |
444 | t.invertNormal(); | 444 | t.invertNormal(); |
445 | } | 445 | } |
446 | 446 | ||
447 | foreach (Triangle t in PZ.triangles) | 447 | foreach (Triangle t in PZ.triangles) |
448 | { | 448 | { |
449 | PhysicsVector n = t.getNormal(); | 449 | PhysicsVector n = t.getNormal(); |
450 | if (n.Z < 0.0) | 450 | if (n.Z < 0.0) |
451 | t.invertNormal(); | 451 | t.invertNormal(); |
452 | } | 452 | } |
453 | 453 | ||
454 | Mesh result = new Mesh(); | 454 | Mesh result = new Mesh(); |
455 | result.Append(MZ); | 455 | result.Append(MZ); |
456 | result.Append(PZ); | 456 | result.Append(PZ); |
457 | 457 | ||
458 | return result; | 458 | return result; |
459 | } | 459 | } |
460 | 460 | ||
461 | static Mesh CreateBoxMesh(PrimitiveBaseShape primShape, PhysicsVector size) | 461 | static Mesh CreateBoxMesh(PrimitiveBaseShape primShape, PhysicsVector size) |
462 | { | 462 | { |
463 | Mesh result = new Mesh(); | 463 | Mesh result = new Mesh(); |
464 | 464 | ||
465 | 465 | ||
466 | 466 | ||
467 | Mesh MeshX = Meshmerizer.CreateBoxMeshX(primShape, size); | 467 | Mesh MeshX = Meshmerizer.CreateBoxMeshX(primShape, size); |
468 | Mesh MeshY = Meshmerizer.CreateBoxMeshY(primShape, size); | 468 | Mesh MeshY = Meshmerizer.CreateBoxMeshY(primShape, size); |
469 | Mesh MeshZ = Meshmerizer.CreateBoxMeshZ(primShape, size); | 469 | Mesh MeshZ = Meshmerizer.CreateBoxMeshZ(primShape, size); |
470 | 470 | ||
471 | result.Append(MeshX); | 471 | result.Append(MeshX); |
472 | result.Append(MeshY); | 472 | result.Append(MeshY); |
473 | result.Append(MeshZ); | 473 | result.Append(MeshZ); |
474 | 474 | ||
475 | return result; | 475 | return result; |
476 | } | 476 | } |
477 | 477 | ||
478 | 478 | ||
479 | public static void CalcNormals(Mesh mesh) | 479 | public static void CalcNormals(Mesh mesh) |
480 | { | 480 | { |
481 | int iTriangles = mesh.triangles.Count; | 481 | int iTriangles = mesh.triangles.Count; |
482 | 482 | ||
483 | mesh.normals = new float[iTriangles*3]; | 483 | mesh.normals = new float[iTriangles*3]; |
484 | 484 | ||
485 | int i=0; | 485 | int i=0; |
486 | foreach (Triangle t in mesh.triangles) | 486 | foreach (Triangle t in mesh.triangles) |
487 | { | 487 | { |
488 | 488 | ||
489 | float ux, uy, uz; | 489 | float ux, uy, uz; |
490 | float vx, vy, vz; | 490 | float vx, vy, vz; |
491 | float wx, wy, wz; | 491 | float wx, wy, wz; |
492 | 492 | ||
493 | ux = t.v1.point.X; | 493 | ux = t.v1.point.X; |
494 | uy = t.v1.point.Y; | 494 | uy = t.v1.point.Y; |
495 | uz = t.v1.point.Z; | 495 | uz = t.v1.point.Z; |
496 | 496 | ||
497 | vx = t.v2.point.X; | 497 | vx = t.v2.point.X; |
498 | vy = t.v2.point.Y; | 498 | vy = t.v2.point.Y; |
499 | vz = t.v2.point.Z; | 499 | vz = t.v2.point.Z; |
500 | 500 | ||
501 | wx = t.v3.point.X; | 501 | wx = t.v3.point.X; |
502 | wy = t.v3.point.Y; | 502 | wy = t.v3.point.Y; |
503 | wz = t.v3.point.Z; | 503 | wz = t.v3.point.Z; |
504 | 504 | ||
505 | // Vectors for edges | 505 | // Vectors for edges |
506 | float e1x, e1y, e1z; | 506 | float e1x, e1y, e1z; |
507 | float e2x, e2y, e2z; | 507 | float e2x, e2y, e2z; |
508 | 508 | ||
509 | e1x = ux - vx; | 509 | e1x = ux - vx; |
510 | e1y = uy - vy; | 510 | e1y = uy - vy; |
511 | e1z = uz - vz; | 511 | e1z = uz - vz; |
512 | 512 | ||
513 | e2x = ux - wx; | 513 | e2x = ux - wx; |
514 | e2y = uy - wy; | 514 | e2y = uy - wy; |
515 | e2z = uz - wz; | 515 | e2z = uz - wz; |
516 | 516 | ||
517 | 517 | ||
518 | // Cross product for normal | 518 | // Cross product for normal |
519 | float nx, ny, nz; | 519 | float nx, ny, nz; |
520 | nx = e1y * e2z - e1z * e2y; | 520 | nx = e1y * e2z - e1z * e2y; |
521 | ny = e1z * e2x - e1x * e2z; | 521 | ny = e1z * e2x - e1x * e2z; |
522 | nz = e1x * e2y - e1y * e2x; | 522 | nz = e1x * e2y - e1y * e2x; |
523 | 523 | ||
524 | // Length | 524 | // Length |
525 | float l = (float)Math.Sqrt(nx * nx + ny * ny + nz * nz); | 525 | float l = (float)Math.Sqrt(nx * nx + ny * ny + nz * nz); |
526 | 526 | ||
527 | // Normalized "normal" | 527 | // Normalized "normal" |
528 | nx /= l; | 528 | nx /= l; |
529 | ny /= l; | 529 | ny /= l; |
530 | nz /= l; | 530 | nz /= l; |
531 | 531 | ||
532 | mesh.normals[i] = nx; | 532 | mesh.normals[i] = nx; |
533 | mesh.normals[i + 1] = ny; | 533 | mesh.normals[i + 1] = ny; |
534 | mesh.normals[i + 2] = nz; | 534 | mesh.normals[i + 2] = nz; |
535 | 535 | ||
536 | i+=3; | 536 | i+=3; |
537 | } | 537 | } |
538 | } | 538 | } |
539 | 539 | ||
540 | public static Mesh CreateMesh(PrimitiveBaseShape primShape, PhysicsVector size) | 540 | public static Mesh CreateMesh(PrimitiveBaseShape primShape, PhysicsVector size) |
541 | { | 541 | { |
542 | Mesh mesh = null; | 542 | Mesh mesh = null; |
543 | 543 | ||
544 | switch (primShape.ProfileShape) | 544 | switch (primShape.ProfileShape) |
545 | { | 545 | { |
546 | case ProfileShape.Square: | 546 | case ProfileShape.Square: |
547 | mesh=CreateBoxMesh(primShape, size); | 547 | mesh=CreateBoxMesh(primShape, size); |
548 | CalcNormals(mesh); | 548 | CalcNormals(mesh); |
549 | break; | 549 | break; |
550 | default: | 550 | default: |
551 | mesh=null; | 551 | mesh=null; |
552 | break; | 552 | break; |
553 | } | 553 | } |
554 | 554 | ||
555 | return mesh; | 555 | return mesh; |
556 | 556 | ||
557 | } | 557 | } |
558 | } | 558 | } |
559 | } | 559 | } |
560 | 560 | ||