source: OGRE/trunk/ogrenew/PlugIns/BSPSceneManager/src/OgreQuake3Level.cpp @ 692

Revision 692, 10.1 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreQuake3Level.h"
26#include "OgreLogManager.h"
27#include "OgreTextureManager.h"
28
29namespace Ogre {
30
31    //-----------------------------------------------------------------------
32    Quake3Level::Quake3Level()
33    {
34
35    }
36    //-----------------------------------------------------------------------
37    void Quake3Level::loadHeaderFromStream(DataStreamPtr& inStream)
38    {
39        // Load just the header
40        bsp_header_t* pHeader =  new bsp_header_t();
41        inStream->read(pHeader, sizeof(bsp_header_t));
42        mChunk = MemoryDataStreamPtr(
43            new MemoryDataStream(pHeader, sizeof(bsp_header_t), false));
44        // Grab all the counts, header only
45        initialise(true);
46                // Delete manually since delete and delete[] (as used by MemoryDataStream)
47                // are not compatible
48                delete pHeader;
49
50    }
51    //-----------------------------------------------------------------------
52    void Quake3Level::loadFromStream(DataStreamPtr& stream)
53    {
54        mChunk = MemoryDataStreamPtr(new MemoryDataStream(stream));
55        initialise();
56
57#ifdef _DEBUG
58        dumpContents();
59#endif
60
61
62    }
63    //-----------------------------------------------------------------------
64   // byte swapping functions
65   void SwapFourBytes(uint32* dw)
66   {
67      uint32 tmp;
68      tmp =  (*dw & 0x000000FF);
69      tmp = ((*dw & 0x0000FF00) >> 0x08) | (tmp << 0x08);
70      tmp = ((*dw & 0x00FF0000) >> 0x10) | (tmp << 0x08);
71      tmp = ((*dw & 0xFF000000) >> 0x18) | (tmp << 0x08);
72      memcpy (dw, &tmp, sizeof(uint32));
73   }
74   //-----------------------------------------------------------------------
75   void SwapFourBytesGrup (uint32* src, int size)
76   {
77      uint32* ptr = (uint32*)src;
78      int i;
79      for (i = 0; i < size/4; ++i) {
80         SwapFourBytes (&ptr[i]);
81      }
82   }
83   //-----------------------------------------------------------------------
84    void Quake3Level::initialise(bool headerOnly)
85    {
86        mHeader = (bsp_header_t*)mChunk->getPtr();
87
88        // Header counts
89        initialiseCounts();
90        // Data pointers
91        if (headerOnly)
92        {
93            mLumpStart = 0;
94        }
95        else
96        {
97            mLumpStart = ((unsigned char*)mHeader) + sizeof(mHeader);
98        }
99        initialisePointers();
100
101
102#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
103      // swap header
104        SwapFourBytes ((uint32*)&mHeader->version);
105#endif
106    }
107    //-----------------------------------------------------------------------
108    void Quake3Level::initialiseCounts(void)
109    {
110        mNumEntities = getLumpSize(BSP_ENTITIES_LUMP);
111        mNumElements = getLumpSize(BSP_ELEMENTS_LUMP) / sizeof(int);
112        mNumFaces = getLumpSize(BSP_FACES_LUMP) / sizeof(bsp_face_t);
113        mNumLeafFaces = getLumpSize(BSP_LFACES_LUMP) / sizeof(int);
114        mNumLeaves = getLumpSize(BSP_LEAVES_LUMP) / sizeof(bsp_leaf_t);
115        mNumLightmaps = getLumpSize(BSP_LIGHTMAPS_LUMP)/BSP_LIGHTMAP_BANKSIZE;
116        mNumModels = getLumpSize(BSP_MODELS_LUMP) / sizeof(bsp_model_t);
117        mNumNodes = getLumpSize(BSP_NODES_LUMP) / sizeof(bsp_node_t);
118        mNumPlanes = getLumpSize(BSP_PLANES_LUMP)/sizeof(bsp_plane_t);
119        mNumShaders = getLumpSize(BSP_SHADERS_LUMP)/sizeof(bsp_shader_t);
120        mNumVertices = getLumpSize(BSP_VERTICES_LUMP)/sizeof(bsp_vertex_t);
121        mNumLeafBrushes = getLumpSize(BSP_LBRUSHES_LUMP)/sizeof(int);
122        mNumBrushes = getLumpSize(BSP_BRUSH_LUMP)/sizeof(bsp_brush_t);
123        mNumBrushSides = getLumpSize(BSP_BRUSHSIDES_LUMP)/sizeof(bsp_brushside_t);
124    }
125    //-----------------------------------------------------------------------
126    void Quake3Level::initialisePointers(void)
127    {
128        mEntities = (unsigned char*)getLump(BSP_ENTITIES_LUMP);
129        mElements = (int*)getLump(BSP_ELEMENTS_LUMP);
130        mFaces = (bsp_face_t*)getLump(BSP_FACES_LUMP);
131        mLeafFaces = (int*)getLump(BSP_LFACES_LUMP);
132        mLeaves = (bsp_leaf_t*)getLump(BSP_LEAVES_LUMP);
133        mLightmaps = (unsigned char*)getLump(BSP_LIGHTMAPS_LUMP);
134        mModels = (bsp_model_t*)getLump(BSP_MODELS_LUMP);
135        mNodes = (bsp_node_t*)getLump(BSP_NODES_LUMP);
136        mPlanes = (bsp_plane_t*) getLump(BSP_PLANES_LUMP);
137        mShaders = (bsp_shader_t*) getLump(BSP_SHADERS_LUMP);
138        mVis = (bsp_vis_t*)getLump(BSP_VISIBILITY_LUMP);
139        mVertices = (bsp_vertex_t*) getLump(BSP_VERTICES_LUMP);
140        mLeafBrushes = (int*)getLump(BSP_LBRUSHES_LUMP);
141        mBrushes = (bsp_brush_t*) getLump(BSP_BRUSH_LUMP);
142        mBrushSides = (bsp_brushside_t*) getLump(BSP_BRUSHSIDES_LUMP);
143#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
144        SwapFourBytesGrup ((uint32*)mElements, mNumElements*sizeof(int));
145        SwapFourBytesGrup ((uint32*)mFaces, mNumFaces*sizeof(bsp_face_t));
146        SwapFourBytesGrup ((uint32*)mLeafFaces, mNumLeafFaces*sizeof(int));
147        SwapFourBytesGrup ((uint32*)mLeaves, mNumLeaves*sizeof(bsp_leaf_t));
148        SwapFourBytesGrup ((uint32*)mModels, mNumModels*sizeof(bsp_model_t));
149        SwapFourBytesGrup ((uint32*)mNodes, mNumNodes*sizeof(bsp_node_t));
150        SwapFourBytesGrup ((uint32*)mPlanes, mNumPlanes*sizeof(bsp_plane_t));
151        for (int i=0; i < mNumShaders; ++i) {
152            SwapFourBytes((uint32*)&mShaders[i].surface_flags);
153            SwapFourBytes((uint32*)&mShaders[i].content_flags);
154        }   
155        SwapFourBytes((uint32*)&mVis->cluster_count);
156        SwapFourBytes((uint32*)&mVis->row_size);
157        SwapFourBytesGrup ((uint32*)mVertices, mNumVertices*sizeof(bsp_vertex_t));
158        SwapFourBytesGrup ((uint32*)mLeafBrushes, mNumLeafBrushes*sizeof(int));
159        SwapFourBytesGrup ((uint32*)mBrushes,  mNumBrushes*sizeof(bsp_brush_t));
160        SwapFourBytesGrup ((uint32*)mBrushSides, mNumBrushSides*sizeof(bsp_brushside_t));
161#endif
162    }
163    //-----------------------------------------------------------------------
164    void* Quake3Level::getLump(int lumpType)
165    {
166        if (mLumpStart)
167        {
168       
169#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
170            // swap lump offset
171            SwapFourBytes ((uint32*)&mHeader->lumps[lumpType].offset);
172#endif
173            return (unsigned char*)mHeader + mHeader->lumps[lumpType].offset;
174        }
175        else
176        {
177            return 0;
178        }
179    }
180    //-----------------------------------------------------------------------
181    int Quake3Level::getLumpSize(int lumpType)
182    {
183
184#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
185        // swap lump size
186        SwapFourBytes ((uint32*)&mHeader->lumps[lumpType].size);
187#endif
188        return mHeader->lumps[lumpType].size;
189    }
190    //-----------------------------------------------------------------------
191    void Quake3Level::dumpContents(void)
192    {
193        std::ofstream of;
194        of.open("Quake3Level.log");
195
196
197        of << "Quake3 level statistics" << std::endl;
198        of << "-----------------------" << std::endl;
199        of << "Entities     : " << mNumEntities << std::endl;
200        of << "Faces        : " << mNumFaces << std::endl;
201        of << "Leaf Faces   : " << mNumLeafFaces << std::endl;
202        of << "Leaves       : " << mNumLeaves << std::endl;
203        of << "Lightmaps    : " << mNumLightmaps << std::endl;
204        of << "Elements     : " << mNumElements << std::endl;
205        of << "Models       : " << mNumModels << std::endl;
206        of << "Nodes        : " << mNumNodes << std::endl;
207        of << "Planes       : " << mNumPlanes << std::endl;
208        of << "Shaders      : " << mNumShaders << std::endl;
209        of << "Vertices     : " << mNumVertices << std::endl;
210        of << "Vis Clusters : " << mVis->cluster_count << std::endl;
211
212        of << std::endl;
213        of << "-= Shaders =-";
214        of << std::endl;
215        for (int i = 0; i < mNumShaders; ++i)
216        {
217            of << "Shader " << i << ": " << mShaders[i].name << std::endl;
218        }
219
220        of << std::endl;
221        of << "-= Entities =-";
222        of << std::endl;
223        char* strEnt = strtok((char*)mEntities, "\0");
224        while (strEnt != 0)
225        {
226            of << strEnt << std::endl;
227            strEnt = strtok(0, "\0");
228        }
229
230
231
232
233        of.close();
234    }
235    //-----------------------------------------------------------------------
236    void Quake3Level::extractLightmaps(void) const
237    {
238        // Lightmaps are always 128x128x24 (RGB)
239        unsigned char* pLightmap = mLightmaps;
240        for (int i = 0; i < mNumLightmaps; ++i)
241        {
242                        StringUtil::StrStreamType name;
243            name << "@lightmap" << i;
244
245            // Load, no mipmaps, brighten by factor 2.5
246                        DataStreamPtr stream(new MemoryDataStream(pLightmap, 128 * 128 * 3, false));
247            Image img;
248                        img.loadRawData( stream, 128, 128, PF_BYTE_RGB );
249            TextureManager::getSingleton().loadImage( name.str(),
250                                ResourceGroupManager::getSingleton().getWorldResourceGroupName(), img, TEX_TYPE_2D, 0, 4.0f );
251            pLightmap += BSP_LIGHTMAP_BANKSIZE;
252        }
253
254
255    }
256    //-----------------------------------------------------------------------
257
258
259}
Note: See TracBrowser for help on using the repository browser.