-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathMatTexCollection.cpp
More file actions
638 lines (524 loc) · 19.4 KB
/
Copy pathMatTexCollection.cpp
File metadata and controls
638 lines (524 loc) · 19.4 KB
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* Copyright (c) 2006, NIF File Format Library and Tools
All rights reserved. Please see niflib.h for license. */
#include "../include/MatTexCollection.h"
#include "../include/obj/NiGeometry.h"
#include "../include/obj/NiAVObject.h"
#include "../include/obj/NiNode.h"
#include "../include/obj/NiMaterialProperty.h"
#include "../include/obj/NiTexturingProperty.h"
#include "../include/obj/NiTextureProperty.h"
#include "../include/obj/NiMultiTextureProperty.h"
#include "../include/obj/NiSpecularProperty.h"
#include "../include/obj/NiAlphaProperty.h"
#include "../include/obj/NiSourceTexture.h"
#include "../include/obj/NiImage.h"
#include "../include/obj/NiStencilProperty.h"
namespace Niflib {
MatTexCollection::MatTexCollection( NiAVObject * scene_root ) {
if ( scene_root != NULL ) {
GatherMaterials( scene_root );
}
}
MatTexCollection::~MatTexCollection() {}
void MatTexCollection::GatherMaterials( NiAVObject * scene_root ) {
if ( scene_root == NULL ) {
throw runtime_error( "MatTexCollection::GatherMaterials was called on a NULL scene root." );
}
//Check and see if this object is a geometry object
if ( scene_root->IsDerivedType( NiGeometry::TYPE ) ) {
//Check and see if this geometry's unique combination of material and texture properties has already been found
if ( GetMaterialIndex( scene_root ) == NO_MATERIAL ) {
//Material was not already found. Look for material/texture related properties.
NiPropertyRef mat = scene_root->GetPropertyByType( NiMaterialProperty::TYPE );
NiPropertyRef texing = scene_root->GetPropertyByType( NiTexturingProperty::TYPE );
NiPropertyRef tex = scene_root->GetPropertyByType( NiTextureProperty::TYPE );
NiPropertyRef multi = scene_root->GetPropertyByType( NiMultiTextureProperty::TYPE );
NiPropertyRef spec = scene_root->GetPropertyByType( NiSpecularProperty::TYPE );
NiPropertyRef alpha = scene_root->GetPropertyByType( NiAlphaProperty::TYPE );
NiPropertyRef stencil = scene_root->GetPropertyByType( NiStencilProperty::TYPE);
//Make sure at least one isn't NULL
if ( mat != NULL || texing != NULL || tex != NULL || multi != NULL || stencil != NULL) {
//One isn't NULL, so create a new Material
NiMaterialPropertyRef matC = DynamicCast<NiMaterialProperty>(mat);
NiTexturingPropertyRef texingC = DynamicCast<NiTexturingProperty>(texing);
NiTexturePropertyRef texC = DynamicCast<NiTextureProperty>(tex);
NiMultiTexturePropertyRef multiC = DynamicCast<NiMultiTextureProperty>(multi);
NiSpecularPropertyRef specC = DynamicCast<NiSpecularProperty>(spec);
NiAlphaPropertyRef alphaC = DynamicCast<NiAlphaProperty>(alpha);
NiStencilPropertyRef stencilC = DynamicCast<NiStencilProperty>(stencil);
//First, check if the material's textures have been found yet
if ( texingC != NULL ) {
for ( int i = 0; i < 8; ++i ) {
if ( texingC->HasTexture(i) ) {
TexDesc td = texingC->GetTexture(i);
if ( td.source != NULL ) {
unsigned int index = GetTextureIndex( td.source );
if ( index == NO_TEXTURE ) {
//Texture has not yet been found. Create a new one.
textures.push_back( TextureWrapper( td.source ) );
}
}
}
}
} else if ( texC != NULL ) {
NiImageRef image = texC->GetImage();
if ( image != NULL ) {
unsigned int index = GetTextureIndex( image );
if ( index == NO_TEXTURE ) {
//Texture has not yet been found. Create a new one.
textures.push_back( TextureWrapper( image ) );
}
}
}
//TODO: Implement this for NiMultiTextureProperty as well
materials.push_back( MaterialWrapper( matC, texingC, texC, multiC, specC, alphaC, stencilC, this ) );
}
}
//Done with this branch, so return.
return;
}
//If this object is a NiNode, then call this function on its children
NiNodeRef node = DynamicCast<NiNode>(scene_root);
if ( node != NULL ) {
vector<NiAVObjectRef> children = node->GetChildren();
for ( size_t i = 0; i < children.size(); ++i ) {
GatherMaterials( children[i] );
}
}
}
void MatTexCollection::Clear() {
materials.clear();
textures.clear();
}
unsigned int MatTexCollection::GetNumMaterials() {
return materials.size();
}
unsigned int MatTexCollection::GetNumTextures() {
return textures.size();
}
MaterialWrapper MatTexCollection::GetMaterial( unsigned int index ) {
if ( index >= materials.size() ) {
throw runtime_error("The index passed to MatTexCollection::GetMaterial was out of range.");
}
return materials[index];
}
TextureWrapper MatTexCollection::GetTexture( unsigned int index ) {
if ( index >= textures.size() ) {
throw runtime_error("The index passed to MatTexCollection::GetTexture was out of range.");
}
return textures[index];
}
unsigned int MatTexCollection::GetMaterialIndex( NiAVObject * obj ) {
//Search for a material that matches the properties that the NiAVObject has.
vector< Ref<NiProperty> > properties = obj->GetProperties();
return GetMaterialIndex( properties );
}
unsigned int MatTexCollection::GetMaterialIndex( NiMaterialProperty * mat, NiTexturingProperty * texing, NiTextureProperty * tex, NiMultiTextureProperty * multi, NiSpecularProperty * spec, NiAlphaProperty * alpha, NiStencilProperty * stencil ) {
for( size_t i = 0; i < materials.size(); ++i ) {
if ( materials[i].mat_prop == mat &&
materials[i].texing_prop == texing &&
materials[i].tex_prop == tex &&
materials[i].multi_prop == multi &&
materials[i].spec_prop == spec &&
materials[i].alpha_prop == alpha &&
materials[i].stencil_prop == stencil) {
//Match found, return its index
return i;
}
}
//No match was found, return NO_MATERIAL
return NO_MATERIAL;
}
unsigned int MatTexCollection::GetTextureIndex( NiSourceTexture * src_tex ) {
for( size_t i = 0; i < textures.size(); ++i ) {
if ( textures[i].src_tex == src_tex ) {
//Match found, return its index
return i;
}
}
//No match was found, return NO_MATERIAL
return NO_TEXTURE;
}
unsigned int MatTexCollection::GetTextureIndex( NiImage * image ) {
for( size_t i = 0; i < textures.size(); ++i ) {
if ( textures[i].image == image ) {
//Match found, return its index
return i;
}
}
//No match was found, return NO_MATERIAL
return NO_TEXTURE;
}
unsigned int MatTexCollection::GetMaterialIndex( const vector< Ref<NiProperty> > & properties ) {
//Get Material and Texturing properties, if any
NiMaterialPropertyRef mat = NULL;
NiTexturingPropertyRef texing = NULL;
NiTexturePropertyRef tex = NULL;
NiMultiTexturePropertyRef multi = NULL;
NiSpecularPropertyRef spec = NULL;
NiAlphaPropertyRef alpha = NULL;
NiStencilPropertyRef stencil = NULL;
for ( unsigned i = 0; i < properties.size(); ++i ) {
if ( properties[i] == NULL ) {
continue;
}
if ( properties[i]->IsDerivedType( NiMaterialProperty::TYPE ) ) {
mat = DynamicCast<NiMaterialProperty>( properties[i] );
} else if ( properties[i]->IsDerivedType( NiTexturingProperty::TYPE ) ) {
texing = DynamicCast<NiTexturingProperty>( properties[i] );
} else if ( properties[i]->IsDerivedType( NiTextureProperty::TYPE ) ) {
tex = DynamicCast<NiTextureProperty>( properties[i] );
} else if ( properties[i]->IsDerivedType( NiMultiTextureProperty::TYPE ) ) {
multi = DynamicCast<NiMultiTextureProperty>( properties[i] );
} else if ( properties[i]->IsDerivedType( NiSpecularProperty::TYPE ) ) {
spec = DynamicCast<NiSpecularProperty>( properties[i] );
} else if ( properties[i]->IsDerivedType( NiAlphaProperty::TYPE ) ) {
alpha = DynamicCast<NiAlphaProperty>( properties[i] );
} else if ( properties[i]->IsDerivedType( NiStencilProperty::TYPE )) {
stencil = DynamicCast<NiStencilProperty>( properties[i] );
}
}
//Do the search
return GetMaterialIndex( mat, texing, tex, multi, spec, alpha, stencil );
}
unsigned int MatTexCollection::CreateTexture( unsigned int version ) {
if ( version < VER_3_3_0_13 ) {
//Old image object style
NiImageRef image = new NiImage;
//Create texture wrapper and add it to the array
textures.push_back( TextureWrapper( image ) );
} else {
//New iamge object style
NiSourceTextureRef src_tex = new NiSourceTexture;
//Create texture wrapper and add it to the array
textures.push_back( TextureWrapper( src_tex ) );
}
//Return the index of the newly created texture
return textures.size() - 1;
}
unsigned int MatTexCollection::CreateMaterial( bool color, bool texture, bool multi_tex, bool specular, bool translucency, unsigned int version ) {
//Make sure at least one option is set to true
if ( color == false && texture == false && multi_tex == false && specular == false && translucency == false ) {
throw runtime_error( "At least one of the types of texture/material info needs to be stored in a new material. All the argumetns to MatTexCollection::CreateMaterial cannot be false." );
}
NiMaterialPropertyRef mat = NULL;
NiTexturingPropertyRef texing = NULL;
NiTexturePropertyRef tex = NULL;
NiMultiTexturePropertyRef multi = NULL;
NiSpecularPropertyRef spec = NULL;
NiAlphaPropertyRef alpha = NULL;
NiStencilPropertyRef stencil = NULL;
if ( color == true ) {
mat = new NiMaterialProperty;
}
if ( specular == true ) {
spec = new NiSpecularProperty;
}
if ( translucency == true ) {
alpha = new NiAlphaProperty;
}
if ( version < VER_3_3_0_13 ) {
//Old texturing property style
if ( texture == true ) {
tex = new NiTextureProperty;
}
if ( multi_tex == true ) {
multi = new NiMultiTextureProperty;
}
} else {
//New texturing property style
if ( texture == true ) {
texing = new NiTexturingProperty;
}
}
//Create Material and add it to the array
materials.push_back( MaterialWrapper( mat, texing, tex, multi, spec, alpha, stencil, this ) );
//Return the index of the newly created material
return materials.size() - 1;
}
//MaterialWrapper//////////////////////////////////////////////////////////////
MaterialWrapper::MaterialWrapper( NiMaterialProperty * mat, NiTexturingProperty * texing, NiTextureProperty * tex, NiMultiTextureProperty * multi, NiSpecularProperty * spec, NiAlphaProperty * alpha, NiStencilProperty * stencil, MatTexCollection * creator ) {
mat_prop = mat;
texing_prop = texing;
tex_prop = tex;
multi_prop = multi;
spec_prop = spec;
alpha_prop = alpha;
stencil_prop = stencil;
_creator = creator;
}
MaterialWrapper::~MaterialWrapper() {}
void MaterialWrapper::ApplyToObject( NiAVObject * target ) {
//Add any non-NULL properties to the target object
if ( mat_prop != NULL ) {
target->AddProperty( mat_prop );
}
if ( texing_prop != NULL ) {
target->AddProperty( texing_prop );
}
if ( tex_prop != NULL ) {
target->AddProperty( tex_prop );
}
if ( multi_prop != NULL ) {
target->AddProperty( multi_prop );
}
if ( spec_prop != NULL ) {
target->AddProperty( spec_prop );
}
if ( alpha_prop != NULL ) {
target->AddProperty( alpha_prop );
}
if ( stencil_prop != NULL) {
target->AddProperty( stencil_prop );
}
}
vector< Ref<NiProperty> > MaterialWrapper::GetProperties() {
vector<NiPropertyRef> properties;
NiPropertyRef prop;
if ( mat_prop != NULL ) {
prop = StaticCast<NiProperty>(mat_prop);
properties.push_back(prop);
}
if ( texing_prop != NULL ) {
prop = StaticCast<NiProperty>(texing_prop);
properties.push_back(prop);
}
if ( tex_prop != NULL ) {
prop = StaticCast<NiProperty>(tex_prop);
properties.push_back(prop);
}
if ( multi_prop != NULL ) {
prop = StaticCast<NiProperty>(multi_prop);
properties.push_back(prop);
}
if ( spec_prop != NULL ) {
prop = StaticCast<NiProperty>(spec_prop);
properties.push_back(prop);
}
if ( alpha_prop != NULL ) {
prop = StaticCast<NiProperty>(alpha_prop);
properties.push_back(prop);
}
if( stencil_prop != NULL ) {
prop = StaticCast<NiProperty>(stencil_prop);
properties.push_back(prop);
}
return properties;
}
//--Color Function--//
Ref<NiMaterialProperty> MaterialWrapper::GetColorInfo() {
return mat_prop;
}
Ref<NiSpecularProperty> MaterialWrapper::GetSpecularInfo() {
return spec_prop;
}
Ref<NiAlphaProperty> MaterialWrapper::GetTranslucencyInfo() {
return alpha_prop;
}
//--Texture Functions--//
bool MaterialWrapper::HasTexture( TexType slot ) {
if ( texing_prop != NULL ) {
return texing_prop->HasTexture( int(slot) );
}
if ( tex_prop != NULL && slot == BASE_MAP ) {
if ( tex_prop->GetImage() != NULL ) {
return true;
}
}
//TODO: Figure out which slots are what in NiMultiTextureProperty so this can be implemented for that too
//Texture not found
return false;;
}
unsigned int MaterialWrapper::GetTextureIndex( TexType slot ) {
if ( texing_prop != NULL ) {
if ( texing_prop->HasTexture( int(slot) ) == false ) {
return NO_TEXTURE;
}
TexDesc td = texing_prop->GetTexture( int(slot) );
if ( td.source == NULL ) {
return NO_TEXTURE;
}
return _creator->GetTextureIndex( td.source );
}
if ( tex_prop != NULL && slot == BASE_MAP ) {
NiImageRef img = tex_prop->GetImage();
if ( img == NULL ) {
return NO_TEXTURE;
}
return _creator->GetTextureIndex(img);
}
//TODO: Figure out which slots are what in NiMultiTextureProperty so this can be implemented for that too
//Texture not found
return NO_TEXTURE;
}
void MaterialWrapper::SetTextureIndex( TexType slot, unsigned int tex_index ) {
//Get the texture from the creator. This will cause an exception if it fails.
TextureWrapper tw = _creator->GetTexture(tex_index);
if ( texing_prop != NULL ) {
TexDesc td;
td.source = tw.src_tex;
texing_prop->SetTexture( int(slot), td );
}
if ( tex_prop != NULL && slot == BASE_MAP ) {
tex_prop->SetImage( tw.image );
}
//TODO: Figure out which slots are what in NiMultiTextureProperty so this can be implemented for that too
}
unsigned int MaterialWrapper::GetTexUVSetIndex( TexType slot ) {
if ( texing_prop != NULL ) {
if ( texing_prop->HasTexture( int(slot) ) == false ) {
throw runtime_error("The texture at the specified index does not exist.");
}
TexDesc td = texing_prop->GetTexture( int(slot) );
return td.uvSet;
}
//Just return default value for now. Not sure where this data may or may not be stored in the old style texture properties.
return 0;
}
void MaterialWrapper::SetTexUVSetIndex( TexType slot, unsigned int uv_set ) {
if ( texing_prop != NULL ) {
if ( texing_prop->HasTexture( int(slot) ) == false ) {
throw runtime_error("The texture at the specified index does not exist.");
}
TexDesc td = texing_prop->GetTexture( int(slot) );
td.uvSet = uv_set;
texing_prop->SetTexture( int(slot), td );
}
//Just silently fail for now. Not sure where this data may or may not be stored in the old style texture properties.
}
TexClampMode MaterialWrapper::GetTexClampMode( TexType slot ) {
if ( texing_prop != NULL ) {
if ( texing_prop->HasTexture( int(slot) ) == false ) {
throw runtime_error("The texture at the specified index does not exist.");
}
TexDesc td = texing_prop->GetTexture( int(slot) );
return td.clampMode;
}
//Just return default value for now. Not sure where this data may or may not be stored in the old style texture properties.
return WRAP_S_WRAP_T;
}
void MaterialWrapper::SetTexClampMode( TexType slot, TexClampMode mode ) {
if ( texing_prop != NULL ) {
if ( texing_prop->HasTexture( int(slot) ) == false ) {
throw runtime_error("The texture at the specified index does not exist.");
}
TexDesc td = texing_prop->GetTexture( int(slot) );
td.clampMode = mode;
texing_prop->SetTexture( int(slot), td );
}
//Just silently fail for now. Not sure where this data may or may not be stored in the old style texture properties.
}
TexFilterMode MaterialWrapper::GetTexFilterMode( TexType slot ) {
if ( texing_prop != NULL ) {
if ( texing_prop->HasTexture( int(slot) ) == false ) {
throw runtime_error("The texture at the specified index does not exist.");
}
TexDesc td = texing_prop->GetTexture( int(slot) );
return td.filterMode;
}
//Just return default value for now. Not sure where this data may or may not be stored in the old style texture properties.
return FILTER_BILERP;
}
void MaterialWrapper::SetTexFilterMode( TexType slot, TexFilterMode mode ) {
if ( texing_prop != NULL ) {
if ( texing_prop->HasTexture( int(slot) ) == false ) {
throw runtime_error("The texture at the specified index does not exist.");
}
TexDesc td = texing_prop->GetTexture( int(slot) );
td.filterMode = mode;
texing_prop->SetTexture( int(slot), td );
}
//Just silently fail for now. Not sure where this data may or may not be stored in the old style texture properties.
}
///Texture Wrapper/////////////////////////////////////////////////////////////
TextureWrapper::TextureWrapper( NiSourceTexture * src ) {
src_tex = src;
}
TextureWrapper::TextureWrapper( NiImage * img ) {
image = img;
}
TextureWrapper::~TextureWrapper() {}
bool TextureWrapper::IsTextureExternal() {
if ( src_tex != NULL ) {
return src_tex->IsTextureExternal();
} else if ( image != NULL ) {
return image->IsTextureExternal();
} else {
//Texture not found
throw runtime_error("TextureWrapper holds no data. This should not be able to happen.");
}
}
string TextureWrapper::GetTextureFileName() {
if ( src_tex != NULL ) {
return src_tex->GetTextureFileName();
} else if ( image != NULL ) {
return image->GetTextureFileName();
} else {
//Texture not found
throw runtime_error("TextureWrapper holds no data. This should not be able to happen.");
}
}
void TextureWrapper::SetExternalTexture( const string & file_name ) {
if ( src_tex != NULL ) {
src_tex->SetExternalTexture( file_name );
return;
} else if ( image != NULL ) {
image->SetExternalTexture( file_name );
} else {
//Texture not found
throw runtime_error("TextureWrapper holds no data. This should not be able to happen.");
}
}
PixelLayout TextureWrapper::GetPixelLayout() {
if ( src_tex != NULL ) {
return src_tex->GetPixelLayout();
}
//Just return default value for now. Not sure where this data may or may not be stored in the old style image object.
return PIX_LAY_DEFAULT;
}
void TextureWrapper::SetPixelLayout( PixelLayout layout ) {
if ( src_tex != NULL ) {
src_tex->SetPixelLayout(layout);
}
//Just silently fail for now. Not sure where this data may or may not be stored in the old style image object.
}
MipMapFormat TextureWrapper::GetMipMapFormat() {
if ( src_tex != NULL ) {
return src_tex->GetMipMapFormat();
}
//Just return default value for now. Not sure where this data may or may not be stored in the old style image object.
return MIP_FMT_DEFAULT;
}
void TextureWrapper::SetMipMapFormat( MipMapFormat format ) {
if ( src_tex != NULL ) {
src_tex->SetMipMapFormat(format);
}
//Just silently fail for now. Not sure where this data may or may not be stored in the old style image objects.
}
AlphaFormat TextureWrapper::GetAlphaFormat() {
if ( src_tex != NULL ) {
return src_tex->GetAlphaFormat();
}
//Just return default value for now. Not sure where this data may or may not be stored in the old style image object.
return ALPHA_DEFAULT;
}
void TextureWrapper::SetAlphaFormat( AlphaFormat format ) {
if ( src_tex != NULL ) {
src_tex->SetAlphaFormat(format);
}
//Just silently fail for now. Not sure where this data may or may not be stored in the old style image objects.
}
string TextureWrapper::GetObjectName() {
if ( src_tex != NULL ) {
return src_tex->GetName();
}
//NiImage objects don't seem to have a name since they derive from NiObject. Return an empty string.
return string();
}
void TextureWrapper::SetObjectName( const string & name ) {
if ( src_tex != NULL ) {
src_tex->SetName( name );
}
//NiImage objects don't seem to have a name since they derive from NiObject. Do nothing.
}
} //End namespace Niflib