~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to source/Irrlicht/CCSMLoader.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2002-2011 Nikolaus Gebhardt
 
2
// This file is part of the "Irrlicht Engine".
 
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
 
4
//
 
5
// This file was written by Saurav Mohapatra and modified by Nikolaus Gebhardt.
 
6
// See CCSMLoader.h for details.
 
7
 
 
8
#include "IrrCompileConfig.h"
 
9
#ifdef _IRR_COMPILE_WITH_CSM_LOADER_
 
10
 
 
11
#include "CCSMLoader.h"
 
12
#include "os.h"
 
13
#include "IFileSystem.h"
 
14
#include "IReadFile.h"
 
15
#include "ISceneManager.h"
 
16
#include "IAttributes.h"
 
17
#include "SMesh.h"
 
18
#include "IVideoDriver.h"
 
19
#include "SAnimatedMesh.h"
 
20
#include "SMeshBufferLightMap.h"
 
21
 
 
22
#ifdef _DEBUG
 
23
#define _IRR_DEBUG_CSM_LOADER_
 
24
#endif
 
25
 
 
26
namespace irr
 
27
{
 
28
namespace scene
 
29
{
 
30
        //
 
31
        //      the CSM data types
 
32
        //
 
33
        struct color_rgb_t
 
34
        {
 
35
                s32 red;
 
36
                s32 green;
 
37
                s32 blue;
 
38
 
 
39
                color_rgb_t() : red(0), green(0), blue(0) {}
 
40
                void clear() { red=0; green=0; blue=0; }
 
41
                video::SColor toSColor() const { return video::SColor(255, red, green, blue); }
 
42
        };
 
43
 
 
44
 
 
45
        //
 
46
        //      A Binary File Reader
 
47
        //
 
48
        struct BinaryFileReader
 
49
        {
 
50
                BinaryFileReader(io::IReadFile* pFile) : file(pFile) { }
 
51
 
 
52
                s32 readBuffer(void* buffer, s32 len)
 
53
                {
 
54
                        return file->read(buffer,len);
 
55
                }
 
56
 
 
57
                s32 readLong();
 
58
                f32 readFloat();
 
59
 
 
60
                void readString(core::stringc &str);
 
61
                void readVec3f(core::vector3df* v);
 
62
                void readVec2f(core::vector2df* v);
 
63
                void readColorRGB(color_rgb_t* color);
 
64
 
 
65
                io::IReadFile *file;
 
66
        };
 
67
 
 
68
        //
 
69
        //      The file header
 
70
        //
 
71
        class Header
 
72
        {
 
73
        public:
 
74
 
 
75
                enum E_CSM_VERSION
 
76
                {
 
77
                        VERSION_4 = 4,
 
78
                        VERSION_4_1 = 5
 
79
                };
 
80
 
 
81
                Header(){ clear(); }
 
82
 
 
83
                s32 getVersion() const { return version; }
 
84
                void clear(){ version = 0; }
 
85
                void load(BinaryFileReader* pReader)
 
86
                {
 
87
                        version = pReader->readLong();
 
88
                }
 
89
 
 
90
        private:
 
91
 
 
92
                s32 version;
 
93
        };
 
94
 
 
95
 
 
96
        //
 
97
        //      The groups
 
98
        //
 
99
        class Group
 
100
        {
 
101
        public:
 
102
 
 
103
                Group(){ clear(); }
 
104
                ~Group(){ clear(); }
 
105
 
 
106
                void clear();
 
107
                void load(BinaryFileReader* pReader);
 
108
 
 
109
                s32 getFlags() const { return flags; }
 
110
                s32 getParentGroupID() const { return parentGroup; }
 
111
                const core::stringc& getProperties() const { return props; }
 
112
                video::SColor getColor() const { return color.toSColor(); }
 
113
 
 
114
        private:
 
115
 
 
116
                s32 flags;
 
117
                s32 parentGroup;
 
118
                core::stringc props;
 
119
                color_rgb_t color;
 
120
        };
 
121
 
 
122
 
 
123
        //
 
124
        //      The visgroups
 
125
        //
 
126
        class VisGroup
 
127
        {
 
128
        public:
 
129
 
 
130
                VisGroup(){ clear(); }
 
131
                ~VisGroup(){ clear(); }
 
132
                void clear();
 
133
                void load(BinaryFileReader* pReader);
 
134
 
 
135
                s32 getFlags() const{ return flags; }
 
136
                const core::stringc& getName() const{ return name; }
 
137
                video::SColor getColor() const{ return color.toSColor(); }
 
138
 
 
139
        private:
 
140
 
 
141
                core::stringc name;
 
142
                s32 flags;
 
143
                color_rgb_t color;
 
144
        };
 
145
 
 
146
 
 
147
        //
 
148
        //      Lightmaps
 
149
        //
 
150
        class LightMap
 
151
        {
 
152
        public:
 
153
 
 
154
                LightMap() : pixelData(0){ clear(); }
 
155
                ~LightMap(){ clear(); }
 
156
                void clear();
 
157
                void load(BinaryFileReader* pReader);
 
158
                s32 getWidth() const{ return width; }
 
159
                s32 getHeight() const{ return height; }
 
160
                s32* getPixelData() const{ return pixelData; }
 
161
 
 
162
        private:
 
163
 
 
164
                s32 width;
 
165
                s32 height;
 
166
                s32* pixelData;
 
167
        };
 
168
 
 
169
        struct Triangle
 
170
        {
 
171
                s32 a,b,c;
 
172
        };
 
173
 
 
174
 
 
175
        struct Line
 
176
        {
 
177
                s32 a,b;
 
178
        };
 
179
 
 
180
 
 
181
        class Vertex
 
182
        {
 
183
        public:
 
184
 
 
185
                Vertex(){ clear(); }
 
186
                ~Vertex(){ clear(); }
 
187
                void clear();
 
188
                void load(BinaryFileReader* pReader);
 
189
 
 
190
                const core::vector3df& getPosition() const { return position; }
 
191
                const core::vector3df& getNormal() const { return normal; }
 
192
                video::SColor getColor() const { return color.toSColor(); }
 
193
                const core::vector3df& getTextureCoordinates() const { return texCoords; }
 
194
                const core::vector3df& getLightMapCoordinates() const { return lmapCoords; }
 
195
 
 
196
        private:
 
197
 
 
198
                core::vector3df position;
 
199
                core::vector3df normal;
 
200
                color_rgb_t color;
 
201
                core::vector3df texCoords;
 
202
                core::vector3df lmapCoords;
 
203
        };
 
204
 
 
205
 
 
206
        class Surface
 
207
        {
 
208
        public:
 
209
 
 
210
                Surface() { clear(); }
 
211
                ~Surface(){ clear(); }
 
212
 
 
213
                void clear();
 
214
                void load(BinaryFileReader *pReader);
 
215
 
 
216
                s32 getFlags() const{ return flags; }
 
217
                const core::stringc& getTextureName() const{ return textureName; }
 
218
                s32 getLightMapId() const{ return lightMapId; }
 
219
                const core::vector2df* getUVOffset() const{ return &uvOffset; }
 
220
                const core::vector2df* getUVScale() const{ return &uvScale; }
 
221
                f32 getUVRotation() const{ return uvRotation; }
 
222
 
 
223
                u32 getVertexCount() const{ return vertices.size(); }
 
224
                const Vertex& getVertexAt(const s32 index) const{ return vertices[index]; }
 
225
 
 
226
                u32 getTriangleCount() const{ return triangles.size(); }
 
227
                const Triangle& getTriangleAt(const s32 index) const{ return triangles[index]; }
 
228
 
 
229
        private:
 
230
 
 
231
                s32 flags;
 
232
                core::stringc textureName;
 
233
                s32 lightMapId;
 
234
                core::vector2df uvOffset;
 
235
                core::vector2df uvScale;
 
236
                f32 uvRotation;
 
237
                core::array<Vertex> vertices;
 
238
                core::array<Triangle> triangles;
 
239
                core::array<Line> lines;
 
240
        };
 
241
 
 
242
        class Mesh
 
243
        {
 
244
        public:
 
245
 
 
246
                Mesh(){ clear(); }
 
247
                ~Mesh(){ clear(); }
 
248
 
 
249
                void clear();
 
250
                void load(BinaryFileReader* pReader, bool bReadVisGroups);
 
251
 
 
252
                s32 getFlags() const { return flags; }
 
253
                s32 getGroupID() const { return groupId; }
 
254
                const core::stringc& getProperties() const { return props; }
 
255
                video::SColor getColor() const { return color.toSColor(); }
 
256
                const core::vector3df* getPosition() const { return &position; }
 
257
                s32 getVisgroupID() const { return visgroupId; }
 
258
                s32 getSurfaceCount() const { return surfaces.size(); }
 
259
                const Surface* getSurfaceAt(const s32 index) const { return surfaces[index]; }
 
260
 
 
261
        private:
 
262
 
 
263
                s32 flags;
 
264
                s32 groupId;
 
265
                core::stringc props;
 
266
                color_rgb_t color;
 
267
                core::vector3df position;
 
268
                s32 visgroupId;
 
269
 
 
270
                core::array<Surface*> surfaces;
 
271
        };
 
272
 
 
273
        class Entity
 
274
        {
 
275
        public:
 
276
 
 
277
                Entity() { clear(); }
 
278
                ~Entity() { clear(); }
 
279
 
 
280
                void clear();
 
281
                void load(BinaryFileReader* pReader);
 
282
                s32 getVisgroupID() const { return visgroupId; }
 
283
                s32 getGroupID() const { return groupId; }
 
284
                const core::stringc& getProperties() const { return props; }
 
285
                const core::vector3df* getPosition() const { return &position; }
 
286
 
 
287
        private:
 
288
 
 
289
                s32 visgroupId;
 
290
                s32 groupId;
 
291
                core::stringc props;
 
292
                core::vector3df position;
 
293
        };
 
294
 
 
295
 
 
296
        class CameraData
 
297
        {
 
298
        public:
 
299
 
 
300
                CameraData(){ clear(); }
 
301
                ~CameraData(){ clear(); }
 
302
 
 
303
                void clear();
 
304
                void load(BinaryFileReader* pReader);
 
305
 
 
306
                const core::vector3df* getPosition(){ return &position; }
 
307
                f32 getPitch(){ return pitch; }
 
308
                f32 getYaw(){ return yaw; }
 
309
 
 
310
        private:
 
311
 
 
312
                core::vector3df position;
 
313
                f32 pitch;
 
314
                f32 yaw;
 
315
        };
 
316
 
 
317
        //
 
318
        //      A CSM File
 
319
        //
 
320
        class CSMFile
 
321
        {
 
322
        public:
 
323
 
 
324
                CSMFile(){ clear(); }
 
325
                ~CSMFile(){ clear(); }
 
326
                void clear();
 
327
                void load(BinaryFileReader* pReader);
 
328
 
 
329
                const Header* getHeader() const{ return &header; }
 
330
 
 
331
                u32 getGroupCount() const{ return groups.size(); }
 
332
                const Group* getGroupAt(const s32 index) const{ return groups[index]; }
 
333
 
 
334
                u32 getVisGroupCount() const{ return visgroups.size(); }
 
335
                const VisGroup* getVisGroupAt(const s32 index) const{ return visgroups[index]; }
 
336
 
 
337
                u32 getLightMapCount() const{ return lightmaps.size(); }
 
338
                const LightMap* getLightMapAt(const s32 index) const { return lightmaps[index]; }
 
339
 
 
340
                u32 getMeshCount() const{ return meshes.size(); }
 
341
                const Mesh* getMeshAt(const s32 index) const{ return meshes[index]; }
 
342
 
 
343
                u32 getEntityCount() const{ return entities.size(); }
 
344
                const Entity* getEntityAt(const s32 index) const{ return entities[index]; }
 
345
 
 
346
                const CameraData* getCameraData() const{ return &cameraData; }
 
347
 
 
348
        private:
 
349
 
 
350
                Header header;
 
351
                core::array<Group*> groups;
 
352
                core::array<VisGroup*> visgroups;
 
353
                core::array<LightMap*> lightmaps;
 
354
                core::array<Mesh*> meshes;
 
355
                core::array<Entity*> entities;
 
356
                CameraData cameraData;
 
357
        };
 
358
 
 
359
        CCSMLoader::CCSMLoader(scene::ISceneManager* manager, io::IFileSystem* fs)
 
360
                : FileSystem(fs), SceneManager(manager)
 
361
        {
 
362
 
 
363
                #ifdef _DEBUG
 
364
                setDebugName("CCSMLoader");
 
365
                #endif
 
366
        }
 
367
 
 
368
 
 
369
        //! returns true if the file maybe is able to be loaded by this class
 
370
        //! based on the file extension (e.g. ".bsp")
 
371
        bool CCSMLoader::isALoadableFileExtension(const io::path& filename) const
 
372
        {
 
373
                return core::hasFileExtension ( filename, "csm" );
 
374
        }
 
375
 
 
376
 
 
377
        //! creates/loads an animated mesh from the file.
 
378
        IAnimatedMesh* CCSMLoader::createMesh(io::IReadFile* file)
 
379
        {
 
380
                scene::IMesh* m = createCSMMesh(file);
 
381
 
 
382
                if (!m)
 
383
                        return 0;
 
384
 
 
385
                SAnimatedMesh* am = new SAnimatedMesh();
 
386
                am->Type = EAMT_CSM;
 
387
                am->addMesh(m);
 
388
                m->drop();
 
389
 
 
390
                am->recalculateBoundingBox();
 
391
                return am;
 
392
        }
 
393
 
 
394
        scene::IMesh* CCSMLoader::createCSMMesh(io::IReadFile* file)
 
395
        {
 
396
                if (!file)
 
397
                        return 0;
 
398
 
 
399
                BinaryFileReader reader(file);
 
400
                CSMFile csmFile;
 
401
                csmFile.load(&reader);
 
402
 
 
403
                return createIrrlichtMesh(&csmFile,
 
404
                        SceneManager->getParameters()->getAttributeAsString(CSM_TEXTURE_PATH),
 
405
                        file->getFileName());
 
406
        }
 
407
 
 
408
 
 
409
        scene::IMesh* CCSMLoader::createIrrlichtMesh(const CSMFile* csmFile,
 
410
                const core::stringc& textureRoot, const io::path& lmprefix)
 
411
        {
 
412
                scene::SMesh *pMesh = new scene::SMesh();
 
413
                video::IVideoDriver* driver = SceneManager->getVideoDriver();
 
414
 
 
415
                for(u32 l = 0; l<csmFile->getLightMapCount(); l++)
 
416
                {
 
417
                        const LightMap* lmap = csmFile->getLightMapAt(l);
 
418
 
 
419
                        io::path lmapName = lmprefix;
 
420
                        lmapName += "LMAP_";
 
421
                        lmapName += io::path(l+1);
 
422
                        os::Printer::log("CCSMLoader loading light map", lmapName.c_str());
 
423
 
 
424
                        video::IImage* lmapImg = driver->createImageFromData(
 
425
                                video::ECF_A8R8G8B8,
 
426
                                core::dimension2d<u32>(lmap->getWidth(),lmap->getHeight()),
 
427
                                lmap->getPixelData());
 
428
 
 
429
                        driver->addTexture(lmapName.c_str(), lmapImg);
 
430
                        lmapImg->drop();
 
431
                }
 
432
 
 
433
                for(u32 m = 0; m<csmFile->getMeshCount(); m++)
 
434
                {
 
435
                        const Mesh* mshPtr = csmFile->getMeshAt(m);
 
436
 
 
437
                        for(s32 s = 0; s < mshPtr->getSurfaceCount(); s++)
 
438
                        {
 
439
                                const Surface* surface = mshPtr->getSurfaceAt(s);
 
440
 
 
441
                                core::stringc texName;
 
442
                                if (textureRoot.size())
 
443
                                {
 
444
                                        texName += textureRoot;
 
445
                                        texName += "/";
 
446
                                }
 
447
                                texName+= surface->getTextureName();
 
448
 
 
449
                                video::ITexture* texture = 0;
 
450
                                if (texName.size())
 
451
                                {
 
452
                                        if (FileSystem->existFile(texName))
 
453
                                                texture = driver->getTexture(texName);
 
454
                                        else if (FileSystem->existFile(surface->getTextureName()))
 
455
                                                texture = driver->getTexture(surface->getTextureName());
 
456
                                        else if (FileSystem->existFile(FileSystem->getFileBasename(surface->getTextureName())))
 
457
                                                texture = driver->getTexture(FileSystem->getFileBasename(surface->getTextureName()));
 
458
                                        else if (FileSystem->existFile(FileSystem->getFileDir(lmprefix)+"/"+surface->getTextureName()))
 
459
                                                texture = driver->getTexture(FileSystem->getFileDir(lmprefix)+"/"+surface->getTextureName());
 
460
                                        else
 
461
                                                texture = driver->getTexture(FileSystem->getFileDir(lmprefix)+"/"+FileSystem->getFileBasename(surface->getTextureName()));
 
462
                                }
 
463
 
 
464
                                //material
 
465
                                io::path lmapName = lmprefix;
 
466
                                lmapName += "LMAP_";
 
467
                                lmapName += io::path(surface->getLightMapId());
 
468
 
 
469
                                scene::SMeshBufferLightMap *buffer = new scene::SMeshBufferLightMap();
 
470
                                buffer->Material.setTexture(0, texture);
 
471
                                if (surface->getLightMapId())
 
472
                                {
 
473
                                        buffer->Material.setTexture(1, driver->getTexture(lmapName));
 
474
                                        buffer->Material.Lighting = false;
 
475
                                        buffer->Material.MaterialType = video::EMT_LIGHTMAP_ADD;
 
476
                                }
 
477
 
 
478
                                buffer->Vertices.reallocate(surface->getVertexCount());
 
479
                                for(u32 v = 0; v < surface->getVertexCount(); ++v)
 
480
                                {
 
481
                                        const Vertex& vtxPtr = surface->getVertexAt(v);
 
482
                                        video::S3DVertex2TCoords vtx;
 
483
                                        vtx.Pos = vtxPtr.getPosition();
 
484
                                        vtx.Normal = vtxPtr.getPosition();
 
485
                                        vtx.Color=vtxPtr.getColor();
 
486
                                        vtx.TCoords.set(vtxPtr.getTextureCoordinates().X, 1.f-vtxPtr.getTextureCoordinates().Y);
 
487
                                        vtx.TCoords2.set(vtxPtr.getLightMapCoordinates().X, 1.f-vtxPtr.getLightMapCoordinates().Y);
 
488
 
 
489
                                        buffer->Vertices.push_back(vtx);
 
490
                                }
 
491
 
 
492
                                buffer->Indices.reallocate(surface->getTriangleCount()*3);
 
493
                                for(u32 t = 0; t < surface->getTriangleCount(); ++t)
 
494
                                {
 
495
                                        const Triangle& tri = surface->getTriangleAt(t);
 
496
                                        buffer->Indices.push_back(tri.c);
 
497
                                        buffer->Indices.push_back(tri.b);
 
498
                                        buffer->Indices.push_back(tri.a);
 
499
                                }
 
500
 
 
501
                                buffer->recalculateBoundingBox();
 
502
                                pMesh->addMeshBuffer(buffer);
 
503
                                buffer->drop();
 
504
                        }
 
505
                }
 
506
 
 
507
                pMesh->recalculateBoundingBox();
 
508
                return pMesh;
 
509
        }
 
510
 
 
511
        void Group::clear()
 
512
        {
 
513
                color.clear();
 
514
                flags = 0;
 
515
                parentGroup = 0;
 
516
                props = "";
 
517
        }
 
518
 
 
519
        void Group::load(BinaryFileReader* pReader)
 
520
        {
 
521
                flags = pReader->readLong();
 
522
                parentGroup = pReader->readLong();
 
523
                pReader->readString(props);
 
524
                pReader->readColorRGB(&color);
 
525
        }
 
526
 
 
527
        void VisGroup::clear()
 
528
        {
 
529
                color.clear();
 
530
                flags = 0;
 
531
                name = "";
 
532
        }
 
533
 
 
534
        void VisGroup::load(BinaryFileReader* pReader)
 
535
        {
 
536
                pReader->readString(name);
 
537
                flags = pReader->readLong();
 
538
                pReader->readColorRGB(&color);
 
539
        }
 
540
 
 
541
        void LightMap::clear()
 
542
        {
 
543
                delete[] pixelData;
 
544
                pixelData = 0;
 
545
                width = height = 0;
 
546
        }
 
547
 
 
548
        void LightMap::load(BinaryFileReader* pReader)
 
549
        {
 
550
                width = pReader->readLong();
 
551
                height = pReader->readLong();
 
552
                pixelData = new s32[width * height];
 
553
                pReader->readBuffer(pixelData, width * height * sizeof(s32));
 
554
        }
 
555
 
 
556
        void Mesh::clear()
 
557
        {
 
558
                flags = 0;
 
559
                groupId = 0;
 
560
                visgroupId = 0;
 
561
                props = "";
 
562
                color.clear();
 
563
                position.set(0,0,0);
 
564
 
 
565
                for(u32 s = 0; s < surfaces.size(); s++)
 
566
                {
 
567
                        delete surfaces[s];
 
568
                }
 
569
                surfaces.clear();
 
570
        }
 
571
 
 
572
        void Mesh::load(BinaryFileReader* pReader, bool bReadVisGroups)
 
573
        {
 
574
                flags = pReader->readLong();
 
575
                groupId = pReader->readLong();
 
576
                pReader->readString(props);
 
577
                pReader->readColorRGB(&color);
 
578
                pReader->readVec3f(&position);
 
579
                if(bReadVisGroups)
 
580
                        visgroupId = pReader->readLong();
 
581
                else
 
582
                        visgroupId = 0;
 
583
 
 
584
                s32 count = pReader->readLong();
 
585
 
 
586
                for(s32 i = 0; i < count; i++)
 
587
                {
 
588
                        Surface* surf = new Surface();
 
589
                        surf->load(pReader);
 
590
                        surfaces.push_back(surf);
 
591
                }
 
592
        }
 
593
 
 
594
        void Surface::clear()
 
595
        {
 
596
                flags = 0;
 
597
                lightMapId = 0;
 
598
                textureName = "";
 
599
                uvOffset.set(0.0f,0.0f);
 
600
                uvScale.set(0.0f,0.0f);
 
601
                uvRotation = 0.0f;
 
602
                triangles.clear();
 
603
                lines.clear();
 
604
                vertices.clear();
 
605
        }
 
606
 
 
607
        void Surface::load(BinaryFileReader* pReader)
 
608
        {
 
609
                flags = pReader->readLong();
 
610
                pReader->readString(textureName);
 
611
                textureName.replace('\\', '/');
 
612
 
 
613
                lightMapId = pReader->readLong();
 
614
                pReader->readVec2f(&uvOffset);
 
615
                pReader->readVec2f(&uvScale);
 
616
                uvRotation = pReader->readFloat();
 
617
                s32 vtxCount = pReader->readLong();
 
618
                s32 triCount = pReader->readLong();
 
619
                s32 lineCount = pReader->readLong();
 
620
 
 
621
                for(s32 v = 0; v < vtxCount; v++)
 
622
                {
 
623
                        vertices.push_back(Vertex());
 
624
                        vertices.getLast().load(pReader);
 
625
                }
 
626
 
 
627
                for(s32 t = 0; t < triCount; t++)
 
628
                {
 
629
                        Triangle tri;
 
630
                        pReader->readBuffer(&tri, sizeof(tri));
 
631
                        triangles.push_back(tri);
 
632
                }
 
633
 
 
634
                for(s32 l = 0; l < lineCount; l++)
 
635
                {
 
636
                        Line line;
 
637
                        pReader->readBuffer(&line,sizeof(line));
 
638
                        lines.push_back(line);
 
639
 
 
640
                }
 
641
 
 
642
        }
 
643
 
 
644
        void Vertex::clear()
 
645
        {
 
646
                position.set(0,0,0);
 
647
                normal.set(0,0,0);
 
648
                color.clear();
 
649
                texCoords.set(0,0,0);
 
650
                lmapCoords.set(0,0,0);
 
651
        }
 
652
 
 
653
        void Vertex::load(BinaryFileReader* pReader)
 
654
        {
 
655
                pReader->readVec3f(&position);
 
656
                pReader->readVec3f(&normal);
 
657
                pReader->readColorRGB(&color);
 
658
                pReader->readVec3f(&texCoords);
 
659
                pReader->readVec3f(&lmapCoords);
 
660
        }
 
661
 
 
662
        void Entity::clear()
 
663
        {
 
664
                visgroupId = groupId = 0;
 
665
                props = "";
 
666
                position.set(0,0,0);
 
667
        }
 
668
 
 
669
        void Entity::load(BinaryFileReader* pReader)
 
670
        {
 
671
                visgroupId = pReader->readLong();
 
672
                groupId = pReader->readLong();
 
673
                pReader->readString(props);
 
674
                pReader->readVec3f(&position);
 
675
        }
 
676
 
 
677
        void CameraData::clear()
 
678
        {
 
679
                position.set(0,0,0);
 
680
                pitch = 0;
 
681
                yaw = 0;
 
682
        }
 
683
 
 
684
        void CameraData::load(BinaryFileReader* pReader)
 
685
        {
 
686
                pReader->readVec3f(&position);
 
687
                pitch = pReader->readFloat();
 
688
                yaw = pReader->readFloat();
 
689
        }
 
690
 
 
691
        void CSMFile::clear()
 
692
        {
 
693
                header.clear();
 
694
                cameraData.clear();
 
695
 
 
696
                u32 x =0;
 
697
                for( x= 0; x < groups.size(); x++)
 
698
                        delete groups[x];
 
699
 
 
700
                groups.clear();
 
701
 
 
702
                for(x= 0; x < visgroups.size(); x++)
 
703
                        delete visgroups[x];
 
704
 
 
705
                visgroups.clear();
 
706
 
 
707
                for(x= 0; x < lightmaps.size(); x++)
 
708
                        delete lightmaps[x];
 
709
 
 
710
                lightmaps.clear();
 
711
 
 
712
                for(x= 0; x < meshes.size(); x++)
 
713
                        delete meshes[x];
 
714
 
 
715
                meshes.clear();
 
716
 
 
717
                for(x= 0; x < entities.size(); x++)
 
718
                        delete entities[x];
 
719
 
 
720
                entities.clear();
 
721
        }
 
722
 
 
723
        void CSMFile::load(BinaryFileReader* pReader)
 
724
        {
 
725
                clear();
 
726
 
 
727
                header.load(pReader);
 
728
 
 
729
                //groups
 
730
                {
 
731
                        const s32 count = pReader->readLong();
 
732
#ifdef _IRR_DEBUG_CSM_LOADER_
 
733
                        os::Printer::log("CSM Version", core::stringc(header.getVersion()).c_str());
 
734
                        os::Printer::log("Loading groups. Count", core::stringc(count));
 
735
#endif
 
736
 
 
737
                        groups.reallocate(count);
 
738
                        for (s32 i = 0; i < count; i++)
 
739
                        {
 
740
                                Group* grp = new Group();
 
741
                                grp->load(pReader);
 
742
                                groups.push_back(grp);
 
743
                        }
 
744
                }
 
745
                const bool bHasVGroups = (header.getVersion() == Header::VERSION_4_1);
 
746
 
 
747
                if (bHasVGroups)
 
748
                {
 
749
                        //visgroups
 
750
                        const s32 count = pReader->readLong();
 
751
#ifdef _IRR_DEBUG_CSM_LOADER_
 
752
                        os::Printer::log("Loading visgroups. Count", core::stringc(count));
 
753
#endif
 
754
 
 
755
                        visgroups.reallocate(count);
 
756
                        for (s32 i = 0; i < count; i++)
 
757
                        {
 
758
                                VisGroup* grp = new VisGroup();
 
759
                                grp->load(pReader);
 
760
                                visgroups.push_back(grp);
 
761
                        }
 
762
                }
 
763
 
 
764
                //lightmaps
 
765
                {
 
766
                        const s32 count = pReader->readLong();
 
767
#ifdef _IRR_DEBUG_CSM_LOADER_
 
768
                        os::Printer::log("Loading lightmaps. Count", core::stringc(count));
 
769
#endif
 
770
 
 
771
                        lightmaps.reallocate(count);
 
772
                        for(s32 i = 0; i < count; i++)
 
773
                        {
 
774
                                LightMap* lm = new LightMap();
 
775
                                lm->load(pReader);
 
776
                                lightmaps.push_back(lm);
 
777
                        }
 
778
                }
 
779
 
 
780
                //meshes
 
781
                {
 
782
                        const s32 count = pReader->readLong();
 
783
#ifdef _IRR_DEBUG_CSM_LOADER_
 
784
                        os::Printer::log("Loading meshes. Count", core::stringc(count));
 
785
#endif
 
786
 
 
787
                        meshes.reallocate(count);
 
788
                        for(s32 i = 0; i < count; i++)
 
789
                        {
 
790
                                Mesh* mesh = new Mesh();
 
791
                                mesh->load(pReader,bHasVGroups);
 
792
                                meshes.push_back(mesh);
 
793
                        }
 
794
                }
 
795
 
 
796
                //entities
 
797
                {
 
798
                        const s32 count = pReader->readLong();
 
799
#ifdef _IRR_DEBUG_CSM_LOADER_
 
800
                        os::Printer::log("Loading entitites. Count", core::stringc(count));
 
801
#endif
 
802
 
 
803
                        entities.reallocate(count);
 
804
                        for(s32 i = 0; i < count; i++)
 
805
                        {
 
806
                                Entity* ent = new Entity();
 
807
                                ent->load(pReader);
 
808
                                entities.push_back(ent);
 
809
                        }
 
810
                }
 
811
 
 
812
                //camera data
 
813
#ifdef _IRR_DEBUG_CSM_LOADER_
 
814
                os::Printer::log("Loading camera data.");
 
815
#endif
 
816
                cameraData.load(pReader);
 
817
        }
 
818
 
 
819
        s32 BinaryFileReader::readLong()
 
820
        {
 
821
                int ret = 0;
 
822
                readBuffer(&ret,sizeof(int));
 
823
#ifdef __BIG_ENDIAN__
 
824
                ret = os::Byteswap::byteswap(ret);
 
825
#endif
 
826
                return ret;
 
827
        }
 
828
 
 
829
        f32 BinaryFileReader::readFloat()
 
830
        {
 
831
                float ret = 0;
 
832
                readBuffer(&ret,sizeof(float));
 
833
#ifdef __BIG_ENDIAN__
 
834
                ret = os::Byteswap::byteswap(ret);
 
835
#endif
 
836
                return ret;
 
837
        }
 
838
 
 
839
        void BinaryFileReader::readString(core::stringc &str)
 
840
        {
 
841
                str = "";
 
842
                c8 c;
 
843
                readBuffer(&c,sizeof(char));
 
844
                while(c != 0)
 
845
                {
 
846
                        str += c;
 
847
                        readBuffer(&c,sizeof(char));
 
848
                }
 
849
        }
 
850
 
 
851
        void BinaryFileReader::readVec3f(core::vector3df* v)
 
852
        {
 
853
                v->X = readFloat();
 
854
                v->Y = readFloat();
 
855
                v->Z = readFloat();
 
856
        }
 
857
 
 
858
        void BinaryFileReader::readVec2f(core::vector2df* v)
 
859
        {
 
860
                v->X = readFloat();
 
861
                v->Y = readFloat();
 
862
        }
 
863
 
 
864
        void BinaryFileReader::readColorRGB(color_rgb_t* color)
 
865
        {
 
866
                readBuffer(color,sizeof(color_rgb_t));
 
867
        }
 
868
 
 
869
} // end namespace
 
870
} // end namespace
 
871
 
 
872
#endif // _IRR_COMPILE_WITH_CSM_LOADER_