vrshoot

view libs/assimp/Q3BSPFileParser.cpp @ 2:334d17aed7de

visual studio project files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 18:36:38 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2008, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
40 #include "AssimpPCH.h"
42 #ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER
44 #include "Q3BSPFileParser.h"
45 #include "DefaultIOSystem.h"
46 #include "Q3BSPFileData.h"
47 #include "Q3BSPZipArchive.h"
48 #include <vector>
50 namespace Assimp
51 {
53 using namespace Q3BSP;
55 // ------------------------------------------------------------------------------------------------
56 Q3BSPFileParser::Q3BSPFileParser( const std::string &rMapName, Q3BSPZipArchive *pZipArchive ) :
57 m_sOffset( 0 ),
58 m_Data(),
59 m_pModel( NULL ),
60 m_pZipArchive( pZipArchive )
61 {
62 ai_assert( NULL != m_pZipArchive );
63 ai_assert( !rMapName.empty() );
65 if ( !readData( rMapName ) )
66 return;
68 m_pModel = new Q3BSPModel;
69 m_pModel->m_ModelName = rMapName;
70 if ( !parseFile() )
71 {
72 delete m_pModel;
73 m_pModel = NULL;
74 }
75 }
77 // ------------------------------------------------------------------------------------------------
78 Q3BSPFileParser::~Q3BSPFileParser()
79 {
80 delete m_pModel;
81 m_pModel = NULL;
82 }
84 // ------------------------------------------------------------------------------------------------
85 Q3BSP::Q3BSPModel *Q3BSPFileParser::getModel() const
86 {
87 return m_pModel;
88 }
90 // ------------------------------------------------------------------------------------------------
91 bool Q3BSPFileParser::readData( const std::string &rMapName )
92 {
93 if ( !m_pZipArchive->Exists( rMapName.c_str() ) )
94 return false;
96 IOStream *pMapFile = m_pZipArchive->Open( rMapName.c_str() );
97 if ( NULL == pMapFile )
98 return false;
100 const size_t size = pMapFile->FileSize();
101 m_Data.resize( size );
103 const size_t readSize = pMapFile->Read( &m_Data[0], sizeof( char ), size );
104 if ( readSize != size )
105 {
106 m_Data.clear();
107 return false;
108 }
109 m_pZipArchive->Close( pMapFile );
111 return true;
112 }
114 // ------------------------------------------------------------------------------------------------
115 bool Q3BSPFileParser::parseFile()
116 {
117 if ( m_Data.empty() )
118 {
119 return false;
120 }
122 if ( !validateFormat() )
123 {
124 return false;
125 }
127 // Imports the dictionary of the level
128 getLumps();
130 // Conunt data and prepare model data
131 countLumps();
133 // Read in Vertices
134 getVertices();
136 // Read in Indices
137 getIndices();
139 // Read Faces
140 getFaces();
142 // Read Textures
143 getTextures();
145 // Read Lightmaps
146 getLightMaps();
148 // Load the entities
149 getEntities();
151 return true;
152 }
154 // ------------------------------------------------------------------------------------------------
155 bool Q3BSPFileParser::validateFormat()
156 {
157 sQ3BSPHeader *pHeader = (sQ3BSPHeader*) &m_Data[ 0 ];
158 m_sOffset += sizeof( sQ3BSPHeader );
160 // Version and identify string validation
161 if (pHeader->strID[ 0 ] != 'I' || pHeader->strID[ 1 ] != 'B' || pHeader->strID[ 2 ] != 'S'
162 || pHeader->strID[ 3 ] != 'P')
163 {
164 return false;
165 }
167 return true;
168 }
170 // ------------------------------------------------------------------------------------------------
171 void Q3BSPFileParser::getLumps()
172 {
173 size_t Offset = m_sOffset;
174 m_pModel->m_Lumps.resize( kMaxLumps );
175 for ( size_t idx=0; idx < kMaxLumps; idx++ )
176 {
177 sQ3BSPLump *pLump = new sQ3BSPLump;
178 memcpy( pLump, &m_Data[ Offset ], sizeof( sQ3BSPLump ) );
179 Offset += sizeof( sQ3BSPLump );
180 m_pModel->m_Lumps[ idx ] = pLump;
181 }
182 }
184 // ------------------------------------------------------------------------------------------------
185 void Q3BSPFileParser::countLumps()
186 {
187 m_pModel->m_Vertices.resize( m_pModel->m_Lumps[ kVertices ]->iSize / sizeof( sQ3BSPVertex ) );
188 m_pModel->m_Indices.resize( m_pModel->m_Lumps[ kMeshVerts ]->iSize / sizeof( int ) );
189 m_pModel->m_Faces.resize( m_pModel->m_Lumps[ kFaces ]->iSize / sizeof( sQ3BSPFace ) );
190 m_pModel->m_Textures.resize( m_pModel->m_Lumps[ kTextures ]->iSize / sizeof( sQ3BSPTexture ) );
191 m_pModel->m_Lightmaps.resize( m_pModel->m_Lumps[ kLightmaps ]->iSize / sizeof( sQ3BSPLightmap ) );
192 }
194 // ------------------------------------------------------------------------------------------------
195 void Q3BSPFileParser::getVertices()
196 {
197 size_t Offset = m_pModel->m_Lumps[ kVertices ]->iOffset;
198 for ( size_t idx = 0; idx < m_pModel->m_Vertices.size(); idx++ )
199 {
200 sQ3BSPVertex *pVertex = new sQ3BSPVertex;
201 memcpy( pVertex, &m_Data[ Offset ], sizeof( sQ3BSPVertex ) );
202 Offset += sizeof( sQ3BSPVertex );
203 m_pModel->m_Vertices[ idx ] = pVertex;
204 }
205 }
207 // ------------------------------------------------------------------------------------------------
208 void Q3BSPFileParser::getIndices()
209 {
210 ai_assert( NULL != m_pModel );
212 sQ3BSPLump *lump = m_pModel->m_Lumps[ kMeshVerts ];
213 size_t Offset = (size_t) lump->iOffset;
214 const size_t nIndices = lump->iSize / sizeof( int );
215 m_pModel->m_Indices.resize( nIndices );
216 memcpy( &m_pModel->m_Indices[ 0 ], &m_Data[ Offset ], lump->iSize );
217 }
219 // ------------------------------------------------------------------------------------------------
220 void Q3BSPFileParser::getFaces()
221 {
222 ai_assert( NULL != m_pModel );
224 size_t Offset = m_pModel->m_Lumps[ kFaces ]->iOffset;
225 for ( size_t idx = 0; idx < m_pModel->m_Faces.size(); idx++ )
226 {
227 sQ3BSPFace *pFace = new sQ3BSPFace;
228 memcpy( pFace, &m_Data[ Offset ], sizeof( sQ3BSPFace ) );
229 m_pModel->m_Faces[ idx ] = pFace;
230 Offset += sizeof( sQ3BSPFace );
231 }
232 }
234 // ------------------------------------------------------------------------------------------------
235 void Q3BSPFileParser::getTextures()
236 {
237 ai_assert( NULL != m_pModel );
239 size_t Offset = m_pModel->m_Lumps[ kTextures ]->iOffset;
240 for ( size_t idx=0; idx < m_pModel->m_Textures.size(); idx++ )
241 {
242 sQ3BSPTexture *pTexture = new sQ3BSPTexture;
243 memcpy( pTexture, &m_Data[ Offset ], sizeof(sQ3BSPTexture) );
244 m_pModel->m_Textures[ idx ] = pTexture;
245 Offset += sizeof(sQ3BSPTexture);
246 }
247 }
249 // ------------------------------------------------------------------------------------------------
250 void Q3BSPFileParser::getLightMaps()
251 {
252 ai_assert( NULL != m_pModel );
254 size_t Offset = m_pModel->m_Lumps[kLightmaps]->iOffset;
255 for ( size_t idx=0; idx < m_pModel->m_Lightmaps.size(); idx++ )
256 {
257 sQ3BSPLightmap *pLightmap = new sQ3BSPLightmap;
258 memcpy( pLightmap, &m_Data[ Offset ], sizeof( sQ3BSPLightmap ) );
259 Offset += sizeof( sQ3BSPLightmap );
260 m_pModel->m_Lightmaps[ idx ] = pLightmap;
261 }
262 }
264 // ------------------------------------------------------------------------------------------------
265 void Q3BSPFileParser::getEntities()
266 {
267 int size = m_pModel->m_Lumps[ kEntities ]->iSize;
268 m_pModel->m_EntityData.resize( size );
269 if ( size > 0 )
270 {
271 size_t Offset = m_pModel->m_Lumps[ kEntities ]->iOffset;
272 memcpy( &m_pModel->m_EntityData[ 0 ], &m_Data[ Offset ], sizeof( char ) * size );
273 }
274 }
276 // ------------------------------------------------------------------------------------------------
278 } // Namespace Assimp
280 #endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER