00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __BSPLOADER_H__
00012 #define __BSPLOADER_H__
00013
00014 #include "Bsp/BspDll.h"
00015 #include "Common/File.h"
00016 #include "Common/Table.h"
00017 #include "Render/Shader.h"
00018
00019 class KBspEntity;
00020 class KRender;
00021
00022
00023 #define BSP_OK 0
00024 #define BSP_ERROR_OPENFILE 1
00025 #define BSP_ERROR_BADVERSION 2
00026 #define BSP_ERROR_LIGHTMAPS 4
00027 #define BSP_ERROR_ENTITY 5
00028 #define BSP_ERROR_TEXTURENOTFOUND 6
00029 #define BSP_ERROR_LOADSHADERS 7
00030
00031
00032 typedef enum EBspLump
00033 {
00034 LUMP_ENTITIES = 0,
00035 LUMP_SHADERS = 1,
00036 LUMP_PLANES = 2,
00037 LUMP_NODES = 3,
00038 LUMP_LEAVES = 4,
00039 LUMP_FACELIST = 5,
00040 LUMP_BRUSHLIST = 6,
00041 LUMP_MODELS = 7,
00042 LUMP_BRUSHES = 8,
00043 LUMP_BRUSHSIDES = 9,
00044 LUMP_VERTICES = 10,
00045 LUMP_MESHVERTS = 11,
00046 LUMP_EFFECTS = 12,
00047 LUMP_FACES = 13,
00048 LUMP_LIGHTMAPS = 14,
00049 LUMP_LIGHTVOLS = 15,
00050 LUMP_VISILIST = 16,
00051 LUMP_COUNT
00052 };
00053
00054
00055 #pragma pack( push, 1 )
00056
00057
00058 typedef enum _KBFACETYPE
00059 {
00060 KBFT_POLYGON = 1,
00061 KBFT_PATCH = 2,
00062 KBFT_MESH = 3,
00063 KBFT_BILLBOARD = 4
00064 } KBFACETYPE;
00065
00066
00067 class BSP_API KBspEntry
00068 {
00069 public:
00070 s32 m_Offset;
00071 s32 m_Size;
00072 };
00073
00074
00075 class BSP_API KBspHeader
00076 {
00077 public:
00078 char m_Magic[4];
00079 s32 m_Version;
00080 KBspEntry m_Entry[LUMP_COUNT];
00081 };
00082
00083
00084 class BSP_API KBspShader
00085 {
00086 public:
00087 char m_pName[64];
00088 s32 m_Flags;
00089 s32 m_Contents;
00090 };
00091
00092
00093 class BSP_API KBspPlane
00094 {
00095 public:
00096 KVector m_Normal;
00097 float m_Distance;
00098 };
00099
00100
00101 class BSP_API KBspNode
00102 {
00103 public:
00104 s32 m_PlaneId;
00105 s32 m_Front;
00106
00107 s32 m_Back;
00108
00109 s32 m_BBMins[3];
00110 s32 m_BBMaxs[3];
00111 };
00112
00113
00114 class BSP_API KBspLeafInt
00115 {
00116 public:
00117 s32 m_FirstVisListId;
00118 s32 m_Area;
00119 s32 m_BBMin[3];
00120 s32 m_BBMax[3];
00121 s32 m_FirstFaceId;
00122 s32 m_NumFaces;
00123 s32 m_FirstBrushId;
00124 s32 m_NumBrushes;
00125 };
00126
00127 class BSP_API KBspLeaf
00128 {
00129 public:
00130 s32 m_FirstVisListId;
00131 s32 m_Area;
00132 float m_BBMin[3];
00133 float m_BBMax[3];
00134 s32 m_FirstFaceId;
00135 s32 m_NumFaces;
00136 s32 m_FirstBrushId;
00137 s32 m_NumBrushes;
00138 };
00139
00140
00141 class BSP_API KBspFaceList
00142 {
00143 public:
00144 s32 m_Face;
00145 };
00146
00147
00148 class BSP_API KBspBrushList
00149 {
00150 public:
00151 s32 m_Brush;
00152 };
00153
00154
00155 class BSP_API KBspModel
00156 {
00157 public:
00158 KVector m_BBMin;
00159 KVector m_BBMax;
00160 s32 m_FirstFaceId;
00161 s32 m_NumFaces;
00162 s32 m_FirstBrushId;
00163 s32 m_NumBrushes;
00164 };
00165
00166
00167 class BSP_API KBspBrush
00168 {
00169 public:
00170 s32 m_FirstBrushSideId;
00171 s32 m_NumBrushSides;
00172 s32 m_TextureId;
00173 };
00174
00175
00176 class BSP_API KBspBrushSide
00177 {
00178 public:
00179 s32 m_PlaneId;
00180 s32 m_TextureId;
00181 };
00182
00183
00184 class BSP_API KBspVertex
00185 {
00186 public:
00187 KVector m_Position;
00188 KFPt m_UV[2];
00189 KVector m_Normal;
00190 KRgba m_Color;
00191 };
00192
00193
00194 class BSP_API KBspMeshVert
00195 {
00196 public:
00197 s32 m_FirstVertexId;
00198 };
00199
00200
00201 class BSP_API KBspEffect
00202 {
00203 public:
00204 char m_ShaderName[64];
00205 s32 m_BrushId;
00206 s32 m_Unknown;
00207 };
00208
00209
00210 class BSP_API KBspFace
00211 {
00212 public:
00213 s32 m_ShaderId;
00214 s32 m_EffectId;
00215 s32 m_Type;
00216 s32 m_FirstVertexId;
00217 s32 m_NumVertices;
00218 s32 m_FirstMeshVert;
00219 s32 m_NumMeshVerts;
00220 s32 m_LightMapId;
00221 KPt m_LightMapStart;
00222 KPt m_LightMapSize;
00223 KVector m_LightMapOrigin;
00224 KVector m_LightMapVecs[2];
00225 KVector m_Normal;
00226 KPt m_Size;
00227 };
00228
00229
00230 class BSP_API KBspLightMap
00231 {
00232 public:
00233 KRgb m_Color[128][128];
00234 };
00235
00236
00237 class BSP_API KBspLightVol
00238 {
00239 public:
00240 KRgb m_Ambient;
00241 KRgb m_Directional;
00242 u8 m_Direction[2];
00243 };
00244
00245
00246 class BSP_API KBspVisiList
00247 {
00248 public:
00249 u32 m_NumVectors;
00250 u32 m_SizeOfVector;
00251 u8* m_VisiList;
00252
00253 KBspVisiList()
00254 {
00255 m_NumVectors = 0;
00256 m_SizeOfVector = 0;
00257 m_VisiList = NULL;
00258 }
00259
00260 ~KBspVisiList()
00261 {
00262 if( m_VisiList )
00263 Deletev( m_VisiList );
00264 }
00265 };
00266
00267 #pragma pack( pop )
00268
00269
00270 class BSP_API KBspLoader : public KFile
00271 {
00272 private:
00273 KBspHeader m_Header;
00274
00275 protected:
00276 bool m_bLoaded;
00277
00278 KTable<KBspEntity*> m_EntityList;
00279 KBspShader* m_pShaders;
00280 KBspPlane* m_pPlanes;
00281 KBspNode* m_pNodes;
00282 KBspLeaf* m_pLeaves;
00283 KBspFaceList* m_pFaceList;
00284 KBspBrushList* m_pBrushList;
00285 KBspModel* m_pModels;
00286 KBspBrush* m_pBrushes;
00287 KBspBrushSide* m_pBrushSides;
00288 KBspVertex* m_pVertices;
00289 KBspMeshVert* m_pMeshVerts;
00290 KBspEffect* m_pEffects;
00291 KBspFace* m_pFaces;
00292 KBspLightMap* m_pLightMaps;
00293 KBspLightVol* m_pLightVols;
00294 KBspVisiList* m_pVisiList;
00295
00296 u32 m_nShaders;
00297 u32 m_nPlanes;
00298 u32 m_nNodes;
00299 u32 m_nLeaves;
00300 u32 m_nFaceList;
00301 u32 m_nBrushList;
00302 u32 m_nModels;
00303 u32 m_nBrushes;
00304 u32 m_nBrushSides;
00305 u32 m_nVertices;
00306 u32 m_nMeshVerts;
00307 u32 m_nEffects;
00308 u32 m_nFaces;
00309 u32 m_nLightMaps;
00310 u32 m_nLightVols;
00311 u32 m_nVisiList;
00312
00313 float m_fScale;
00314
00315 s32 LoadVisiList();
00316 s32 LoadEntities();
00317 void DestroyVisiList();
00318 void DestroyEntities();
00319
00320 u32 ReadLump( EBspLump Lump, u8** pData, u32 Size );
00321 template <class T> u32 ReadLump( EBspLump Lump, T** pData )
00322 {
00323 *pData = new T[m_Header.m_Entry[Lump].m_Size / sizeof( T )];
00324 return ReadLump( Lump, (u8**)pData, sizeof( T ) );
00325 }
00326
00327 public:
00328 KBspLoader();
00329 virtual ~KBspLoader();
00330
00331 virtual s32 LoadBSP( const char* pFileName );
00332 virtual s32 CloseBSP();
00333
00334 inline KBspPlane* GetpPlanes( u32 PlaneId ) { KASSERT( PlaneId < m_nPlanes ); return &m_pPlanes[PlaneId]; }
00335 inline KBspNode* GetpNodes( u32 NodeId ) { KASSERT( NodeId < m_nNodes ); return &m_pNodes[NodeId]; }
00336 inline KBspLeaf* GetpLeaves( u32 LeafId ) { KASSERT( LeafId < m_nLeaves ); return &m_pLeaves[LeafId]; }
00337 inline KBspFaceList* GetpFaceList( u32 FaceId ) { KASSERT( FaceId < m_nFaceList ); return &m_pFaceList[FaceId]; }
00338 inline KBspFace* GetpFaces( u32 FaceId ) { KASSERT( FaceId < m_nFaces ); return &m_pFaces[FaceId]; }
00339 inline u8* GetpVisiList( u32 ClusterId ) { KASSERT( ClusterId < m_pVisiList->m_NumVectors ); return &m_pVisiList->m_VisiList[ ClusterId * m_pVisiList->m_SizeOfVector ]; }
00340 inline KBspVertex* GetpVertices( u32 VertexId ) { KASSERT( VertexId < m_nVertices ) ; return &m_pVertices[VertexId]; }
00341 inline KBspMeshVert* GetpMeshVerts( u32 VertexId ) { KASSERT( VertexId < m_nMeshVerts ); return &m_pMeshVerts[VertexId]; }
00342 inline KBspShader* GetpShaders( u32 ShaderId ) { KASSERT( ShaderId < m_nShaders ); return &m_pShaders[ShaderId]; }
00343 inline KBspModel* GetpModels( u32 ModelId ) { KASSERT( ModelId < m_nModels ); return &m_pModels[ModelId]; }
00344 inline KBspBrush* GetpBrushes( u32 BrushId ) { KASSERT( BrushId < m_nBrushes ); return &m_pBrushes[BrushId]; }
00345 inline KBspBrushSide* GetpBrushSides( u32 BrushSidesId ) { KASSERT( BrushSidesId < m_nBrushSides ); return &m_pBrushSides[BrushSidesId]; }
00346 inline u32 GetBrushList( u32 BrushId ) { KASSERT( BrushId < m_nBrushList ); return m_pBrushList[BrushId].m_Brush; }
00347
00348 inline s32 GetnFaces() { return m_nFaces; }
00349 inline s32 GetnLeaves() { return m_nLeaves; }
00350 inline s32 GetnFaceList() { return m_nFaceList; }
00351 inline s32 GetnShaders() { return m_nShaders; }
00352 inline s32 GetnModels() { return m_nModels; }
00353 inline u32 GetnEntities() { return m_EntityList.GetSize(); }
00354
00355 KBspEntity* GetNextEntity( KBspEntity* pCurrentEntity, char* pClassName );
00356 KBspEntity* GetpEntity( u32 EntityId ) { KASSERT( EntityId < m_EntityList.GetSize() ); return m_EntityList[EntityId]; }
00357
00358 bool IsLoaded() { return m_bLoaded; }
00359
00360 float GetScale() { return m_fScale; }
00361 };
00362
00363 #endif __BSPLOADER_H__