vrshoot

diff libs/assimp/ObjFileMtlImporter.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/ObjFileMtlImporter.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,311 @@
     1.4 +/*
     1.5 +---------------------------------------------------------------------------
     1.6 +Open Asset Import Library (assimp)
     1.7 +---------------------------------------------------------------------------
     1.8 +
     1.9 +Copyright (c) 2006-2012, assimp team
    1.10 +
    1.11 +All rights reserved.
    1.12 +
    1.13 +Redistribution and use of this software in source and binary forms, 
    1.14 +with or without modification, are permitted provided that the following 
    1.15 +conditions are met:
    1.16 +
    1.17 +* Redistributions of source code must retain the above
    1.18 +  copyright notice, this list of conditions and the
    1.19 +  following disclaimer.
    1.20 +
    1.21 +* Redistributions in binary form must reproduce the above
    1.22 +  copyright notice, this list of conditions and the
    1.23 +  following disclaimer in the documentation and/or other
    1.24 +  materials provided with the distribution.
    1.25 +
    1.26 +* Neither the name of the assimp team, nor the names of its
    1.27 +  contributors may be used to endorse or promote products
    1.28 +  derived from this software without specific prior
    1.29 +  written permission of the assimp team.
    1.30 +
    1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.42 +---------------------------------------------------------------------------
    1.43 +*/
    1.44 +
    1.45 +#include "AssimpPCH.h"
    1.46 +#ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
    1.47 +
    1.48 +#include "ObjFileMtlImporter.h"
    1.49 +#include "ObjTools.h"
    1.50 +#include "ObjFileData.h"
    1.51 +#include "fast_atof.h"
    1.52 +
    1.53 +namespace Assimp	{
    1.54 +
    1.55 +// Material specific token
    1.56 +static const std::string DiffuseTexture      = "map_kd";
    1.57 +static const std::string AmbientTexture      = "map_ka";
    1.58 +static const std::string SpecularTexture     = "map_ks";
    1.59 +static const std::string OpacityTexture      = "map_d";
    1.60 +static const std::string BumpTexture1        = "map_bump";
    1.61 +static const std::string BumpTexture2        = "map_Bump";
    1.62 +static const std::string BumpTexture3        = "bump";
    1.63 +static const std::string NormalTexture       = "map_Kn";
    1.64 +static const std::string DisplacementTexture = "disp";
    1.65 +static const std::string SpecularityTexture  = "map_ns";
    1.66 +
    1.67 +// -------------------------------------------------------------------
    1.68 +//	Constructor
    1.69 +ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer, 
    1.70 +									   const std::string & /*strAbsPath*/,
    1.71 +									   ObjFile::Model *pModel ) :
    1.72 +	m_DataIt( buffer.begin() ),
    1.73 +	m_DataItEnd( buffer.end() ),
    1.74 +	m_pModel( pModel ),
    1.75 +	m_uiLine( 0 )
    1.76 +{
    1.77 +	ai_assert( NULL != m_pModel );
    1.78 +	if ( NULL == m_pModel->m_pDefaultMaterial )
    1.79 +	{
    1.80 +		m_pModel->m_pDefaultMaterial = new ObjFile::Material;
    1.81 +		m_pModel->m_pDefaultMaterial->MaterialName.Set( "default" );
    1.82 +	}
    1.83 +	load();
    1.84 +}
    1.85 +
    1.86 +// -------------------------------------------------------------------
    1.87 +//	Destructor
    1.88 +ObjFileMtlImporter::~ObjFileMtlImporter()
    1.89 +{
    1.90 +	// empty
    1.91 +}
    1.92 +
    1.93 +// -------------------------------------------------------------------
    1.94 +//	Private copy constructor
    1.95 +ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter & /* rOther */ )
    1.96 +{
    1.97 +	// empty
    1.98 +}
    1.99 +	
   1.100 +// -------------------------------------------------------------------
   1.101 +//	Private copy constructor
   1.102 +ObjFileMtlImporter &ObjFileMtlImporter::operator = ( const ObjFileMtlImporter & /*rOther */ )
   1.103 +{
   1.104 +	return *this;
   1.105 +}
   1.106 +
   1.107 +// -------------------------------------------------------------------
   1.108 +//	Loads the material description
   1.109 +void ObjFileMtlImporter::load()
   1.110 +{
   1.111 +	if ( m_DataIt == m_DataItEnd )
   1.112 +		return;
   1.113 +
   1.114 +	while ( m_DataIt != m_DataItEnd )
   1.115 +	{
   1.116 +		switch (*m_DataIt)
   1.117 +		{
   1.118 +		case 'K':
   1.119 +			{
   1.120 +				++m_DataIt;
   1.121 +				if (*m_DataIt == 'a') // Ambient color
   1.122 +				{
   1.123 +					++m_DataIt;
   1.124 +					getColorRGBA( &m_pModel->m_pCurrentMaterial->ambient );
   1.125 +				}
   1.126 +				else if (*m_DataIt == 'd')	// Diffuse color
   1.127 +				{
   1.128 +					++m_DataIt;
   1.129 +					getColorRGBA( &m_pModel->m_pCurrentMaterial->diffuse );
   1.130 +				}
   1.131 +				else if (*m_DataIt == 's')
   1.132 +				{
   1.133 +					++m_DataIt;
   1.134 +					getColorRGBA( &m_pModel->m_pCurrentMaterial->specular );
   1.135 +				}
   1.136 +				m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
   1.137 +			}
   1.138 +			break;
   1.139 +
   1.140 +		case 'd':	// Alpha value
   1.141 +			{
   1.142 +				++m_DataIt;
   1.143 +				getFloatValue( m_pModel->m_pCurrentMaterial->alpha );
   1.144 +				m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
   1.145 +			}
   1.146 +			break;
   1.147 +
   1.148 +		case 'N':	// Shineness
   1.149 +			{
   1.150 +				++m_DataIt;
   1.151 +				switch(*m_DataIt) 
   1.152 +				{
   1.153 +				case 's':
   1.154 +					++m_DataIt;
   1.155 +					getFloatValue(m_pModel->m_pCurrentMaterial->shineness);
   1.156 +					break;
   1.157 +				case 'i': //Index Of refraction 
   1.158 +					++m_DataIt;
   1.159 +					getFloatValue(m_pModel->m_pCurrentMaterial->ior);
   1.160 +					break;
   1.161 +				}
   1.162 +				m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
   1.163 +				break;
   1.164 +			}
   1.165 +			break;
   1.166 +		
   1.167 +
   1.168 +		case 'm':	// Texture
   1.169 +		case 'b':   // quick'n'dirty - for 'bump' sections
   1.170 +			{
   1.171 +				getTexture();
   1.172 +				m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
   1.173 +			}
   1.174 +			break;
   1.175 +
   1.176 +		case 'n':	// New material name
   1.177 +			{
   1.178 +				createMaterial();
   1.179 +				m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
   1.180 +			}
   1.181 +			break;
   1.182 +
   1.183 +		case 'i':	// Illumination model
   1.184 +			{
   1.185 +				m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
   1.186 +				getIlluminationModel( m_pModel->m_pCurrentMaterial->illumination_model );
   1.187 +				m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
   1.188 +			}
   1.189 +			break;
   1.190 +
   1.191 +		default:
   1.192 +			{
   1.193 +				m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
   1.194 +			}
   1.195 +			break;
   1.196 +		}
   1.197 +	}
   1.198 +}
   1.199 +
   1.200 +// -------------------------------------------------------------------
   1.201 +//	Loads a color definition
   1.202 +void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
   1.203 +{
   1.204 +	ai_assert( NULL != pColor );
   1.205 +	
   1.206 +	float r, g, b;
   1.207 +	m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, r );
   1.208 +	pColor->r = r;
   1.209 +	
   1.210 +	m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, g );
   1.211 +	pColor->g = g;
   1.212 +
   1.213 +	m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, b );
   1.214 +	pColor->b = b;
   1.215 +}
   1.216 +
   1.217 +// -------------------------------------------------------------------
   1.218 +//	Loads the kind of illumination model.
   1.219 +void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
   1.220 +{
   1.221 +	m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
   1.222 +	illum_model = atoi(m_buffer);
   1.223 +}
   1.224 +
   1.225 +// -------------------------------------------------------------------
   1.226 +//	Loads a single float value. 
   1.227 +void ObjFileMtlImporter::getFloatValue( float &value )
   1.228 +{
   1.229 +	m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
   1.230 +	value = (float) fast_atof(m_buffer);
   1.231 +}
   1.232 +
   1.233 +// -------------------------------------------------------------------
   1.234 +//	Creates a material from loaded data.
   1.235 +void ObjFileMtlImporter::createMaterial()
   1.236 +{	
   1.237 +	std::string line( "" );
   1.238 +	while ( !isNewLine( *m_DataIt ) ) {
   1.239 +		line += *m_DataIt;
   1.240 +		++m_DataIt;
   1.241 +	}
   1.242 +	
   1.243 +	std::vector<std::string> token;
   1.244 +	const unsigned int numToken = tokenize<std::string>( line, token, " " );
   1.245 +	std::string name( "" );
   1.246 +	if ( numToken == 1 ) {
   1.247 +		name = AI_DEFAULT_MATERIAL_NAME;
   1.248 +	} else {
   1.249 +		name = token[ 1 ];
   1.250 +	}
   1.251 +
   1.252 +	std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( name );
   1.253 +	if ( m_pModel->m_MaterialMap.end() == it) {
   1.254 +		// New Material created
   1.255 +		m_pModel->m_pCurrentMaterial = new ObjFile::Material();	
   1.256 +		m_pModel->m_pCurrentMaterial->MaterialName.Set( name );
   1.257 +		m_pModel->m_MaterialLib.push_back( name );
   1.258 +		m_pModel->m_MaterialMap[ name ] = m_pModel->m_pCurrentMaterial;
   1.259 +	} else {
   1.260 +		// Use older material
   1.261 +		m_pModel->m_pCurrentMaterial = (*it).second;
   1.262 +	}
   1.263 +}
   1.264 +
   1.265 +// -------------------------------------------------------------------
   1.266 +//	Gets a texture name from data.
   1.267 +void ObjFileMtlImporter::getTexture() {
   1.268 +	aiString *out( NULL );
   1.269 +
   1.270 +	const char *pPtr( &(*m_DataIt) );
   1.271 +	if ( !ASSIMP_strincmp( pPtr, DiffuseTexture.c_str(), DiffuseTexture.size() ) ) {
   1.272 +		// Diffuse texture
   1.273 +		out = & m_pModel->m_pCurrentMaterial->texture;
   1.274 +	} else if ( !ASSIMP_strincmp( pPtr,AmbientTexture.c_str(),AmbientTexture.size() ) ) {
   1.275 +		// Ambient texture
   1.276 +		out = & m_pModel->m_pCurrentMaterial->textureAmbient;
   1.277 +	} else if (!ASSIMP_strincmp( pPtr, SpecularTexture.c_str(), SpecularTexture.size())) {
   1.278 +		// Specular texture
   1.279 +		out = & m_pModel->m_pCurrentMaterial->textureSpecular;
   1.280 +	} else if ( !ASSIMP_strincmp( pPtr, OpacityTexture.c_str(), OpacityTexture.size() ) ) {
   1.281 +		// Opacity texture
   1.282 +		out = & m_pModel->m_pCurrentMaterial->textureOpacity;
   1.283 +	} else if (!ASSIMP_strincmp( pPtr,"map_ka",6)) {
   1.284 +		// Ambient texture
   1.285 +		out = & m_pModel->m_pCurrentMaterial->textureAmbient;
   1.286 +	} else if ( !ASSIMP_strincmp( pPtr, BumpTexture1.c_str(), BumpTexture1.size() ) ||
   1.287 +		        !ASSIMP_strincmp( pPtr, BumpTexture2.c_str(), BumpTexture2.size() ) || 
   1.288 +		        !ASSIMP_strincmp( pPtr, BumpTexture3.c_str(), BumpTexture3.size() ) ) {
   1.289 +		// Bump texture 
   1.290 +		out = & m_pModel->m_pCurrentMaterial->textureBump;
   1.291 +	} else if (!ASSIMP_strincmp( pPtr,NormalTexture.c_str(), NormalTexture.size())) { 
   1.292 +		// Normal map
   1.293 +		out = & m_pModel->m_pCurrentMaterial->textureNormal;
   1.294 +	} else if (!ASSIMP_strincmp( pPtr, DisplacementTexture.c_str(), DisplacementTexture.size() ) ) {
   1.295 +		// Displacement texture
   1.296 +		out = &m_pModel->m_pCurrentMaterial->textureDisp;
   1.297 +	} else if (!ASSIMP_strincmp( pPtr, SpecularityTexture.c_str(),SpecularityTexture.size() ) ) {
   1.298 +		// Specularity scaling (glossiness)
   1.299 +		out = & m_pModel->m_pCurrentMaterial->textureSpecularity;
   1.300 +	} else {
   1.301 +		DefaultLogger::get()->error("OBJ/MTL: Encountered unknown texture type");
   1.302 +		return;
   1.303 +	}
   1.304 +
   1.305 +	std::string strTexture;
   1.306 +	m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strTexture );
   1.307 +	out->Set( strTexture );
   1.308 +}
   1.309 +
   1.310 +// -------------------------------------------------------------------
   1.311 +
   1.312 +} // Namespace Assimp
   1.313 +
   1.314 +#endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER