1 | #include "GeoMesh.h"
|
---|
2 |
|
---|
3 | using namespace Geometry;
|
---|
4 | using namespace std;
|
---|
5 |
|
---|
6 | //---------------------------------------------------------------------------
|
---|
7 | // Cosntructor.
|
---|
8 | //---------------------------------------------------------------------------
|
---|
9 | Mesh::Mesh():
|
---|
10 | mVertexBuffer(0), mSubMeshCount(0), mSubMesh(0), hasSkeleton(false)
|
---|
11 | {
|
---|
12 | mMeshBounds.maxX = 0.0;
|
---|
13 | mMeshBounds.maxY = 0.0;
|
---|
14 | mMeshBounds.maxZ = 0.0;
|
---|
15 | mMeshBounds.minX = 0.0;
|
---|
16 | mMeshBounds.minY = 0.0;
|
---|
17 | mMeshBounds.minZ = 0.0;
|
---|
18 | mMeshBounds.scaleFactor = 0.0;
|
---|
19 | }
|
---|
20 |
|
---|
21 | //---------------------------------------------------------------------------
|
---|
22 | // Destroyer.
|
---|
23 | //---------------------------------------------------------------------------
|
---|
24 | Mesh::~Mesh()
|
---|
25 | {
|
---|
26 | delete[] mSubMesh;
|
---|
27 | delete mVertexBuffer;
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | //---------------------------------------------------------------------------
|
---|
32 | // Copy constructor.
|
---|
33 | //---------------------------------------------------------------------------
|
---|
34 | Mesh::Mesh(const Mesh &objmesh)
|
---|
35 | {
|
---|
36 | // constructor copia
|
---|
37 | mVertexBuffer = new VertexBuffer();
|
---|
38 | mSubMeshCount = objmesh.mSubMeshCount;
|
---|
39 | mSubMesh = new SubMesh[objmesh.mSubMeshCount];
|
---|
40 |
|
---|
41 | // Fill up bounding box settings.
|
---|
42 | mMeshBounds.maxX = objmesh.mMeshBounds.maxX;
|
---|
43 | mMeshBounds.maxY = objmesh.mMeshBounds.maxY;
|
---|
44 | mMeshBounds.maxZ = objmesh.mMeshBounds.maxZ;
|
---|
45 | mMeshBounds.minX = objmesh.mMeshBounds.minX;
|
---|
46 | mMeshBounds.minY = objmesh.mMeshBounds.minY;
|
---|
47 | mMeshBounds.minZ = objmesh.mMeshBounds.minZ;
|
---|
48 | mMeshBounds.radius = objmesh.mMeshBounds.radius;
|
---|
49 | mMeshBounds.scaleFactor = objmesh.mMeshBounds.scaleFactor;
|
---|
50 |
|
---|
51 | // For each submesh.
|
---|
52 | for(size_t i = 0; i < objmesh.mSubMeshCount; i++)
|
---|
53 | {
|
---|
54 | mSubMesh[i].mSharedVertexBuffer = false;
|
---|
55 | mSubMesh[i].mVertexBuffer = new VertexBuffer();
|
---|
56 | mSubMesh[i].mVertexBuffer->mPosition = new Vector3[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
57 | mSubMesh[i].mVertexBuffer->mNormal = new Vector3[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
58 | mSubMesh[i].mVertexBuffer->mTexCoords = new Vector2[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
59 | mSubMesh[i].mVertexBuffer->mVertexCount = objmesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
60 | mSubMesh[i].mVertexBuffer->mVertexInfo = objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo;
|
---|
61 | mSubMesh[i].mType = objmesh.mSubMesh[i].mType;
|
---|
62 | strcpy(mSubMesh[i].mMaterialName,objmesh.mSubMesh[i].mMaterialName);
|
---|
63 |
|
---|
64 | if(objmesh.mSubMesh[i].mSharedVertexBuffer)
|
---|
65 | {
|
---|
66 | for(size_t s = 0;
|
---|
67 | s<objmesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
68 | s++)
|
---|
69 | {
|
---|
70 | // Copy mPosition.
|
---|
71 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_POSITION)
|
---|
72 | {
|
---|
73 | mSubMesh[i].mVertexBuffer->mPosition[s].x = objmesh.mVertexBuffer->mPosition[s].x;
|
---|
74 | mSubMesh[i].mVertexBuffer->mPosition[s].y = objmesh.mVertexBuffer->mPosition[s].y;
|
---|
75 | mSubMesh[i].mVertexBuffer->mPosition[s].z = objmesh.mVertexBuffer->mPosition[s].z;
|
---|
76 | }
|
---|
77 |
|
---|
78 | // Copy mNormal.
|
---|
79 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_NORMAL)
|
---|
80 | {
|
---|
81 | mSubMesh[i].mVertexBuffer->mNormal[s].x = objmesh.mVertexBuffer->mNormal[s].x;
|
---|
82 | mSubMesh[i].mVertexBuffer->mNormal[s].y = objmesh.mVertexBuffer->mNormal[s].y;
|
---|
83 | mSubMesh[i].mVertexBuffer->mNormal[s].z = objmesh.mVertexBuffer->mNormal[s].z;
|
---|
84 | }
|
---|
85 |
|
---|
86 | // Copy mTexCoords.
|
---|
87 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_TEXCOORDS)
|
---|
88 | {
|
---|
89 | mSubMesh[i].mVertexBuffer->mTexCoords[s].x = objmesh.mVertexBuffer->mTexCoords[s].x;
|
---|
90 | mSubMesh[i].mVertexBuffer->mTexCoords[s].y = objmesh.mVertexBuffer->mTexCoords[s].y;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | objmesh.mSubMesh[i].mSharedVertexBuffer = false;
|
---|
94 | }
|
---|
95 | else
|
---|
96 | {
|
---|
97 | for(size_t s = 0; s < objmesh.mSubMesh[i].mVertexBuffer->mVertexCount; s++)
|
---|
98 | {
|
---|
99 | // Copy mPosition.
|
---|
100 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_POSITION)
|
---|
101 | {
|
---|
102 | mSubMesh[i].mVertexBuffer->mPosition[s].x = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].x;
|
---|
103 | mSubMesh[i].mVertexBuffer->mPosition[s].y = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].y;
|
---|
104 | mSubMesh[i].mVertexBuffer->mPosition[s].z = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].z;
|
---|
105 | }
|
---|
106 |
|
---|
107 | //Copiamos mNormal
|
---|
108 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_NORMAL)
|
---|
109 | {
|
---|
110 | mSubMesh[i].mVertexBuffer->mNormal[s].x = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].x;
|
---|
111 | mSubMesh[i].mVertexBuffer->mNormal[s].y = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].y;
|
---|
112 | mSubMesh[i].mVertexBuffer->mNormal[s].z = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].z;
|
---|
113 | }
|
---|
114 |
|
---|
115 | //Copiamos mTexCoords
|
---|
116 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_TEXCOORDS)
|
---|
117 | {
|
---|
118 | mSubMesh[i].mVertexBuffer->mTexCoords[s].x = objmesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].x;
|
---|
119 | mSubMesh[i].mVertexBuffer->mTexCoords[s].y = objmesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].y;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | // Copy indices.
|
---|
126 | mSubMesh[i].mIndexCount = objmesh.mSubMesh[i].mIndexCount;
|
---|
127 | mSubMesh[i].mIndex = new Index[mSubMesh[i].mIndexCount];
|
---|
128 |
|
---|
129 | memcpy(mSubMesh[i].mIndex,objmesh.mSubMesh[i].mIndex,objmesh.mSubMesh[i].mIndexCount*sizeof(Index));
|
---|
130 |
|
---|
131 | // Copy strips.
|
---|
132 | __w64 int offset = 0; // offset between memory positions.
|
---|
133 | mSubMesh[i].mStripCount=objmesh.mSubMesh[i].mStripCount;
|
---|
134 | if (objmesh.mSubMesh[i].mStripCount>0)
|
---|
135 | {
|
---|
136 | mSubMesh[i].mStrip = new Index*[objmesh.mSubMesh[i].mStripCount];
|
---|
137 |
|
---|
138 | offset = &(mSubMesh[i].mIndex[0]) - &(objmesh.mSubMesh[i].mIndex[0]);
|
---|
139 | for (size_t j=0;j<objmesh.mSubMesh[i].mStripCount;j++)
|
---|
140 | {
|
---|
141 | mSubMesh[i].mStrip[j]=objmesh.mSubMesh[i].mStrip[j] + offset;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | // Copy submesh bones.
|
---|
146 | if (!objmesh.mSubMesh[i].mBones.empty())
|
---|
147 | {
|
---|
148 | for (int j = 0; j < objmesh.mSubMesh[i].mBones.size(); j++)
|
---|
149 | {
|
---|
150 | mSubMesh[i].mBones.push_back(objmesh.mSubMesh[i].mBones[j]);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | // Copy skeleton name.
|
---|
156 | if (objmesh.hasSkeleton)
|
---|
157 | {
|
---|
158 | hasSkeleton = true;
|
---|
159 |
|
---|
160 | strcpy(mSkeletonName,objmesh.mSkeletonName);
|
---|
161 | }
|
---|
162 |
|
---|
163 | // Copy mesh bones.
|
---|
164 | if (!objmesh.mBones.empty())
|
---|
165 | {
|
---|
166 | for (int j = 0; j < objmesh.mBones.size(); j++)
|
---|
167 | {
|
---|
168 | mBones.push_back(objmesh.mBones[j]);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | }
|
---|
173 |
|
---|
174 | //---------------------------------------------------------------------------
|
---|
175 | // Assignment operator.
|
---|
176 | //---------------------------------------------------------------------------
|
---|
177 | Mesh &Mesh::operator =(const Geometry::Mesh &objmesh)
|
---|
178 | {
|
---|
179 | // Operador de asignación
|
---|
180 | bool copiados = false; // indica si los vértices compartidos han sido copiados
|
---|
181 | mVertexBuffer = new VertexBuffer();
|
---|
182 | mSubMeshCount = objmesh.mSubMeshCount;
|
---|
183 | mSubMesh = new Geometry::SubMesh[objmesh.mSubMeshCount];
|
---|
184 |
|
---|
185 | // Fill up bounding box settings.
|
---|
186 | mMeshBounds.maxX = objmesh.mMeshBounds.maxX;
|
---|
187 | mMeshBounds.maxY = objmesh.mMeshBounds.maxY;
|
---|
188 | mMeshBounds.maxZ = objmesh.mMeshBounds.maxZ;
|
---|
189 | mMeshBounds.minX = objmesh.mMeshBounds.minX;
|
---|
190 | mMeshBounds.minY = objmesh.mMeshBounds.minY;
|
---|
191 | mMeshBounds.minZ = objmesh.mMeshBounds.minZ;
|
---|
192 | mMeshBounds.radius = objmesh.mMeshBounds.radius;
|
---|
193 | mMeshBounds.scaleFactor = objmesh.mMeshBounds.scaleFactor;
|
---|
194 |
|
---|
195 | // For each submesh.
|
---|
196 | for(size_t i = 0; i < objmesh.mSubMeshCount; i++)
|
---|
197 | {
|
---|
198 | mSubMesh[i].mSharedVertexBuffer = objmesh.mSubMesh[i].mSharedVertexBuffer; //.false;
|
---|
199 | mSubMesh[i].mVertexBuffer = new Geometry::VertexBuffer();
|
---|
200 | mSubMesh[i].mVertexBuffer->mPosition = new Geometry::Vector3[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
201 | mSubMesh[i].mVertexBuffer->mNormal = new Geometry::Vector3[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
202 | mSubMesh[i].mVertexBuffer->mTexCoords = new Geometry::Vector2[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
203 | mSubMesh[i].mVertexBuffer->mVertexCount = objmesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
204 | mSubMesh[i].mVertexBuffer->mVertexInfo = objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo;
|
---|
205 | mSubMesh[i].mType = objmesh.mSubMesh[i].mType;
|
---|
206 | strcpy(mSubMesh[i].mMaterialName,objmesh.mSubMesh[i].mMaterialName);
|
---|
207 |
|
---|
208 | if (objmesh.mSubMesh[i].mSharedVertexBuffer && !copiados)
|
---|
209 | {
|
---|
210 | mVertexBuffer = mSubMesh[i].mVertexBuffer;
|
---|
211 | copiados = true;
|
---|
212 | }
|
---|
213 |
|
---|
214 | for(size_t s = 0; s < objmesh.mSubMesh[i].mVertexBuffer->mVertexCount; s++)
|
---|
215 | {
|
---|
216 | //Copiamos mPosition
|
---|
217 | //if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_POSITION)
|
---|
218 | //{
|
---|
219 | mSubMesh[i].mVertexBuffer->mPosition[s].x = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].x;
|
---|
220 | mSubMesh[i].mVertexBuffer->mPosition[s].y = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].y;
|
---|
221 | mSubMesh[i].mVertexBuffer->mPosition[s].z = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].z;
|
---|
222 | //}
|
---|
223 | //Copiamos mNormal
|
---|
224 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_NORMAL)
|
---|
225 | {
|
---|
226 | mSubMesh[i].mVertexBuffer->mNormal[s].x = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].x;
|
---|
227 | mSubMesh[i].mVertexBuffer->mNormal[s].y = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].y;
|
---|
228 | mSubMesh[i].mVertexBuffer->mNormal[s].z = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].z;
|
---|
229 | }
|
---|
230 |
|
---|
231 | //Copiamos mTexCoords
|
---|
232 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_TEXCOORDS)
|
---|
233 | {
|
---|
234 | mSubMesh[i].mVertexBuffer->mTexCoords[s].x = objmesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].x;
|
---|
235 | mSubMesh[i].mVertexBuffer->mTexCoords[s].y = objmesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].y;
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | // Copiar los indices
|
---|
240 | mSubMesh[i].mIndexCount = objmesh.mSubMesh[i].mIndexCount;
|
---|
241 | mSubMesh[i].mIndex = new Index[mSubMesh[i].mIndexCount];
|
---|
242 |
|
---|
243 | memcpy( mSubMesh[i].mIndex,
|
---|
244 | objmesh.mSubMesh[i].mIndex,
|
---|
245 | objmesh.mSubMesh[i].mIndexCount*sizeof(Geometry::Index));
|
---|
246 |
|
---|
247 | // Copiar las tiras
|
---|
248 | __w64 offset = 0; // desplazamiento entre posiciones de memoria.
|
---|
249 | mSubMesh[i].mStripCount = objmesh.mSubMesh[i].mStripCount;
|
---|
250 |
|
---|
251 | if (objmesh.mSubMesh[i].mStripCount>0)
|
---|
252 | {
|
---|
253 | mSubMesh[i].mStrip = new Index*[objmesh.mSubMesh[i].mStripCount];
|
---|
254 | offset = &(mSubMesh[i].mIndex[0]) - &(objmesh.mSubMesh[i].mIndex[0]);
|
---|
255 |
|
---|
256 | for (size_t j = 0; j < objmesh.mSubMesh[i].mStripCount; j++)
|
---|
257 | {
|
---|
258 | mSubMesh[i].mStrip[j] = objmesh.mSubMesh[i].mStrip[j] + offset;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | // Copy submesh bones.
|
---|
263 | if (!objmesh.mSubMesh[i].mBones.empty())
|
---|
264 | {
|
---|
265 | for (int j = 0; j < objmesh.mSubMesh[i].mBones.size(); j++)
|
---|
266 | {
|
---|
267 | mSubMesh[i].mBones.push_back(objmesh.mSubMesh[i].mBones[j]);
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | // Copy skeleton name.
|
---|
273 | if (objmesh.hasSkeleton)
|
---|
274 | {
|
---|
275 | hasSkeleton = true;
|
---|
276 |
|
---|
277 | strcpy(mSkeletonName,objmesh.mSkeletonName);
|
---|
278 | }
|
---|
279 |
|
---|
280 | // Copy mesh bones.
|
---|
281 | if (!objmesh.mBones.empty())
|
---|
282 | {
|
---|
283 | for (int j = 0; j < objmesh.mBones.size(); j++)
|
---|
284 | {
|
---|
285 | mBones.push_back(objmesh.mBones[j]);
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | return *this;
|
---|
290 | }
|
---|
291 |
|
---|
292 | //---------------------------------------------------------------------------
|
---|
293 | // Load mesh.
|
---|
294 | //---------------------------------------------------------------------------
|
---|
295 | void Mesh::Load(Serializer &s)
|
---|
296 | {
|
---|
297 | bool sharedVertexBuffer = false;
|
---|
298 |
|
---|
299 | //Clear Data
|
---|
300 | delete[] mSubMesh; mSubMesh = 0;
|
---|
301 | delete mVertexBuffer; mVertexBuffer = 0;
|
---|
302 | mSubMeshCount = 0;
|
---|
303 |
|
---|
304 | s.ReadArray(&sharedVertexBuffer,1);
|
---|
305 | if (sharedVertexBuffer)
|
---|
306 | {
|
---|
307 | mVertexBuffer = new VertexBuffer;
|
---|
308 | mVertexBuffer->Load(s);
|
---|
309 | }
|
---|
310 | s.ReadArray(&mSubMeshCount,1);
|
---|
311 | mSubMesh = new SubMesh[mSubMeshCount];
|
---|
312 | for(size_t i = 0; i < mSubMeshCount; ++i)
|
---|
313 | {
|
---|
314 | mSubMesh[i].Load(s);
|
---|
315 | if (mSubMesh[i].mSharedVertexBuffer && sharedVertexBuffer)
|
---|
316 | {
|
---|
317 | mSubMesh[i].mVertexBuffer = mVertexBuffer;
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | //---------------------------------------------------------------------------
|
---|
323 | // Save mesh.
|
---|
324 | //---------------------------------------------------------------------------
|
---|
325 | void Mesh::Save(Serializer &s)
|
---|
326 | {
|
---|
327 | bool sharedVertexBuffer = (mVertexBuffer != 0);
|
---|
328 | s.WriteArray(&sharedVertexBuffer, 1);
|
---|
329 | if (sharedVertexBuffer)
|
---|
330 | {
|
---|
331 | mVertexBuffer->Save(s);
|
---|
332 | }
|
---|
333 | s.WriteArray(&mSubMeshCount,1);
|
---|
334 | for(size_t i = 0; i < mSubMeshCount; ++i)
|
---|
335 | {
|
---|
336 | mSubMesh[i].Save(s);
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | //---------------------------------------------------------------------------
|
---|
341 | // Export to obj mesh.
|
---|
342 | //---------------------------------------------------------------------------
|
---|
343 | void Mesh::exportToOBJ(char *nomfich)
|
---|
344 | {
|
---|
345 | // Genera un fichero obj con vértices, triángulos
|
---|
346 |
|
---|
347 | std::ofstream obj(nomfich);
|
---|
348 | obj << "begin" << std::endl;
|
---|
349 | // Vértices
|
---|
350 | for (size_t i=0; i < mSubMeshCount; i++)
|
---|
351 | {
|
---|
352 | for (size_t j=0; j < mSubMesh[i].mVertexBuffer->mVertexCount; j++)
|
---|
353 | {
|
---|
354 | obj << "v " << mSubMesh[i].mVertexBuffer->mPosition[j].x << " " <<
|
---|
355 | mSubMesh[i].mVertexBuffer->mPosition[j].y << " " <<
|
---|
356 | mSubMesh[i].mVertexBuffer->mPosition[j].z << " " << std::endl;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | // Caras
|
---|
362 | for (size_t i=0; i < mSubMeshCount; i++)
|
---|
363 | {
|
---|
364 | for (size_t j=0; j < mSubMesh[i].mIndexCount; j=j+3)
|
---|
365 | {
|
---|
366 | obj << "f " << mSubMesh[i].mIndex[j]+1 << " " <<
|
---|
367 | mSubMesh[i].mIndex[j+1]+1 << " " <<
|
---|
368 | mSubMesh[i].mIndex[j+2]+1 << std::endl;
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | obj << "end" << std::endl;
|
---|
373 | obj.close();
|
---|
374 | }
|
---|
375 |
|
---|
376 | //---------------------------------------------------------------------------
|
---|
377 | // Transform to shared vertex mesh.
|
---|
378 | //---------------------------------------------------------------------------
|
---|
379 | Mesh *Mesh::toSharedVertex()
|
---|
380 | {
|
---|
381 |
|
---|
382 | // Move all vertex to the shared vertex buffer.
|
---|
383 | Mesh *mesh = new Mesh();
|
---|
384 |
|
---|
385 | if (mSubMesh[0].mSharedVertexBuffer)
|
---|
386 | {
|
---|
387 | *mesh = *this;
|
---|
388 |
|
---|
389 | return mesh;
|
---|
390 | }
|
---|
391 |
|
---|
392 | mesh->mVertexBuffer = new VertexBuffer();
|
---|
393 | mesh->mSubMeshCount = mSubMeshCount;
|
---|
394 |
|
---|
395 | // Reserva memoria para los submeshes
|
---|
396 | mesh->mSubMesh = new SubMesh[mSubMeshCount];
|
---|
397 |
|
---|
398 | // Fill up bounding box settings.
|
---|
399 | mesh->mMeshBounds.maxX = mMeshBounds.maxX;
|
---|
400 | mesh->mMeshBounds.maxY = mMeshBounds.maxY;
|
---|
401 | mesh->mMeshBounds.maxZ = mMeshBounds.maxZ;
|
---|
402 | mesh->mMeshBounds.minX = mMeshBounds.minX;
|
---|
403 | mesh->mMeshBounds.minY = mMeshBounds.minY;
|
---|
404 | mesh->mMeshBounds.minZ = mMeshBounds.minZ;
|
---|
405 | mesh->mMeshBounds.radius = mMeshBounds.radius;
|
---|
406 | mesh->mMeshBounds.scaleFactor = mMeshBounds.scaleFactor;
|
---|
407 |
|
---|
408 | // construcción de los submeshes
|
---|
409 | long int acumVerts = 0;
|
---|
410 |
|
---|
411 | for (size_t i = 0; i < mesh->mSubMeshCount; i++)
|
---|
412 | {
|
---|
413 | mesh->mSubMesh[i].mSharedVertexBuffer = true;
|
---|
414 | mesh->mSubMesh[i].mVertexBuffer = mesh->mVertexBuffer;
|
---|
415 | mesh->mSubMesh[i].mStripCount = 0;
|
---|
416 | mesh->mSubMesh[i].mStrip = NULL;
|
---|
417 | mesh->mSubMesh[i].mType = mSubMesh[i].mType;
|
---|
418 |
|
---|
419 | strcpy(mesh->mSubMesh[i].mMaterialName,mSubMesh[i].mMaterialName);
|
---|
420 |
|
---|
421 | // copiar los índices
|
---|
422 | mesh->mSubMesh[i].mIndexCount = mSubMesh[i].mIndexCount;
|
---|
423 | mesh->mSubMesh[i].mIndex = new Index[mSubMesh[i].mIndexCount];
|
---|
424 |
|
---|
425 | for (size_t j = 0; j < mSubMesh[i].mIndexCount; j++)
|
---|
426 | {
|
---|
427 | mesh->mSubMesh[i].mIndex[j] = mSubMesh[i].mIndex[j]+acumVerts;
|
---|
428 | }
|
---|
429 |
|
---|
430 | acumVerts += mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
431 |
|
---|
432 | // Copiar las tiras
|
---|
433 | int offset = 0; // desplazamiento entre posiciones de memoria.
|
---|
434 |
|
---|
435 | mesh->mSubMesh[i].mStripCount = mSubMesh[i].mStripCount;
|
---|
436 |
|
---|
437 | if (mSubMesh[i].mStripCount > 0)
|
---|
438 | {
|
---|
439 | mesh->mSubMesh[i].mStrip = new Index*[mSubMesh[i].mStripCount];
|
---|
440 |
|
---|
441 | offset = &(mesh->mSubMesh[i].mIndex[0]) - &(mSubMesh[i].mIndex[0]);
|
---|
442 |
|
---|
443 | for (size_t j = 0; j < mSubMesh[i].mStripCount; j++)
|
---|
444 | {
|
---|
445 | mesh->mSubMesh[i].mStrip[j] = mSubMesh[i].mStrip[j] + offset;
|
---|
446 | }
|
---|
447 | }
|
---|
448 | }
|
---|
449 |
|
---|
450 | mesh->mVertexBuffer->mVertexCount = acumVerts;
|
---|
451 | mesh->mVertexBuffer->mVertexInfo = mSubMesh[0].mVertexBuffer->mVertexInfo;
|
---|
452 |
|
---|
453 | mesh->mVertexBuffer->mPosition = new Vector3[mesh->mVertexBuffer->mVertexCount];
|
---|
454 | mesh->mVertexBuffer->mNormal = new Vector3[mesh->mVertexBuffer->mVertexCount];
|
---|
455 | mesh->mVertexBuffer->mTexCoords = new Vector2[mesh->mVertexBuffer->mVertexCount];
|
---|
456 |
|
---|
457 | // copiar los vértices
|
---|
458 | acumVerts = 0;
|
---|
459 |
|
---|
460 | size_t newIndex;
|
---|
461 |
|
---|
462 | for (size_t i = 0; i < mSubMeshCount; i++)
|
---|
463 | {
|
---|
464 | for(size_t j = 0; j < mSubMesh[i].mVertexBuffer->mVertexCount; j++)
|
---|
465 | {
|
---|
466 | newIndex = acumVerts + j;
|
---|
467 |
|
---|
468 | mesh->mVertexBuffer->
|
---|
469 | mPosition[newIndex].x = mSubMesh[i].mVertexBuffer->
|
---|
470 | mPosition[j].x;
|
---|
471 |
|
---|
472 | mesh->mVertexBuffer->
|
---|
473 | mPosition[newIndex].y = mSubMesh[i].mVertexBuffer->
|
---|
474 | mPosition[j].y;
|
---|
475 |
|
---|
476 | mesh->mVertexBuffer->
|
---|
477 | mPosition[newIndex].z = mSubMesh[i].mVertexBuffer->
|
---|
478 | mPosition[j].z;
|
---|
479 |
|
---|
480 | mesh->mVertexBuffer->
|
---|
481 | mNormal[newIndex].x = mSubMesh[i].mVertexBuffer->
|
---|
482 | mNormal[j].x;
|
---|
483 |
|
---|
484 | mesh->mVertexBuffer->
|
---|
485 | mNormal[newIndex].y = mSubMesh[i].mVertexBuffer->
|
---|
486 | mNormal[j].y;
|
---|
487 |
|
---|
488 | mesh->mVertexBuffer->
|
---|
489 | mNormal[newIndex].z = mSubMesh[i].mVertexBuffer->
|
---|
490 | mNormal[j].z;
|
---|
491 |
|
---|
492 | mesh->mVertexBuffer->
|
---|
493 | mTexCoords[newIndex].x = mSubMesh[i].mVertexBuffer->
|
---|
494 | mTexCoords[j].x;
|
---|
495 |
|
---|
496 | mesh->mVertexBuffer->
|
---|
497 | mTexCoords[newIndex].y = mSubMesh[i].mVertexBuffer->
|
---|
498 | mTexCoords[j].y;
|
---|
499 | }
|
---|
500 |
|
---|
501 | acumVerts += mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
502 | }
|
---|
503 |
|
---|
504 | return mesh;
|
---|
505 | }
|
---|
506 |
|
---|