/
OSGColladaEffect.cpp
691 lines (570 loc) · 22.6 KB
/
OSGColladaEffect.cpp
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/*---------------------------------------------------------------------------*\
* OpenSG *
* *
* *
* Copyright (C) 2009 by the OpenSG Forum *
* *
* www.opensg.org *
* *
* contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
* *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
* License *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Library General Public License as published *
* by the Free Software Foundation, version 2. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
* *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
* Changes *
* *
* *
* *
* *
* *
* *
\*---------------------------------------------------------------------------*/
#if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
#include "OSGColladaEffect.h"
#ifdef OSG_WITH_COLLADA
#include "OSGColladaLog.h"
#include "OSGColladaGlobal.h"
#include "OSGColladaImage.h"
#include "OSGColladaSampler2D.h"
#include "OSGColladaInstanceEffect.h"
#include "OSGGeometry.h"
#include "OSGChunkMaterial.h"
#include "OSGMaterialChunk.h"
#include "OSGTextureEnvChunk.h"
#include <dom/domEffect.h>
#include <dom/domInstance_effect.h>
#include <dom/domProfile_COMMON.h>
#include <dom/domProfile_GLSL.h>
#include <dom/domProfile_CG.h>
OSG_BEGIN_NAMESPACE
ColladaElementRegistrationHelper ColladaEffect::_regHelper(
&ColladaEffect::create, "effect");
ColladaElementTransitPtr
ColladaEffect::create(daeElement *elem, ColladaGlobal *global)
{
return ColladaElementTransitPtr(new ColladaEffect(elem, global));
}
void
ColladaEffect::read(void)
{
OSG_COLLADA_LOG(("ColladaEffect::read\n"));
domEffectRef effect = getDOMElementAs<domEffect>();
const domImage_Array &images = effect->getImage_array();
readImageArray(images);
const domFx_newparam_common_Array &newParams = effect->getNewparam_array();
for(UInt32 i = 0; i < newParams.getCount(); ++i)
{
if(newParams[i]->getFx_basic_type_common() == NULL)
{
SWARNING << "ColladaEffect::read: <newparam> ["
<< newParams[i]->getSid()
<< "] without contents encountered, ignored."
<< std::endl;
continue;
}
// must read surface params before sampler params, because
// a samplers <source> can refer to a surface
domFx_surface_commonRef paramSurface =
newParams[i]->getFx_basic_type_common()->getSurface();
if(paramSurface != NULL)
{
ParamSurface param;
param.surface = paramSurface;
param.colSurface = dynamic_pointer_cast<ColladaSurface>(
ColladaElementFactory::the()->create(
paramSurface, getGlobal()));
param.colSurface->read();
_surfaceParams[newParams[i]->getSid()] = param;
continue;
}
domFx_sampler2D_commonRef paramSampler2D =
newParams[i]->getFx_basic_type_common()->getSampler2D();
if(paramSampler2D != NULL)
{
ParamSampler2D param;
param.sampler2D = paramSampler2D;
param.colSampler2D = dynamic_pointer_cast<ColladaSampler2D>(
ColladaElementFactory::the()->create(
paramSampler2D, getGlobal()));
param.colSampler2D->setEffect(this);
param.colSampler2D->read ( );
_sampler2DParams[newParams[i]->getSid()] = param;
continue;
}
}
const domFx_profile_abstract_Array &profiles =
effect->getFx_profile_abstract_array();
for(UInt32 i = 0; i < profiles.getCount(); ++i)
{
if(domProfile_COMMON::ID() == profiles[i]->typeID())
{
readProfileCommon(daeSafeCast<domProfile_COMMON>(profiles[i]));
}
else if(domProfile_GLSL::ID() == profiles[i]->typeID())
{
readProfileGLSL(daeSafeCast<domProfile_GLSL>(profiles[i]));
}
else if(domProfile_CG::ID() == profiles[i]->typeID())
{
readProfileCG(daeSafeCast<domProfile_CG>(profiles[i]));
}
}
}
Material *
ColladaEffect::createInstance(ColladaInstanceElement *colInstElem)
{
OSG_COLLADA_LOG(("ColladaEffect::createInstance\n"));
MaterialUnrecPtr retVal = NULL;
domEffectRef effect = getDOMElementAs<domEffect>();
ColladaInstanceEffectRefPtr colInstEffect =
dynamic_cast<ColladaInstanceEffect *>(colInstElem);
domInstance_effectRef instEffect =
colInstElem->getDOMElementAs<domInstance_effect>();
const domFx_profile_abstract_Array &profiles =
effect->getFx_profile_abstract_array();
for(UInt32 i = 0; i < profiles.getCount(); ++i)
{
if(domProfile_COMMON::ID() == profiles[i]->typeID())
{
retVal = createInstanceProfileCommon(
daeSafeCast<domProfile_COMMON>(profiles[i]),
effect, instEffect);
}
else if(domProfile_GLSL::ID() == profiles[i]->typeID())
{
retVal = createInstanceProfileGLSL(
daeSafeCast<domProfile_GLSL>(profiles[i]),
effect, instEffect);
}
else if(domProfile_CG::ID() == profiles[i]->typeID())
{
retVal = createInstanceProfileCG(
daeSafeCast<domProfile_CG>(profiles[i]),
effect, instEffect);
}
}
editInstStore().push_back(retVal);
return retVal;
}
/*! Return parameter of the effect (\c <newparam> tags) with the given \a name.
*/
daeElement *
ColladaEffect::findDOMParam(const std::string &name) const
{
ParamSampler2DMapConstIt sampler2DIt = _sampler2DParams.find(name);
if(sampler2DIt != _sampler2DParams.end())
{
return sampler2DIt->second.sampler2D;
}
ParamSurfaceMapConstIt surfaceIt = _surfaceParams.find(name);
if(surfaceIt != _surfaceParams.end())
{
return surfaceIt->second.surface;
}
}
/*! Return the loader element for the parameter of the effect (\c <newparam>
tags) with the given \a name.
*/
ColladaElement *
ColladaEffect::findParam(const std::string &name) const
{
ParamSampler2DMapConstIt sampler2DIt = _sampler2DParams.find(name);
if(sampler2DIt != _sampler2DParams.end())
{
return sampler2DIt->second.colSampler2D;
}
ParamSurfaceMapConstIt surfaceIt = _surfaceParams.find(name);
if(surfaceIt != _surfaceParams.end())
{
return surfaceIt->second.colSurface;
}
}
ColladaEffect::ColladaEffect(daeElement *elem, ColladaGlobal *global)
: Inherited(elem, global)
{
}
ColladaEffect::~ColladaEffect(void)
{
}
/*! Fills internal data structures for \c <profile_COMMON>.
This mainly collects relevant parameters so they can be looked up
efficiently when creating an instance of this effect.
*/
void
ColladaEffect::readProfileCommon(domProfile_COMMON *prof)
{
if(prof == NULL)
{
SWARNING << "ColladaEffect::readProfileCommon: No DOM element or "
<< "incorrect type." << std::endl;
return;
}
readImageArray(prof->getImage_array());
const domCommon_newparam_type_Array &newParams = prof->getNewparam_array();
for(UInt32 i = 0; i < newParams.getCount(); ++i)
{
// must read surface params before sampler params, because their <source>
// element can refer to a surface
domFx_surface_commonRef paramSurface =
newParams[i]->getSurface();
if(paramSurface != NULL)
{
ParamSurface param;
param.surface = paramSurface;
param.colSurface = dynamic_pointer_cast<ColladaSurface>(
ColladaElementFactory::the()->create(
paramSurface, getGlobal()));
param.colSurface->read();
_surfaceParams[newParams[i]->getSid()] = param;
continue;
}
domFx_sampler2D_commonRef paramSampler2D =
newParams[i]->getSampler2D();
if(paramSampler2D != NULL)
{
ParamSampler2D param;
param.sampler2D = paramSampler2D;
param.colSampler2D = dynamic_pointer_cast<ColladaSampler2D>(
ColladaElementFactory::the()->create(
paramSampler2D, getGlobal()));
param.colSampler2D->setEffect(this);
param.colSampler2D->read ( );
_sampler2DParams[newParams[i]->getSid()] = param;
continue;
}
// param is unhandled if we get to here
SWARNING << "ColladaEffect::readProfileCommon: Unhandled <newparam> ["
<< newParams[i]->getSid() << "]."
<< std::endl;
}
domProfile_COMMON::domTechniqueRef tech = prof->getTechnique();
readImageArray(tech->getImage_array());
}
void
ColladaEffect::readProfileGLSL(domProfile_GLSL *prof)
{
SWARNING << "ColladaEffect::readProfileGLSL: NIY." << std::endl;
}
void
ColladaEffect::readProfileCG(domProfile_CG *prof)
{
SWARNING << "ColladaEffect::readProfileCG: NIY." << std::endl;
}
/*! Create an OpenSG material that matches this \c <profile_COMMON> material
(to the extent possible).
*/
MaterialTransitPtr
ColladaEffect::createInstanceProfileCommon(
domProfile_COMMON *prof, domEffect *effect,
domInstance_effect *instEffect )
{
OSG_COLLADA_LOG(("ColladaEffect::createInstanceProfileCommon\n"));
ColladaInstanceEffectRefPtr colInstEffect =
getUserDataAs<ColladaInstanceEffect>(instEffect);
domProfile_COMMON::domTechniqueRef tech = prof->getTechnique();
domCommon_color_or_texture_typeRef emission = NULL;
domCommon_color_or_texture_typeRef ambient = NULL;
domCommon_color_or_texture_typeRef diffuse = NULL;
domCommon_color_or_texture_typeRef specular = NULL;
domCommon_float_or_param_typeRef shininess = NULL;
if(tech->getConstant() != NULL)
{
OSG_COLLADA_LOG(("ColladaEffect::createInstanceProfileCommon: "
"using <constant> shader\n"));
domProfile_COMMON::domTechnique::domConstantRef constant =
tech->getConstant();
emission = constant->getEmission();
}
else if(tech->getLambert() != NULL)
{
OSG_COLLADA_LOG(("ColladaEffect::createInstanceProfileCommon: "
"using <lambert> shader\n"));
domProfile_COMMON::domTechnique::domLambertRef lambert =
tech->getLambert();
emission = lambert->getEmission();
ambient = lambert->getAmbient ();
diffuse = lambert->getDiffuse ();
}
else if(tech->getPhong() != NULL)
{
OSG_COLLADA_LOG(("ColladaEffect::createInstanceProfileCommon: "
"using <phong> shader\n"));
domProfile_COMMON::domTechnique::domPhongRef phong =
tech->getPhong();
emission = phong->getEmission ();
ambient = phong->getAmbient ();
diffuse = phong->getDiffuse ();
specular = phong->getSpecular ();
shininess = phong->getShininess();
}
else if(tech->getBlinn() != NULL)
{
OSG_COLLADA_LOG(("ColladaEffect::createInstanceProfileCommon: "
"using <blinn> shader\n"));
domProfile_COMMON::domTechnique::domBlinnRef blinn =
tech->getBlinn();
emission = blinn->getEmission ();
ambient = blinn->getAmbient ();
diffuse = blinn->getDiffuse ();
specular = blinn->getSpecular ();
shininess = blinn->getShininess();
}
UInt32 texCount = 0;
ChunkMaterialUnrecPtr mat = ChunkMaterial::create();
MaterialChunkUnrecPtr matChunk = MaterialChunk::create();
getGlobal()->getStatCollector()->getElem(
ColladaGlobal::statNMaterialCreated)->inc();
if(emission != NULL)
{
domCommon_color_or_texture_type::domColorRef color;
domCommon_color_or_texture_type::domParamRef param;
domCommon_color_or_texture_type::domTextureRef texture;
fillColorParamTex(emission, color, param, texture);
if(color != NULL)
{
Color4f colVal(color->getValue()[0],
color->getValue()[1],
color->getValue()[2],
color->getValue()[3] );
matChunk->setEmission(colVal);
}
else if(param != NULL)
{
SWARNING << "ColladaEffect::createInstanceProfileCommon: "
<< "<emission>/<param> not supported."
<< std::endl;
}
else if(texture != NULL)
{
xsNCName texId = texture->getTexture ();
xsNCName tcSemantic = texture->getTexcoord();
SWARNING << "ColladaEffect::createInstanceProfileCommon: "
<< "<emission>/<texture> not supported."
<< std::endl;
}
}
if(ambient != NULL)
{
domCommon_color_or_texture_type::domColorRef color;
domCommon_color_or_texture_type::domParamRef param;
domCommon_color_or_texture_type::domTextureRef texture;
fillColorParamTex(ambient, color, param, texture);
if(color != NULL)
{
Color4f colVal(color->getValue()[0],
color->getValue()[1],
color->getValue()[2],
color->getValue()[3] );
matChunk->setAmbient(colVal);
}
else if(param != NULL)
{
SWARNING << "ColladaEffect::createInstanceProfileCommon: "
<< "<ambient>/<param> not supported."
<< std::endl;
}
else if(texture != NULL)
{
std::string texId = texture->getTexture ();
std::string tcSemantic = texture->getTexcoord();
ParamSampler2DMapConstIt paramIt = _sampler2DParams.find(texId);
if(paramIt != _sampler2DParams.end())
{
addTexture(texId, tcSemantic, colInstEffect,
paramIt->second.colSampler2D, mat, GL_REPLACE,
texCount );
}
else
{
SWARNING << "ColladaEffect::createInstanceProfileCommon: "
<< "<ambient>/<texture> could not find sampler2D ["
<< texId << "]."
<< std::endl;
}
}
}
if(diffuse != NULL)
{
domCommon_color_or_texture_type::domColorRef color;
domCommon_color_or_texture_type::domParamRef param;
domCommon_color_or_texture_type::domTextureRef texture;
fillColorParamTex(diffuse, color, param, texture);
if(color != NULL)
{
Color4f colVal(color->getValue()[0],
color->getValue()[1],
color->getValue()[2],
color->getValue()[3] );
matChunk->setDiffuse(colVal);
}
else if(param != NULL)
{
SWARNING << "ColladaEffect::createInstanceProfileCommon: "
<< "<diffuse>/<param> not supported."
<< std::endl;
}
else if(texture != NULL)
{
std::string texId = texture->getTexture ();
std::string tcSemantic = texture->getTexcoord();
ParamSampler2DMapConstIt paramIt = _sampler2DParams.find(texId);
if(paramIt != _sampler2DParams.end())
{
addTexture(texId, tcSemantic, colInstEffect,
paramIt->second.colSampler2D, mat, GL_MODULATE,
texCount );
}
else
{
SWARNING << "ColladaEffect::createInstanceProfileCommon: "
<< "<diffuse>/<texture> could not find sampler2D ["
<< texId << "]."
<< std::endl;
}
matChunk->setDiffuse(Color4f(0.8f, 0.8f, 0.8f, 1.f));
}
}
if(specular != NULL)
{
domCommon_color_or_texture_type::domColorRef color;
domCommon_color_or_texture_type::domParamRef param;
domCommon_color_or_texture_type::domTextureRef texture;
fillColorParamTex(specular, color, param, texture);
if(color != NULL)
{
Color4f colVal(color->getValue()[0],
color->getValue()[1],
color->getValue()[2],
color->getValue()[3] );
matChunk->setSpecular(colVal);
}
else if(param != NULL)
{
SWARNING << "ColladaEffect::createInstanceProfileCommon: "
<< "<specular>/<param> not supported."
<< std::endl;
}
else if(texture != NULL)
{
xsNCName texId = texture->getTexture ();
xsNCName tcSemantic = texture->getTexcoord();
SWARNING << "ColladaEffect::createInstanceProfileCommon: "
<< "<specular>/<texture> not supported."
<< std::endl;
}
}
if(shininess != NULL)
{
domCommon_float_or_param_type::domFloatRef value;
domCommon_float_or_param_type::domParamRef param;
fillFloatParam(shininess, value, param);
if(value != NULL)
{
matChunk->setShininess(value->getValue());
}
else if(param != NULL)
{
SWARNING << "ColladaEffect::createInstanceProfileCommon: "
<< "<shininess>/<param> not supported."
<< std::endl;
}
}
mat->addChunk(matChunk);
return MaterialTransitPtr(mat);
}
MaterialTransitPtr
ColladaEffect::createInstanceProfileGLSL(
domProfile_GLSL *prof, domEffect *effect,
domInstance_effect *instEffect )
{
SWARNING << "ColladaEffect::createInstanceProfileGLSL: NIY" << std::endl;
return MaterialTransitPtr();
}
MaterialTransitPtr
ColladaEffect::createInstanceProfileCG(
domProfile_CG *prof, domEffect *effect,
domInstance_effect *instEffect )
{
SWARNING << "ColladaEffect::createInstanceProfileCG: NIY" << std::endl;
return MaterialTransitPtr();
}
void
ColladaEffect::readImageArray(const domImage_Array &images)
{
for(UInt32 i = 0; i < images.getCount(); ++i)
{
ColladaImageRefPtr colImg = getUserDataAs<ColladaImage>(images[i]);
if(colImg == NULL)
{
colImg = dynamic_pointer_cast<ColladaImage>(
ColladaElementFactory::the()->create(
images[i], getGlobal()));
colImg->read();
}
}
}
void
ColladaEffect::fillColorParamTex(
domCommon_color_or_texture_type *colTex,
domCommon_color_or_texture_type::domColorRef &colOut,
domCommon_color_or_texture_type::domParamRef ¶mOut,
domCommon_color_or_texture_type::domTextureRef &texOut )
{
if(colTex != NULL)
{
colOut = colTex->getColor ();
paramOut = colTex->getParam ();
texOut = colTex->getTexture();
}
}
void
ColladaEffect::fillFloatParam(
domCommon_float_or_param_type *floatParam,
domCommon_float_or_param_type::domFloatRef &floatOut,
domCommon_float_or_param_type::domParamRef ¶mOut )
{
if(floatParam != NULL)
{
floatOut = floatParam->getFloat();
paramOut = floatParam->getParam();
}
}
void
ColladaEffect::addTexture(
const std::string &texId, const std::string &tcSemantic,
ColladaInstanceEffect *colInstEffect, ColladaSampler2D *colSampler2D,
ChunkMaterial *mat, GLenum envMode,
UInt32 &texCount )
{
TextureEnvChunkUnrecPtr texEnv = TextureEnvChunk::create();
texEnv->setEnvMode(envMode);
mat->addChunk(colSampler2D->getTexture(), texCount);
mat->addChunk(texEnv, texCount);
OSG_COLLADA_LOG(("ColladaEffect::addTexture: "
"texCoord symbol [%s] in slot [%d]\n",
tcSemantic.c_str(),
Geometry::TexCoordsIndex + texCount));
colInstEffect->editTCMap()[tcSemantic] =
Geometry::TexCoordsIndex + texCount;
++texCount;
}
OSG_END_NAMESPACE
#endif // OSG_WITH_COLLADA