diff options
Diffstat (limited to 'OpenSim/Region/Physics/UbitMeshing/Mesh.cs')
-rw-r--r-- | OpenSim/Region/Physics/UbitMeshing/Mesh.cs | 614 |
1 files changed, 614 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/UbitMeshing/Mesh.cs b/OpenSim/Region/Physics/UbitMeshing/Mesh.cs new file mode 100644 index 0000000..b67422f --- /dev/null +++ b/OpenSim/Region/Physics/UbitMeshing/Mesh.cs | |||
@@ -0,0 +1,614 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.Runtime.InteropServices; | ||
32 | using OpenSim.Region.Physics.Manager; | ||
33 | using PrimMesher; | ||
34 | using OpenMetaverse; | ||
35 | using System.Runtime.Serialization; | ||
36 | using System.Runtime.Serialization.Formatters.Binary; | ||
37 | |||
38 | namespace OpenSim.Region.Physics.Meshing | ||
39 | { | ||
40 | public class MeshBuildingData | ||
41 | { | ||
42 | public Dictionary<Vertex, int> m_vertices; | ||
43 | public List<Triangle> m_triangles; | ||
44 | public float m_obbXmin; | ||
45 | public float m_obbXmax; | ||
46 | public float m_obbYmin; | ||
47 | public float m_obbYmax; | ||
48 | public float m_obbZmin; | ||
49 | public float m_obbZmax; | ||
50 | public Vector3 m_centroid; | ||
51 | public int m_centroidDiv; | ||
52 | } | ||
53 | |||
54 | [Serializable()] | ||
55 | public class Mesh : IMesh | ||
56 | { | ||
57 | float[] vertices; | ||
58 | int[] indexes; | ||
59 | Vector3 m_obb; | ||
60 | Vector3 m_obboffset; | ||
61 | [NonSerialized()] | ||
62 | MeshBuildingData m_bdata; | ||
63 | [NonSerialized()] | ||
64 | GCHandle vhandler; | ||
65 | [NonSerialized()] | ||
66 | GCHandle ihandler; | ||
67 | [NonSerialized()] | ||
68 | IntPtr m_verticesPtr = IntPtr.Zero; | ||
69 | [NonSerialized()] | ||
70 | IntPtr m_indicesPtr = IntPtr.Zero; | ||
71 | [NonSerialized()] | ||
72 | int m_vertexCount = 0; | ||
73 | [NonSerialized()] | ||
74 | int m_indexCount = 0; | ||
75 | |||
76 | public int RefCount { get; set; } | ||
77 | public AMeshKey Key { get; set; } | ||
78 | |||
79 | private class vertexcomp : IEqualityComparer<Vertex> | ||
80 | { | ||
81 | public bool Equals(Vertex v1, Vertex v2) | ||
82 | { | ||
83 | if (v1.X == v2.X && v1.Y == v2.Y && v1.Z == v2.Z) | ||
84 | return true; | ||
85 | else | ||
86 | return false; | ||
87 | } | ||
88 | public int GetHashCode(Vertex v) | ||
89 | { | ||
90 | int a = v.X.GetHashCode(); | ||
91 | int b = v.Y.GetHashCode(); | ||
92 | int c = v.Z.GetHashCode(); | ||
93 | return (a << 16) ^ (b << 8) ^ c; | ||
94 | } | ||
95 | } | ||
96 | |||
97 | public Mesh() | ||
98 | { | ||
99 | vertexcomp vcomp = new vertexcomp(); | ||
100 | |||
101 | m_bdata = new MeshBuildingData(); | ||
102 | m_bdata.m_vertices = new Dictionary<Vertex, int>(vcomp); | ||
103 | m_bdata.m_triangles = new List<Triangle>(); | ||
104 | m_bdata.m_centroid = Vector3.Zero; | ||
105 | m_bdata.m_centroidDiv = 0; | ||
106 | m_bdata.m_obbXmin = float.MaxValue; | ||
107 | m_bdata.m_obbXmax = float.MinValue; | ||
108 | m_bdata.m_obbYmin = float.MaxValue; | ||
109 | m_bdata.m_obbYmax = float.MinValue; | ||
110 | m_bdata.m_obbZmin = float.MaxValue; | ||
111 | m_bdata.m_obbZmax = float.MinValue; | ||
112 | m_obb = new Vector3(0.5f, 0.5f, 0.5f); | ||
113 | m_obboffset = Vector3.Zero; | ||
114 | } | ||
115 | |||
116 | |||
117 | public Mesh Scale(Vector3 scale) | ||
118 | { | ||
119 | if (m_verticesPtr == null || m_indicesPtr == null) | ||
120 | return null; | ||
121 | |||
122 | Mesh result = new Mesh(); | ||
123 | |||
124 | float x = scale.X; | ||
125 | float y = scale.Y; | ||
126 | float z = scale.Z; | ||
127 | |||
128 | result.m_obb.X = m_obb.X * x; | ||
129 | result.m_obb.Y = m_obb.Y * y; | ||
130 | result.m_obb.Z = m_obb.Z * z; | ||
131 | result.m_obboffset.X = m_obboffset.X * x; | ||
132 | result.m_obboffset.Y = m_obboffset.Y * y; | ||
133 | result.m_obboffset.Z = m_obboffset.Z * z; | ||
134 | |||
135 | result.vertices = new float[vertices.Length]; | ||
136 | int j = 0; | ||
137 | for (int i = 0; i < m_vertexCount; i++) | ||
138 | { | ||
139 | result.vertices[j] = vertices[j] * x; | ||
140 | j++; | ||
141 | result.vertices[j] = vertices[j] * y; | ||
142 | j++; | ||
143 | result.vertices[j] = vertices[j] * z; | ||
144 | j++; | ||
145 | } | ||
146 | |||
147 | result.indexes = new int[indexes.Length]; | ||
148 | indexes.CopyTo(result.indexes,0); | ||
149 | |||
150 | result.pinMemory(); | ||
151 | |||
152 | return result; | ||
153 | } | ||
154 | |||
155 | public Mesh Clone() | ||
156 | { | ||
157 | Mesh result = new Mesh(); | ||
158 | |||
159 | if (m_bdata != null) | ||
160 | { | ||
161 | result.m_bdata = new MeshBuildingData(); | ||
162 | foreach (Triangle t in m_bdata.m_triangles) | ||
163 | { | ||
164 | result.Add(new Triangle(t.v1.Clone(), t.v2.Clone(), t.v3.Clone())); | ||
165 | } | ||
166 | result.m_bdata.m_centroid = m_bdata.m_centroid; | ||
167 | result.m_bdata.m_centroidDiv = m_bdata.m_centroidDiv; | ||
168 | result.m_bdata.m_obbXmin = m_bdata.m_obbXmin; | ||
169 | result.m_bdata.m_obbXmax = m_bdata.m_obbXmax; | ||
170 | result.m_bdata.m_obbYmin = m_bdata.m_obbYmin; | ||
171 | result.m_bdata.m_obbYmax = m_bdata.m_obbYmax; | ||
172 | result.m_bdata.m_obbZmin = m_bdata.m_obbZmin; | ||
173 | result.m_bdata.m_obbZmax = m_bdata.m_obbZmax; | ||
174 | } | ||
175 | result.m_obb = m_obb; | ||
176 | result.m_obboffset = m_obboffset; | ||
177 | return result; | ||
178 | } | ||
179 | |||
180 | public void addVertexLStats(Vertex v) | ||
181 | { | ||
182 | float x = v.X; | ||
183 | float y = v.Y; | ||
184 | float z = v.Z; | ||
185 | |||
186 | m_bdata.m_centroid.X += x; | ||
187 | m_bdata.m_centroid.Y += y; | ||
188 | m_bdata.m_centroid.Z += z; | ||
189 | m_bdata.m_centroidDiv++; | ||
190 | |||
191 | if (x > m_bdata.m_obbXmax) | ||
192 | m_bdata.m_obbXmax = x; | ||
193 | else if (x < m_bdata.m_obbXmin) | ||
194 | m_bdata.m_obbXmin = x; | ||
195 | |||
196 | if (y > m_bdata.m_obbYmax) | ||
197 | m_bdata.m_obbYmax = y; | ||
198 | else if (y < m_bdata.m_obbYmin) | ||
199 | m_bdata.m_obbYmin = y; | ||
200 | |||
201 | if (z > m_bdata.m_obbZmax) | ||
202 | m_bdata.m_obbZmax = z; | ||
203 | else if (z < m_bdata.m_obbZmin) | ||
204 | m_bdata.m_obbZmin = z; | ||
205 | |||
206 | } | ||
207 | |||
208 | private float fRound(float f) | ||
209 | { | ||
210 | int i; | ||
211 | if (f == 0f) | ||
212 | return f; | ||
213 | else if (f > 0f) | ||
214 | i = (int)(1e5f * f + 0.5f); | ||
215 | else | ||
216 | i = (int)(1e5f * f - 0.5f); | ||
217 | |||
218 | return ((float)i * 1e-5f); | ||
219 | } | ||
220 | |||
221 | public void Add(Triangle triangle) | ||
222 | { | ||
223 | if (m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero) | ||
224 | throw new NotSupportedException("Attempt to Add to a pinned Mesh"); | ||
225 | |||
226 | // round down | ||
227 | triangle.v1.X = fRound(triangle.v1.X); | ||
228 | triangle.v1.Y = fRound(triangle.v1.Y); | ||
229 | triangle.v1.Z = fRound(triangle.v1.Z); | ||
230 | triangle.v2.X = fRound(triangle.v2.X); | ||
231 | triangle.v2.Y = fRound(triangle.v2.Y); | ||
232 | triangle.v2.Z = fRound(triangle.v2.Z); | ||
233 | triangle.v3.X = fRound(triangle.v3.X); | ||
234 | triangle.v3.Y = fRound(triangle.v3.Y); | ||
235 | triangle.v3.Z = fRound(triangle.v3.Z); | ||
236 | |||
237 | if ((triangle.v1.X == triangle.v2.X && triangle.v1.Y == triangle.v2.Y && triangle.v1.Z == triangle.v2.Z) | ||
238 | || (triangle.v1.X == triangle.v3.X && triangle.v1.Y == triangle.v3.Y && triangle.v1.Z == triangle.v3.Z) | ||
239 | || (triangle.v2.X == triangle.v3.X && triangle.v2.Y == triangle.v3.Y && triangle.v2.Z == triangle.v3.Z) | ||
240 | ) | ||
241 | { | ||
242 | return; | ||
243 | } | ||
244 | |||
245 | if (m_bdata.m_vertices.Count == 0) | ||
246 | { | ||
247 | m_bdata.m_centroidDiv = 0; | ||
248 | m_bdata.m_centroid = Vector3.Zero; | ||
249 | } | ||
250 | |||
251 | if (!m_bdata.m_vertices.ContainsKey(triangle.v1)) | ||
252 | { | ||
253 | m_bdata.m_vertices[triangle.v1] = m_bdata.m_vertices.Count; | ||
254 | addVertexLStats(triangle.v1); | ||
255 | } | ||
256 | if (!m_bdata.m_vertices.ContainsKey(triangle.v2)) | ||
257 | { | ||
258 | m_bdata.m_vertices[triangle.v2] = m_bdata.m_vertices.Count; | ||
259 | addVertexLStats(triangle.v2); | ||
260 | } | ||
261 | if (!m_bdata.m_vertices.ContainsKey(triangle.v3)) | ||
262 | { | ||
263 | m_bdata.m_vertices[triangle.v3] = m_bdata.m_vertices.Count; | ||
264 | addVertexLStats(triangle.v3); | ||
265 | } | ||
266 | m_bdata.m_triangles.Add(triangle); | ||
267 | } | ||
268 | |||
269 | public Vector3 GetCentroid() | ||
270 | { | ||
271 | return m_obboffset; | ||
272 | |||
273 | } | ||
274 | |||
275 | public Vector3 GetOBB() | ||
276 | { | ||
277 | return m_obb; | ||
278 | float x, y, z; | ||
279 | if (m_bdata.m_centroidDiv > 0) | ||
280 | { | ||
281 | x = (m_bdata.m_obbXmax - m_bdata.m_obbXmin) * 0.5f; | ||
282 | y = (m_bdata.m_obbYmax - m_bdata.m_obbYmin) * 0.5f; | ||
283 | z = (m_bdata.m_obbZmax - m_bdata.m_obbZmin) * 0.5f; | ||
284 | } | ||
285 | else // ?? | ||
286 | { | ||
287 | x = 0.5f; | ||
288 | y = 0.5f; | ||
289 | z = 0.5f; | ||
290 | } | ||
291 | return new Vector3(x, y, z); | ||
292 | } | ||
293 | |||
294 | public List<Vector3> getVertexList() | ||
295 | { | ||
296 | List<Vector3> result = new List<Vector3>(); | ||
297 | foreach (Vertex v in m_bdata.m_vertices.Keys) | ||
298 | { | ||
299 | result.Add(new Vector3(v.X, v.Y, v.Z)); | ||
300 | } | ||
301 | return result; | ||
302 | } | ||
303 | |||
304 | private float[] getVertexListAsFloat() | ||
305 | { | ||
306 | if (m_bdata.m_vertices == null) | ||
307 | throw new NotSupportedException(); | ||
308 | float[] result = new float[m_bdata.m_vertices.Count * 3]; | ||
309 | foreach (KeyValuePair<Vertex, int> kvp in m_bdata.m_vertices) | ||
310 | { | ||
311 | Vertex v = kvp.Key; | ||
312 | int i = kvp.Value; | ||
313 | result[3 * i + 0] = v.X; | ||
314 | result[3 * i + 1] = v.Y; | ||
315 | result[3 * i + 2] = v.Z; | ||
316 | } | ||
317 | return result; | ||
318 | } | ||
319 | |||
320 | public float[] getVertexListAsFloatLocked() | ||
321 | { | ||
322 | return null; | ||
323 | } | ||
324 | |||
325 | public void getVertexListAsPtrToFloatArray(out IntPtr _vertices, out int vertexStride, out int vertexCount) | ||
326 | { | ||
327 | // A vertex is 3 floats | ||
328 | vertexStride = 3 * sizeof(float); | ||
329 | |||
330 | // If there isn't an unmanaged array allocated yet, do it now | ||
331 | if (m_verticesPtr == IntPtr.Zero && m_bdata != null) | ||
332 | { | ||
333 | vertices = getVertexListAsFloat(); | ||
334 | // Each vertex is 3 elements (floats) | ||
335 | m_vertexCount = vertices.Length / 3; | ||
336 | vhandler = GCHandle.Alloc(vertices, GCHandleType.Pinned); | ||
337 | m_verticesPtr = vhandler.AddrOfPinnedObject(); | ||
338 | GC.AddMemoryPressure(Buffer.ByteLength(vertices)); | ||
339 | } | ||
340 | _vertices = m_verticesPtr; | ||
341 | vertexCount = m_vertexCount; | ||
342 | } | ||
343 | |||
344 | public int[] getIndexListAsInt() | ||
345 | { | ||
346 | if (m_bdata.m_triangles == null) | ||
347 | throw new NotSupportedException(); | ||
348 | int[] result = new int[m_bdata.m_triangles.Count * 3]; | ||
349 | for (int i = 0; i < m_bdata.m_triangles.Count; i++) | ||
350 | { | ||
351 | Triangle t = m_bdata.m_triangles[i]; | ||
352 | result[3 * i + 0] = m_bdata.m_vertices[t.v1]; | ||
353 | result[3 * i + 1] = m_bdata.m_vertices[t.v2]; | ||
354 | result[3 * i + 2] = m_bdata.m_vertices[t.v3]; | ||
355 | } | ||
356 | return result; | ||
357 | } | ||
358 | |||
359 | /// <summary> | ||
360 | /// creates a list of index values that defines triangle faces. THIS METHOD FREES ALL NON-PINNED MESH DATA | ||
361 | /// </summary> | ||
362 | /// <returns></returns> | ||
363 | public int[] getIndexListAsIntLocked() | ||
364 | { | ||
365 | return null; | ||
366 | } | ||
367 | |||
368 | public void getIndexListAsPtrToIntArray(out IntPtr indices, out int triStride, out int indexCount) | ||
369 | { | ||
370 | // If there isn't an unmanaged array allocated yet, do it now | ||
371 | if (m_indicesPtr == IntPtr.Zero && m_bdata != null) | ||
372 | { | ||
373 | indexes = getIndexListAsInt(); | ||
374 | m_indexCount = indexes.Length; | ||
375 | ihandler = GCHandle.Alloc(indexes, GCHandleType.Pinned); | ||
376 | m_indicesPtr = ihandler.AddrOfPinnedObject(); | ||
377 | GC.AddMemoryPressure(Buffer.ByteLength(indexes)); | ||
378 | } | ||
379 | // A triangle is 3 ints (indices) | ||
380 | triStride = 3 * sizeof(int); | ||
381 | indices = m_indicesPtr; | ||
382 | indexCount = m_indexCount; | ||
383 | } | ||
384 | |||
385 | public void releasePinned() | ||
386 | { | ||
387 | if (m_verticesPtr != IntPtr.Zero) | ||
388 | { | ||
389 | vhandler.Free(); | ||
390 | vertices = null; | ||
391 | m_verticesPtr = IntPtr.Zero; | ||
392 | } | ||
393 | if (m_indicesPtr != IntPtr.Zero) | ||
394 | { | ||
395 | ihandler.Free(); | ||
396 | indexes = null; | ||
397 | m_indicesPtr = IntPtr.Zero; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | /// <summary> | ||
402 | /// frees up the source mesh data to minimize memory - call this method after calling get*Locked() functions | ||
403 | /// </summary> | ||
404 | public void releaseSourceMeshData() | ||
405 | { | ||
406 | if (m_bdata != null) | ||
407 | { | ||
408 | m_bdata.m_triangles = null; | ||
409 | m_bdata.m_vertices = null; | ||
410 | } | ||
411 | } | ||
412 | |||
413 | public void releaseBuildingMeshData() | ||
414 | { | ||
415 | if (m_bdata != null) | ||
416 | { | ||
417 | m_bdata.m_triangles = null; | ||
418 | m_bdata.m_vertices = null; | ||
419 | m_bdata = null; | ||
420 | } | ||
421 | } | ||
422 | |||
423 | public void Append(IMesh newMesh) | ||
424 | { | ||
425 | if (m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero) | ||
426 | throw new NotSupportedException("Attempt to Append to a pinned Mesh"); | ||
427 | |||
428 | if (!(newMesh is Mesh)) | ||
429 | return; | ||
430 | |||
431 | foreach (Triangle t in ((Mesh)newMesh).m_bdata.m_triangles) | ||
432 | Add(t); | ||
433 | } | ||
434 | |||
435 | // Do a linear transformation of mesh. | ||
436 | public void TransformLinear(float[,] matrix, float[] offset) | ||
437 | { | ||
438 | if (m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero) | ||
439 | throw new NotSupportedException("Attempt to TransformLinear a pinned Mesh"); | ||
440 | |||
441 | foreach (Vertex v in m_bdata.m_vertices.Keys) | ||
442 | { | ||
443 | if (v == null) | ||
444 | continue; | ||
445 | float x, y, z; | ||
446 | x = v.X*matrix[0, 0] + v.Y*matrix[1, 0] + v.Z*matrix[2, 0]; | ||
447 | y = v.X*matrix[0, 1] + v.Y*matrix[1, 1] + v.Z*matrix[2, 1]; | ||
448 | z = v.X*matrix[0, 2] + v.Y*matrix[1, 2] + v.Z*matrix[2, 2]; | ||
449 | v.X = x + offset[0]; | ||
450 | v.Y = y + offset[1]; | ||
451 | v.Z = z + offset[2]; | ||
452 | } | ||
453 | } | ||
454 | |||
455 | public void DumpRaw(String path, String name, String title) | ||
456 | { | ||
457 | if (path == null) | ||
458 | return; | ||
459 | if (m_bdata == null) | ||
460 | return; | ||
461 | String fileName = name + "_" + title + ".raw"; | ||
462 | String completePath = System.IO.Path.Combine(path, fileName); | ||
463 | StreamWriter sw = new StreamWriter(completePath); | ||
464 | foreach (Triangle t in m_bdata.m_triangles) | ||
465 | { | ||
466 | String s = t.ToStringRaw(); | ||
467 | sw.WriteLine(s); | ||
468 | } | ||
469 | sw.Close(); | ||
470 | } | ||
471 | |||
472 | public void TrimExcess() | ||
473 | { | ||
474 | m_bdata.m_triangles.TrimExcess(); | ||
475 | } | ||
476 | |||
477 | public void pinMemory() | ||
478 | { | ||
479 | m_vertexCount = vertices.Length / 3; | ||
480 | vhandler = GCHandle.Alloc(vertices, GCHandleType.Pinned); | ||
481 | m_verticesPtr = vhandler.AddrOfPinnedObject(); | ||
482 | GC.AddMemoryPressure(Buffer.ByteLength(vertices)); | ||
483 | |||
484 | m_indexCount = indexes.Length; | ||
485 | ihandler = GCHandle.Alloc(indexes, GCHandleType.Pinned); | ||
486 | m_indicesPtr = ihandler.AddrOfPinnedObject(); | ||
487 | GC.AddMemoryPressure(Buffer.ByteLength(indexes)); | ||
488 | } | ||
489 | |||
490 | public void PrepForOde() | ||
491 | { | ||
492 | // If there isn't an unmanaged array allocated yet, do it now | ||
493 | if (m_verticesPtr == IntPtr.Zero) | ||
494 | vertices = getVertexListAsFloat(); | ||
495 | |||
496 | // If there isn't an unmanaged array allocated yet, do it now | ||
497 | if (m_indicesPtr == IntPtr.Zero) | ||
498 | indexes = getIndexListAsInt(); | ||
499 | |||
500 | pinMemory(); | ||
501 | |||
502 | float x, y, z; | ||
503 | |||
504 | if (m_bdata.m_centroidDiv > 0) | ||
505 | { | ||
506 | m_obboffset = new Vector3(m_bdata.m_centroid.X / m_bdata.m_centroidDiv, m_bdata.m_centroid.Y / m_bdata.m_centroidDiv, m_bdata.m_centroid.Z / m_bdata.m_centroidDiv); | ||
507 | x = (m_bdata.m_obbXmax - m_bdata.m_obbXmin) * 0.5f; | ||
508 | y = (m_bdata.m_obbYmax - m_bdata.m_obbYmin) * 0.5f; | ||
509 | z = (m_bdata.m_obbZmax - m_bdata.m_obbZmin) * 0.5f; | ||
510 | } | ||
511 | |||
512 | else | ||
513 | { | ||
514 | m_obboffset = Vector3.Zero; | ||
515 | x = 0.5f; | ||
516 | y = 0.5f; | ||
517 | z = 0.5f; | ||
518 | } | ||
519 | m_obb = new Vector3(x, y, z); | ||
520 | |||
521 | releaseBuildingMeshData(); | ||
522 | } | ||
523 | public bool ToStream(Stream st) | ||
524 | { | ||
525 | if (m_indicesPtr == IntPtr.Zero || m_verticesPtr == IntPtr.Zero) | ||
526 | return false; | ||
527 | |||
528 | BinaryWriter bw = new BinaryWriter(st); | ||
529 | bool ok = true; | ||
530 | |||
531 | try | ||
532 | { | ||
533 | |||
534 | bw.Write(m_vertexCount); | ||
535 | bw.Write(m_indexCount); | ||
536 | |||
537 | for (int i = 0; i < 3 * m_vertexCount; i++) | ||
538 | bw.Write(vertices[i]); | ||
539 | for (int i = 0; i < m_indexCount; i++) | ||
540 | bw.Write(indexes[i]); | ||
541 | bw.Write(m_obb.X); | ||
542 | bw.Write(m_obb.Y); | ||
543 | bw.Write(m_obb.Z); | ||
544 | bw.Write(m_obboffset.X); | ||
545 | bw.Write(m_obboffset.Y); | ||
546 | bw.Write(m_obboffset.Z); | ||
547 | } | ||
548 | catch | ||
549 | { | ||
550 | ok = false; | ||
551 | } | ||
552 | |||
553 | if (bw != null) | ||
554 | { | ||
555 | bw.Flush(); | ||
556 | bw.Close(); | ||
557 | } | ||
558 | |||
559 | return ok; | ||
560 | } | ||
561 | |||
562 | public static Mesh FromStream(Stream st, AMeshKey key) | ||
563 | { | ||
564 | Mesh mesh = new Mesh(); | ||
565 | mesh.releaseBuildingMeshData(); | ||
566 | |||
567 | BinaryReader br = new BinaryReader(st); | ||
568 | |||
569 | bool ok = true; | ||
570 | try | ||
571 | { | ||
572 | mesh.m_vertexCount = br.ReadInt32(); | ||
573 | mesh.m_indexCount = br.ReadInt32(); | ||
574 | |||
575 | int n = 3 * mesh.m_vertexCount; | ||
576 | mesh.vertices = new float[n]; | ||
577 | for (int i = 0; i < n; i++) | ||
578 | mesh.vertices[i] = br.ReadSingle(); | ||
579 | |||
580 | mesh.indexes = new int[mesh.m_indexCount]; | ||
581 | for (int i = 0; i < mesh.m_indexCount; i++) | ||
582 | mesh.indexes[i] = br.ReadInt32(); | ||
583 | |||
584 | mesh.m_obb.X = br.ReadSingle(); | ||
585 | mesh.m_obb.Y = br.ReadSingle(); | ||
586 | mesh.m_obb.Z = br.ReadSingle(); | ||
587 | |||
588 | mesh.m_obboffset.X = br.ReadSingle(); | ||
589 | mesh.m_obboffset.Y = br.ReadSingle(); | ||
590 | mesh.m_obboffset.Z = br.ReadSingle(); | ||
591 | } | ||
592 | catch | ||
593 | { | ||
594 | ok = false; | ||
595 | } | ||
596 | |||
597 | br.Close(); | ||
598 | |||
599 | if (ok) | ||
600 | { | ||
601 | mesh.pinMemory(); | ||
602 | |||
603 | mesh.Key = key; | ||
604 | mesh.RefCount = 1; | ||
605 | |||
606 | return mesh; | ||
607 | } | ||
608 | |||
609 | mesh.vertices = null; | ||
610 | mesh.indexes = null; | ||
611 | return null; | ||
612 | } | ||
613 | } | ||
614 | } | ||