summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/assimp/code/IRRShared.cpp
blob: 197c3df991b7264bd68106cb339856aa2b950f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------

Copyright (c) 2006-2012, assimp team

All rights reserved.

Redistribution and use of this software in source and binary forms, 
with or without modification, are permitted provided that the following 
conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.

* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/

/** @file  IRRShared.cpp
 *  @brief Shared utilities for the IRR and IRRMESH loaders
 */

#include "AssimpPCH.h"

//This section should be excluded only if both the Irrlicht AND the Irrlicht Mesh importers were omitted.
#if !(defined(ASSIMP_BUILD_NO_IRR_IMPORTER) && defined(ASSIMP_BUILD_NO_IRRMESH_IMPORTER))

#include "IRRShared.h"
#include "ParsingUtils.h"
#include "fast_atof.h"

using namespace Assimp;
using namespace irr;
using namespace irr::io;

// Transformation matrix to convert from Assimp to IRR space
const aiMatrix4x4 Assimp::AI_TO_IRR_MATRIX = aiMatrix4x4 ( 
	1.0f, 0.0f, 0.0f, 0.0f, 
	0.0f, 0.0f, 1.0f, 0.0f,
	0.0f, 1.0f, 0.0f, 0.0f, 
	0.0f, 0.0f, 0.0f, 1.0f);

// ------------------------------------------------------------------------------------------------
// read a property in hexadecimal format (i.e. ffffffff)
void IrrlichtBase::ReadHexProperty    (HexProperty&    out)
{
	for (int i = 0; i < reader->getAttributeCount();++i)
	{
		if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
		{
			out.name = std::string( reader->getAttributeValue(i) );
		}
		else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
		{
			// parse the hexadecimal value
			out.value = strtoul16(reader->getAttributeValue(i));
		}
	}
}

// ------------------------------------------------------------------------------------------------
// read a decimal property
void IrrlichtBase::ReadIntProperty    (IntProperty&    out)
{
	for (int i = 0; i < reader->getAttributeCount();++i)
	{
		if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
		{
			out.name = std::string( reader->getAttributeValue(i) );
		}
		else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
		{
			// parse the ecimal value
			out.value = strtol10(reader->getAttributeValue(i));
		}
	}
}

// ------------------------------------------------------------------------------------------------
// read a string property
void IrrlichtBase::ReadStringProperty (StringProperty& out)
{
	for (int i = 0; i < reader->getAttributeCount();++i)
	{
		if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
		{
			out.name = std::string( reader->getAttributeValue(i) );
		}
		else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
		{
			// simple copy the string
			out.value = std::string (reader->getAttributeValue(i));
		}
	}
}

// ------------------------------------------------------------------------------------------------
// read a boolean property
void IrrlichtBase::ReadBoolProperty   (BoolProperty&   out)
{
	for (int i = 0; i < reader->getAttributeCount();++i)
	{
		if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
		{
			out.name = std::string( reader->getAttributeValue(i) );
		}
		else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
		{
			// true or false, case insensitive
			out.value = (ASSIMP_stricmp( reader->getAttributeValue(i), 
				"true") ? false : true);
		}
	}
}

// ------------------------------------------------------------------------------------------------
// read a float property
void IrrlichtBase::ReadFloatProperty  (FloatProperty&  out)
{
	for (int i = 0; i < reader->getAttributeCount();++i)
	{
		if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
		{
			out.name = std::string( reader->getAttributeValue(i) );
		}
		else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
		{
			// just parse the float
			out.value = fast_atof( reader->getAttributeValue(i) );
		}
	}
}

// ------------------------------------------------------------------------------------------------
// read a vector property
void IrrlichtBase::ReadVectorProperty  (VectorProperty&  out)
{
	for (int i = 0; i < reader->getAttributeCount();++i)
	{
		if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
		{
			out.name = std::string( reader->getAttributeValue(i) );
		}
		else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
		{
			// three floats, separated with commas
			const char* ptr = reader->getAttributeValue(i);

			SkipSpaces(&ptr);
			ptr = fast_atoreal_move<float>( ptr,(float&)out.value.x );
			SkipSpaces(&ptr);
			if (',' != *ptr)
			{
				DefaultLogger::get()->error("IRR(MESH): Expected comma in vector definition");
			}
			else SkipSpaces(ptr+1,&ptr);
			ptr = fast_atoreal_move<float>( ptr,(float&)out.value.y );
			SkipSpaces(&ptr);
			if (',' != *ptr)
			{
				DefaultLogger::get()->error("IRR(MESH): Expected comma in vector definition");
			}
			else SkipSpaces(ptr+1,&ptr);
			ptr = fast_atoreal_move<float>( ptr,(float&)out.value.z );
		}
	}
}

// ------------------------------------------------------------------------------------------------
// Convert a string to a proper aiMappingMode
int ConvertMappingMode(const std::string& mode)
{
	if (mode == "texture_clamp_repeat")
	{
		return aiTextureMapMode_Wrap;
	}
	else if (mode == "texture_clamp_mirror")
		return aiTextureMapMode_Mirror;

	return aiTextureMapMode_Clamp;
}

// ------------------------------------------------------------------------------------------------
// Parse a material from the XML file
aiMaterial* IrrlichtBase::ParseMaterial(unsigned int& matFlags)
{
	aiMaterial* mat = new aiMaterial();
	aiColor4D clr;
	aiString s;

	matFlags = 0; // zero output flags
	int cnt  = 0; // number of used texture channels
	unsigned int nd = 0;

	// Continue reading from the file
	while (reader->read())
	{
		switch (reader->getNodeType())
		{
		case EXN_ELEMENT:

			// Hex properties
			if (!ASSIMP_stricmp(reader->getNodeName(),"color"))
			{
				HexProperty prop;
				ReadHexProperty(prop);
				if (prop.name == "Diffuse")
				{
					ColorFromARGBPacked(prop.value,clr);
					mat->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
				}
				else if (prop.name == "Ambient")
				{
					ColorFromARGBPacked(prop.value,clr);
					mat->AddProperty(&clr,1,AI_MATKEY_COLOR_AMBIENT);
				}
				else if (prop.name == "Specular")
				{
					ColorFromARGBPacked(prop.value,clr);
					mat->AddProperty(&clr,1,AI_MATKEY_COLOR_SPECULAR);
				}

				// NOTE: The 'emissive' property causes problems. It is
				// often != 0, even if there is obviously no light
				// emitted by the described surface. In fact I think
				// IRRLICHT ignores this property, too.
#if 0
				else if (prop.name == "Emissive")
				{
					ColorFromARGBPacked(prop.value,clr);
					mat->AddProperty(&clr,1,AI_MATKEY_COLOR_EMISSIVE);
				}
#endif
			}
			// Float properties
			else if (!ASSIMP_stricmp(reader->getNodeName(),"float"))
			{
				FloatProperty prop;
				ReadFloatProperty(prop);
				if (prop.name == "Shininess")
				{
					mat->AddProperty(&prop.value,1,AI_MATKEY_SHININESS);
				}
			}
			// Bool properties
			else if (!ASSIMP_stricmp(reader->getNodeName(),"bool"))
			{
				BoolProperty prop;
				ReadBoolProperty(prop);
				if (prop.name == "Wireframe")
				{
					int val = (prop.value ? true : false);
					mat->AddProperty(&val,1,AI_MATKEY_ENABLE_WIREFRAME);
				}
				else if (prop.name == "GouraudShading")
				{
					int val = (prop.value ? aiShadingMode_Gouraud 
						: aiShadingMode_NoShading);
					mat->AddProperty(&val,1,AI_MATKEY_SHADING_MODEL);
				}
				else if (prop.name == "BackfaceCulling")
				{
					int val = (!prop.value);
					mat->AddProperty(&val,1,AI_MATKEY_TWOSIDED);
				}
			}
			// String properties - textures and texture related properties
			else if (!ASSIMP_stricmp(reader->getNodeName(),"texture") ||
				     !ASSIMP_stricmp(reader->getNodeName(),"enum"))
			{
				StringProperty prop;
				ReadStringProperty(prop);
				if (prop.value.length())
				{
					// material type (shader)
					if (prop.name == "Type")
					{
						if (prop.value == "solid")
						{
							// default material ...
						}
						else if (prop.value == "trans_vertex_alpha")
						{
							matFlags = AI_IRRMESH_MAT_trans_vertex_alpha;
						}
						else if (prop.value == "lightmap")
						{
							matFlags = AI_IRRMESH_MAT_lightmap;
						}
						else if (prop.value == "solid_2layer")
						{
							matFlags = AI_IRRMESH_MAT_solid_2layer;
						}
						else if (prop.value == "lightmap_m2")
						{
							matFlags = AI_IRRMESH_MAT_lightmap_m2;
						}
						else if (prop.value == "lightmap_m4")
						{
							matFlags = AI_IRRMESH_MAT_lightmap_m4;
						}
						else if (prop.value == "lightmap_light")
						{
							matFlags = AI_IRRMESH_MAT_lightmap_light;
						}
						else if (prop.value == "lightmap_light_m2")
						{
							matFlags = AI_IRRMESH_MAT_lightmap_light_m2;
						}
						else if (prop.value == "lightmap_light_m4")
						{
							matFlags = AI_IRRMESH_MAT_lightmap_light_m4;
						}
						else if (prop.value == "lightmap_add")
						{
							matFlags = AI_IRRMESH_MAT_lightmap_add;
						}
						// Normal and parallax maps are treated equally
						else if (prop.value == "normalmap_solid" ||
							prop.value == "parallaxmap_solid")
						{
							matFlags = AI_IRRMESH_MAT_normalmap_solid;
						}
						else if (prop.value == "normalmap_trans_vertex_alpha" ||
							prop.value == "parallaxmap_trans_vertex_alpha")
						{
							matFlags = AI_IRRMESH_MAT_normalmap_tva;
						}
						else if (prop.value == "normalmap_trans_add" ||
							prop.value == "parallaxmap_trans_add")
						{
							matFlags = AI_IRRMESH_MAT_normalmap_ta;
						}
						else {
							DefaultLogger::get()->warn("IRRMat: Unrecognized material type: " + prop.value);
						}
					}

					// Up to 4 texture channels are supported
					if (prop.name == "Texture1")
					{
						// Always accept the primary texture channel
						++cnt;
						s.Set(prop.value);
						mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
					}
					else if (prop.name == "Texture2" && cnt == 1)
					{
						// 2-layer material lightmapped?
						if (matFlags & AI_IRRMESH_MAT_lightmap)	{
							++cnt;
							s.Set(prop.value);
							mat->AddProperty(&s,AI_MATKEY_TEXTURE_LIGHTMAP(0));

							// set the corresponding material flag
							matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
						}
						// alternatively: normal or parallax mapping
						else if (matFlags & AI_IRRMESH_MAT_normalmap_solid)	{
							++cnt;
							s.Set(prop.value);
							mat->AddProperty(&s,AI_MATKEY_TEXTURE_NORMALS(0));

							// set the corresponding material flag
							matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
						}
						// or just as second diffuse texture
						else if (matFlags & AI_IRRMESH_MAT_solid_2layer)	{
							++cnt;
							s.Set(prop.value);
							mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(1));
							++nd;

							// set the corresponding material flag
							matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
						}
						else DefaultLogger::get()->warn("IRRmat: Skipping second texture");
					}

					else if (prop.name == "Texture3" && cnt == 2)
					{
						// Irrlicht does not seem to use these channels.
						++cnt;
						s.Set(prop.value);
						mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(nd+1));
					}
					else if (prop.name == "Texture4" && cnt == 3)
					{
						// Irrlicht does not seem to use these channels.
						++cnt;
						s.Set(prop.value);
						mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(nd+2));
					}

					// Texture mapping options
					if (prop.name == "TextureWrap1" && cnt >= 1)
					{
						int map = ConvertMappingMode(prop.value);
						mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
						mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
					}
					else if (prop.name == "TextureWrap2" && cnt >= 2)
					{
						int map = ConvertMappingMode(prop.value);
						if (matFlags & AI_IRRMESH_MAT_lightmap) {
							mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_LIGHTMAP(0));
							mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_LIGHTMAP(0));	
						}
						else if (matFlags & (AI_IRRMESH_MAT_normalmap_solid)) {
							mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_NORMALS(0));
							mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_NORMALS(0));	
						}
						else if (matFlags & AI_IRRMESH_MAT_solid_2layer) {
							mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(1));
							mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(1));	
						}
					}
					else if (prop.name == "TextureWrap3" && cnt >= 3)
					{
						int map = ConvertMappingMode(prop.value);
						mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(nd+1));
						mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(nd+1));
					}
					else if (prop.name == "TextureWrap4" && cnt >= 4)
					{
						int map = ConvertMappingMode(prop.value);
						mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(nd+2));
						mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(nd+2));
					}
				}
			}
			break;
			case EXN_ELEMENT_END:

				/* Assume there are no further nested nodes in <material> elements
				 */
				if (/* IRRMESH */ !ASSIMP_stricmp(reader->getNodeName(),"material") ||
					/* IRR     */ !ASSIMP_stricmp(reader->getNodeName(),"attributes"))
				{
					// Now process lightmapping flags
					// We should have at least one textur to do that ..
					if (cnt && matFlags & AI_IRRMESH_MAT_lightmap)
					{
						float f = 1.f;
						unsigned int unmasked = matFlags&~AI_IRRMESH_MAT_lightmap;

						// Additive lightmap?
						int op = (unmasked & AI_IRRMESH_MAT_lightmap_add
							? aiTextureOp_Add : aiTextureOp_Multiply);

						// Handle Irrlicht's lightmapping scaling factor
						if (unmasked & AI_IRRMESH_MAT_lightmap_m2 ||
							unmasked & AI_IRRMESH_MAT_lightmap_light_m2)
						{
							f = 2.f;
						}
						else if (unmasked & AI_IRRMESH_MAT_lightmap_m4 ||
							unmasked & AI_IRRMESH_MAT_lightmap_light_m4)
						{
							f = 4.f;
						}
						mat->AddProperty( &f, 1, AI_MATKEY_TEXBLEND_LIGHTMAP(0));
						mat->AddProperty( &op,1, AI_MATKEY_TEXOP_LIGHTMAP(0));
					}

					return mat;
				}
			default:

				// GCC complains here ...
				break;
		}
	}
	DefaultLogger::get()->error("IRRMESH: Unexpected end of file. Material is not complete");
	return mat;
}

#endif // !(defined(ASSIMP_BUILD_NO_IRR_IMPORTER) && defined(ASSIMP_BUILD_NO_IRRMESH_IMPORTER))