vrshoot

view libs/assimp/ObjFileMtlImporter.cpp @ 3:c179c72369be

rename candy->vr
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 03 Feb 2014 08:52:13 +0200
parents
children
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
42 #include "AssimpPCH.h"
43 #ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
45 #include "ObjFileMtlImporter.h"
46 #include "ObjTools.h"
47 #include "ObjFileData.h"
48 #include "fast_atof.h"
50 namespace Assimp {
52 // Material specific token
53 static const std::string DiffuseTexture = "map_kd";
54 static const std::string AmbientTexture = "map_ka";
55 static const std::string SpecularTexture = "map_ks";
56 static const std::string OpacityTexture = "map_d";
57 static const std::string BumpTexture1 = "map_bump";
58 static const std::string BumpTexture2 = "map_Bump";
59 static const std::string BumpTexture3 = "bump";
60 static const std::string NormalTexture = "map_Kn";
61 static const std::string DisplacementTexture = "disp";
62 static const std::string SpecularityTexture = "map_ns";
64 // -------------------------------------------------------------------
65 // Constructor
66 ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
67 const std::string & /*strAbsPath*/,
68 ObjFile::Model *pModel ) :
69 m_DataIt( buffer.begin() ),
70 m_DataItEnd( buffer.end() ),
71 m_pModel( pModel ),
72 m_uiLine( 0 )
73 {
74 ai_assert( NULL != m_pModel );
75 if ( NULL == m_pModel->m_pDefaultMaterial )
76 {
77 m_pModel->m_pDefaultMaterial = new ObjFile::Material;
78 m_pModel->m_pDefaultMaterial->MaterialName.Set( "default" );
79 }
80 load();
81 }
83 // -------------------------------------------------------------------
84 // Destructor
85 ObjFileMtlImporter::~ObjFileMtlImporter()
86 {
87 // empty
88 }
90 // -------------------------------------------------------------------
91 // Private copy constructor
92 ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter & /* rOther */ )
93 {
94 // empty
95 }
97 // -------------------------------------------------------------------
98 // Private copy constructor
99 ObjFileMtlImporter &ObjFileMtlImporter::operator = ( const ObjFileMtlImporter & /*rOther */ )
100 {
101 return *this;
102 }
104 // -------------------------------------------------------------------
105 // Loads the material description
106 void ObjFileMtlImporter::load()
107 {
108 if ( m_DataIt == m_DataItEnd )
109 return;
111 while ( m_DataIt != m_DataItEnd )
112 {
113 switch (*m_DataIt)
114 {
115 case 'K':
116 {
117 ++m_DataIt;
118 if (*m_DataIt == 'a') // Ambient color
119 {
120 ++m_DataIt;
121 getColorRGBA( &m_pModel->m_pCurrentMaterial->ambient );
122 }
123 else if (*m_DataIt == 'd') // Diffuse color
124 {
125 ++m_DataIt;
126 getColorRGBA( &m_pModel->m_pCurrentMaterial->diffuse );
127 }
128 else if (*m_DataIt == 's')
129 {
130 ++m_DataIt;
131 getColorRGBA( &m_pModel->m_pCurrentMaterial->specular );
132 }
133 m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
134 }
135 break;
137 case 'd': // Alpha value
138 {
139 ++m_DataIt;
140 getFloatValue( m_pModel->m_pCurrentMaterial->alpha );
141 m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
142 }
143 break;
145 case 'N': // Shineness
146 {
147 ++m_DataIt;
148 switch(*m_DataIt)
149 {
150 case 's':
151 ++m_DataIt;
152 getFloatValue(m_pModel->m_pCurrentMaterial->shineness);
153 break;
154 case 'i': //Index Of refraction
155 ++m_DataIt;
156 getFloatValue(m_pModel->m_pCurrentMaterial->ior);
157 break;
158 }
159 m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
160 break;
161 }
162 break;
165 case 'm': // Texture
166 case 'b': // quick'n'dirty - for 'bump' sections
167 {
168 getTexture();
169 m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
170 }
171 break;
173 case 'n': // New material name
174 {
175 createMaterial();
176 m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
177 }
178 break;
180 case 'i': // Illumination model
181 {
182 m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
183 getIlluminationModel( m_pModel->m_pCurrentMaterial->illumination_model );
184 m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
185 }
186 break;
188 default:
189 {
190 m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
191 }
192 break;
193 }
194 }
195 }
197 // -------------------------------------------------------------------
198 // Loads a color definition
199 void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
200 {
201 ai_assert( NULL != pColor );
203 float r, g, b;
204 m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, r );
205 pColor->r = r;
207 m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, g );
208 pColor->g = g;
210 m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, b );
211 pColor->b = b;
212 }
214 // -------------------------------------------------------------------
215 // Loads the kind of illumination model.
216 void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
217 {
218 m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
219 illum_model = atoi(m_buffer);
220 }
222 // -------------------------------------------------------------------
223 // Loads a single float value.
224 void ObjFileMtlImporter::getFloatValue( float &value )
225 {
226 m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
227 value = (float) fast_atof(m_buffer);
228 }
230 // -------------------------------------------------------------------
231 // Creates a material from loaded data.
232 void ObjFileMtlImporter::createMaterial()
233 {
234 std::string line( "" );
235 while ( !isNewLine( *m_DataIt ) ) {
236 line += *m_DataIt;
237 ++m_DataIt;
238 }
240 std::vector<std::string> token;
241 const unsigned int numToken = tokenize<std::string>( line, token, " " );
242 std::string name( "" );
243 if ( numToken == 1 ) {
244 name = AI_DEFAULT_MATERIAL_NAME;
245 } else {
246 name = token[ 1 ];
247 }
249 std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( name );
250 if ( m_pModel->m_MaterialMap.end() == it) {
251 // New Material created
252 m_pModel->m_pCurrentMaterial = new ObjFile::Material();
253 m_pModel->m_pCurrentMaterial->MaterialName.Set( name );
254 m_pModel->m_MaterialLib.push_back( name );
255 m_pModel->m_MaterialMap[ name ] = m_pModel->m_pCurrentMaterial;
256 } else {
257 // Use older material
258 m_pModel->m_pCurrentMaterial = (*it).second;
259 }
260 }
262 // -------------------------------------------------------------------
263 // Gets a texture name from data.
264 void ObjFileMtlImporter::getTexture() {
265 aiString *out( NULL );
267 const char *pPtr( &(*m_DataIt) );
268 if ( !ASSIMP_strincmp( pPtr, DiffuseTexture.c_str(), DiffuseTexture.size() ) ) {
269 // Diffuse texture
270 out = & m_pModel->m_pCurrentMaterial->texture;
271 } else if ( !ASSIMP_strincmp( pPtr,AmbientTexture.c_str(),AmbientTexture.size() ) ) {
272 // Ambient texture
273 out = & m_pModel->m_pCurrentMaterial->textureAmbient;
274 } else if (!ASSIMP_strincmp( pPtr, SpecularTexture.c_str(), SpecularTexture.size())) {
275 // Specular texture
276 out = & m_pModel->m_pCurrentMaterial->textureSpecular;
277 } else if ( !ASSIMP_strincmp( pPtr, OpacityTexture.c_str(), OpacityTexture.size() ) ) {
278 // Opacity texture
279 out = & m_pModel->m_pCurrentMaterial->textureOpacity;
280 } else if (!ASSIMP_strincmp( pPtr,"map_ka",6)) {
281 // Ambient texture
282 out = & m_pModel->m_pCurrentMaterial->textureAmbient;
283 } else if ( !ASSIMP_strincmp( pPtr, BumpTexture1.c_str(), BumpTexture1.size() ) ||
284 !ASSIMP_strincmp( pPtr, BumpTexture2.c_str(), BumpTexture2.size() ) ||
285 !ASSIMP_strincmp( pPtr, BumpTexture3.c_str(), BumpTexture3.size() ) ) {
286 // Bump texture
287 out = & m_pModel->m_pCurrentMaterial->textureBump;
288 } else if (!ASSIMP_strincmp( pPtr,NormalTexture.c_str(), NormalTexture.size())) {
289 // Normal map
290 out = & m_pModel->m_pCurrentMaterial->textureNormal;
291 } else if (!ASSIMP_strincmp( pPtr, DisplacementTexture.c_str(), DisplacementTexture.size() ) ) {
292 // Displacement texture
293 out = &m_pModel->m_pCurrentMaterial->textureDisp;
294 } else if (!ASSIMP_strincmp( pPtr, SpecularityTexture.c_str(),SpecularityTexture.size() ) ) {
295 // Specularity scaling (glossiness)
296 out = & m_pModel->m_pCurrentMaterial->textureSpecularity;
297 } else {
298 DefaultLogger::get()->error("OBJ/MTL: Encountered unknown texture type");
299 return;
300 }
302 std::string strTexture;
303 m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strTexture );
304 out->Set( strTexture );
305 }
307 // -------------------------------------------------------------------
309 } // Namespace Assimp
311 #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER