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