vrshoot

diff libs/assimp/Q3BSPFileParser.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/Q3BSPFileParser.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,280 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2008, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +#include "AssimpPCH.h"
    1.44 +
    1.45 +#ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER
    1.46 +
    1.47 +#include "Q3BSPFileParser.h"
    1.48 +#include "DefaultIOSystem.h"
    1.49 +#include "Q3BSPFileData.h"
    1.50 +#include "Q3BSPZipArchive.h"
    1.51 +#include <vector>
    1.52 +
    1.53 +namespace Assimp
    1.54 +{
    1.55 +
    1.56 +using namespace Q3BSP;
    1.57 +
    1.58 +// ------------------------------------------------------------------------------------------------
    1.59 +Q3BSPFileParser::Q3BSPFileParser( const std::string &rMapName, Q3BSPZipArchive *pZipArchive ) :
    1.60 +	m_sOffset( 0 ),
    1.61 +	m_Data(),
    1.62 +	m_pModel( NULL ),
    1.63 +	m_pZipArchive( pZipArchive )
    1.64 +{
    1.65 +	ai_assert( NULL != m_pZipArchive );
    1.66 +	ai_assert( !rMapName.empty() );
    1.67 +
    1.68 +	if ( !readData( rMapName ) )
    1.69 +		return;
    1.70 +
    1.71 +	m_pModel = new Q3BSPModel;
    1.72 +	m_pModel->m_ModelName = rMapName;
    1.73 +	if ( !parseFile() )
    1.74 +	{
    1.75 +		delete m_pModel;
    1.76 +		m_pModel = NULL;
    1.77 +	}
    1.78 +}
    1.79 +
    1.80 +// ------------------------------------------------------------------------------------------------
    1.81 +Q3BSPFileParser::~Q3BSPFileParser()
    1.82 +{
    1.83 +	delete m_pModel;
    1.84 +	m_pModel = NULL;
    1.85 +}
    1.86 +
    1.87 +// ------------------------------------------------------------------------------------------------
    1.88 +Q3BSP::Q3BSPModel *Q3BSPFileParser::getModel() const
    1.89 +{
    1.90 +	return m_pModel;
    1.91 +}
    1.92 +
    1.93 +// ------------------------------------------------------------------------------------------------
    1.94 +bool Q3BSPFileParser::readData( const std::string &rMapName )
    1.95 +{
    1.96 +	if ( !m_pZipArchive->Exists( rMapName.c_str() ) )
    1.97 +		return false;
    1.98 +
    1.99 +	IOStream *pMapFile = m_pZipArchive->Open( rMapName.c_str() );
   1.100 +	if ( NULL == pMapFile )
   1.101 +		return false;
   1.102 +		
   1.103 +	const size_t size = pMapFile->FileSize();
   1.104 +	m_Data.resize( size );
   1.105 +
   1.106 +	const size_t readSize = pMapFile->Read( &m_Data[0], sizeof( char ), size );
   1.107 +	if ( readSize != size )
   1.108 +	{
   1.109 +		m_Data.clear();
   1.110 +		return false;
   1.111 +	}
   1.112 +	m_pZipArchive->Close( pMapFile );
   1.113 +
   1.114 +	return true;
   1.115 +}
   1.116 +
   1.117 +// ------------------------------------------------------------------------------------------------
   1.118 +bool Q3BSPFileParser::parseFile()
   1.119 +{
   1.120 +	if ( m_Data.empty() )
   1.121 +	{
   1.122 +		return false;
   1.123 +	}
   1.124 +
   1.125 +	if ( !validateFormat() )
   1.126 +	{
   1.127 +		return false;
   1.128 +	}
   1.129 +
   1.130 +	// Imports the dictionary of the level
   1.131 +	getLumps();
   1.132 +
   1.133 +	// Conunt data and prepare model data 
   1.134 +	countLumps();
   1.135 +
   1.136 +	// Read in Vertices
   1.137 +	getVertices();
   1.138 +
   1.139 +	// Read in Indices
   1.140 +	getIndices();
   1.141 +	
   1.142 +	// Read Faces
   1.143 +	getFaces();
   1.144 +
   1.145 +	// Read Textures
   1.146 +	getTextures();
   1.147 +
   1.148 +	// Read Lightmaps
   1.149 +	getLightMaps();
   1.150 +
   1.151 +	// Load the entities
   1.152 +	getEntities();
   1.153 +
   1.154 +	return true;
   1.155 +}
   1.156 +
   1.157 +// ------------------------------------------------------------------------------------------------
   1.158 +bool Q3BSPFileParser::validateFormat()
   1.159 +{
   1.160 +	sQ3BSPHeader *pHeader = (sQ3BSPHeader*) &m_Data[ 0 ];
   1.161 +	m_sOffset += sizeof( sQ3BSPHeader );
   1.162 +
   1.163 +	// Version and identify string validation
   1.164 +	if (pHeader->strID[ 0 ] != 'I' || pHeader->strID[ 1 ] != 'B' || pHeader->strID[ 2 ] != 'S' 
   1.165 +		|| pHeader->strID[ 3 ] != 'P') 
   1.166 +	{
   1.167 +		return false;
   1.168 +	}
   1.169 +
   1.170 +	return true;
   1.171 +}
   1.172 +
   1.173 +// ------------------------------------------------------------------------------------------------
   1.174 +void Q3BSPFileParser::getLumps()
   1.175 +{
   1.176 +	size_t Offset = m_sOffset;
   1.177 +	m_pModel->m_Lumps.resize( kMaxLumps );
   1.178 +	for ( size_t idx=0; idx < kMaxLumps; idx++ )
   1.179 +	{
   1.180 +		sQ3BSPLump *pLump = new sQ3BSPLump;
   1.181 +		memcpy( pLump, &m_Data[ Offset ], sizeof( sQ3BSPLump ) );
   1.182 +		Offset += sizeof( sQ3BSPLump );
   1.183 +		m_pModel->m_Lumps[ idx ] = pLump;
   1.184 +	}
   1.185 +}
   1.186 +
   1.187 +// ------------------------------------------------------------------------------------------------
   1.188 +void Q3BSPFileParser::countLumps()
   1.189 +{
   1.190 +	m_pModel->m_Vertices.resize( m_pModel->m_Lumps[ kVertices ]->iSize / sizeof( sQ3BSPVertex ) );
   1.191 +	m_pModel->m_Indices.resize( m_pModel->m_Lumps[ kMeshVerts ]->iSize  / sizeof( int ) );
   1.192 +	m_pModel->m_Faces.resize( m_pModel->m_Lumps[ kFaces ]->iSize / sizeof( sQ3BSPFace ) );
   1.193 +	m_pModel->m_Textures.resize( m_pModel->m_Lumps[ kTextures ]->iSize / sizeof( sQ3BSPTexture ) );
   1.194 +	m_pModel->m_Lightmaps.resize( m_pModel->m_Lumps[ kLightmaps ]->iSize / sizeof( sQ3BSPLightmap ) );
   1.195 +}
   1.196 +
   1.197 +// ------------------------------------------------------------------------------------------------
   1.198 +void Q3BSPFileParser::getVertices()
   1.199 +{
   1.200 +	size_t Offset = m_pModel->m_Lumps[ kVertices ]->iOffset;
   1.201 +	for ( size_t idx = 0; idx < m_pModel->m_Vertices.size(); idx++ )
   1.202 +	{
   1.203 +		sQ3BSPVertex *pVertex = new sQ3BSPVertex;
   1.204 +		memcpy( pVertex, &m_Data[ Offset ], sizeof( sQ3BSPVertex ) );
   1.205 +		Offset += sizeof( sQ3BSPVertex );
   1.206 +		m_pModel->m_Vertices[ idx ] = pVertex;
   1.207 +	}
   1.208 +}
   1.209 +
   1.210 +// ------------------------------------------------------------------------------------------------
   1.211 +void Q3BSPFileParser::getIndices()
   1.212 +{
   1.213 +	ai_assert( NULL != m_pModel );
   1.214 +
   1.215 +	sQ3BSPLump *lump = m_pModel->m_Lumps[ kMeshVerts ];	
   1.216 +	size_t Offset = (size_t) lump->iOffset;
   1.217 +	const size_t nIndices = lump->iSize / sizeof( int );
   1.218 +	m_pModel->m_Indices.resize( nIndices );
   1.219 +	memcpy( &m_pModel->m_Indices[ 0 ], &m_Data[ Offset ], lump->iSize );
   1.220 +}
   1.221 +
   1.222 +// ------------------------------------------------------------------------------------------------
   1.223 +void Q3BSPFileParser::getFaces()
   1.224 +{
   1.225 +	ai_assert( NULL != m_pModel );
   1.226 +	
   1.227 +	size_t Offset = m_pModel->m_Lumps[ kFaces ]->iOffset;
   1.228 +	for ( size_t idx = 0; idx < m_pModel->m_Faces.size(); idx++ )
   1.229 +	{
   1.230 +		sQ3BSPFace *pFace = new sQ3BSPFace;
   1.231 +		memcpy( pFace, &m_Data[ Offset ], sizeof( sQ3BSPFace ) );
   1.232 +		m_pModel->m_Faces[ idx ] = pFace;
   1.233 +		Offset += sizeof( sQ3BSPFace );
   1.234 +	}
   1.235 +}
   1.236 +
   1.237 +// ------------------------------------------------------------------------------------------------
   1.238 +void Q3BSPFileParser::getTextures()
   1.239 +{
   1.240 +	ai_assert( NULL != m_pModel );
   1.241 +
   1.242 +	size_t Offset = m_pModel->m_Lumps[ kTextures ]->iOffset;
   1.243 +	for ( size_t idx=0; idx < m_pModel->m_Textures.size(); idx++ )
   1.244 +	{
   1.245 +		sQ3BSPTexture *pTexture = new sQ3BSPTexture;
   1.246 +		memcpy( pTexture, &m_Data[ Offset ], sizeof(sQ3BSPTexture) );
   1.247 +		m_pModel->m_Textures[ idx ] = pTexture;
   1.248 +		Offset += sizeof(sQ3BSPTexture);
   1.249 +	}
   1.250 +}
   1.251 +
   1.252 +// ------------------------------------------------------------------------------------------------
   1.253 +void Q3BSPFileParser::getLightMaps()
   1.254 +{
   1.255 +	ai_assert( NULL != m_pModel );
   1.256 +
   1.257 +	size_t Offset = m_pModel->m_Lumps[kLightmaps]->iOffset;
   1.258 +	for ( size_t idx=0; idx < m_pModel->m_Lightmaps.size(); idx++ )
   1.259 +	{
   1.260 +		sQ3BSPLightmap *pLightmap = new sQ3BSPLightmap;
   1.261 +		memcpy( pLightmap, &m_Data[ Offset ], sizeof( sQ3BSPLightmap ) );
   1.262 +		Offset += sizeof( sQ3BSPLightmap );
   1.263 +		m_pModel->m_Lightmaps[ idx ] = pLightmap;
   1.264 +	}
   1.265 +}
   1.266 +
   1.267 +// ------------------------------------------------------------------------------------------------
   1.268 +void Q3BSPFileParser::getEntities()
   1.269 +{
   1.270 +	int size = m_pModel->m_Lumps[ kEntities ]->iSize;
   1.271 +	m_pModel->m_EntityData.resize( size );
   1.272 +	if ( size > 0 ) 
   1.273 +	{
   1.274 +		size_t Offset = m_pModel->m_Lumps[ kEntities ]->iOffset;
   1.275 +		memcpy( &m_pModel->m_EntityData[ 0 ], &m_Data[ Offset ], sizeof( char ) * size );
   1.276 +	}
   1.277 +}
   1.278 +
   1.279 +// ------------------------------------------------------------------------------------------------
   1.280 +
   1.281 +} // Namespace Assimp
   1.282 +
   1.283 +#endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER