vrshoot

annotate libs/assimp/Q3BSPFileParser.cpp @ 3:c179c72369be

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