diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs | 568 |
1 files changed, 284 insertions, 284 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs index bbb014a..a199078 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs | |||
@@ -1,284 +1,284 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (c) Contributors, http://opensimulator.org/ | 2 | * Copyright (c) Contributors, http://opensimulator.org/ |
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | 3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. |
4 | * | 4 | * |
5 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: | 6 | * modification, are permitted provided that the following conditions are met: |
7 | * * Redistributions of source code must retain the above copyright | 7 | * * Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. | 8 | * notice, this list of conditions and the following disclaimer. |
9 | * * Redistributions in binary form must reproduce the above copyrightD | 9 | * * Redistributions in binary form must reproduce the above copyrightD |
10 | * notice, this list of conditions and the following disclaimer in the | 10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. | 11 | * documentation and/or other materials provided with the distribution. |
12 | * * Neither the name of the OpenSimulator Project nor the | 12 | * * Neither the name of the OpenSimulator Project nor the |
13 | * names of its contributors may be used to endorse or promote products | 13 | * names of its contributors may be used to endorse or promote products |
14 | * derived from this software without specific prior written permission. | 14 | * derived from this software without specific prior written permission. |
15 | * | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | 16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY |
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | 19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ | 26 | */ |
27 | using System; | 27 | using System; |
28 | using System.Collections.Generic; | 28 | using System.Collections.Generic; |
29 | using System.Text; | 29 | using System.Text; |
30 | 30 | ||
31 | using OpenSim.Framework; | 31 | using OpenSim.Framework; |
32 | using OpenSim.Region.Framework; | 32 | using OpenSim.Region.Framework; |
33 | using OpenSim.Region.CoreModules; | 33 | using OpenSim.Region.CoreModules; |
34 | using OpenSim.Region.Physics.Manager; | 34 | using OpenSim.Region.Physics.Manager; |
35 | 35 | ||
36 | using Nini.Config; | 36 | using Nini.Config; |
37 | using log4net; | 37 | using log4net; |
38 | 38 | ||
39 | using OpenMetaverse; | 39 | using OpenMetaverse; |
40 | 40 | ||
41 | namespace OpenSim.Region.Physics.BulletSPlugin | 41 | namespace OpenSim.Region.Physics.BulletSPlugin |
42 | { | 42 | { |
43 | public sealed class BSTerrainMesh : BSTerrainPhys | 43 | public sealed class BSTerrainMesh : BSTerrainPhys |
44 | { | 44 | { |
45 | static string LogHeader = "[BULLETSIM TERRAIN MESH]"; | 45 | static string LogHeader = "[BULLETSIM TERRAIN MESH]"; |
46 | 46 | ||
47 | private float[] m_savedHeightMap; | 47 | private float[] m_savedHeightMap; |
48 | int m_sizeX; | 48 | int m_sizeX; |
49 | int m_sizeY; | 49 | int m_sizeY; |
50 | 50 | ||
51 | BulletShape m_terrainShape; | 51 | BulletShape m_terrainShape; |
52 | BulletBody m_terrainBody; | 52 | BulletBody m_terrainBody; |
53 | 53 | ||
54 | public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize) | 54 | public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize) |
55 | : base(physicsScene, regionBase, id) | 55 | : base(physicsScene, regionBase, id) |
56 | { | 56 | { |
57 | } | 57 | } |
58 | 58 | ||
59 | public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id /* parameters for making mesh */) | 59 | public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id /* parameters for making mesh */) |
60 | : base(physicsScene, regionBase, id) | 60 | : base(physicsScene, regionBase, id) |
61 | { | 61 | { |
62 | } | 62 | } |
63 | 63 | ||
64 | // Create terrain mesh from a heightmap. | 64 | // Create terrain mesh from a heightmap. |
65 | public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap, | 65 | public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap, |
66 | Vector3 minCoords, Vector3 maxCoords) | 66 | Vector3 minCoords, Vector3 maxCoords) |
67 | : base(physicsScene, regionBase, id) | 67 | : base(physicsScene, regionBase, id) |
68 | { | 68 | { |
69 | int indicesCount; | 69 | int indicesCount; |
70 | int[] indices; | 70 | int[] indices; |
71 | int verticesCount; | 71 | int verticesCount; |
72 | float[] vertices; | 72 | float[] vertices; |
73 | 73 | ||
74 | m_savedHeightMap = initialMap; | 74 | m_savedHeightMap = initialMap; |
75 | 75 | ||
76 | m_sizeX = (int)(maxCoords.X - minCoords.X); | 76 | m_sizeX = (int)(maxCoords.X - minCoords.X); |
77 | m_sizeY = (int)(maxCoords.Y - minCoords.Y); | 77 | m_sizeY = (int)(maxCoords.Y - minCoords.Y); |
78 | 78 | ||
79 | if (!BSTerrainMesh.ConvertHeightmapToMesh(PhysicsScene, initialMap, m_sizeX, m_sizeY, | 79 | if (!BSTerrainMesh.ConvertHeightmapToMesh(PhysicsScene, initialMap, m_sizeX, m_sizeY, |
80 | (float)m_sizeX, (float)m_sizeY, | 80 | (float)m_sizeX, (float)m_sizeY, |
81 | Vector3.Zero, 1.0f, | 81 | Vector3.Zero, 1.0f, |
82 | out indicesCount, out indices, out verticesCount, out vertices)) | 82 | out indicesCount, out indices, out verticesCount, out vertices)) |
83 | { | 83 | { |
84 | // DISASTER!! | 84 | // DISASTER!! |
85 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedConversionOfHeightmap", ID); | 85 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedConversionOfHeightmap", ID); |
86 | PhysicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh! base={1}", LogHeader, TerrainBase); | 86 | PhysicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh! base={1}", LogHeader, TerrainBase); |
87 | // Something is very messed up and a crash is in our future. | 87 | // Something is very messed up and a crash is in our future. |
88 | return; | 88 | return; |
89 | } | 89 | } |
90 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,afterConvertHeightmapToMesh,ver={1},ind={2}", | 90 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,afterConvertHeightmapToMesh,ver={1},ind={2}", |
91 | ID, verticesCount, indicesCount); | 91 | ID, verticesCount, indicesCount); |
92 | 92 | ||
93 | m_terrainShape = new BulletShape(BulletSimAPI.CreateMeshShape2(PhysicsScene.World.ptr, | 93 | m_terrainShape = new BulletShape(BulletSimAPI.CreateMeshShape2(PhysicsScene.World.ptr, |
94 | indicesCount, indices, verticesCount, vertices), | 94 | indicesCount, indices, verticesCount, vertices), |
95 | PhysicsShapeType.SHAPE_MESH); | 95 | PhysicsShapeType.SHAPE_MESH); |
96 | if (m_terrainShape.ptr == IntPtr.Zero) | 96 | if (m_terrainShape.ptr == IntPtr.Zero) |
97 | { | 97 | { |
98 | // DISASTER!! | 98 | // DISASTER!! |
99 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID); | 99 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID); |
100 | physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain mesh! base={1}", LogHeader, TerrainBase); | 100 | physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain mesh! base={1}", LogHeader, TerrainBase); |
101 | // Something is very messed up and a crash is in our future. | 101 | // Something is very messed up and a crash is in our future. |
102 | return; | 102 | return; |
103 | } | 103 | } |
104 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,afterCreateShape,shape={1}", ID, m_terrainShape); | 104 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,afterCreateShape,shape={1}", ID, m_terrainShape); |
105 | 105 | ||
106 | // The terrain object initial position is at the center of the object | 106 | // The terrain object initial position is at the center of the object |
107 | Vector3 centerPos; | 107 | Vector3 centerPos; |
108 | centerPos.X = minCoords.X + (m_sizeX / 2f); | 108 | centerPos.X = minCoords.X + (m_sizeX / 2f); |
109 | centerPos.Y = minCoords.Y + (m_sizeY / 2f); | 109 | centerPos.Y = minCoords.Y + (m_sizeY / 2f); |
110 | centerPos.Z = minCoords.Z + ((maxCoords.Z - minCoords.Z) / 2f); | 110 | centerPos.Z = minCoords.Z + ((maxCoords.Z - minCoords.Z) / 2f); |
111 | Quaternion rot = Quaternion.Identity; | 111 | Quaternion rot = Quaternion.Identity; |
112 | 112 | ||
113 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,creatingBody,centerPos={1},rot={2}", ID, centerPos, rot); | 113 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,creatingBody,centerPos={1},rot={2}", ID, centerPos, rot); |
114 | m_terrainBody = new BulletBody(id, BulletSimAPI.CreateBodyWithDefaultMotionState2( | 114 | m_terrainBody = new BulletBody(id, BulletSimAPI.CreateBodyWithDefaultMotionState2( |
115 | m_terrainShape.ptr, ID, centerPos, rot)); | 115 | m_terrainShape.ptr, ID, centerPos, rot)); |
116 | if (m_terrainBody.ptr == IntPtr.Zero) | 116 | if (m_terrainBody.ptr == IntPtr.Zero) |
117 | { | 117 | { |
118 | // DISASTER!! | 118 | // DISASTER!! |
119 | physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase); | 119 | physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase); |
120 | // Something is very messed up and a crash is in our future. | 120 | // Something is very messed up and a crash is in our future. |
121 | return; | 121 | return; |
122 | } | 122 | } |
123 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,afterCreateBody,body={1}", ID, m_terrainBody); | 123 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,afterCreateBody,body={1}", ID, m_terrainBody); |
124 | 124 | ||
125 | // Set current terrain attributes | 125 | // Set current terrain attributes |
126 | BulletSimAPI.SetFriction2(m_terrainBody.ptr, PhysicsScene.Params.terrainFriction); | 126 | BulletSimAPI.SetFriction2(m_terrainBody.ptr, PhysicsScene.Params.terrainFriction); |
127 | BulletSimAPI.SetHitFraction2(m_terrainBody.ptr, PhysicsScene.Params.terrainHitFraction); | 127 | BulletSimAPI.SetHitFraction2(m_terrainBody.ptr, PhysicsScene.Params.terrainHitFraction); |
128 | BulletSimAPI.SetRestitution2(m_terrainBody.ptr, PhysicsScene.Params.terrainRestitution); | 128 | BulletSimAPI.SetRestitution2(m_terrainBody.ptr, PhysicsScene.Params.terrainRestitution); |
129 | BulletSimAPI.SetCollisionFlags2(m_terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT); | 129 | BulletSimAPI.SetCollisionFlags2(m_terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT); |
130 | 130 | ||
131 | // Static objects are not very massive. | 131 | // Static objects are not very massive. |
132 | BulletSimAPI.SetMassProps2(m_terrainBody.ptr, 0f, Vector3.Zero); | 132 | BulletSimAPI.SetMassProps2(m_terrainBody.ptr, 0f, Vector3.Zero); |
133 | 133 | ||
134 | // Return the new terrain to the world of physical objects | 134 | // Return the new terrain to the world of physical objects |
135 | BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr); | 135 | BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr); |
136 | 136 | ||
137 | // redo its bounding box now that it is in the world | 137 | // redo its bounding box now that it is in the world |
138 | BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_terrainBody.ptr); | 138 | BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_terrainBody.ptr); |
139 | 139 | ||
140 | BulletSimAPI.SetCollisionFilterMask2(m_terrainBody.ptr, | 140 | BulletSimAPI.SetCollisionFilterMask2(m_terrainBody.ptr, |
141 | (uint)CollisionFilterGroups.TerrainFilter, | 141 | (uint)CollisionFilterGroups.TerrainFilter, |
142 | (uint)CollisionFilterGroups.TerrainMask); | 142 | (uint)CollisionFilterGroups.TerrainMask); |
143 | 143 | ||
144 | // Make it so the terrain will not move or be considered for movement. | 144 | // Make it so the terrain will not move or be considered for movement. |
145 | BulletSimAPI.ForceActivationState2(m_terrainBody.ptr, ActivationState.DISABLE_SIMULATION); | 145 | BulletSimAPI.ForceActivationState2(m_terrainBody.ptr, ActivationState.DISABLE_SIMULATION); |
146 | } | 146 | } |
147 | 147 | ||
148 | public override void Dispose() | 148 | public override void Dispose() |
149 | { | 149 | { |
150 | if (m_terrainBody.ptr != IntPtr.Zero) | 150 | if (m_terrainBody.ptr != IntPtr.Zero) |
151 | { | 151 | { |
152 | BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr); | 152 | BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr); |
153 | // Frees both the body and the shape. | 153 | // Frees both the body and the shape. |
154 | BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_terrainBody.ptr); | 154 | BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_terrainBody.ptr); |
155 | } | 155 | } |
156 | } | 156 | } |
157 | 157 | ||
158 | public override float GetHeightAtXYZ(Vector3 pos) | 158 | public override float GetHeightAtXYZ(Vector3 pos) |
159 | { | 159 | { |
160 | // For the moment use the saved heightmap to get the terrain height. | 160 | // For the moment use the saved heightmap to get the terrain height. |
161 | // TODO: raycast downward to find the true terrain below the position. | 161 | // TODO: raycast downward to find the true terrain below the position. |
162 | float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET; | 162 | float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET; |
163 | 163 | ||
164 | int mapIndex = (int)pos.Y * m_sizeY + (int)pos.X; | 164 | int mapIndex = (int)pos.Y * m_sizeY + (int)pos.X; |
165 | try | 165 | try |
166 | { | 166 | { |
167 | ret = m_savedHeightMap[mapIndex]; | 167 | ret = m_savedHeightMap[mapIndex]; |
168 | } | 168 | } |
169 | catch | 169 | catch |
170 | { | 170 | { |
171 | // Sometimes they give us wonky values of X and Y. Give a warning and return something. | 171 | // Sometimes they give us wonky values of X and Y. Give a warning and return something. |
172 | PhysicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}", | 172 | PhysicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}", |
173 | LogHeader, TerrainBase, pos); | 173 | LogHeader, TerrainBase, pos); |
174 | ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET; | 174 | ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET; |
175 | } | 175 | } |
176 | return ret; | 176 | return ret; |
177 | } | 177 | } |
178 | 178 | ||
179 | // Convert the passed heightmap to mesh information suitable for CreateMeshShape2(). | 179 | // Convert the passed heightmap to mesh information suitable for CreateMeshShape2(). |
180 | // Return 'true' if successfully created. | 180 | // Return 'true' if successfully created. |
181 | public static bool ConvertHeightmapToMesh( | 181 | public static bool ConvertHeightmapToMesh( |
182 | BSScene physicsScene, | 182 | BSScene physicsScene, |
183 | float[] heightMap, int sizeX, int sizeY, // parameters of incoming heightmap | 183 | float[] heightMap, int sizeX, int sizeY, // parameters of incoming heightmap |
184 | float extentX, float extentY, // zero based range for output vertices | 184 | float extentX, float extentY, // zero based range for output vertices |
185 | Vector3 extentBase, // base to be added to all vertices | 185 | Vector3 extentBase, // base to be added to all vertices |
186 | float magnification, // number of vertices to create between heightMap coords | 186 | float magnification, // number of vertices to create between heightMap coords |
187 | out int indicesCountO, out int[] indicesO, | 187 | out int indicesCountO, out int[] indicesO, |
188 | out int verticesCountO, out float[] verticesO) | 188 | out int verticesCountO, out float[] verticesO) |
189 | { | 189 | { |
190 | bool ret = false; | 190 | bool ret = false; |
191 | 191 | ||
192 | int indicesCount = 0; | 192 | int indicesCount = 0; |
193 | int verticesCount = 0; | 193 | int verticesCount = 0; |
194 | int[] indices = new int[0]; | 194 | int[] indices = new int[0]; |
195 | float[] vertices = new float[0]; | 195 | float[] vertices = new float[0]; |
196 | 196 | ||
197 | // Simple mesh creation which assumes magnification == 1, sizeX == extentX and sizeY == extentY. | 197 | // Simple mesh creation which assumes magnification == 1, sizeX == extentX and sizeY == extentY. |
198 | // TODO: do a more general solution that scales, adds new vertices and smoothes the result. | 198 | // TODO: do a more general solution that scales, adds new vertices and smoothes the result. |
199 | 199 | ||
200 | try | 200 | try |
201 | { | 201 | { |
202 | // One vertice per heightmap value plus the vertices off the top and bottom edge. | 202 | // One vertice per heightmap value plus the vertices off the top and bottom edge. |
203 | int totalVertices = (sizeX + 1) * (sizeY + 1); | 203 | int totalVertices = (sizeX + 1) * (sizeY + 1); |
204 | vertices = new float[totalVertices * 3]; | 204 | vertices = new float[totalVertices * 3]; |
205 | int totalIndices = sizeX * sizeY * 6; | 205 | int totalIndices = sizeX * sizeY * 6; |
206 | indices = new int[totalIndices]; | 206 | indices = new int[totalIndices]; |
207 | 207 | ||
208 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2}", | 208 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2}", |
209 | BSScene.DetailLogZero, totalVertices, totalIndices); | 209 | BSScene.DetailLogZero, totalVertices, totalIndices); |
210 | float magX = (float)sizeX / extentX; | 210 | float magX = (float)sizeX / extentX; |
211 | float magY = (float)sizeY / extentY; | 211 | float magY = (float)sizeY / extentY; |
212 | // Note that sizeX+1 vertices are created since there is land between this and the next region. | 212 | // Note that sizeX+1 vertices are created since there is land between this and the next region. |
213 | for (int yy = 0; yy <= sizeY; yy++) | 213 | for (int yy = 0; yy <= sizeY; yy++) |
214 | { | 214 | { |
215 | for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we got through sizeX + 1 times | 215 | for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we got through sizeX + 1 times |
216 | { | 216 | { |
217 | int offset = yy * sizeX + xx; | 217 | int offset = yy * sizeX + xx; |
218 | // Extend the height from the height from the last row or column | 218 | // Extend the height from the height from the last row or column |
219 | if (yy == sizeY) offset -= sizeX; | 219 | if (yy == sizeY) offset -= sizeX; |
220 | if (xx == sizeX) offset -= 1; | 220 | if (xx == sizeX) offset -= 1; |
221 | float height = heightMap[offset]; | 221 | float height = heightMap[offset]; |
222 | vertices[verticesCount + 0] = (float)xx * magX + extentBase.X; | 222 | vertices[verticesCount + 0] = (float)xx * magX + extentBase.X; |
223 | vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y; | 223 | vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y; |
224 | vertices[verticesCount + 2] = height + extentBase.Z; | 224 | vertices[verticesCount + 2] = height + extentBase.Z; |
225 | if (physicsScene.PhysicsLogging.Enabled && verticesCount < 900) // DEBUG DEBUG DEBUG | 225 | if (physicsScene.PhysicsLogging.Enabled && verticesCount < 900) // DEBUG DEBUG DEBUG |
226 | { | 226 | { |
227 | Vector3 genVertex = new Vector3( | 227 | Vector3 genVertex = new Vector3( |
228 | vertices[verticesCount + 0], | 228 | vertices[verticesCount + 0], |
229 | vertices[verticesCount + 1], | 229 | vertices[verticesCount + 1], |
230 | vertices[verticesCount + 2]); | 230 | vertices[verticesCount + 2]); |
231 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,ii={1},vertex={2}", | 231 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,ii={1},vertex={2}", |
232 | BSScene.DetailLogZero, verticesCount/3, genVertex); | 232 | BSScene.DetailLogZero, verticesCount/3, genVertex); |
233 | } | 233 | } |
234 | verticesCount += 3; | 234 | verticesCount += 3; |
235 | } | 235 | } |
236 | } | 236 | } |
237 | verticesCount = verticesCount / 3; | 237 | verticesCount = verticesCount / 3; |
238 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeVerts,verCount={1}", | 238 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeVerts,verCount={1}", |
239 | BSScene.DetailLogZero, verticesCount); | 239 | BSScene.DetailLogZero, verticesCount); |
240 | 240 | ||
241 | for (int yy = 0; yy < sizeY; yy++) | 241 | for (int yy = 0; yy < sizeY; yy++) |
242 | { | 242 | { |
243 | for (int xx = 0; xx < sizeX; xx++) | 243 | for (int xx = 0; xx < sizeX; xx++) |
244 | { | 244 | { |
245 | int offset = yy * sizeX + xx; | 245 | int offset = yy * sizeX + xx; |
246 | // Each vertices is presumed to be the upper left corner of a box of two triangles | 246 | // Each vertices is presumed to be the upper left corner of a box of two triangles |
247 | indices[indicesCount + 0] = offset; | 247 | indices[indicesCount + 0] = offset; |
248 | indices[indicesCount + 1] = offset + 1; | 248 | indices[indicesCount + 1] = offset + 1; |
249 | indices[indicesCount + 2] = offset + sizeX + 1; // accounting for the extra column | 249 | indices[indicesCount + 2] = offset + sizeX + 1; // accounting for the extra column |
250 | indices[indicesCount + 3] = offset + 1; | 250 | indices[indicesCount + 3] = offset + 1; |
251 | indices[indicesCount + 4] = offset + sizeX + 2; | 251 | indices[indicesCount + 4] = offset + sizeX + 2; |
252 | indices[indicesCount + 5] = offset + sizeX + 1; | 252 | indices[indicesCount + 5] = offset + sizeX + 1; |
253 | if (indicesCount < (300 * 6)) // DEBUG DEBUG DEBUG | 253 | if (indicesCount < (300 * 6)) // DEBUG DEBUG DEBUG |
254 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,i0={1},i1={2},i2={3},i3={4},i4={5},i5={6}", // DEEBUG DEBUG DEBUG | 254 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,i0={1},i1={2},i2={3},i3={4},i4={5},i5={6}", // DEEBUG DEBUG DEBUG |
255 | BSScene.DetailLogZero, | 255 | BSScene.DetailLogZero, |
256 | indices[indicesCount + 0], | 256 | indices[indicesCount + 0], |
257 | indices[indicesCount + 1], | 257 | indices[indicesCount + 1], |
258 | indices[indicesCount + 2], | 258 | indices[indicesCount + 2], |
259 | indices[indicesCount + 3], | 259 | indices[indicesCount + 3], |
260 | indices[indicesCount + 4], | 260 | indices[indicesCount + 4], |
261 | indices[indicesCount + 5] | 261 | indices[indicesCount + 5] |
262 | ); | 262 | ); |
263 | indicesCount += 6; | 263 | indicesCount += 6; |
264 | } | 264 | } |
265 | } | 265 | } |
266 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeIndices,indCount={1}", // DEEBUG DEBUG DEBUG | 266 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeIndices,indCount={1}", // DEEBUG DEBUG DEBUG |
267 | LogHeader, indicesCount); // DEBUG | 267 | LogHeader, indicesCount); // DEBUG |
268 | ret = true; | 268 | ret = true; |
269 | } | 269 | } |
270 | catch (Exception e) | 270 | catch (Exception e) |
271 | { | 271 | { |
272 | physicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh. Base={1}, e={2}", | 272 | physicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh. Base={1}, e={2}", |
273 | LogHeader, extentBase, e); | 273 | LogHeader, extentBase, e); |
274 | } | 274 | } |
275 | 275 | ||
276 | indicesCountO = indicesCount; | 276 | indicesCountO = indicesCount; |
277 | indicesO = indices; | 277 | indicesO = indices; |
278 | verticesCountO = verticesCount; | 278 | verticesCountO = verticesCount; |
279 | verticesO = vertices; | 279 | verticesO = vertices; |
280 | 280 | ||
281 | return ret; | 281 | return ret; |
282 | } | 282 | } |
283 | } | 283 | } |
284 | } | 284 | } |