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 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
|
/*
* Copyright 2006 Sony Computer Entertainment Inc.
*
* Licensed under the SCEA Shared Source License, Version 1.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at:
* http://research.scea.com/scea_shared_source_license.html
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and limitations under the
* License.
*/
#include "daeReader.h"
#include "ReaderWriterDAE.h"
#include <dae.h>
#include <dae/daeSIDResolver.h>
#include <dae/domAny.h>
#include <dom/domCOLLADA.h>
#include <dom/domProfile_COMMON.h>
#include <dom/domConstants.h>
#include <osg/BlendColor>
#include <osg/BlendFunc>
#include <osg/Texture2D>
#include <osg/TexEnv>
#include <osg/LightModel>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <sstream>
using namespace osgDAE;
#ifdef COLLADA_DOM_2_4_OR_LATER
#include <dom/domAny.h>
using namespace ColladaDOM141;
#endif
template <typename T>
void daeReader::getTransparencyCounts(daeDatabase* database, int& transparentCount, int& opaqueCount) const
{
std::vector<T*> constantVec;
database->typeLookup(constantVec);
for (size_t i = 0; i < constantVec.size(); ++i)
{
if (const domCommon_transparent_type* pTransparent = constantVec[i]->getTransparent())
{
domFx_opaque_enum opaque = pTransparent->getOpaque();
if (opaque == FX_OPAQUE_ENUM_RGB_ZERO)
{
++opaqueCount;
continue;
}
}
if (const domCommon_float_or_param_type* pTransparency = constantVec[i]->getTransparency())
{
float transparency;
domFloat transparencyParam = 1.0;
if (pTransparency->getFloat())
{
transparency = pTransparency->getFloat()->getValue();
}
else if (pTransparency->getParam() &&
GetFloatParam(pTransparency->getParam()->getRef(), transparencyParam))
{
transparency = transparencyParam;
}
else
{
continue;
}
if (transparency < 0.01f)
{
++transparentCount;
}
else if (transparency > 0.99f)
{
++opaqueCount;
}
}
}
}
bool daeReader::findInvertTransparency(daeDatabase* database) const
{
int transparentCount = 0, opaqueCount = 0;
getTransparencyCounts<domProfile_COMMON::domTechnique::domConstant>(database, transparentCount, opaqueCount);
getTransparencyCounts<domProfile_COMMON::domTechnique::domLambert>(database, transparentCount, opaqueCount);
getTransparencyCounts<domProfile_COMMON::domTechnique::domPhong>(database, transparentCount, opaqueCount);
getTransparencyCounts<domProfile_COMMON::domTechnique::domBlinn>(database, transparentCount, opaqueCount);
return transparentCount > opaqueCount;
}
// <bind_material>
// elements:
// 0..* <param>
// name
// sid
// semantic
// type
// 1 <technique_common>
// 0..* <instance_material>
// symbol
// target
// sid
// name
// 0..* <technique>
// profile
// 0..* <extra>
// id
// name
// type
void daeReader::processBindMaterial( domBind_material *bm, domGeometry *geom, osg::Geode *geode, osg::Geode *cachedGeode )
{
if (bm->getTechnique_common() == NULL )
{
OSG_WARN << "No COMMON technique for bind_material" << std::endl;
return;
}
for (size_t i =0; i < geode->getNumDrawables(); i++)
{
osg::Drawable* drawable = geode->getDrawable(i);
std::string materialName = drawable->getName();
osg::Geometry *cachedGeometry = dynamic_cast<osg::Geometry*>(cachedGeode->getDrawable(i)->asGeometry());
domInstance_material_Array &ima = bm->getTechnique_common()->getInstance_material_array();
std::string symbol;
bool found = false;
for ( size_t j = 0; j < ima.getCount(); j++)
{
symbol = ima[j]->getSymbol();
if (symbol.compare(materialName) == 0)
{
found = true;
domMaterial *mat = daeSafeCast< domMaterial >(getElementFromURI( ima[j]->getTarget()));
if (mat)
{
// Check material cache if this material already exists
osg::StateSet* ss;
domMaterialStateSetMap::iterator iter = _materialMap.find( mat );
if (iter != _materialMap.end() )
{
// Reuse material
ss = iter->second.get();
}
else
{
// Create new material
ss = new osg::StateSet;
processMaterial(ss, mat);
_materialMap.insert(std::make_pair(mat, ss));
}
drawable->setStateSet(ss);
// Need to process bind_vertex_inputs here
// 1. Clear the texcoord arrays and associated texcoord vertex indices
// from the current (cloned) drawable.
osg::Geometry *clonedGeometry = drawable->asGeometry();
if (NULL == clonedGeometry)
{
OSG_WARN << "Failed to convert drawable to geometry object" << std::endl;
break;
}
clonedGeometry->getTexCoordArrayList().clear();
// 2. For each possible texture unit find the correct texcoord array and
// indices from the cached drawable and place in the cloned drawable
// in the correct texture unit slot
unsigned int textureUnit(0);
if (copyTextureCoordinateSet(ss, cachedGeometry, clonedGeometry, ima[j], AMBIENT_OCCLUSION_UNIT, textureUnit)) ++textureUnit;
if (copyTextureCoordinateSet(ss, cachedGeometry, clonedGeometry, ima[j], MAIN_TEXTURE_UNIT , textureUnit)) ++textureUnit;
if (copyTextureCoordinateSet(ss, cachedGeometry, clonedGeometry, ima[j], TRANSPARENCY_MAP_UNIT , textureUnit)) ++textureUnit;
}
else
{
OSG_WARN << "Failed to locate <material> with id " << ima[i]->getTarget().getURI() << std::endl;
}
break;
}
}
if (!found)
{
OSG_WARN << "Failed to locate <instance_material> with symbol " << materialName << std::endl;
}
}
}
// <material>
// attributes:
// 0..1 id
// 0..1 name
// elements:
// 0..1 <asset>
// 1 <instance_effect>
// 0..* <extra>
void daeReader::processMaterial(osg::StateSet *ss, domMaterial *mat )
{
if (!mat)
{
return;
}
if (mat->getName()) {
ss->setName(mat->getName());
}
_currentInstance_effect = mat->getInstance_effect();
if (!_currentInstance_effect)
{
return;
}
domEffect *effect = daeSafeCast< domEffect >( getElementFromURI( _currentInstance_effect->getUrl() ) );
if (effect)
{
processEffect(ss, effect);
//TODO: process all of the setParams that could happen here in the material. ESP. the textures
}
else
{
OSG_WARN << "Failed to locate effect " << mat->getInstance_effect()->getUrl().getURI() << std::endl;
}
}
// <effect>
// attributes:
// 1 id
// 0..1 name
// elements:
// 0..1 <asset>
// 0..* <annotate>
// 0..* <image>
// 0..* <newparam>
// 1..* <fx_profile_abstract>
// 0..* <extra>
void daeReader::processEffect(osg::StateSet *ss, domEffect *effect )
{
bool hasCOMMON = false;
for ( size_t i = 0; i < effect->getFx_profile_abstract_array().getCount(); i++ )
{
domProfile_COMMON *pc = daeSafeCast< domProfile_COMMON >( effect->getFx_profile_abstract_array()[i] );
if (pc != NULL )
{
if (hasCOMMON )
{
OSG_WARN << "Effect already has a profile_COMMON. Skipping this one" << std::endl;
continue;
}
_currentEffect = effect;
processProfileCOMMON(ss, pc);
hasCOMMON = true;
continue;
}
OSG_WARN << "unsupported effect profile " << effect->getFx_profile_abstract_array()[i]->getTypeName() << std::endl;
}
}
// <profile_COMMON>
// elements:
// 0..* <image>, <newparam>
// 1 <technique>
// attributes:
// elements:
// 0..1 <asset>
// 0..* <image>, <newparam>
// 1 <constant>, <lambert>, <phong>, <blinn>
// 0..* <extra>
// 0..* <extra>
void daeReader::processProfileCOMMON(osg::StateSet *ss, domProfile_COMMON *pc )
{
domProfile_COMMON::domTechnique *teq = pc->getTechnique();
domProfile_COMMON::domTechnique::domConstant *c = teq->getConstant();
domProfile_COMMON::domTechnique::domLambert *l = teq->getLambert();
domProfile_COMMON::domTechnique::domPhong *p = teq->getPhong();
domProfile_COMMON::domTechnique::domBlinn *b = teq->getBlinn();
ss->setMode( GL_CULL_FACE, osg::StateAttribute::ON ); // Cull Back faces
// See if there are any extra's that are supported by OpenSceneGraph
const domExtra_Array& ExtraArray = pc->getExtra_array();
size_t NumberOfExtras = ExtraArray.getCount();
size_t CurrentExtra;
for (CurrentExtra = 0; CurrentExtra < NumberOfExtras; CurrentExtra++)
{
const domTechnique_Array& TechniqueArray = ExtraArray[CurrentExtra]->getTechnique_array();
size_t NumberOfTechniques = TechniqueArray.getCount();
size_t CurrentTechnique;
for (CurrentTechnique = 0; CurrentTechnique < NumberOfTechniques; CurrentTechnique++)
{
// <technique profile="GOOGLEEARTH">
// <double_sided>0</double_sided>
// </technique>
const domTechniqueRef& TechniqueRef = TechniqueArray[CurrentTechnique];
if (TechniqueRef->getProfile() && strcmp(TechniqueRef->getProfile(), "GOOGLEEARTH") == 0)
{
const daeElementRefArray& ElementArray = TechniqueRef->getContents();
size_t NumberOfElements = ElementArray.getCount();
size_t CurrentElement;
for (CurrentElement = 0; CurrentElement < NumberOfElements; CurrentElement++)
{
domAny* pAny = (domAny*)ElementArray[CurrentElement].cast();
if (strcmp(pAny->getElementName(), "double_sided") == 0)
{
daeString Value = pAny->getValue();
if (Value && strcmp(Value, "1") == 0)
ss->setMode( GL_CULL_FACE, osg::StateAttribute::OFF );
}
}
}
}
}
osg::ref_ptr< osg::Material > mat = new osg::Material();
// <blinn>
// elements:
// 0..1 <emission>
// 0..1 <ambient>
// 0..1 <diffuse>
// 0..1 <specular>
// 0..1 <shininess>
// 0..1 <reflective>
// 0..1 <reflectivity>
// 0..1 <transparent>
// 0..1 <transparency>
// 0..1 <index_of_refraction>
if (b != NULL )
{
osg::Texture2D *EmissionStateAttribute = NULL;
osg::Texture2D *AmbientStateAttribute = NULL;
osg::Texture2D *DiffuseStateAttribute = NULL;
processColorOrTextureType(ss, b->getEmission(), osg::Material::EMISSION, mat.get(), NULL, &EmissionStateAttribute );
if (NULL != EmissionStateAttribute)
OSG_WARN << "Currently no support for <texture> in Emission channel " << std::endl;
processColorOrTextureType(ss, b->getAmbient(), osg::Material::AMBIENT, mat.get(), NULL, &AmbientStateAttribute );
processColorOrTextureType(ss, b->getDiffuse(), osg::Material::DIFFUSE, mat.get(), NULL, &DiffuseStateAttribute );
if (DiffuseStateAttribute != NULL )
{
if (AmbientStateAttribute != NULL )
{
// Set the ambient and diffuse colour white so that the incoming fragment colour ends up as a
// lit white colour. I modulate both textures onto this to approximate the lighting equation.
// Using a zero diffuse and then an ADD of the diffuse texture seems overlit to me.
mat->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
mat->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
// Use the ambient texture map as an occlusion map.
unsigned int textureUnit( _pluginOptions.usePredefinedTextureUnits ? AMBIENT_OCCLUSION_UNIT : 0);
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::MODULATE) );
ss->setTextureAttribute( textureUnit, AmbientStateAttribute );
// Modulate in the diffuse texture
textureUnit = _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 1;
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::MODULATE) );
ss->setTextureAttribute( textureUnit, DiffuseStateAttribute );
}
else
{
// Set the diffuse colour white so that the incoming fragment colour ends up as the global diffuse lighting colour
// plus any constant ambient contribution after the lighting calculation. This means that I am modulating the the
// ambient with the texture as well but I cannot see a way of avoiding that.
mat->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
unsigned int textureUnit( _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 0);
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::MODULATE) );
ss->setTextureAttribute( textureUnit, DiffuseStateAttribute );
}
}
else
{
if (NULL != AmbientStateAttribute )
OSG_WARN << "Ambient occlusion map only supported when diffuse texture also specified" << std::endl;
}
if (processColorOrTextureType(ss, b->getSpecular(), osg::Material::SPECULAR, mat.get(), b->getShininess() ) && (NULL != DiffuseStateAttribute) )
{
// Diffuse texture will defeat specular highlighting
// So postpone specular - Not sure if I should do this here
// because it will override any global light model states
osg::LightModel* lightmodel = new osg::LightModel;
lightmodel->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR);
ss->setAttributeAndModes(lightmodel, osg::StateAttribute::ON);
}
processTransparencySettings(b->getTransparent(), b->getTransparency(), ss, mat.get(), _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 0 );
}
// <phong>
// elements:
// 0..1 <emission>
// 0..1 <ambient>
// 0..1 <diffuse>
// 0..1 <specular>
// 0..1 <shininess>
// 0..1 <reflective>
// 0..1 <reflectivity>
// 0..1 <transparent>
// 0..1 <transparency>
// 0..1 <index_of_refraction>
else if (p != NULL )
{
osg::Texture2D *EmissionStateAttribute = NULL;
osg::Texture2D *AmbientStateAttribute = NULL;
osg::Texture2D *DiffuseStateAttribute = NULL;
processColorOrTextureType(ss, p->getEmission(), osg::Material::EMISSION, mat.get(), NULL, &EmissionStateAttribute );
if (NULL != EmissionStateAttribute)
OSG_WARN << "Currently no support for <texture> in Emission channel " << std::endl;
processColorOrTextureType(ss, p->getAmbient(), osg::Material::AMBIENT, mat.get(), NULL, &AmbientStateAttribute );
processColorOrTextureType(ss, p->getDiffuse(), osg::Material::DIFFUSE, mat.get(), NULL, &DiffuseStateAttribute );
if (DiffuseStateAttribute != NULL )
{
if (AmbientStateAttribute != NULL )
{
// Set the ambient and diffuse colour white so that the incoming fragment colour ends up as a
// lit white colour. I modulate both textures onto this to approximate the lighting equation.
// Using a zero diffuse and then an ADD of the diffuse texture seems overlit to me.
mat->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
mat->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
// Use the ambient texture map as an occlusion map.
unsigned int textureUnit( _pluginOptions.usePredefinedTextureUnits ? AMBIENT_OCCLUSION_UNIT : 0);
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::MODULATE) );
ss->setTextureAttribute( textureUnit, AmbientStateAttribute );
// Modulate in the diffuse texture
textureUnit = _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 1;
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::MODULATE) );
ss->setTextureAttribute( textureUnit, DiffuseStateAttribute );
}
else
{
// Set the diffuse colour white so that the incoming fragment colour ends up as the global diffuse lighting colour
// plus any constant ambient contribution after the lighting calculation. This means that I am modulating the the
// ambient with the texture as well but I cannot see a way of avoiding that.
mat->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
unsigned int textureUnit( _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 0);
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::MODULATE) );
ss->setTextureAttribute( textureUnit, DiffuseStateAttribute );
}
}
else
{
if (NULL != AmbientStateAttribute )
OSG_WARN << "Ambient occlusion map only supported when diffuse texture also specified" << std::endl;
}
if (processColorOrTextureType(ss, p->getSpecular(), osg::Material::SPECULAR, mat.get(), p->getShininess() ) && (NULL != DiffuseStateAttribute) )
{
// Diffuse texture will defeat specular highlighting
// So postpone specular - Not sure if I should do this here
// because it will override any global light model states
osg::LightModel* lightmodel = new osg::LightModel;
lightmodel->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR);
ss->setAttributeAndModes(lightmodel, osg::StateAttribute::ON);
}
processTransparencySettings(p->getTransparent(), p->getTransparency(), ss, mat.get(), _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 0 );
}
// <lambert>
// elements:
// 0..1 <emission>
// 0..1 <ambient>
// 0..1 <diffuse>
// 0..1 <reflective>
// 0..1 <reflectivity>
// 0..1 <transparent>
// 0..1 <transparency>
// 0..1 <index_of_refraction>
else if (l != NULL )
{
osg::Texture2D *EmissionStateAttribute = NULL;
osg::Texture2D *AmbientStateAttribute = NULL;
osg::Texture2D *DiffuseStateAttribute = NULL;
processColorOrTextureType(ss, l->getEmission(), osg::Material::EMISSION, mat.get(), NULL, &EmissionStateAttribute );
if (NULL != EmissionStateAttribute)
OSG_WARN << "Currently no support for <texture> in Emission channel " << std::endl;
processColorOrTextureType(ss, l->getAmbient(), osg::Material::AMBIENT, mat.get(), NULL, &AmbientStateAttribute);
processColorOrTextureType(ss, l->getDiffuse(), osg::Material::DIFFUSE, mat.get(), NULL, &DiffuseStateAttribute );
if (DiffuseStateAttribute != NULL )
{
if (AmbientStateAttribute != NULL )
{
// Set the ambient and diffuse colour white so that the incoming fragment colour ends up as a
// lit white colour. I modulate both textures onto this to approximate the lighting equation.
// Using a zero diffuse and then an ADD of the diffuse texture seems overlit to me.
mat->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
mat->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
// Use the ambient texture map as an occlusion map.
unsigned int textureUnit( _pluginOptions.usePredefinedTextureUnits ? AMBIENT_OCCLUSION_UNIT : 0);
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::MODULATE) );
ss->setTextureAttribute( textureUnit, AmbientStateAttribute );
// Modulate in the diffuse texture
textureUnit = _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 1;
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::MODULATE) );
ss->setTextureAttribute( textureUnit, DiffuseStateAttribute );
}
else
{
// Set the diffuse colour white so that the incoming fragment colour ends up as the global diffuse lighting colour
// plus any constant ambient contribution after the lighting calculation. This means that I am modulating the the
// ambient with the texture as well but I cannot see a way of avoiding that.
mat->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
unsigned int textureUnit( _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 0);
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::MODULATE) );
ss->setTextureAttribute( textureUnit, DiffuseStateAttribute );
}
}
else
{
if (NULL != AmbientStateAttribute )
OSG_WARN << "Ambient occlusion map only supported when diffuse texture also specified" << std::endl;
}
processTransparencySettings(l->getTransparent(), l->getTransparency(), ss, mat.get(), _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 0 );
}
// <constant>
// elements:
// 0..1 <emission>
// 0..1 <reflective>
// 0..1 <reflectivity>
// 0..1 <transparent>
// 0..1 <transparency>
// 0..1 <index_of_refraction>
else if (c != NULL )
{
osg::Texture2D *sa = NULL;
processColorOrTextureType(ss, c->getEmission(), osg::Material::EMISSION, mat.get(), NULL, &sa );
if (sa != NULL )
{
unsigned int textureUnit( _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 0);
ss->setTextureMode( textureUnit, GL_TEXTURE_2D, GL_TRUE );
ss->setTextureAttribute( textureUnit, new osg::TexEnv(osg::TexEnv::REPLACE) );
ss->setTextureAttribute( textureUnit, sa );
}
// Use the emission colour as the main colour in transparency calculations
mat->setDiffuse(osg::Material::FRONT_AND_BACK, mat->getEmission(osg::Material::FRONT_AND_BACK));
processTransparencySettings(c->getTransparent(), c->getTransparency(), ss, mat.get(), _pluginOptions.usePredefinedTextureUnits ? MAIN_TEXTURE_UNIT : 0 );
// Kill the lighting
mat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0));
mat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.0, 0.0, 1.0));
}
ss->setAttribute( mat.get() );
}
// colorOrTexture
// 1 of
// <color>
// <param>
// attributes:
// 1 ref
// <texture>
// attributes:
// 1 texture
// 1 texcoord
// 0..* extra
bool daeReader::processColorOrTextureType(const osg::StateSet* ss,
domCommon_color_or_texture_type *cot,
osg::Material::ColorMode channel,
osg::Material *mat,
domCommon_float_or_param_type *fop,
osg::Texture2D **sa,
bool blinn)
{
if (cot == NULL )
{
return false;
}
bool retVal = false;
//osg::StateAttribute *sa = NULL;
//TODO: Make all channels process <param ref=""> type of value
if (channel == osg::Material::EMISSION )
{
if (cot->getColor() != NULL )
{
domFloat4 &f4 = cot->getColor()->getValue();
mat->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4( f4[0], f4[1], f4[2], (f4.getCount()==4)? f4[3] : 1.0 ) );
retVal = true;
}
else if (cot->getParam() != NULL)
{
domFloat4 f4;
if (GetFloat4Param(cot->getParam()->getRef(), f4))
{
mat->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4( f4[0], f4[1], f4[2], f4[3] ) );
retVal = true;
}
}
else if (cot->getTexture() != NULL)
{
if (sa != NULL)
{
*sa = processTexture( cot->getTexture(), ss, MAIN_TEXTURE_UNIT);
retVal = true;
}
else
OSG_WARN << "Currently no support for <texture> in Emission channel " << std::endl;
}
else
{
OSG_WARN << "Missing <color>, <param> or <texture> in Emission channel " << std::endl;
}
}
else if (channel == osg::Material::AMBIENT )
{
if (cot->getColor() != NULL )
{
domFloat4 &f4 = cot->getColor()->getValue();
mat->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4( f4[0], f4[1], f4[2], (f4.getCount()==4)? f4[3] : 1.0 ) );
retVal = true;
}
else if (cot->getParam() != NULL)
{
domFloat4 f4;
if (cot->getParam()->getRef() != 0 && GetFloat4Param(cot->getParam()->getRef(), f4))
{
mat->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4( f4[0], f4[1], f4[2], f4[3] ) );
retVal = true;
}
}
else if (cot->getTexture() != NULL)
{
if (sa != NULL)
*sa = processTexture( cot->getTexture(), ss, AMBIENT_OCCLUSION_UNIT);
else
{
OSG_WARN << "Currently no support for <texture> in Ambient channel " << std::endl;
mat->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.2f, 0.2f, 0.2f, 1.0f ) );
}
retVal = true;
}
else
{
OSG_WARN << "Missing <color>, <param> or <texture> in Ambient channel " << std::endl;
}
}
else if (channel == osg::Material::DIFFUSE )
{
if (cot->getColor() != NULL)
{
domFloat4 &f4 = cot->getColor()->getValue();
mat->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4( f4[0], f4[1], f4[2], (f4.getCount()==4)? f4[3] : 1.0 ) );
retVal = true;
}
else if (cot->getTexture() != NULL)
{
if (sa != NULL)
*sa = processTexture( cot->getTexture(), ss, MAIN_TEXTURE_UNIT);
else
{
OSG_WARN << "Currently no support for <texture> in Diffuse channel " << std::endl;
mat->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.8f, 0.8f, 0.8f, 1.0f ) );
}
domExtra *extra = cot->getTexture()->getExtra();
if (extra != NULL && extra->getType() != NULL && strcmp( extra->getType(), "color" ) == 0 )
{
//the extra data for osg. Diffuse color can happen with a texture.
for ( unsigned int i = 0; i < extra->getTechnique_array().getCount(); i++ )
{
domTechnique *teq = extra->getTechnique_array()[i];
if (strcmp( teq->getProfile(), "SCEI" ) == 0 )
{
osg::Vec4 col;
domAny *dcol = (domAny*)(daeElement*)teq->getContents()[0];
std::istringstream diffuse_colour((const char *)dcol->getValue());
diffuse_colour >> col.r() >> col.g() >> col.b() >> col.a();
mat->setDiffuse( osg::Material::FRONT_AND_BACK, col );
break;
}
}
}
retVal = true;
}
else if (cot->getParam() != NULL)
{
domFloat4 f4;
if (GetFloat4Param(cot->getParam()->getRef(), f4))
{
mat->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4( f4[0], f4[1], f4[2], f4[3] ) );
retVal = true;
}
}
else
{
OSG_WARN << "Missing <color>, <param> or <texture> in Diffuse channel " << std::endl;
}
}
else if (channel == osg::Material::SPECULAR )
{
if (cot->getColor() != NULL )
{
domFloat4 &f4 = cot->getColor()->getValue();
mat->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4( f4[0], f4[1], f4[2], (f4.getCount()==4)? f4[3] : 1.0 ) );
retVal = true;
}
else if (cot->getParam() != NULL)
{
domFloat4 f4;
if (GetFloat4Param(cot->getParam()->getRef(), f4))
{
mat->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4( f4[0], f4[1], f4[2], f4[3] ) );
retVal = true;
}
}
else if (cot->getTexture() != NULL)
{
OSG_WARN << "Currently no support for <texture> in Specular channel " << std::endl;
}
else
{
OSG_WARN << "Missing <color>, <param> or <texture> in Specular channel " << std::endl;
}
if (fop != NULL && fop->getFloat() != NULL )
{
float shininess = fop->getFloat()->getValue();
if (blinn)
{
// If the blinn mode is in the range [0,1] rescale it to [0,128]
if (shininess < 1)
shininess *= 128.0f;
}
mat->setShininess( osg::Material::FRONT_AND_BACK, shininess );
}
}
return retVal;
}
bool daeReader::GetFloat4Param(xsNCName Reference, domFloat4 &f4) const
{
std::string MyReference = Reference;
MyReference.insert(0, "./");
daeSIDResolver Resolver(_currentEffect, MyReference.c_str());
daeElement *el = Resolver.getElement();
if (NULL == el)
return false;
if (NULL != _currentInstance_effect)
{
// look here first for setparams
// I am sure there must be a better way of doing this
// Maybe the Collada DAE guys can give us a parameter management mechanism !
const domInstance_effect::domSetparam_Array& SetParamArray = _currentInstance_effect->getSetparam_array();
size_t NumberOfSetParams = SetParamArray.getCount();
for (size_t i = 0; i < NumberOfSetParams; i++)
{
// Just do a simple comaprison of the ref strings for the time being
if (0 == strcmp(SetParamArray[i]->getRef(), Reference))
{
if (NULL != SetParamArray[i]->getFx_basic_type_common() && (NULL != SetParamArray[i]->getFx_basic_type_common()->getFloat4()))
{
f4 = SetParamArray[i]->getFx_basic_type_common()->getFloat4()->getValue();
return true;
}
}
}
}
domCommon_newparam_type *cnp = daeSafeCast< domCommon_newparam_type >( el );
domFx_newparam_common *npc = daeSafeCast< domFx_newparam_common >( el );
if ((cnp != NULL) && (NULL != cnp->getFloat4()))
{
f4 = cnp->getFloat4()->getValue();
return true;
}
else if ((npc != NULL) && (NULL != npc->getFx_basic_type_common()) && (NULL != npc->getFx_basic_type_common()->getFloat4()))
{
f4 = npc->getFx_basic_type_common()->getFloat4()->getValue();
return true;
}
else
return false;
}
bool daeReader::GetFloatParam(xsNCName Reference, domFloat &f) const
{
std::string MyReference = Reference;
MyReference.insert(0, "./");
daeSIDResolver Resolver(_currentEffect, MyReference.c_str());
daeElement *el = Resolver.getElement();
if (NULL == el)
return false;
if (NULL != _currentInstance_effect)
{
// look here first for setparams
// I am sure there must be a better way of doing this
// Maybe the Collada DAE guys can give us a parameter management mechanism !
const domInstance_effect::domSetparam_Array& SetParamArray = _currentInstance_effect->getSetparam_array();
size_t NumberOfSetParams = SetParamArray.getCount();
for (size_t i = 0; i < NumberOfSetParams; i++)
{
// Just do a simple comaprison of the ref strings for the time being
if (0 == strcmp(SetParamArray[i]->getRef(), Reference))
{
if (NULL != SetParamArray[i]->getFx_basic_type_common() && (NULL != SetParamArray[i]->getFx_basic_type_common()->getFloat()))
{
f = SetParamArray[i]->getFx_basic_type_common()->getFloat()->getValue();
return true;
}
}
}
}
domCommon_newparam_type *cnp = daeSafeCast< domCommon_newparam_type >( el );
domFx_newparam_common *npc = daeSafeCast< domFx_newparam_common >( el );
if ((cnp != NULL) && (NULL != cnp->getFloat()))
{
f = cnp->getFloat()->getValue();
return true;
}
else if ((npc != NULL) && (NULL != npc->getFx_basic_type_common()) && (NULL != npc->getFx_basic_type_common()->getFloat()))
{
f = npc->getFx_basic_type_common()->getFloat()->getValue();
return true;
}
else
return false;
}
osg::Texture::WrapMode getWrapMode(domFx_sampler_wrap_common domWrap)
{
switch (domWrap)
{
case FX_SAMPLER_WRAP_COMMON_WRAP:
return osg::Texture::REPEAT;
case FX_SAMPLER_WRAP_COMMON_MIRROR:
return osg::Texture::MIRROR;
case FX_SAMPLER_WRAP_COMMON_CLAMP:
return osg::Texture::CLAMP_TO_EDGE;
case FX_SAMPLER_WRAP_COMMON_NONE:
case FX_SAMPLER_WRAP_COMMON_BORDER:
return osg::Texture::CLAMP_TO_BORDER;
default:
OSG_WARN << "Unrecognised domFx_sampler_wrap_common." << std::endl;
}
return osg::Texture::CLAMP;
}
osg::Texture::FilterMode getFilterMode(domFx_sampler_filter_common domFilter, bool allowMipMap)
{
switch (domFilter)
{
case FX_SAMPLER_FILTER_COMMON_NONE:
case FX_SAMPLER_FILTER_COMMON_NEAREST:
return osg::Texture::NEAREST;
case FX_SAMPLER_FILTER_COMMON_LINEAR:
return osg::Texture::LINEAR;
case FX_SAMPLER_FILTER_COMMON_NEAREST_MIPMAP_NEAREST:
return allowMipMap ? osg::Texture::NEAREST_MIPMAP_NEAREST : osg::Texture::NEAREST;
case FX_SAMPLER_FILTER_COMMON_LINEAR_MIPMAP_NEAREST:
return allowMipMap ? osg::Texture::LINEAR_MIPMAP_NEAREST : osg::Texture::LINEAR;
case FX_SAMPLER_FILTER_COMMON_NEAREST_MIPMAP_LINEAR:
return allowMipMap ? osg::Texture::NEAREST_MIPMAP_LINEAR : osg::Texture::NEAREST;
case FX_SAMPLER_FILTER_COMMON_LINEAR_MIPMAP_LINEAR:
return allowMipMap ? osg::Texture::LINEAR_MIPMAP_LINEAR : osg::Texture::LINEAR;
default:
OSG_WARN << "Unrecognised domFx_sampler_filter_common." << std::endl;
}
return osg::Texture::LINEAR;
}
std::string daeReader::processImagePath(const domImage* pDomImage) const
{
if (pDomImage == NULL)
{
OSG_WARN << "Could not locate image for texture" << std::endl;
return std::string();
}
//Got a sampler and a surface and an imaged. Time to create the texture stuff for osg
if (pDomImage->getInit_from())
{
pDomImage->getInit_from()->getValue().validate();
if (strcmp(pDomImage->getInit_from()->getValue().getProtocol(), "file") == 0)
{
std::string path = pDomImage->getInit_from()->getValue().pathDir() +
pDomImage->getInit_from()->getValue().pathFile();
path = ReaderWriterDAE::ConvertColladaCompatibleURIToFilePath(path);
if (path.empty())
{
OSG_WARN << "Unable to get path from URI." << std::endl;
return std::string();
}
#ifdef WIN32
// If the path has a drive specifier or a UNC name then strip the leading /
if (path.size() > 2 && (path[2] == ':' || (path[1] == '/' && path[2] == '/')))
return path.substr(1, std::string::npos);
#endif
return path;
}
else
{
OSG_WARN << "Only images with a \"file\" scheme URI are supported in this version." << std::endl;
}
}
else
{
OSG_WARN << "Embedded image data is not supported in this version." << std::endl;
}
return std::string();
}
float luminance(const osg::Vec4& color)
{
return
color.r() * 0.212671f +
color.g() * 0.715160f +
color.b() * 0.072169f;
}
osg::Image* daeReader::processImageTransparency(const osg::Image* srcImg, domFx_opaque_enum opaque, float transparency) const
{
int s = srcImg->s();
int t = srcImg->t();
unsigned char* pixels = new unsigned char [s * t];
if (opaque == FX_OPAQUE_ENUM_RGB_ZERO)
{
for (int i = 0; i < t; ++i)
{
for (int j = 0; j < s; ++j)
{
osg::Vec4 color(srcImg->getColor(j, i));
pixels[i * s + j] = static_cast<unsigned char>(
(1.0f - luminance(color) * transparency) * 255.0f);
}
}
}
else
{
bool texHasAlpha = false;
switch (srcImg->getPixelFormat())
{
case GL_ALPHA:
case GL_LUMINANCE_ALPHA:
case GL_RGBA:
case GL_BGRA:
texHasAlpha = true;
}
if (texHasAlpha)
{
for (int i = 0; i < t; ++i)
{
for (int j = 0; j < s; ++j)
{
osg::Vec4 color(srcImg->getColor(j, i));
pixels[i * s + j] = static_cast<unsigned char>(
color.a() * transparency * 255.0f);
}
}
}
else
{
for (int i = 0; i < t; ++i)
{
for (int j = 0; j < s; ++j)
{
osg::Vec4 color(srcImg->getColor(j, i));
pixels[i * s + j] = static_cast<unsigned char>(
luminance(color) * transparency * 255.0f);
}
}
}
}
osg::Image* transparentImage = new osg::Image;
transparentImage->setWriteHint(osg::Image::STORE_INLINE);
transparentImage->setImage(s, t, 1, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, pixels, osg::Image::USE_NEW_DELETE);
return transparentImage;
}
osg::Texture2D* daeReader::processTexture(
domCommon_color_or_texture_type_complexType::domTexture *tex,
const osg::StateSet* ss, TextureUnitUsage tuu,
domFx_opaque_enum opaque, float transparency)
{
TextureParameters parameters;
parameters.transparent = tuu == TRANSPARENCY_MAP_UNIT;
parameters.opaque = opaque;
parameters.transparency = transparency;
//find the newparam for the sampler based on the texture attribute
domFx_sampler2D_common *sampler = NULL;
domFx_surface_common *surface = NULL;
domImage *dImg = NULL;
if(tex->getTexture() == NULL)
{
return NULL;
}
std::string target = std::string("./") + std::string(tex->getTexture());
OSG_INFO<<"processTexture("<<target<<")"<<std::endl;
daeSIDResolver res1( _currentEffect, target.c_str() );
daeElement *el = res1.getElement();
if (el == NULL )
{
OSG_WARN << "Could not locate newparam for texture sampler2D \"" << tex->getTexture() <<
"\". Checking if data does incorrect linking straight to the image" << std::endl;
_dae->getDatabase()->getElement( (daeElement**)&dImg, 0, tex->getTexture(), "image" );
if (dImg != NULL )
{
OSG_WARN << "Direct image link found. Data is incorrect but will continue to load texture" << std::endl;
}
}
else
{
domCommon_newparam_type *cnp = daeSafeCast< domCommon_newparam_type >( el );
domFx_newparam_common *npc = daeSafeCast< domFx_newparam_common >( el );
if (cnp != NULL )
{
sampler = cnp->getSampler2D();
}
else if (npc != NULL )
{
sampler = npc->getFx_basic_type_common()->getSampler2D();
}
if (sampler == NULL )
{
OSG_WARN << "Wrong newparam type. Expected sampler2D" << std::endl;
return NULL;
}
if (sampler->getSource() == NULL || sampler->getSource()->getValue() == NULL)
{
OSG_WARN << "Could not locate source for sampler2D" << std::endl;
return NULL;
}
//find the newparam for the surface based on the sampler2D->source value
target = std::string("./") + std::string( sampler->getSource()->getValue() );
daeSIDResolver res2( _currentEffect, target.c_str() );
el = res2.getElement();
if (el == NULL )
{
OSG_WARN << "Could not locate newparam for source " << sampler->getSource()->getValue() << std::endl;
return NULL;
}
cnp = daeSafeCast< domCommon_newparam_type >( el );
npc = daeSafeCast< domFx_newparam_common >( el );
if (cnp != NULL )
{
surface = cnp->getSurface();
}
else if (npc != NULL )
{
surface = npc->getFx_basic_type_common()->getSurface();
}
if (surface == NULL )
{
OSG_WARN << "Wrong newparam type. Expected surface" << std::endl;
return NULL;
}
//look for the domImage based on the surface initialization stuff
daeIDRef &ref = surface->getFx_surface_init_common()->getInit_from_array()[0]->getValue();
dImg = daeSafeCast< domImage >( getElementFromIDRef( ref ) );
}
parameters.filename = processImagePath(dImg);
if (parameters.filename.empty())
{
return NULL;
}
//set texture parameters
if (sampler)
{
if (sampler->getWrap_s())
{
parameters.wrap_s = getWrapMode(sampler->getWrap_s()->getValue());
}
if (sampler->getWrap_t())
{
parameters.wrap_t = getWrapMode(sampler->getWrap_s()->getValue());
}
if (sampler->getMinfilter() && sampler->getMinfilter()->getValue() != FX_SAMPLER_FILTER_COMMON_NONE)
{
parameters.filter_min = getFilterMode(sampler->getMinfilter()->getValue(), true);
}
if (sampler->getMagfilter() && sampler->getMagfilter()->getValue() != FX_SAMPLER_FILTER_COMMON_NONE)
{
parameters.filter_mag = getFilterMode(sampler->getMagfilter()->getValue(), false);
}
if (sampler->getBorder_color() != NULL )
{
const domFloat4& col = sampler->getBorder_color()->getValue();
parameters.border.set(col[0], col[1], col[2], col[3]);
}
}
osg::Texture2D* t2D = NULL;
TextureParametersMap::const_iterator mapIt = _textureParamMap.find(parameters);
if (mapIt != _textureParamMap.end())
{
t2D = mapIt->second.get();
}
else
{
osg::ref_ptr<osg::Image> img = osgDB::readRefImageFile(parameters.filename);
if (!img.valid())
{
_textureParamMap[parameters] = NULL;
return NULL;
}
OSG_INFO<<" processTexture(..) - readImage("<<parameters.filename<<")"<<std::endl;
if (tuu == TRANSPARENCY_MAP_UNIT)
{
img = processImageTransparency(img.get(), opaque, transparency);
}
t2D = new osg::Texture2D(img.get());
t2D->setWrap( osg::Texture::WRAP_S, parameters.wrap_s);
t2D->setWrap( osg::Texture::WRAP_T, parameters.wrap_t);
t2D->setFilter( osg::Texture::MIN_FILTER, parameters.filter_min);
t2D->setFilter( osg::Texture::MAG_FILTER, parameters.filter_mag);
t2D->setBorderColor(parameters.border);
_textureParamMap[parameters] = t2D;
}
if(tex->getTexcoord() != NULL)
{
_texCoordSetMap[TextureToCoordSetMap::key_type(ss, tuu)] = tex->getTexcoord();
}
return t2D;
}
/*
Collada 1.4.1 Specification (2nd Edition) Patch Release Notes: Revision C Release notes
In <blinn>, <constant>, <lambert>, and <phong>, the child element <transparent> now has an
optional opaque attribute whose valid values are:
A_ONE (the default): Takes the transparency information from the colors alpha channel, where the value 1.0 is opaque.
RGB_ZERO: Takes the transparency information from the colors red, green, and blue channels, where the value 0.0 is opaque,
with each channel modulated independently.
In the Specification, this is described in the FX Reference chapter in the
common_color_or_texture_type entry, along with a description of how transparency works in the
Getting Started with COLLADA FX chapter in the Determining Transparency section.
Collada Digital Asset Schema Release 1.5.0 Release Notes
The <transparent> elements opaque attribute now allows, in addition to A_ONE and RGB_ZERO, the following values:
A_ZERO: Takes the transparency information from the colors alpha channel, where the value 0.0 is opaque.
RGB_ONE: Takes the transparency information from the colors red, green, and blue channels, where the value 1.0
is opaque, with each channel modulated independently.
When we update to a version of the dom using that schema we will need to modify the code below
*/
void daeReader::processTransparencySettings( domCommon_transparent_type *ctt,
domCommon_float_or_param_type *pTransparency,
osg::StateSet *ss,
osg::Material *material,
unsigned int diffuseTextureUnit )
{
if (ss == NULL)
return;
if (NULL == ctt && NULL == pTransparency)
return;
float transparency = 1.0f;
if (pTransparency)
{
if (pTransparency->getFloat())
{
transparency = pTransparency->getFloat()->getValue();
}
else if (pTransparency->getParam())
{
domFloat transparencyParam;
if (GetFloatParam(pTransparency->getParam()->getRef(), transparencyParam))
{
transparency = transparencyParam;
}
}
if (_invertTransparency)
{
transparency = 1.0f - transparency;
}
}
osg::Texture2D* pTransparentTexture = NULL;
osg::Vec4 transparentColor(transparency, transparency, transparency, transparency);
// Fix up defaults according to "Determining Transparency" chapter of 1.4.1 spec
domFx_opaque_enum opaque = FX_OPAQUE_ENUM_A_ONE;
if (ctt)
{
opaque = ctt->getOpaque();
if (ctt->getColor())
{
const domFx_color_common& domColorValue = ctt->getColor()->getValue();
transparentColor.set(
domColorValue.get(0),
domColorValue.get(1),
domColorValue.get(2),
domColorValue.get(3));
if (opaque == FX_OPAQUE_ENUM_RGB_ZERO)
{
transparentColor.set(
1.0f - transparentColor.r() * transparency,
1.0f - transparentColor.g() * transparency,
1.0f - transparentColor.b() * transparency,
1.0f - luminance(transparentColor) * transparency);
}
else
{
float a = transparentColor.a() * transparency;
transparentColor.set(a, a, a, a);
}
}
else if (ctt->getTexture())
{
pTransparentTexture = processTexture(ctt->getTexture(), ss, TRANSPARENCY_MAP_UNIT, opaque, transparency);
}
}
if (pTransparentTexture)
{
ss->setTextureAttributeAndModes(TRANSPARENCY_MAP_UNIT, pTransparentTexture);
ss->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
}
else
{
bool strictTransparency = _pluginOptions.strictTransparency;
if (!strictTransparency)
{
const osg::Texture* pMainTexture = dynamic_cast<osg::Texture*>(
ss->getTextureAttribute(diffuseTextureUnit, osg::StateAttribute::TEXTURE));
bool haveTranslucentTexture = pMainTexture &&
pMainTexture->getImage(0) && pMainTexture->getImage(0)->isImageTranslucent();
strictTransparency = !haveTranslucentTexture;
}
if (strictTransparency)
{
if (transparentColor == osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f))
{
return;
}
ss->setAttributeAndModes(new osg::BlendColor(transparentColor));
ss->setAttributeAndModes(new osg::BlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR));
}
else
{
ss->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
}
}
ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
}
bool daeReader::copyTextureCoordinateSet(const osg::StateSet* ss, const osg::Geometry* cachedGeometry, osg::Geometry* clonedGeometry, const domInstance_material* im, TextureUnitUsage tuu, unsigned int textureUnit)
{
unsigned int localTextureUnit( _pluginOptions.usePredefinedTextureUnits ? tuu : textureUnit);
if (!ss->getTextureAttribute(localTextureUnit, osg::StateAttribute::TEXTURE))
return false;
const std::string& texCoordSetName = _texCoordSetMap
[TextureToCoordSetMap::key_type(ss, tuu)];
if (texCoordSetName.empty()) return false;
const domInstance_material::domBind_vertex_input_Array &bvia = im->getBind_vertex_input_array();
size_t k;
for (k = 0; k < bvia.getCount(); k++)
{
if (!strcmp(bvia[k]->getSemantic(), texCoordSetName.c_str()) && !strcmp(bvia[k]->getInput_semantic(), COMMON_PROFILE_INPUT_TEXCOORD))
{
unsigned int set = bvia[k]->getInput_set();
if (set < cachedGeometry->getNumTexCoordArrays())
{
clonedGeometry->setTexCoordArray(localTextureUnit, const_cast<osg::Array*>(cachedGeometry->getTexCoordArray(set)));
}
else
{
OSG_WARN << "Texture coordinate set " << set << " not found." << std::endl;
}
break;
}
}
if (k == bvia.getCount())
{
OSG_WARN << "Failed to find matching <bind_vertex_input> for " << texCoordSetName << std::endl;
//bind_vertex_input failed, we try bind
const domInstance_material::domBind_Array &ba = im->getBind_array();
for (k = 0; k < ba.getCount(); k++)
{
if (!strcmp(ba[k]->getSemantic(), texCoordSetName.c_str()) )
{
IdToCoordIndexMap::const_iterator it = _texCoordIdMap.find(ba[k]->getTarget());
if (it!=_texCoordIdMap.end()&& (cachedGeometry->getNumTexCoordArrays()>it->second))
{
clonedGeometry->setTexCoordArray(localTextureUnit, const_cast<osg::Array*>(cachedGeometry->getTexCoordArray(it->second)));
}
else
{
OSG_WARN << "Texture coordinate set " << ba[k]->getTarget() << " not found." << std::endl;
}
break;
}
}
if (k == ba.getCount())
{
if (cachedGeometry->getNumTexCoordArrays())
{
clonedGeometry->setTexCoordArray(localTextureUnit, const_cast<osg::Array*>(cachedGeometry->getTexCoordArray(0)));
}
}
}
return true;
}
|