~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to source/Irrlicht/CQ3LevelMesh.h

  • 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
#ifndef __C_Q3_LEVEL_MESH_H_INCLUDED__
 
6
#define __C_Q3_LEVEL_MESH_H_INCLUDED__
 
7
 
 
8
#include "IQ3LevelMesh.h"
 
9
#include "IReadFile.h"
 
10
#include "IFileSystem.h"
 
11
#include "SMesh.h"
 
12
#include "SMeshBufferLightMap.h"
 
13
#include "IVideoDriver.h"
 
14
#include "irrString.h"
 
15
#include "ISceneManager.h"
 
16
#include "os.h"
 
17
 
 
18
namespace irr
 
19
{
 
20
namespace scene
 
21
{
 
22
        class CQ3LevelMesh : public IQ3LevelMesh
 
23
        {
 
24
        public:
 
25
 
 
26
                //! constructor
 
27
                CQ3LevelMesh(io::IFileSystem* fs, scene::ISceneManager* smgr,
 
28
                             const quake3::Q3LevelLoadParameter &loadParam);
 
29
 
 
30
                //! destructor
 
31
                virtual ~CQ3LevelMesh();
 
32
 
 
33
                //! loads a level from a .bsp-File. Also tries to load all
 
34
                //! needed textures. Returns true if successful.
 
35
                bool loadFile(io::IReadFile* file);
 
36
 
 
37
                //! returns the amount of frames in milliseconds. If the amount
 
38
                //! is 1, it is a static (=non animated) mesh.
 
39
                virtual u32 getFrameCount() const;
 
40
 
 
41
                //! returns the animated mesh based on a detail level. 0 is the
 
42
                //! lowest, 255 the highest detail. Note, that some Meshes will
 
43
                //! ignore the detail level.
 
44
                virtual IMesh* getMesh(s32 frameInMs, s32 detailLevel=255,
 
45
                                s32 startFrameLoop=-1, s32 endFrameLoop=-1);
 
46
 
 
47
                //! Returns an axis aligned bounding box of the mesh.
 
48
                //! \return A bounding box of this mesh is returned.
 
49
                virtual const core::aabbox3d<f32>& getBoundingBox() const;
 
50
 
 
51
                virtual void setBoundingBox( const core::aabbox3df& box);
 
52
 
 
53
 
 
54
                //! Returns the type of the animated mesh.
 
55
                virtual E_ANIMATED_MESH_TYPE getMeshType() const;
 
56
 
 
57
                //! loads the shader definition
 
58
                virtual void getShader( io::IReadFile* file );
 
59
 
 
60
                //! loads the shader definition
 
61
                virtual const quake3::IShader * getShader( const c8 * filename, bool fileNameIsValid=true );
 
62
 
 
63
                //! returns a already loaded Shader
 
64
                virtual const quake3::IShader * getShader( u32 index  ) const;
 
65
 
 
66
 
 
67
                //! loads a configuration file
 
68
                virtual void getConfiguration( io::IReadFile* file );
 
69
                //! get's an interface to the entities
 
70
                virtual quake3::tQ3EntityList & getEntityList();
 
71
 
 
72
 
 
73
                //Link to held meshes? ...
 
74
 
 
75
 
 
76
                //! returns amount of mesh buffers.
 
77
                virtual u32 getMeshBufferCount() const
 
78
                {
 
79
                        return 0;
 
80
                }
 
81
 
 
82
                //! returns pointer to a mesh buffer
 
83
                virtual IMeshBuffer* getMeshBuffer(u32 nr) const
 
84
                {
 
85
                        return 0;
 
86
                }
 
87
 
 
88
                //! Returns pointer to a mesh buffer which fits a material
 
89
                /** \param material: material to search for
 
90
                \return Pointer to the mesh buffer or 0 if there is no such mesh buffer. */
 
91
                virtual IMeshBuffer* getMeshBuffer( const video::SMaterial &material) const
 
92
                {
 
93
                        return 0;
 
94
                }
 
95
 
 
96
                virtual void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
 
97
                {
 
98
                        return;
 
99
                }
 
100
 
 
101
                //! set the hardware mapping hint, for driver
 
102
                virtual void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX)
 
103
                {
 
104
                        return;
 
105
                }
 
106
 
 
107
                //! flags the meshbuffer as changed, reloads hardware buffers
 
108
                virtual void setDirty(E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX)
 
109
                {
 
110
                        return;
 
111
                }
 
112
 
 
113
        private:
 
114
 
 
115
 
 
116
                void constructMesh();
 
117
                void solveTJunction();
 
118
                void loadTextures();
 
119
 
 
120
                struct STexShader
 
121
                {
 
122
                        video::ITexture* Texture;
 
123
                        s32 ShaderID;
 
124
                };
 
125
 
 
126
                core::array< STexShader > Tex;
 
127
                core::array<video::ITexture*> Lightmap;
 
128
 
 
129
                enum eLumps
 
130
                {
 
131
                        kEntities               = 0,    // Stores player/object positions, etc...
 
132
                        kShaders                = 1,    // Stores texture information
 
133
                        kPlanes                 = 2,    // Stores the splitting planes
 
134
                        kNodes                  = 3,    // Stores the BSP nodes
 
135
                        kLeafs                  = 4,    // Stores the leafs of the nodes
 
136
                        kLeafFaces              = 5,    // Stores the leaf's indices into the faces
 
137
                        kLeafBrushes    = 6,    // Stores the leaf's indices into the brushes
 
138
                        kModels                 = 7,    // Stores the info of world models
 
139
                        kBrushes                = 8,    // Stores the brushes info (for collision)
 
140
                        kBrushSides             = 9,    // Stores the brush surfaces info
 
141
                        kVertices               = 10,   // Stores the level vertices
 
142
                        kMeshVerts              = 11,   // Stores the model vertices offsets
 
143
                        kFogs                   = 12,   // Stores the shader files (blending, anims..)
 
144
                        kFaces                  = 13,   // Stores the faces for the level
 
145
                        kLightmaps              = 14,   // Stores the lightmaps for the level
 
146
                        kLightGrid              = 15,   // Stores extra world lighting information
 
147
                        kVisData                = 16,   // Stores PVS and cluster info (visibility)
 
148
                        kLightArray             = 17,   // RBSP
 
149
                        kMaxLumps                               // A constant to store the number of lumps
 
150
                };
 
151
 
 
152
                enum eBspSurfaceType
 
153
                {
 
154
                        BSP_MST_BAD,
 
155
                        BSP_MST_PLANAR,
 
156
                        BSP_MST_PATCH,
 
157
                        BSP_MST_TRIANGLE_SOUP,
 
158
                        BSP_MST_FLARE,
 
159
                        BSP_MST_FOLIAGE
 
160
 
 
161
                };
 
162
 
 
163
                struct tBSPHeader
 
164
                {
 
165
                        s32 strID;     // This should always be 'IBSP'
 
166
                        s32 version;       // This should be 0x2e for Quake 3 files
 
167
                };
 
168
                tBSPHeader header;
 
169
 
 
170
                struct tBSPLump
 
171
                {
 
172
                        s32 offset;
 
173
                        s32 length;
 
174
                };
 
175
 
 
176
 
 
177
                struct tBSPVertex
 
178
                {
 
179
                        f32 vPosition[3];      // (x, y, z) position.
 
180
                        f32 vTextureCoord[2];  // (u, v) texture coordinate
 
181
                        f32 vLightmapCoord[2]; // (u, v) lightmap coordinate
 
182
                        f32 vNormal[3];        // (x, y, z) normal vector
 
183
                        u8 color[4];           // RGBA color for the vertex
 
184
                };
 
185
 
 
186
                struct tBSPFace
 
187
                {
 
188
                        s32 textureID;        // The index into the texture array
 
189
                        s32 fogNum;           // The index for the effects (or -1 = n/a)
 
190
                        s32 type;             // 1=polygon, 2=patch, 3=mesh, 4=billboard
 
191
                        s32 vertexIndex;      // The index into this face's first vertex
 
192
                        s32 numOfVerts;       // The number of vertices for this face
 
193
                        s32 meshVertIndex;    // The index into the first meshvertex
 
194
                        s32 numMeshVerts;     // The number of mesh vertices
 
195
                        s32 lightmapID;       // The texture index for the lightmap
 
196
                        s32 lMapCorner[2];    // The face's lightmap corner in the image
 
197
                        s32 lMapSize[2];      // The size of the lightmap section
 
198
                        f32 lMapPos[3];     // The 3D origin of lightmap.
 
199
                        f32 lMapBitsets[2][3]; // The 3D space for s and t unit vectors.
 
200
                        f32 vNormal[3];     // The face normal.
 
201
                        s32 size[2];          // The bezier patch dimensions.
 
202
                };
 
203
 
 
204
                struct tBSPTexture
 
205
                {
 
206
                        c8 strName[64];   // The name of the texture w/o the extension
 
207
                        u32 flags;          // The surface flags (unknown)
 
208
                        u32 contents;       // The content flags (unknown)
 
209
                };
 
210
 
 
211
                struct tBSPLightmap
 
212
                {
 
213
                        u8 imageBits[128][128][3];   // The RGB data in a 128x128 image
 
214
                };
 
215
 
 
216
                struct tBSPNode
 
217
                {
 
218
                        s32 plane;      // The index into the planes array
 
219
                        s32 front;      // The child index for the front node
 
220
                        s32 back;       // The child index for the back node
 
221
                        s32 mins[3];    // The bounding box min position.
 
222
                        s32 maxs[3];    // The bounding box max position.
 
223
                };
 
224
 
 
225
                struct tBSPLeaf
 
226
                {
 
227
                        s32 cluster;           // The visibility cluster
 
228
                        s32 area;              // The area portal
 
229
                        s32 mins[3];           // The bounding box min position
 
230
                        s32 maxs[3];           // The bounding box max position
 
231
                        s32 leafface;          // The first index into the face array
 
232
                        s32 numOfLeafFaces;    // The number of faces for this leaf
 
233
                        s32 leafBrush;         // The first index for into the brushes
 
234
                        s32 numOfLeafBrushes;  // The number of brushes for this leaf
 
235
                };
 
236
 
 
237
                struct tBSPPlane
 
238
                {
 
239
                        f32 vNormal[3];     // Plane normal.
 
240
                        f32 d;              // The plane distance from origin
 
241
                };
 
242
 
 
243
                struct tBSPVisData
 
244
                {
 
245
                        s32 numOfClusters;   // The number of clusters
 
246
                        s32 bytesPerCluster; // Bytes (8 bits) in the cluster's bitset
 
247
                        c8 *pBitsets;      // Array of bytes holding the cluster vis.
 
248
                };
 
249
 
 
250
                struct tBSPBrush
 
251
                {
 
252
                        s32 brushSide;           // The starting brush side for the brush
 
253
                        s32 numOfBrushSides;     // Number of brush sides for the brush
 
254
                        s32 textureID;           // The texture index for the brush
 
255
                };
 
256
 
 
257
                struct tBSPBrushSide
 
258
                {
 
259
                        s32 plane;              // The plane index
 
260
                        s32 textureID;          // The texture index
 
261
                };
 
262
 
 
263
                struct tBSPModel
 
264
                {
 
265
                        f32 min[3];           // The min position for the bounding box
 
266
                        f32 max[3];           // The max position for the bounding box.
 
267
                        s32 faceIndex;          // The first face index in the model
 
268
                        s32 numOfFaces;         // The number of faces in the model
 
269
                        s32 brushIndex;         // The first brush index in the model
 
270
                        s32 numOfBrushes;       // The number brushes for the model
 
271
                };
 
272
 
 
273
                struct tBSPFog
 
274
                {
 
275
                        c8 shader[64];          // The name of the shader file
 
276
                        s32 brushIndex;         // The brush index for this shader
 
277
                        s32 visibleSide;        // the brush side that ray tests need to clip against (-1 == none
 
278
                };
 
279
                core::array < STexShader > FogMap;
 
280
 
 
281
                struct tBSPLights
 
282
                {
 
283
                        u8 ambient[3];     // This is the ambient color in RGB
 
284
                        u8 directional[3]; // This is the directional color in RGB
 
285
                        u8 direction[2];   // The direction of the light: [phi,theta]
 
286
                };
 
287
 
 
288
                void loadTextures   (tBSPLump* l, io::IReadFile* file); // Load the textures
 
289
                void loadLightmaps  (tBSPLump* l, io::IReadFile* file); // Load the lightmaps
 
290
                void loadVerts      (tBSPLump* l, io::IReadFile* file); // Load the vertices
 
291
                void loadFaces      (tBSPLump* l, io::IReadFile* file); // Load the faces
 
292
                void loadPlanes     (tBSPLump* l, io::IReadFile* file); // Load the Planes of the BSP
 
293
                void loadNodes      (tBSPLump* l, io::IReadFile* file); // load the Nodes of the BSP
 
294
                void loadLeafs      (tBSPLump* l, io::IReadFile* file); // load the Leafs of the BSP
 
295
                void loadLeafFaces  (tBSPLump* l, io::IReadFile* file); // load the Faces of the Leafs of the BSP
 
296
                void loadVisData    (tBSPLump* l, io::IReadFile* file); // load the visibility data of the clusters
 
297
                void loadEntities   (tBSPLump* l, io::IReadFile* file); // load the entities
 
298
                void loadModels     (tBSPLump* l, io::IReadFile* file); // load the models
 
299
                void loadMeshVerts  (tBSPLump* l, io::IReadFile* file); // load the mesh vertices
 
300
                void loadBrushes    (tBSPLump* l, io::IReadFile* file); // load the brushes of the BSP
 
301
                void loadBrushSides (tBSPLump* l, io::IReadFile* file); // load the brushsides of the BSP
 
302
                void loadLeafBrushes(tBSPLump* l, io::IReadFile* file); // load the brushes of the leaf
 
303
                void loadFogs    (tBSPLump* l, io::IReadFile* file); // load the shaders
 
304
 
 
305
                //bi-quadratic bezier patches
 
306
                void createCurvedSurface_bezier(SMeshBufferLightMap* meshBuffer,
 
307
                                s32 faceIndex, s32 patchTesselation, s32 storevertexcolor);
 
308
 
 
309
                void createCurvedSurface_nosubdivision(SMeshBufferLightMap* meshBuffer,
 
310
                                s32 faceIndex, s32 patchTesselation, s32 storevertexcolor);
 
311
 
 
312
                struct S3DVertex2TCoords_64
 
313
                {
 
314
                        core::vector3d<f64> Pos;
 
315
                        core::vector3d<f64> Normal;
 
316
                        video::SColorf Color;
 
317
                        core::vector2d<f64> TCoords;
 
318
                        core::vector2d<f64> TCoords2;
 
319
 
 
320
                        void copy( video::S3DVertex2TCoords &dest ) const;
 
321
 
 
322
                        S3DVertex2TCoords_64() {}
 
323
                        S3DVertex2TCoords_64(const core::vector3d<f64>& pos, const core::vector3d<f64>& normal, const video::SColorf& color,
 
324
                                const core::vector2d<f64>& tcoords, const core::vector2d<f64>& tcoords2)
 
325
                                : Pos(pos), Normal(normal), Color(color), TCoords(tcoords), TCoords2(tcoords2) {}
 
326
 
 
327
                        S3DVertex2TCoords_64 getInterpolated_quadratic(const S3DVertex2TCoords_64& v2,
 
328
                                        const S3DVertex2TCoords_64& v3, const f64 d) const
 
329
                        {
 
330
                                return S3DVertex2TCoords_64 (
 
331
                                                Pos.getInterpolated_quadratic ( v2.Pos, v3.Pos, d  ),
 
332
                                                Normal.getInterpolated_quadratic ( v2.Normal, v3.Normal, d ),
 
333
                                                Color.getInterpolated_quadratic ( v2.Color, v3.Color, (f32) d ),
 
334
                                                TCoords.getInterpolated_quadratic ( v2.TCoords, v3.TCoords, d ),
 
335
                                                TCoords2.getInterpolated_quadratic ( v2.TCoords2, v3.TCoords2, d ));
 
336
                        }
 
337
                };
 
338
 
 
339
                inline void copy( video::S3DVertex2TCoords * dest, const tBSPVertex * source,
 
340
                                s32 vertexcolor ) const;
 
341
                void copy( S3DVertex2TCoords_64 * dest, const tBSPVertex * source, s32 vertexcolor ) const;
 
342
 
 
343
 
 
344
                struct SBezier
 
345
                {
 
346
                        SMeshBufferLightMap *Patch;
 
347
                        S3DVertex2TCoords_64 control[9];
 
348
 
 
349
                        void tesselate(s32 level);
 
350
 
 
351
                private:
 
352
                        s32     Level;
 
353
 
 
354
                        core::array<S3DVertex2TCoords_64> column[3];
 
355
 
 
356
                };
 
357
                SBezier Bezier;
 
358
 
 
359
                quake3::Q3LevelLoadParameter LoadParam;
 
360
 
 
361
                tBSPLump Lumps[kMaxLumps];
 
362
 
 
363
                tBSPTexture* Textures;
 
364
                s32 NumTextures;
 
365
 
 
366
                tBSPLightmap* LightMaps;
 
367
                s32 NumLightMaps;
 
368
 
 
369
                tBSPVertex* Vertices;
 
370
                s32 NumVertices;
 
371
 
 
372
                tBSPFace* Faces;
 
373
                s32 NumFaces;
 
374
 
 
375
                tBSPPlane* Planes;
 
376
                s32 NumPlanes;
 
377
 
 
378
                tBSPNode* Nodes;
 
379
                s32 NumNodes;
 
380
 
 
381
                tBSPLeaf* Leafs;
 
382
                s32 NumLeafs;
 
383
 
 
384
                s32 *LeafFaces;
 
385
                s32 NumLeafFaces;
 
386
 
 
387
                s32 *MeshVerts;           // The vertex offsets for a mesh
 
388
                s32 NumMeshVerts;
 
389
 
 
390
                tBSPBrush* Brushes;
 
391
                s32 NumBrushes;
 
392
 
 
393
                scene::SMesh* Mesh[quake3::E_Q3_MESH_SIZE];
 
394
                video::IVideoDriver* Driver;
 
395
                core::stringc LevelName;
 
396
                io::IFileSystem* FileSystem; // needs because there are no file extenstions stored in .bsp files.
 
397
 
 
398
                // Additional content
 
399
                scene::ISceneManager* SceneManager;
 
400
                enum eToken
 
401
                {
 
402
                        Q3_TOKEN_UNRESOLVED     = 0,
 
403
                        Q3_TOKEN_EOF            = 1,
 
404
                        Q3_TOKEN_START_LIST,
 
405
                        Q3_TOKEN_END_LIST,
 
406
                        Q3_TOKEN_ENTITY,
 
407
                        Q3_TOKEN_TOKEN,
 
408
                        Q3_TOKEN_EOL,
 
409
                        Q3_TOKEN_COMMENT,
 
410
                        Q3_TOKEN_MATH_DIVIDE,
 
411
                        Q3_TOKEN_MATH_ADD,
 
412
                        Q3_TOKEN_MATH_MULTIPY
 
413
                };
 
414
                struct SQ3Parser
 
415
                {
 
416
                        const c8 *source;
 
417
                        u32 sourcesize;
 
418
                        u32 index;
 
419
                        core::stringc token;
 
420
                        eToken tokenresult;
 
421
                };
 
422
                SQ3Parser Parser;
 
423
 
 
424
 
 
425
                typedef void( CQ3LevelMesh::*tParserCallback ) ( quake3::SVarGroupList *& groupList, eToken token );
 
426
                void parser_parse( const void * data, u32 size, tParserCallback callback );
 
427
                void parser_nextToken();
 
428
 
 
429
                void dumpVarGroup( const quake3::SVarGroup * group, s32 stack ) const;
 
430
 
 
431
                void scriptcallback_entity( quake3::SVarGroupList *& grouplist, eToken token );
 
432
                void scriptcallback_shader( quake3::SVarGroupList *& grouplist, eToken token );
 
433
                void scriptcallback_config( quake3::SVarGroupList *& grouplist, eToken token );
 
434
 
 
435
                core::array < quake3::IShader > Shader;
 
436
                core::array < quake3::IShader > Entity;         //quake3::tQ3EntityList Entity;
 
437
 
 
438
 
 
439
                quake3::tStringList ShaderFile;
 
440
                void InitShader();
 
441
                void ReleaseShader();
 
442
                void ReleaseEntity();
 
443
 
 
444
 
 
445
                s32 setShaderMaterial( video::SMaterial & material, const tBSPFace * face ) const;
 
446
                s32 setShaderFogMaterial( video::SMaterial &material, const tBSPFace * face ) const;
 
447
 
 
448
                struct SToBuffer
 
449
                {
 
450
                        s32 takeVertexColor;
 
451
                        u32 index;
 
452
                };
 
453
 
 
454
                void cleanMeshes();
 
455
                void cleanLoader ();
 
456
                void calcBoundingBoxes();
 
457
                c8 buf[128];
 
458
        };
 
459
 
 
460
} // end namespace scene
 
461
} // end namespace irr
 
462
 
 
463
#endif
 
464