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 | // Copy constructor.
|
---|
32 | //-------------------------------------------------------------------------
|
---|
33 | Mesh::Mesh(const Mesh &mesh)
|
---|
34 | {
|
---|
35 | mVertexBuffer = new VertexBuffer();
|
---|
36 | mSubMeshCount = mesh.mSubMeshCount;
|
---|
37 | mSubMesh = new SubMesh[mesh.mSubMeshCount];
|
---|
38 |
|
---|
39 | // Fill up bounding box settings.
|
---|
40 | mMeshBounds.maxX = mesh.mMeshBounds.maxX;
|
---|
41 | mMeshBounds.maxY = mesh.mMeshBounds.maxY;
|
---|
42 | mMeshBounds.maxZ = mesh.mMeshBounds.maxZ;
|
---|
43 | mMeshBounds.minX = mesh.mMeshBounds.minX;
|
---|
44 | mMeshBounds.minY = mesh.mMeshBounds.minY;
|
---|
45 | mMeshBounds.minZ = mesh.mMeshBounds.minZ;
|
---|
46 | mMeshBounds.radius = mesh.mMeshBounds.radius;
|
---|
47 | mMeshBounds.scaleFactor = mesh.mMeshBounds.scaleFactor;
|
---|
48 |
|
---|
49 | // For each submesh.
|
---|
50 | for (size_t i = 0; i < mesh.mSubMeshCount; i++)
|
---|
51 | {
|
---|
52 | mSubMesh[i].mSharedVertexBuffer = false;
|
---|
53 | mSubMesh[i].mVertexBuffer = new VertexBuffer();
|
---|
54 | mSubMesh[i].mVertexBuffer->mPosition = new Vector3[mesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
55 | mSubMesh[i].mVertexBuffer->mNormal = new Vector3[mesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
56 | mSubMesh[i].mVertexBuffer->mTexCoords = new Vector2[mesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
57 | mSubMesh[i].mVertexBuffer->mVertexCount = mesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
58 | mSubMesh[i].mVertexBuffer->mVertexInfo = mesh.mSubMesh[i].mVertexBuffer->mVertexInfo;
|
---|
59 | mSubMesh[i].mType = mesh.mSubMesh[i].mType;
|
---|
60 |
|
---|
61 | strcpy(mSubMesh[i].mName,mesh.mSubMesh[i].mName);
|
---|
62 | strcpy(mSubMesh[i].mMaterialName,mesh.mSubMesh[i].mMaterialName);
|
---|
63 |
|
---|
64 | if (mesh.mSubMesh[i].mSharedVertexBuffer)
|
---|
65 | {
|
---|
66 | for (size_t s = 0;
|
---|
67 | s < mesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
68 | s++)
|
---|
69 | {
|
---|
70 | if (mesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_POSITION)
|
---|
71 | {
|
---|
72 | mSubMesh[i].mVertexBuffer->mPosition[s].x = mesh.mVertexBuffer->mPosition[s].x;
|
---|
73 | mSubMesh[i].mVertexBuffer->mPosition[s].y = mesh.mVertexBuffer->mPosition[s].y;
|
---|
74 | mSubMesh[i].mVertexBuffer->mPosition[s].z = mesh.mVertexBuffer->mPosition[s].z;
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (mesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_NORMAL)
|
---|
78 | {
|
---|
79 | mSubMesh[i].mVertexBuffer->mNormal[s].x = mesh.mVertexBuffer->mNormal[s].x;
|
---|
80 | mSubMesh[i].mVertexBuffer->mNormal[s].y = mesh.mVertexBuffer->mNormal[s].y;
|
---|
81 | mSubMesh[i].mVertexBuffer->mNormal[s].z = mesh.mVertexBuffer->mNormal[s].z;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (mesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_TEXCOORDS)
|
---|
85 | {
|
---|
86 | mSubMesh[i].mVertexBuffer->mTexCoords[s].x = mesh.mVertexBuffer->mTexCoords[s].x;
|
---|
87 | mSubMesh[i].mVertexBuffer->mTexCoords[s].y = mesh.mVertexBuffer->mTexCoords[s].y;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | mesh.mSubMesh[i].mSharedVertexBuffer = false;
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | for (size_t s = 0; s < mesh.mSubMesh[i].mVertexBuffer->mVertexCount; s++)
|
---|
96 | {
|
---|
97 | if (mesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_POSITION)
|
---|
98 | {
|
---|
99 | mSubMesh[i].mVertexBuffer->mPosition[s].x = mesh.mSubMesh[i].mVertexBuffer->mPosition[s].x;
|
---|
100 | mSubMesh[i].mVertexBuffer->mPosition[s].y = mesh.mSubMesh[i].mVertexBuffer->mPosition[s].y;
|
---|
101 | mSubMesh[i].mVertexBuffer->mPosition[s].z = mesh.mSubMesh[i].mVertexBuffer->mPosition[s].z;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (mesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_NORMAL)
|
---|
105 | {
|
---|
106 | mSubMesh[i].mVertexBuffer->mNormal[s].x = mesh.mSubMesh[i].mVertexBuffer->mNormal[s].x;
|
---|
107 | mSubMesh[i].mVertexBuffer->mNormal[s].y = mesh.mSubMesh[i].mVertexBuffer->mNormal[s].y;
|
---|
108 | mSubMesh[i].mVertexBuffer->mNormal[s].z = mesh.mSubMesh[i].mVertexBuffer->mNormal[s].z;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (mesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_TEXCOORDS)
|
---|
112 | {
|
---|
113 | mSubMesh[i].mVertexBuffer->mTexCoords[s].x = mesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].x;
|
---|
114 | mSubMesh[i].mVertexBuffer->mTexCoords[s].y = mesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].y;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | }
|
---|
119 |
|
---|
120 | // Copy indices.
|
---|
121 | mSubMesh[i].mIndexCount = mesh.mSubMesh[i].mIndexCount;
|
---|
122 | mSubMesh[i].mIndex = new Index[mSubMesh[i].mIndexCount];
|
---|
123 |
|
---|
124 | memcpy( mSubMesh[i].mIndex,
|
---|
125 | mesh.mSubMesh[i].mIndex,
|
---|
126 | mesh.mSubMesh[i].mIndexCount * sizeof(Index));
|
---|
127 |
|
---|
128 | // Copy strips.
|
---|
129 | // offset between memory positions.
|
---|
130 | __w64 int offset = 0;
|
---|
131 | mSubMesh[i].mStripCount = mesh.mSubMesh[i].mStripCount;
|
---|
132 |
|
---|
133 | if (mesh.mSubMesh[i].mStripCount > 0)
|
---|
134 | {
|
---|
135 | mSubMesh[i].mStrip = new Index*[mesh.mSubMesh[i].mStripCount];
|
---|
136 |
|
---|
137 | offset = &(mSubMesh[i].mIndex[0]) - &(mesh.mSubMesh[i].mIndex[0]);
|
---|
138 |
|
---|
139 | for (size_t j = 0; j < mesh.mSubMesh[i].mStripCount; j++)
|
---|
140 | {
|
---|
141 | mSubMesh[i].mStrip[j] = mesh.mSubMesh[i].mStrip[j] + offset;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | // Copy submesh bones.
|
---|
146 | if (!mesh.mSubMesh[i].mBones.empty())
|
---|
147 | {
|
---|
148 | for (unsigned int j = 0; j < mesh.mSubMesh[i].mBones.size(); j++)
|
---|
149 | {
|
---|
150 | mSubMesh[i].mBones.push_back(mesh.mSubMesh[i].mBones[j]);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | // Copy skeleton name.
|
---|
156 | if (mesh.hasSkeleton)
|
---|
157 | {
|
---|
158 | hasSkeleton = true;
|
---|
159 |
|
---|
160 | strcpy(mSkeletonName, mesh.mSkeletonName);
|
---|
161 | }
|
---|
162 |
|
---|
163 | // Copy mesh bones.
|
---|
164 | if (!mesh.mBones.empty())
|
---|
165 | {
|
---|
166 | for (unsigned int j = 0; j < mesh.mBones.size(); j++)
|
---|
167 | {
|
---|
168 | mBones.push_back(mesh.mBones[j]);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | }
|
---|
173 |
|
---|
174 | //-------------------------------------------------------------------------
|
---|
175 | // Assignment operator.
|
---|
176 | //-------------------------------------------------------------------------
|
---|
177 | Mesh &Mesh::operator =(const Mesh &mesh)
|
---|
178 | {
|
---|
179 | bool copied = false;
|
---|
180 | mVertexBuffer = new VertexBuffer();
|
---|
181 | mSubMeshCount = mesh.mSubMeshCount;
|
---|
182 | mSubMesh = new Geometry::SubMesh[mesh.mSubMeshCount];
|
---|
183 |
|
---|
184 | // Fill up bounding box settings.
|
---|
185 | mMeshBounds.maxX = mesh.mMeshBounds.maxX;
|
---|
186 | mMeshBounds.maxY = mesh.mMeshBounds.maxY;
|
---|
187 | mMeshBounds.maxZ = mesh.mMeshBounds.maxZ;
|
---|
188 | mMeshBounds.minX = mesh.mMeshBounds.minX;
|
---|
189 | mMeshBounds.minY = mesh.mMeshBounds.minY;
|
---|
190 | mMeshBounds.minZ = mesh.mMeshBounds.minZ;
|
---|
191 | mMeshBounds.radius = mesh.mMeshBounds.radius;
|
---|
192 | mMeshBounds.scaleFactor = mesh.mMeshBounds.scaleFactor;
|
---|
193 |
|
---|
194 | // For each submesh.
|
---|
195 | for (size_t i = 0; i < mesh.mSubMeshCount; i++)
|
---|
196 | {
|
---|
197 | mSubMesh[i].mSharedVertexBuffer = mesh.mSubMesh[i].mSharedVertexBuffer; //.false;
|
---|
198 | mSubMesh[i].mVertexBuffer = new Geometry::VertexBuffer();
|
---|
199 | mSubMesh[i].mVertexBuffer->mPosition = new Geometry::Vector3[mesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
200 | mSubMesh[i].mVertexBuffer->mNormal = new Geometry::Vector3[mesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
201 | mSubMesh[i].mVertexBuffer->mTexCoords = new Geometry::Vector2[mesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
202 | mSubMesh[i].mVertexBuffer->mVertexCount = mesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
203 | mSubMesh[i].mVertexBuffer->mVertexInfo = mesh.mSubMesh[i].mVertexBuffer->mVertexInfo;
|
---|
204 | mSubMesh[i].mType = mesh.mSubMesh[i].mType;
|
---|
205 |
|
---|
206 | strcpy(mSubMesh[i].mName,mesh.mSubMesh[i].mName);
|
---|
207 | strcpy(mSubMesh[i].mMaterialName,mesh.mSubMesh[i].mMaterialName);
|
---|
208 |
|
---|
209 | if (mesh.mSubMesh[i].mSharedVertexBuffer && !copied)
|
---|
210 | {
|
---|
211 | mVertexBuffer = mSubMesh[i].mVertexBuffer;
|
---|
212 | copied = true;
|
---|
213 | }
|
---|
214 |
|
---|
215 | for ( size_t s = 0;
|
---|
216 | s < mesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
217 | s++)
|
---|
218 | {
|
---|
219 | mSubMesh[i].mVertexBuffer->mPosition[s].x = mesh.mSubMesh[i].mVertexBuffer->mPosition[s].x;
|
---|
220 | mSubMesh[i].mVertexBuffer->mPosition[s].y = mesh.mSubMesh[i].mVertexBuffer->mPosition[s].y;
|
---|
221 | mSubMesh[i].mVertexBuffer->mPosition[s].z = mesh.mSubMesh[i].mVertexBuffer->mPosition[s].z;
|
---|
222 |
|
---|
223 | if (mesh.mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_NORMAL)
|
---|
224 | {
|
---|
225 | mSubMesh[i].mVertexBuffer->mNormal[s].x = mesh.mSubMesh[i].mVertexBuffer->mNormal[s].x;
|
---|
226 | mSubMesh[i].mVertexBuffer->mNormal[s].y = mesh.mSubMesh[i].mVertexBuffer->mNormal[s].y;
|
---|
227 | mSubMesh[i].mVertexBuffer->mNormal[s].z = mesh.mSubMesh[i].mVertexBuffer->mNormal[s].z;
|
---|
228 | }
|
---|
229 |
|
---|
230 | if (mesh.mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_TEXCOORDS)
|
---|
231 | {
|
---|
232 | mSubMesh[i].mVertexBuffer->mTexCoords[s].x = mesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].x;
|
---|
233 | mSubMesh[i].mVertexBuffer->mTexCoords[s].y = mesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].y;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | // Copy indices.
|
---|
238 | mSubMesh[i].mIndexCount = mesh.mSubMesh[i].mIndexCount;
|
---|
239 | mSubMesh[i].mIndex = new Index[mSubMesh[i].mIndexCount];
|
---|
240 |
|
---|
241 | memcpy( mSubMesh[i].mIndex,
|
---|
242 | mesh.mSubMesh[i].mIndex,
|
---|
243 | mesh.mSubMesh[i].mIndexCount*sizeof(Geometry::Index));
|
---|
244 |
|
---|
245 | // Copy strips.
|
---|
246 | __w64 offset = 0;
|
---|
247 | mSubMesh[i].mStripCount = mesh.mSubMesh[i].mStripCount;
|
---|
248 |
|
---|
249 | if (mesh.mSubMesh[i].mStripCount > 0)
|
---|
250 | {
|
---|
251 | mSubMesh[i].mStrip = new Index*[mesh.mSubMesh[i].mStripCount];
|
---|
252 |
|
---|
253 | offset = &(mSubMesh[i].mIndex[0])
|
---|
254 | -
|
---|
255 | &(mesh.mSubMesh[i].mIndex[0]);
|
---|
256 |
|
---|
257 | for (size_t j = 0; j < mesh.mSubMesh[i].mStripCount; j++)
|
---|
258 | {
|
---|
259 | mSubMesh[i].mStrip[j] = mesh.mSubMesh[i].mStrip[j] + offset;
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | // Copy submesh bones.
|
---|
264 | if (!mesh.mSubMesh[i].mBones.empty())
|
---|
265 | {
|
---|
266 | for (unsigned int j = 0; j < mesh.mSubMesh[i].mBones.size(); j++)
|
---|
267 | {
|
---|
268 | mSubMesh[i].mBones.push_back(mesh.mSubMesh[i].mBones[j]);
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | // Copy skeleton name.
|
---|
274 | if (mesh.hasSkeleton)
|
---|
275 | {
|
---|
276 | hasSkeleton = true;
|
---|
277 |
|
---|
278 | strcpy(mSkeletonName, mesh.mSkeletonName);
|
---|
279 | }
|
---|
280 |
|
---|
281 | // Copy mesh bones.
|
---|
282 | if (!mesh.mBones.empty())
|
---|
283 | {
|
---|
284 | for (unsigned int j = 0; j < mesh.mBones.size(); j++)
|
---|
285 | {
|
---|
286 | mBones.push_back(mesh.mBones[j]);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | return *this;
|
---|
291 | }
|
---|
292 |
|
---|
293 | //-------------------------------------------------------------------------
|
---|
294 | // Load mesh.
|
---|
295 | //-------------------------------------------------------------------------
|
---|
296 | void Mesh::Load(Serializer &s)
|
---|
297 | {
|
---|
298 | bool sharedVertexBuffer = false;
|
---|
299 |
|
---|
300 | // Clear Data.
|
---|
301 | delete [] mSubMesh;
|
---|
302 |
|
---|
303 | mSubMesh = 0;
|
---|
304 |
|
---|
305 | delete mVertexBuffer;
|
---|
306 |
|
---|
307 | mVertexBuffer = 0;
|
---|
308 | mSubMeshCount = 0;
|
---|
309 |
|
---|
310 | s.ReadArray(&sharedVertexBuffer,1);
|
---|
311 |
|
---|
312 | if (sharedVertexBuffer)
|
---|
313 | {
|
---|
314 | mVertexBuffer = new VertexBuffer;
|
---|
315 | mVertexBuffer->Load(s);
|
---|
316 | }
|
---|
317 |
|
---|
318 | s.ReadArray(&mSubMeshCount, 1);
|
---|
319 |
|
---|
320 | mSubMesh = new SubMesh[mSubMeshCount];
|
---|
321 |
|
---|
322 | for(size_t i = 0; i < mSubMeshCount; i++)
|
---|
323 | {
|
---|
324 | mSubMesh[i].Load(s);
|
---|
325 |
|
---|
326 | if (mSubMesh[i].mSharedVertexBuffer && sharedVertexBuffer)
|
---|
327 | {
|
---|
328 | mSubMesh[i].mVertexBuffer = mVertexBuffer;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | //-------------------------------------------------------------------------
|
---|
334 | // Save mesh.
|
---|
335 | //-------------------------------------------------------------------------
|
---|
336 | void Mesh::Save(Serializer &s)
|
---|
337 | {
|
---|
338 | bool sharedVertexBuffer = (mVertexBuffer != 0);
|
---|
339 |
|
---|
340 | s.WriteArray(&sharedVertexBuffer, 1);
|
---|
341 |
|
---|
342 | if (sharedVertexBuffer)
|
---|
343 | {
|
---|
344 | mVertexBuffer->Save(s);
|
---|
345 | }
|
---|
346 |
|
---|
347 | s.WriteArray(&mSubMeshCount,1);
|
---|
348 |
|
---|
349 | for(size_t i = 0; i < mSubMeshCount; i++)
|
---|
350 | {
|
---|
351 | mSubMesh[i].Save(s);
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | //-------------------------------------------------------------------------
|
---|
356 | // Export to obj mesh.
|
---|
357 | //-------------------------------------------------------------------------
|
---|
358 | void Mesh::exportToOBJ(char *fileName)
|
---|
359 | {
|
---|
360 | size_t vertex_count;
|
---|
361 |
|
---|
362 | vertex_count = 0;
|
---|
363 |
|
---|
364 | std::ofstream obj(fileName);
|
---|
365 |
|
---|
366 | obj << "begin" << std::endl;
|
---|
367 |
|
---|
368 | for (size_t j = 0; j < mVertexBuffer->mVertexCount; j++)
|
---|
369 | {
|
---|
370 | obj << "v "
|
---|
371 | << mVertexBuffer->mPosition[j].x
|
---|
372 | << " "
|
---|
373 | << mVertexBuffer->mPosition[j].y
|
---|
374 | << " "
|
---|
375 | << mVertexBuffer->mPosition[j].z
|
---|
376 | << " "
|
---|
377 | << std::endl;
|
---|
378 | }
|
---|
379 |
|
---|
380 | // Vertices.
|
---|
381 | for (size_t i = 0; i < mSubMeshCount; i++)
|
---|
382 | {
|
---|
383 | if (!mSubMesh[i].mSharedVertexBuffer)
|
---|
384 | {
|
---|
385 | for (size_t j = 0; j < mSubMesh[i].mVertexBuffer->mVertexCount; j++)
|
---|
386 | {
|
---|
387 | obj << "v "
|
---|
388 | << mSubMesh[i].mVertexBuffer->mPosition[j].x
|
---|
389 | << " "
|
---|
390 | << mSubMesh[i].mVertexBuffer->mPosition[j].y
|
---|
391 | << " "
|
---|
392 | << mSubMesh[i].mVertexBuffer->mPosition[j].z
|
---|
393 | << " "
|
---|
394 | << std::endl;
|
---|
395 | }
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | // Normals.
|
---|
400 | for (size_t j = 0; j < mVertexBuffer->mVertexCount; j++)
|
---|
401 | {
|
---|
402 | obj << "vn "
|
---|
403 | << mVertexBuffer->mNormal[j].x
|
---|
404 | << " "
|
---|
405 | << mVertexBuffer->mNormal[j].y
|
---|
406 | << " "
|
---|
407 | << mVertexBuffer->mNormal[j].z
|
---|
408 | << " "
|
---|
409 | << std::endl;
|
---|
410 | }
|
---|
411 |
|
---|
412 | for (size_t i = 0; i < mSubMeshCount; i++)
|
---|
413 | {
|
---|
414 | if (!mSubMesh[i].mSharedVertexBuffer)
|
---|
415 | {
|
---|
416 | for (size_t j = 0; j < mSubMesh[i].mVertexBuffer->mVertexCount; j++)
|
---|
417 | {
|
---|
418 | obj << "vn "
|
---|
419 | << mSubMesh[i].mVertexBuffer->mNormal[j].x
|
---|
420 | << " "
|
---|
421 | << mSubMesh[i].mVertexBuffer->mNormal[j].y
|
---|
422 | << " "
|
---|
423 | << mSubMesh[i].mVertexBuffer->mNormal[j].z
|
---|
424 | << " "
|
---|
425 | << std::endl;
|
---|
426 | }
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | // tex coords.
|
---|
431 | for (size_t j = 0; j < mVertexBuffer->mVertexCount; j++)
|
---|
432 | {
|
---|
433 | obj << "vt "
|
---|
434 | << mVertexBuffer->mTexCoords[j].x
|
---|
435 | << " "
|
---|
436 | << mVertexBuffer->mTexCoords[j].y
|
---|
437 | << " "
|
---|
438 | << std::endl;
|
---|
439 | }
|
---|
440 |
|
---|
441 | for (size_t i = 0; i < mSubMeshCount; i++)
|
---|
442 | {
|
---|
443 | if (!mSubMesh[i].mSharedVertexBuffer)
|
---|
444 | {
|
---|
445 | for (size_t j = 0; j < mSubMesh[i].mVertexBuffer->mVertexCount; j++)
|
---|
446 | {
|
---|
447 | obj << "vt "
|
---|
448 | << mSubMesh[i].mVertexBuffer->mTexCoords[j].x
|
---|
449 | << " "
|
---|
450 | << mSubMesh[i].mVertexBuffer->mTexCoords[j].y
|
---|
451 | << " "
|
---|
452 | << std::endl;
|
---|
453 | }
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 |
|
---|
458 | // Faces.
|
---|
459 | for (size_t i = 0; i < mSubMeshCount; i++)
|
---|
460 | {
|
---|
461 | for (size_t j = 0; j < mSubMesh[i].mIndexCount; j = j + 3)
|
---|
462 | {
|
---|
463 | int a = mSubMesh[i].mIndex[j] + 1 + vertex_count;
|
---|
464 | int b = mSubMesh[i].mIndex[j+1] + 1 + vertex_count;
|
---|
465 | int c = mSubMesh[i].mIndex[j+2] + 1 + vertex_count;
|
---|
466 | obj << "f " << a << "/" << a << "/" << a << " "
|
---|
467 | << b << "/" << b << "/" << b << " "
|
---|
468 | << c << "/" << c << "/" << c << " "
|
---|
469 | << std::endl;
|
---|
470 |
|
---|
471 | }
|
---|
472 | vertex_count += mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
473 | }
|
---|
474 |
|
---|
475 | obj << "end" << std::endl;
|
---|
476 |
|
---|
477 | obj.close();
|
---|
478 | }
|
---|
479 |
|
---|
480 | //-------------------------------------------------------------------------
|
---|
481 | // Transform to shared vertex mesh.
|
---|
482 | //-------------------------------------------------------------------------
|
---|
483 | Mesh *Mesh::toSharedVertex()
|
---|
484 | {
|
---|
485 | // Move all vertex to the shared vertex buffer.
|
---|
486 | Mesh *mesh = new Mesh();
|
---|
487 |
|
---|
488 | if (mSubMesh[0].mSharedVertexBuffer)
|
---|
489 | {
|
---|
490 | *mesh = *this;
|
---|
491 |
|
---|
492 | return mesh;
|
---|
493 | }
|
---|
494 |
|
---|
495 | mesh->mVertexBuffer = new VertexBuffer();
|
---|
496 | mesh->mSubMeshCount = mSubMeshCount;
|
---|
497 | mesh->mSubMesh = new SubMesh[mSubMeshCount];
|
---|
498 |
|
---|
499 | // Fill up bounding box settings.
|
---|
500 | mesh->mMeshBounds.maxX = mMeshBounds.maxX;
|
---|
501 | mesh->mMeshBounds.maxY = mMeshBounds.maxY;
|
---|
502 | mesh->mMeshBounds.maxZ = mMeshBounds.maxZ;
|
---|
503 | mesh->mMeshBounds.minX = mMeshBounds.minX;
|
---|
504 | mesh->mMeshBounds.minY = mMeshBounds.minY;
|
---|
505 | mesh->mMeshBounds.minZ = mMeshBounds.minZ;
|
---|
506 | mesh->mMeshBounds.radius = mMeshBounds.radius;
|
---|
507 | mesh->mMeshBounds.scaleFactor = mMeshBounds.scaleFactor;
|
---|
508 |
|
---|
509 | // construcción de los submeshes
|
---|
510 | long int acumVerts = 0;
|
---|
511 |
|
---|
512 | for (size_t i = 0; i < mesh->mSubMeshCount; i++)
|
---|
513 | {
|
---|
514 | mesh->mSubMesh[i].mSharedVertexBuffer = true;
|
---|
515 | mesh->mSubMesh[i].mVertexBuffer = mesh->mVertexBuffer;
|
---|
516 | mesh->mSubMesh[i].mStripCount = 0;
|
---|
517 | mesh->mSubMesh[i].mStrip = NULL;
|
---|
518 | mesh->mSubMesh[i].mType = mSubMesh[i].mType;
|
---|
519 |
|
---|
520 | strcpy(mesh->mSubMesh[i].mName,mSubMesh[i].mName);
|
---|
521 | strcpy(mesh->mSubMesh[i].mMaterialName,mSubMesh[i].mMaterialName);
|
---|
522 |
|
---|
523 | // Copy indices.
|
---|
524 | mesh->mSubMesh[i].mIndexCount = mSubMesh[i].mIndexCount;
|
---|
525 | mesh->mSubMesh[i].mIndex = new Index[mSubMesh[i].mIndexCount];
|
---|
526 |
|
---|
527 | for (size_t j = 0; j < mSubMesh[i].mIndexCount; j++)
|
---|
528 | {
|
---|
529 | mesh->mSubMesh[i].mIndex[j] = mSubMesh[i].mIndex[j]+acumVerts;
|
---|
530 | }
|
---|
531 |
|
---|
532 | acumVerts += long(mSubMesh[i].mVertexBuffer->mVertexCount);
|
---|
533 |
|
---|
534 | // Copy strips.
|
---|
535 | int offset = 0;
|
---|
536 |
|
---|
537 | mesh->mSubMesh[i].mStripCount = mSubMesh[i].mStripCount;
|
---|
538 |
|
---|
539 | if (mSubMesh[i].mStripCount > 0)
|
---|
540 | {
|
---|
541 | mesh->mSubMesh[i].mStrip = new Index*[mSubMesh[i].mStripCount];
|
---|
542 |
|
---|
543 | offset = int(&(mesh->mSubMesh[i].mIndex[0]) - &(mSubMesh[i].mIndex[0]));
|
---|
544 |
|
---|
545 | for (size_t j = 0; j < mSubMesh[i].mStripCount; j++)
|
---|
546 | {
|
---|
547 | mesh->mSubMesh[i].mStrip[j] = mSubMesh[i].mStrip[j] + offset;
|
---|
548 | }
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | mesh->mVertexBuffer->mVertexCount = acumVerts;
|
---|
553 | mesh->mVertexBuffer->mVertexInfo = mSubMesh[0].mVertexBuffer->mVertexInfo;
|
---|
554 |
|
---|
555 | mesh->mVertexBuffer->mPosition = new Vector3[mesh->mVertexBuffer->mVertexCount];
|
---|
556 | mesh->mVertexBuffer->mNormal = new Vector3[mesh->mVertexBuffer->mVertexCount];
|
---|
557 | mesh->mVertexBuffer->mTexCoords = new Vector2[mesh->mVertexBuffer->mVertexCount];
|
---|
558 |
|
---|
559 | acumVerts = 0;
|
---|
560 |
|
---|
561 | size_t newIndex;
|
---|
562 |
|
---|
563 | for (size_t i = 0; i < mSubMeshCount; i++)
|
---|
564 | {
|
---|
565 | for (size_t j = 0; j < mSubMesh[i].mVertexBuffer->mVertexCount; j++)
|
---|
566 | {
|
---|
567 | newIndex = acumVerts + j;
|
---|
568 |
|
---|
569 | mesh->mVertexBuffer->
|
---|
570 | mPosition[newIndex].x = mSubMesh[i].mVertexBuffer->
|
---|
571 | mPosition[j].x;
|
---|
572 |
|
---|
573 | mesh->mVertexBuffer->
|
---|
574 | mPosition[newIndex].y = mSubMesh[i].mVertexBuffer->
|
---|
575 | mPosition[j].y;
|
---|
576 |
|
---|
577 | mesh->mVertexBuffer->
|
---|
578 | mPosition[newIndex].z = mSubMesh[i].mVertexBuffer->
|
---|
579 | mPosition[j].z;
|
---|
580 |
|
---|
581 | mesh->mVertexBuffer->
|
---|
582 | mNormal[newIndex].x = mSubMesh[i].mVertexBuffer->
|
---|
583 | mNormal[j].x;
|
---|
584 |
|
---|
585 | mesh->mVertexBuffer->
|
---|
586 | mNormal[newIndex].y = mSubMesh[i].mVertexBuffer->
|
---|
587 | mNormal[j].y;
|
---|
588 |
|
---|
589 | mesh->mVertexBuffer->
|
---|
590 | mNormal[newIndex].z = mSubMesh[i].mVertexBuffer->
|
---|
591 | mNormal[j].z;
|
---|
592 |
|
---|
593 | mesh->mVertexBuffer->
|
---|
594 | mTexCoords[newIndex].x = mSubMesh[i].mVertexBuffer->
|
---|
595 | mTexCoords[j].x;
|
---|
596 |
|
---|
597 | mesh->mVertexBuffer->
|
---|
598 | mTexCoords[newIndex].y = mSubMesh[i].mVertexBuffer->
|
---|
599 | mTexCoords[j].y;
|
---|
600 | }
|
---|
601 |
|
---|
602 | // Shared bones.
|
---|
603 | if (!mSubMesh[i].mBones.empty())
|
---|
604 | {
|
---|
605 | for (unsigned int j = 0; j < mSubMesh[i].mBones.size(); j++)
|
---|
606 | {
|
---|
607 | mesh->mBones.push_back(mSubMesh[i].mBones[j]);
|
---|
608 |
|
---|
609 | mesh->mBones.back().vertexIndex += acumVerts;
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | acumVerts += long(mSubMesh[i].mVertexBuffer->mVertexCount);
|
---|
614 | }
|
---|
615 |
|
---|
616 | // Copy skeleton name.
|
---|
617 | if (hasSkeleton)
|
---|
618 | {
|
---|
619 | mesh->hasSkeleton = true;
|
---|
620 |
|
---|
621 | strcpy(mesh->mSkeletonName,mSkeletonName);
|
---|
622 | }
|
---|
623 |
|
---|
624 |
|
---|
625 | // Copy mesh bones.
|
---|
626 | if (!mBones.empty())
|
---|
627 | {
|
---|
628 | for (unsigned int j = 0; j < mBones.size(); j++)
|
---|
629 | {
|
---|
630 | mesh->mBones.push_back(mBones[j]);
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | return mesh;
|
---|
635 | }
|
---|
636 |
|
---|