| 1 | |
|---|
| 2 | |
|---|
| 3 | #include "ConvertFromPerformer.h" |
|---|
| 4 | |
|---|
| 5 | #include <osg/Group> |
|---|
| 6 | #include <osg/MatrixTransform> |
|---|
| 7 | #include <osg/LOD> |
|---|
| 8 | #include <osg/Switch> |
|---|
| 9 | #include <osg/Geode> |
|---|
| 10 | #include <osg/Billboard> |
|---|
| 11 | #include <osg/Texture2D> |
|---|
| 12 | #include <osg/Image> |
|---|
| 13 | #include <osg/CullFace> |
|---|
| 14 | #include <osg/TexGen> |
|---|
| 15 | #include <osg/TexEnv> |
|---|
| 16 | #include <osg/TexMat> |
|---|
| 17 | #include <osg/Material> |
|---|
| 18 | #include <osg/Notify> |
|---|
| 19 | #include <osg/Geometry> |
|---|
| 20 | #include <osg/Sequence> |
|---|
| 21 | #include <osg/ShadeModel> |
|---|
| 22 | #include <osg/Depth> |
|---|
| 23 | #include <osg/AlphaFunc> |
|---|
| 24 | |
|---|
| 25 | #include <osgDB/FileNameUtils> |
|---|
| 26 | #include <osgDB/Registry> |
|---|
| 27 | #include <osgDB/WriteFile> |
|---|
| 28 | |
|---|
| 29 | #include <Performer/pf/pfNode.h> |
|---|
| 30 | #include <Performer/pf/pfGeode.h> |
|---|
| 31 | #include <Performer/pf/pfBillboard.h> |
|---|
| 32 | #include <Performer/pf/pfScene.h> |
|---|
| 33 | #include <Performer/pf/pfGroup.h> |
|---|
| 34 | #include <Performer/pf/pfDCS.h> |
|---|
| 35 | #include <Performer/pf/pfSCS.h> |
|---|
| 36 | #include <Performer/pf/pfLOD.h> |
|---|
| 37 | #include <Performer/pf/pfSwitch.h> |
|---|
| 38 | #include <Performer/pf/pfSequence.h> |
|---|
| 39 | #include <Performer/pr/pfGeoState.h> |
|---|
| 40 | #include <Performer/pr/pfMaterial.h> |
|---|
| 41 | #include <Performer/pr/pfTexture.h> |
|---|
| 42 | |
|---|
| 43 | #ifdef WIN32 |
|---|
| 44 | #define snprintf _snprintf |
|---|
| 45 | #endif |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | #ifdef AXIAL_ROT |
|---|
| 50 | #undef AXIAL_ROT |
|---|
| 51 | #endif |
|---|
| 52 | |
|---|
| 53 | #ifdef POINT_ROT_EYE |
|---|
| 54 | #undef POINT_ROT_EYE |
|---|
| 55 | #endif |
|---|
| 56 | |
|---|
| 57 | #ifdef POINT_ROT_WORLD |
|---|
| 58 | #undef POINT_ROT_WORLD |
|---|
| 59 | #endif |
|---|
| 60 | |
|---|
| 61 | extern "C" |
|---|
| 62 | { |
|---|
| 63 | |
|---|
| 64 | extern int |
|---|
| 65 | pfdStoreFile_osg (pfNode* root, char *fileName) |
|---|
| 66 | { |
|---|
| 67 | ConvertFromPerformer converter; |
|---|
| 68 | osg::Node* node = converter.convert(root); |
|---|
| 69 | |
|---|
| 70 | if (node==NULL) return 0; |
|---|
| 71 | if (osgDB::writeNodeFile(*node,fileName)) return 1; |
|---|
| 72 | else return 0; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | ConvertFromPerformer::ConvertFromPerformer() |
|---|
| 79 | { |
|---|
| 80 | _osgRoot = NULL; |
|---|
| 81 | |
|---|
| 82 | _gsetBindMap[PFGS_OFF] = osg::Geometry::BIND_OFF; |
|---|
| 83 | _gsetBindMap[PFGS_OVERALL] = osg::Geometry::BIND_OVERALL; |
|---|
| 84 | _gsetBindMap[PFGS_PER_PRIM] = osg::Geometry::BIND_PER_PRIMITIVE; |
|---|
| 85 | _gsetBindMap[PFGS_PER_VERTEX] = osg::Geometry::BIND_PER_VERTEX; |
|---|
| 86 | |
|---|
| 87 | _saveImagesAsRGB = false; |
|---|
| 88 | _saveAbsoluteImagePath = false; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | ConvertFromPerformer::~ConvertFromPerformer() |
|---|
| 93 | { |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | osg::Node* ConvertFromPerformer::convert(pfNode* node) |
|---|
| 98 | { |
|---|
| 99 | if (node==NULL) return NULL; |
|---|
| 100 | return visitNode(NULL,node); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | osg::Object* ConvertFromPerformer::getOsgObject(pfObject* pfObj) |
|---|
| 105 | { |
|---|
| 106 | PfObjectToOsgObjectMap::iterator fitr = _pfToOsgMap.find(pfObj); |
|---|
| 107 | if (fitr != _pfToOsgMap.end()) |
|---|
| 108 | { |
|---|
| 109 | |
|---|
| 110 | return (*fitr).second; |
|---|
| 111 | } |
|---|
| 112 | else return NULL; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | void ConvertFromPerformer::registerPfObjectForOsgObject(pfObject* pfObj,osg::Object* osgObj) |
|---|
| 117 | { |
|---|
| 118 | _pfToOsgMap[pfObj] = osgObj; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | osg::Node* ConvertFromPerformer::visitNode(osg::Group* osgParent,pfNode* node) |
|---|
| 123 | { |
|---|
| 124 | if (node==NULL) return NULL; |
|---|
| 125 | |
|---|
| 126 | if (node->getType()->isDerivedFrom( pfBillboard::getClassType())) return visitBillboard(osgParent,(pfBillboard*)node); |
|---|
| 127 | else if (node->getType()->isDerivedFrom( pfGeode::getClassType())) return visitGeode(osgParent,(pfGeode*)node); |
|---|
| 128 | else if (node->getType()->isDerivedFrom( pfScene::getClassType())) return visitScene(osgParent,(pfScene*)node); |
|---|
| 129 | else if (node->getType()->isDerivedFrom( pfDCS::getClassType())) return visitDCS(osgParent,(pfDCS*)node); |
|---|
| 130 | else if (node->getType()->isDerivedFrom( pfSCS::getClassType())) return visitSCS(osgParent,(pfSCS*)node); |
|---|
| 131 | else if (node->getType()->isDerivedFrom( pfLOD::getClassType())) return visitLOD(osgParent,(pfLOD*)node); |
|---|
| 132 | else if (node->getType()->isDerivedFrom( pfSequence::getClassType())) return visitSequence(osgParent,(pfSequence*)node); |
|---|
| 133 | else if (node->getType()->isDerivedFrom( pfSwitch::getClassType())) return visitSwitch(osgParent,(pfSwitch*)node); |
|---|
| 134 | else if (node->getType()->isDerivedFrom( pfGroup::getClassType())) return visitGroup(osgParent,(pfGroup*)node); |
|---|
| 135 | |
|---|
| 136 | return NULL; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | osg::Node* ConvertFromPerformer::visitScene(osg::Group* osgParent,pfScene* scene) |
|---|
| 141 | { |
|---|
| 142 | osg::Group* osgScene = dynamic_cast<osg::Group*>(getOsgObject(scene)); |
|---|
| 143 | if (osgScene) |
|---|
| 144 | { |
|---|
| 145 | if (osgParent) osgParent->addChild(osgScene); |
|---|
| 146 | return osgScene; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | osgScene = new osg::Group; |
|---|
| 150 | if (osgParent) osgParent->addChild(osgScene); |
|---|
| 151 | |
|---|
| 152 | registerPfObjectForOsgObject(scene,osgScene); |
|---|
| 153 | |
|---|
| 154 | const char* name = scene->getName(); |
|---|
| 155 | if (name) osgScene->setName(name); |
|---|
| 156 | |
|---|
| 157 | for(int i=0;i<scene->getNumChildren();++i) |
|---|
| 158 | { |
|---|
| 159 | visitNode(osgScene,scene->getChild(i)); |
|---|
| 160 | } |
|---|
| 161 | return (osg::Node*)osgScene; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | osg::Node* ConvertFromPerformer::visitGroup(osg::Group* osgParent,pfGroup* group) |
|---|
| 166 | { |
|---|
| 167 | osg::Group* osgGroup = dynamic_cast<osg::Group*>(getOsgObject(group)); |
|---|
| 168 | if (osgGroup) |
|---|
| 169 | { |
|---|
| 170 | if (osgParent) osgParent->addChild(osgGroup); |
|---|
| 171 | return osgGroup; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | osgGroup = new osg::Group; |
|---|
| 175 | if (osgParent) osgParent->addChild(osgGroup); |
|---|
| 176 | |
|---|
| 177 | registerPfObjectForOsgObject(group,osgGroup); |
|---|
| 178 | |
|---|
| 179 | const char* name = group->getName(); |
|---|
| 180 | if (name) osgGroup->setName(name); |
|---|
| 181 | |
|---|
| 182 | for(int i=0;i<group->getNumChildren();++i) |
|---|
| 183 | { |
|---|
| 184 | visitNode(osgGroup,group->getChild(i)); |
|---|
| 185 | } |
|---|
| 186 | return (osg::Node*)osgGroup; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | osg::Node* ConvertFromPerformer::visitLOD(osg::Group* osgParent,pfLOD* lod) |
|---|
| 191 | { |
|---|
| 192 | osg::LOD* osgLOD = dynamic_cast<osg::LOD*>(getOsgObject(lod)); |
|---|
| 193 | if (osgLOD) |
|---|
| 194 | { |
|---|
| 195 | if (osgParent) osgParent->addChild(osgLOD); |
|---|
| 196 | return osgLOD; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | osgLOD = new osg::LOD; |
|---|
| 200 | if (osgParent) osgParent->addChild(osgLOD); |
|---|
| 201 | |
|---|
| 202 | registerPfObjectForOsgObject(lod,osgLOD); |
|---|
| 203 | |
|---|
| 204 | const char* name = lod->getName(); |
|---|
| 205 | if (name) osgLOD->setName(name); |
|---|
| 206 | |
|---|
| 207 | pfVec3 center; |
|---|
| 208 | lod->getCenter(center); |
|---|
| 209 | osg::Vec3 osgCenter(center[0],center[1],center[2]); |
|---|
| 210 | osgLOD->setCenter(osgCenter); |
|---|
| 211 | |
|---|
| 212 | int i; |
|---|
| 213 | for(i=0;i<lod->getNumRanges()-1;++i) |
|---|
| 214 | { |
|---|
| 215 | osgLOD->setRange(i,lod->getRange(i),lod->getRange(i+1)); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | for(i=0;i<lod->getNumChildren();++i) |
|---|
| 219 | { |
|---|
| 220 | visitNode(osgLOD,lod->getChild(i)); |
|---|
| 221 | } |
|---|
| 222 | return (osg::Node*)osgLOD; |
|---|
| 223 | |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | osg::Node* ConvertFromPerformer::visitSwitch(osg::Group* osgParent,pfSwitch* switchNode) |
|---|
| 228 | { |
|---|
| 229 | osg::Switch* osgSwitch = dynamic_cast<osg::Switch*>(getOsgObject(switchNode)); |
|---|
| 230 | if (osgSwitch) |
|---|
| 231 | { |
|---|
| 232 | if (osgParent) osgParent->addChild(osgSwitch); |
|---|
| 233 | return osgSwitch; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | osgSwitch = new osg::Switch; |
|---|
| 237 | osgSwitch->setAllChildrenOff(); |
|---|
| 238 | if (osgParent) osgParent->addChild(osgSwitch); |
|---|
| 239 | |
|---|
| 240 | registerPfObjectForOsgObject(switchNode,osgSwitch); |
|---|
| 241 | |
|---|
| 242 | const char* name = switchNode->getName(); |
|---|
| 243 | if (name) osgSwitch->setName(name); |
|---|
| 244 | |
|---|
| 245 | float val = switchNode->getVal(); |
|---|
| 246 | if (val==PFSWITCH_ON) |
|---|
| 247 | { |
|---|
| 248 | osgSwitch->setAllChildrenOn(); |
|---|
| 249 | } |
|---|
| 250 | else if (val==PFSWITCH_OFF) |
|---|
| 251 | { |
|---|
| 252 | osgSwitch->setAllChildrenOff(); |
|---|
| 253 | } |
|---|
| 254 | else |
|---|
| 255 | { |
|---|
| 256 | osgSwitch->setSingleChildOn((unsigned int)val); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | for(int i=0;i<switchNode->getNumChildren();++i) |
|---|
| 260 | { |
|---|
| 261 | visitNode(osgSwitch,switchNode->getChild(i)); |
|---|
| 262 | } |
|---|
| 263 | return (osg::Node*)osgSwitch; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | osg::Node* ConvertFromPerformer::visitSequence(osg::Group* osgParent, |
|---|
| 268 | pfSequence* sequence) |
|---|
| 269 | { |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | osg::Sequence* osgSequence = dynamic_cast<osg::Sequence*>(getOsgObject(sequence)); |
|---|
| 274 | if (osgSequence) |
|---|
| 275 | { |
|---|
| 276 | if (osgParent) osgParent->addChild(osgSequence); |
|---|
| 277 | return osgSequence; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | osgSequence = new osg::Sequence; |
|---|
| 281 | if (osgParent) osgParent->addChild(osgSequence); |
|---|
| 282 | |
|---|
| 283 | registerPfObjectForOsgObject(sequence,osgSequence); |
|---|
| 284 | |
|---|
| 285 | #if 0 |
|---|
| 286 | if (sequence->getNumChildren()>0) |
|---|
| 287 | { |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | osgSequence->setValue(0); |
|---|
| 293 | } |
|---|
| 294 | #endif |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | for(int i=0;i<sequence->getNumChildren();++i) |
|---|
| 298 | { |
|---|
| 299 | |
|---|
| 300 | osgSequence->setTime(i, sequence->getTime(i)); |
|---|
| 301 | visitNode(osgSequence,sequence->getChild(i)); |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | int mode, begin, end; |
|---|
| 306 | sequence->getInterval(&mode, &begin, &end); |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | osg::Sequence::LoopMode loopMode = osg::Sequence::LOOP; |
|---|
| 310 | if (mode == PFSEQ_SWING) |
|---|
| 311 | loopMode = osg::Sequence::SWING; |
|---|
| 312 | osgSequence->setInterval(loopMode, begin, end); |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | float speed; |
|---|
| 316 | int repeat; |
|---|
| 317 | sequence->getDuration(&speed, &repeat); |
|---|
| 318 | osgSequence->setDuration(speed, repeat); |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | mode = sequence->getMode(); |
|---|
| 322 | |
|---|
| 323 | osg::Sequence::SequenceMode seqMode = osg::Sequence::START; |
|---|
| 324 | switch (mode) { |
|---|
| 325 | case PFSEQ_STOP: |
|---|
| 326 | seqMode = osg::Sequence::STOP; |
|---|
| 327 | break; |
|---|
| 328 | case PFSEQ_PAUSE: |
|---|
| 329 | seqMode = osg::Sequence::PAUSE; |
|---|
| 330 | break; |
|---|
| 331 | } |
|---|
| 332 | osgSequence->setMode(seqMode); |
|---|
| 333 | |
|---|
| 334 | return (osg::Node*)osgSequence; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | osg::Node* ConvertFromPerformer::visitDCS(osg::Group* osgParent,pfDCS* dcs) |
|---|
| 339 | { |
|---|
| 340 | |
|---|
| 341 | osg::MatrixTransform* osgTransform = dynamic_cast<osg::MatrixTransform*>(getOsgObject(dcs)); |
|---|
| 342 | if (osgTransform) |
|---|
| 343 | { |
|---|
| 344 | if (osgParent) osgParent->addChild(osgTransform); |
|---|
| 345 | return osgTransform; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | osgTransform = new osg::MatrixTransform; |
|---|
| 349 | if (osgParent) osgParent->addChild(osgTransform); |
|---|
| 350 | |
|---|
| 351 | registerPfObjectForOsgObject(dcs,osgTransform); |
|---|
| 352 | |
|---|
| 353 | const char* name = dcs->getName(); |
|---|
| 354 | if (name) osgTransform->setName(name); |
|---|
| 355 | |
|---|
| 356 | pfMatrix matrix; |
|---|
| 357 | dcs->getMat(matrix); |
|---|
| 358 | |
|---|
| 359 | osg::Matrix osgMatrix(matrix[0][0],matrix[0][1],matrix[0][2],matrix[0][3], |
|---|
| 360 | matrix[1][0],matrix[1][1],matrix[1][2],matrix[1][3], |
|---|
| 361 | matrix[2][0],matrix[2][1],matrix[2][2],matrix[2][3], |
|---|
| 362 | matrix[3][0],matrix[3][1],matrix[3][2],matrix[3][3]); |
|---|
| 363 | |
|---|
| 364 | osgTransform->setMatrix(osgMatrix); |
|---|
| 365 | |
|---|
| 366 | for(int i=0;i<dcs->getNumChildren();++i) |
|---|
| 367 | { |
|---|
| 368 | visitNode(osgTransform,dcs->getChild(i)); |
|---|
| 369 | } |
|---|
| 370 | return (osg::Node*)osgTransform; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | osg::Node* ConvertFromPerformer::visitSCS(osg::Group* osgParent,pfSCS* scs) |
|---|
| 375 | { |
|---|
| 376 | |
|---|
| 377 | osg::MatrixTransform* osgTransform = dynamic_cast<osg::MatrixTransform*>(getOsgObject(scs)); |
|---|
| 378 | if (osgTransform) |
|---|
| 379 | { |
|---|
| 380 | if (osgParent) osgParent->addChild(osgTransform); |
|---|
| 381 | return osgTransform; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | osgTransform = new osg::MatrixTransform; |
|---|
| 385 | if (osgParent) osgParent->addChild(osgTransform); |
|---|
| 386 | |
|---|
| 387 | osgTransform->setDataVariance(osg::Object::STATIC); |
|---|
| 388 | |
|---|
| 389 | registerPfObjectForOsgObject(scs,osgTransform); |
|---|
| 390 | |
|---|
| 391 | const char* name = scs->getName(); |
|---|
| 392 | if (name) osgTransform->setName(name); |
|---|
| 393 | |
|---|
| 394 | pfMatrix matrix; |
|---|
| 395 | scs->getMat(matrix); |
|---|
| 396 | osg::Matrix osgMatrix(matrix[0][0],matrix[0][1],matrix[0][2],matrix[0][3], |
|---|
| 397 | matrix[1][0],matrix[1][1],matrix[1][2],matrix[1][3], |
|---|
| 398 | matrix[2][0],matrix[2][1],matrix[2][2],matrix[2][3], |
|---|
| 399 | matrix[3][0],matrix[3][1],matrix[3][2],matrix[3][3]); |
|---|
| 400 | |
|---|
| 401 | osgTransform->setMatrix(osgMatrix); |
|---|
| 402 | |
|---|
| 403 | for(int i=0;i<scs->getNumChildren();++i) |
|---|
| 404 | { |
|---|
| 405 | visitNode(osgTransform,scs->getChild(i)); |
|---|
| 406 | } |
|---|
| 407 | return (osg::Node*)osgTransform; |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | osg::Node* ConvertFromPerformer::visitGeode(osg::Group* osgParent,pfGeode* geode) |
|---|
| 412 | { |
|---|
| 413 | osg::Geode* osgGeode = dynamic_cast<osg::Geode*>(getOsgObject(geode)); |
|---|
| 414 | if (osgGeode) |
|---|
| 415 | { |
|---|
| 416 | if (osgParent) osgParent->addChild(osgGeode); |
|---|
| 417 | return osgGeode; |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | osgGeode = new osg::Geode; |
|---|
| 421 | if (osgParent) osgParent->addChild(osgGeode); |
|---|
| 422 | |
|---|
| 423 | registerPfObjectForOsgObject(geode,osgGeode); |
|---|
| 424 | |
|---|
| 425 | const char* name = geode->getName(); |
|---|
| 426 | if (name) osgGeode->setName(name); |
|---|
| 427 | |
|---|
| 428 | for(int i=0;i<geode->getNumGSets();++i) |
|---|
| 429 | { |
|---|
| 430 | visitGeoSet(osgGeode,geode->getGSet(i)); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | return (osg::Node*)osgGeode; |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | osg::Node* ConvertFromPerformer::visitBillboard(osg::Group* osgParent,pfBillboard* billboard) |
|---|
| 438 | { |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | osg::Billboard* osgBillboard = dynamic_cast<osg::Billboard*>(getOsgObject(billboard)); |
|---|
| 442 | if (osgBillboard) |
|---|
| 443 | { |
|---|
| 444 | if (osgParent) osgParent->addChild(osgBillboard); |
|---|
| 445 | return osgBillboard; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | osgBillboard = new osg::Billboard; |
|---|
| 449 | if (osgParent) osgParent->addChild(osgBillboard); |
|---|
| 450 | |
|---|
| 451 | registerPfObjectForOsgObject(billboard,osgBillboard); |
|---|
| 452 | |
|---|
| 453 | const char* name = billboard->getName(); |
|---|
| 454 | if (name) osgBillboard->setName(name); |
|---|
| 455 | |
|---|
| 456 | pfVec3 axis; |
|---|
| 457 | billboard->getAxis(axis); |
|---|
| 458 | osgBillboard->setAxis(osg::Vec3(axis[0],axis[1],axis[2])); |
|---|
| 459 | |
|---|
| 460 | switch( billboard->getMode( PFBB_ROT ) ) |
|---|
| 461 | { |
|---|
| 462 | case PFBB_AXIAL_ROT: |
|---|
| 463 | osgBillboard->setMode( osg::Billboard::AXIAL_ROT ); |
|---|
| 464 | break; |
|---|
| 465 | case PFBB_POINT_ROT_EYE: |
|---|
| 466 | osgBillboard->setMode( osg::Billboard::POINT_ROT_EYE ); |
|---|
| 467 | break; |
|---|
| 468 | case PFBB_POINT_ROT_WORLD: |
|---|
| 469 | osgBillboard->setMode( osg::Billboard::POINT_ROT_WORLD ); |
|---|
| 470 | break; |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | for(int i=0;i<billboard->getNumGSets();++i) |
|---|
| 474 | { |
|---|
| 475 | |
|---|
| 476 | visitGeoSet(osgBillboard,billboard->getGSet(i)); |
|---|
| 477 | pfVec3 pos; |
|---|
| 478 | billboard->getPos(i,pos); |
|---|
| 479 | osgBillboard->setPosition(i,osg::Vec3(pos[0],pos[1],pos[2])); |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | return (osg::Node*)osgBillboard; |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | int ConvertFromPerformer::getNumVerts(pfGeoSet *gset) |
|---|
| 487 | { |
|---|
| 488 | int nv; |
|---|
| 489 | int np; |
|---|
| 490 | int *lens; |
|---|
| 491 | int i; |
|---|
| 492 | |
|---|
| 493 | np = gset->getNumPrims(); |
|---|
| 494 | nv = 0; |
|---|
| 495 | |
|---|
| 496 | switch( gset->getPrimType() ) |
|---|
| 497 | { |
|---|
| 498 | case PFGS_POINTS : |
|---|
| 499 | nv = np; |
|---|
| 500 | break; |
|---|
| 501 | |
|---|
| 502 | case PFGS_LINES : |
|---|
| 503 | nv = 2 * np; |
|---|
| 504 | break; |
|---|
| 505 | |
|---|
| 506 | case PFGS_TRIS : |
|---|
| 507 | nv = 3 * np; |
|---|
| 508 | break; |
|---|
| 509 | |
|---|
| 510 | case PFGS_QUADS : |
|---|
| 511 | nv = 4 * np; |
|---|
| 512 | break; |
|---|
| 513 | |
|---|
| 514 | case PFGS_TRISTRIPS : |
|---|
| 515 | case PFGS_TRIFANS : |
|---|
| 516 | case PFGS_FLAT_TRISTRIPS : |
|---|
| 517 | case PFGS_POLYS : |
|---|
| 518 | case PFGS_LINESTRIPS : |
|---|
| 519 | case PFGS_FLAT_LINESTRIPS : |
|---|
| 520 | |
|---|
| 521 | lens = gset->getPrimLengths(); |
|---|
| 522 | for( i = 0; i < np; i++ ) |
|---|
| 523 | nv += lens[i]; |
|---|
| 524 | break; |
|---|
| 525 | |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | return nv; |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | osg::Drawable* ConvertFromPerformer::visitGeoSet(osg::Geode* osgGeode,pfGeoSet* geoset) |
|---|
| 533 | { |
|---|
| 534 | if (geoset==NULL) return NULL; |
|---|
| 535 | |
|---|
| 536 | osg::Drawable* osgDrawable = dynamic_cast<osg::Drawable*>(getOsgObject(geoset)); |
|---|
| 537 | if (osgDrawable) |
|---|
| 538 | { |
|---|
| 539 | if (osgGeode) osgGeode->addDrawable(osgDrawable); |
|---|
| 540 | return osgDrawable; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 547 | |
|---|
| 548 | int i; |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | int np = geoset->getNumPrims(); |
|---|
| 552 | int *plen = geoset->getPrimLengths(); |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | int nv = getNumVerts( geoset ); |
|---|
| 556 | |
|---|
| 557 | int prim = geoset->getPrimType(); |
|---|
| 558 | int flat_shaded_skip_per_primitive=0; |
|---|
| 559 | if (prim == PFGS_FLAT_LINESTRIPS) flat_shaded_skip_per_primitive=1; |
|---|
| 560 | else if (prim == PFGS_FLAT_TRISTRIPS) flat_shaded_skip_per_primitive=2; |
|---|
| 561 | else if (prim == PFGS_FLAT_TRIFANS) flat_shaded_skip_per_primitive=2; |
|---|
| 562 | |
|---|
| 563 | int flat_shaded_skip_all_primitives=flat_shaded_skip_per_primitive*np; |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | switch( geoset->getPrimType() ) |
|---|
| 567 | { |
|---|
| 568 | case PFGS_POINTS : |
|---|
| 569 | geom->addPrimitiveSet(new osg::DrawArrays(GL_POINTS,0,np)); |
|---|
| 570 | break; |
|---|
| 571 | |
|---|
| 572 | case PFGS_LINES : |
|---|
| 573 | geom->addPrimitiveSet(new osg::DrawArrays(GL_LINES,0,2*np)); |
|---|
| 574 | break; |
|---|
| 575 | |
|---|
| 576 | case PFGS_TRIS : |
|---|
| 577 | geom->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES,0,3*np)); |
|---|
| 578 | break; |
|---|
| 579 | |
|---|
| 580 | case PFGS_QUADS : |
|---|
| 581 | geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4*np)); |
|---|
| 582 | break; |
|---|
| 583 | |
|---|
| 584 | case PFGS_TRISTRIPS : |
|---|
| 585 | geom->addPrimitiveSet(new osg::DrawArrayLengths(GL_TRIANGLE_STRIP,0,np,plen)); |
|---|
| 586 | break; |
|---|
| 587 | |
|---|
| 588 | case PFGS_TRIFANS : |
|---|
| 589 | geom->addPrimitiveSet(new osg::DrawArrayLengths(GL_TRIANGLE_FAN,0,np,plen)); |
|---|
| 590 | break; |
|---|
| 591 | |
|---|
| 592 | case PFGS_FLAT_TRISTRIPS : |
|---|
| 593 | geom->addPrimitiveSet(new osg::DrawArrayLengths(GL_TRIANGLE_STRIP,0,np,plen)); |
|---|
| 594 | break; |
|---|
| 595 | |
|---|
| 596 | case PFGS_POLYS : |
|---|
| 597 | geom->addPrimitiveSet(new osg::DrawArrayLengths(GL_POLYGON,0,np,plen)); |
|---|
| 598 | break; |
|---|
| 599 | |
|---|
| 600 | case PFGS_LINESTRIPS : |
|---|
| 601 | geom->addPrimitiveSet(new osg::DrawArrayLengths(GL_LINE_STRIP,0,np,plen)); |
|---|
| 602 | break; |
|---|
| 603 | |
|---|
| 604 | case PFGS_FLAT_LINESTRIPS : |
|---|
| 605 | geom->addPrimitiveSet(new osg::DrawArrayLengths(GL_LINE_STRIP,0,np,plen)); |
|---|
| 606 | break; |
|---|
| 607 | |
|---|
| 608 | default: |
|---|
| 609 | OSG_INFO << "ConvertFromPerformer::visitGeoSet: Osg can't convert primitive " |
|---|
| 610 | << geoset->getPrimType() |
|---|
| 611 | << std::endl; |
|---|
| 612 | break; |
|---|
| 613 | |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | pfVec3 *coords; |
|---|
| 618 | ushort *ilist; |
|---|
| 619 | geoset->getAttrLists( PFGS_COORD3, (void **)&coords, &ilist ); |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | if( coords ) |
|---|
| 623 | { |
|---|
| 624 | |
|---|
| 625 | int cc; |
|---|
| 626 | if (ilist) |
|---|
| 627 | { |
|---|
| 628 | cc = 0; |
|---|
| 629 | for( i = 0; i < nv; i++ ) |
|---|
| 630 | if( ilist[i] > cc ) cc = ilist[i]; |
|---|
| 631 | cc++; |
|---|
| 632 | } |
|---|
| 633 | else |
|---|
| 634 | cc = nv; |
|---|
| 635 | |
|---|
| 636 | osg::Vec3Array* osg_coords = new osg::Vec3Array(cc); |
|---|
| 637 | for( i = 0; i < cc; i++ ) |
|---|
| 638 | { |
|---|
| 639 | (*osg_coords)[i][0] = coords[i][0]; |
|---|
| 640 | (*osg_coords)[i][1] = coords[i][1]; |
|---|
| 641 | (*osg_coords)[i][2] = coords[i][2]; |
|---|
| 642 | } |
|---|
| 643 | |
|---|
| 644 | geom->setVertexArray(osg_coords); |
|---|
| 645 | |
|---|
| 646 | if(ilist) |
|---|
| 647 | { |
|---|
| 648 | geom->setVertexIndices(new osg::UShortArray(nv,ilist)); |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | pfVec2 *tcoords; |
|---|
| 655 | geoset->getAttrLists( PFGS_TEXCOORD2, (void **)&tcoords, &ilist ); |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | if(tcoords) |
|---|
| 659 | { |
|---|
| 660 | int bind = geoset->getAttrBind( PFGS_TEXCOORD2 ); |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | if (bind==PFGS_PER_VERTEX && bind != PFGS_OFF) |
|---|
| 664 | { |
|---|
| 665 | int nn = bind == PFGS_OFF ? 0 : |
|---|
| 666 | bind == PFGS_OVERALL ? 1 : |
|---|
| 667 | bind == PFGS_PER_PRIM ? geoset->getNumPrims() : |
|---|
| 668 | bind == PFGS_PER_VERTEX ? nv : 0; |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | int cc; |
|---|
| 675 | if (ilist) |
|---|
| 676 | { |
|---|
| 677 | cc = 0; |
|---|
| 678 | for( i = 0; i < nv; i++ ) |
|---|
| 679 | if( ilist[i] > cc ) cc = ilist[i]; |
|---|
| 680 | cc++; |
|---|
| 681 | } |
|---|
| 682 | else |
|---|
| 683 | cc = nn; |
|---|
| 684 | |
|---|
| 685 | osg::Vec2Array* osg_tcoords = new osg::Vec2Array(cc); |
|---|
| 686 | for( i = 0; i < cc; i++ ) |
|---|
| 687 | { |
|---|
| 688 | (*osg_tcoords)[i][0] = tcoords[i][0]; |
|---|
| 689 | (*osg_tcoords)[i][1] = tcoords[i][1]; |
|---|
| 690 | } |
|---|
| 691 | |
|---|
| 692 | geom->setTexCoordArray(0,osg_tcoords); |
|---|
| 693 | |
|---|
| 694 | if(ilist) |
|---|
| 695 | { |
|---|
| 696 | geom->setTexCoordIndices(0,new osg::UShortArray(nn,ilist)); |
|---|
| 697 | } |
|---|
| 698 | } |
|---|
| 699 | else |
|---|
| 700 | { |
|---|
| 701 | OSG_INFO<<"OSG can't handle non PER_VERTEX tex coord binding at present"<<std::endl; |
|---|
| 702 | } |
|---|
| 703 | |
|---|
| 704 | } |
|---|
| 705 | |
|---|
| 706 | pfVec3 *norms; |
|---|
| 707 | geoset->getAttrLists( PFGS_NORMAL3, (void **)&norms, &ilist ); |
|---|
| 708 | |
|---|
| 709 | |
|---|
| 710 | if(norms) |
|---|
| 711 | { |
|---|
| 712 | int bind = geoset->getAttrBind( PFGS_NORMAL3 ); |
|---|
| 713 | if (bind==PFGS_PER_VERTEX && flat_shaded_skip_all_primitives!=0) |
|---|
| 714 | { |
|---|
| 715 | |
|---|
| 716 | int nn = nv-flat_shaded_skip_all_primitives; |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | geom->setNormalBinding(_gsetBindMap[bind]); |
|---|
| 720 | |
|---|
| 721 | if (ilist) |
|---|
| 722 | { |
|---|
| 723 | |
|---|
| 724 | int cc = 0; |
|---|
| 725 | for( i = 0; i < nn; i++ ) |
|---|
| 726 | if( ilist[i] > cc ) cc = ilist[i]; |
|---|
| 727 | cc++; |
|---|
| 728 | |
|---|
| 729 | |
|---|
| 730 | osg::Vec3Array* osg_norms = new osg::Vec3Array(cc); |
|---|
| 731 | for( i = 0; i < cc; i++ ) |
|---|
| 732 | { |
|---|
| 733 | (*osg_norms)[i][0] = norms[i][0]; |
|---|
| 734 | (*osg_norms)[i][1] = norms[i][1]; |
|---|
| 735 | (*osg_norms)[i][2] = norms[i][2]; |
|---|
| 736 | } |
|---|
| 737 | geom->setNormalArray(osg_norms); |
|---|
| 738 | |
|---|
| 739 | osg::UShortArray* osg_indices = new osg::UShortArray; |
|---|
| 740 | osg_indices->reserve(nv); |
|---|
| 741 | |
|---|
| 742 | int ni=0; |
|---|
| 743 | for( i = 0; i < np; ++i) |
|---|
| 744 | { |
|---|
| 745 | for(int si=0;si<flat_shaded_skip_per_primitive;++si) |
|---|
| 746 | { |
|---|
| 747 | osg_indices->push_back(ilist[ni]); |
|---|
| 748 | } |
|---|
| 749 | for( int pi=0; pi < plen[i]-flat_shaded_skip_per_primitive; ++pi) |
|---|
| 750 | { |
|---|
| 751 | osg_indices->push_back(ilist[ni++]); |
|---|
| 752 | } |
|---|
| 753 | } |
|---|
| 754 | |
|---|
| 755 | if (ni!=nn) |
|---|
| 756 | { |
|---|
| 757 | OSG_INFO << "1 ni!=nn"<<std::endl; |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | geom->setNormalIndices(osg_indices); |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | } |
|---|
| 764 | else |
|---|
| 765 | { |
|---|
| 766 | osg::Vec3Array* osg_norms = new osg::Vec3Array; |
|---|
| 767 | osg_norms->reserve(nv); |
|---|
| 768 | |
|---|
| 769 | int ni=0; |
|---|
| 770 | for( i = 0; i < np; ++i) |
|---|
| 771 | { |
|---|
| 772 | for(int si=0;si<flat_shaded_skip_per_primitive;++si) |
|---|
| 773 | { |
|---|
| 774 | osg_norms->push_back(osg::Vec3(norms[ni][0],norms[ni][1],norms[ni][2])); |
|---|
| 775 | } |
|---|
| 776 | for( int pi=0; pi < plen[i]-flat_shaded_skip_per_primitive; ++pi) |
|---|
| 777 | { |
|---|
| 778 | osg_norms->push_back(osg::Vec3(norms[ni][0],norms[ni][1],norms[ni][2])); |
|---|
| 779 | ni++; |
|---|
| 780 | } |
|---|
| 781 | } |
|---|
| 782 | |
|---|
| 783 | geom->setNormalArray(osg_norms); |
|---|
| 784 | } |
|---|
| 785 | |
|---|
| 786 | } |
|---|
| 787 | else |
|---|
| 788 | { |
|---|
| 789 | int nn = bind == PFGS_OFF ? 0 : |
|---|
| 790 | bind == PFGS_OVERALL ? 1 : |
|---|
| 791 | bind == PFGS_PER_PRIM ? geoset->getNumPrims() : |
|---|
| 792 | bind == PFGS_PER_VERTEX ? nv : 0; |
|---|
| 793 | |
|---|
| 794 | |
|---|
| 795 | geom->setNormalBinding(_gsetBindMap[bind]); |
|---|
| 796 | |
|---|
| 797 | |
|---|
| 798 | int cc; |
|---|
| 799 | if (ilist) |
|---|
| 800 | { |
|---|
| 801 | cc = 0; |
|---|
| 802 | for( i = 0; i < nn; i++ ) |
|---|
| 803 | if( ilist[i] > cc ) cc = ilist[i]; |
|---|
| 804 | cc++; |
|---|
| 805 | } |
|---|
| 806 | else |
|---|
| 807 | cc = nn; |
|---|
| 808 | |
|---|
| 809 | osg::Vec3Array* osg_norms = new osg::Vec3Array(cc); |
|---|
| 810 | for( i = 0; i < cc; i++ ) |
|---|
| 811 | { |
|---|
| 812 | (*osg_norms)[i][0] = norms[i][0]; |
|---|
| 813 | (*osg_norms)[i][1] = norms[i][1]; |
|---|
| 814 | (*osg_norms)[i][2] = norms[i][2]; |
|---|
| 815 | } |
|---|
| 816 | geom->setNormalArray(osg_norms); |
|---|
| 817 | |
|---|
| 818 | if(ilist) |
|---|
| 819 | { |
|---|
| 820 | geom->setNormalIndices(new osg::UShortArray(nn,ilist)); |
|---|
| 821 | } |
|---|
| 822 | |
|---|
| 823 | } |
|---|
| 824 | } |
|---|
| 825 | |
|---|
| 826 | pfVec4 *colors; |
|---|
| 827 | geoset->getAttrLists( PFGS_COLOR4, (void **)&colors, &ilist ); |
|---|
| 828 | |
|---|
| 829 | |
|---|
| 830 | if(colors) |
|---|
| 831 | { |
|---|
| 832 | int bind = geoset->getAttrBind( PFGS_COLOR4 ); |
|---|
| 833 | if (bind==PFGS_PER_VERTEX && flat_shaded_skip_all_primitives!=0) |
|---|
| 834 | { |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | int nn = nv-flat_shaded_skip_all_primitives; |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | geom->setColorBinding(_gsetBindMap[bind]); |
|---|
| 841 | |
|---|
| 842 | if (ilist) |
|---|
| 843 | { |
|---|
| 844 | |
|---|
| 845 | int cc = 0; |
|---|
| 846 | for( i = 0; i < nn; i++ ) |
|---|
| 847 | if( ilist[i] > cc ) cc = ilist[i]; |
|---|
| 848 | cc++; |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | osg::Vec4Array* osg_colors = new osg::Vec4Array(cc); |
|---|
| 852 | for( i = 0; i < cc; i++ ) |
|---|
| 853 | { |
|---|
| 854 | (*osg_colors)[i][0] = colors[i][0]; |
|---|
| 855 | (*osg_colors)[i][1] = colors[i][1]; |
|---|
| 856 | (*osg_colors)[i][2] = colors[i][2]; |
|---|
| 857 | (*osg_colors)[i][3] = colors[i][3]; |
|---|
| 858 | } |
|---|
| 859 | geom->setColorArray(osg_colors); |
|---|
| 860 | |
|---|
| 861 | osg::UShortArray* osg_indices = new osg::UShortArray; |
|---|
| 862 | osg_indices->reserve(nv); |
|---|
| 863 | |
|---|
| 864 | int ni=0; |
|---|
| 865 | for( i = 0; i < np; ++i) |
|---|
| 866 | { |
|---|
| 867 | for(int si=0;si<flat_shaded_skip_per_primitive;++si) |
|---|
| 868 | { |
|---|
| 869 | osg_indices->push_back(ilist[ni]); |
|---|
| 870 | } |
|---|
| 871 | for( int pi=0; pi < plen[i]-flat_shaded_skip_per_primitive; ++pi) |
|---|
| 872 | { |
|---|
| 873 | osg_indices->push_back(ilist[ni++]); |
|---|
| 874 | } |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | if (ni!=nn) |
|---|
| 878 | { |
|---|
| 879 | OSG_INFO << "1 ni!=nn"<<std::endl; |
|---|
| 880 | } |
|---|
| 881 | |
|---|
| 882 | geom->setColorIndices(osg_indices); |
|---|
| 883 | |
|---|
| 884 | |
|---|
| 885 | } |
|---|
| 886 | else |
|---|
| 887 | { |
|---|
| 888 | osg::Vec4Array* osg_colors = new osg::Vec4Array; |
|---|
| 889 | osg_colors->reserve(nv); |
|---|
| 890 | |
|---|
| 891 | int ni=0; |
|---|
| 892 | for( i = 0; i < np; ++i) |
|---|
| 893 | { |
|---|
| 894 | for(int si=0;si<flat_shaded_skip_per_primitive;++si) |
|---|
| 895 | { |
|---|
| 896 | osg_colors->push_back(osg::Vec4(colors[ni][0],colors[ni][1],colors[ni][2],colors[ni][3])); |
|---|
| 897 | } |
|---|
| 898 | for( int pi=0; pi < plen[i]-flat_shaded_skip_per_primitive; ++pi) |
|---|
| 899 | { |
|---|
| 900 | osg_colors->push_back(osg::Vec4(colors[ni][0],colors[ni][1],colors[ni][2],colors[ni][3])); |
|---|
| 901 | ni++; |
|---|
| 902 | } |
|---|
| 903 | } |
|---|
| 904 | |
|---|
| 905 | geom->setColorArray(osg_colors); |
|---|
| 906 | } |
|---|
| 907 | } |
|---|
| 908 | else |
|---|
| 909 | { |
|---|
| 910 | int nn = bind == PFGS_OFF ? 0 : |
|---|
| 911 | bind == PFGS_OVERALL ? 1 : |
|---|
| 912 | bind == PFGS_PER_PRIM ? geoset->getNumPrims() : |
|---|
| 913 | bind == PFGS_PER_VERTEX ? nv : 0; |
|---|
| 914 | |
|---|
| 915 | |
|---|
| 916 | geom->setColorBinding(_gsetBindMap[bind]); |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | int cc; |
|---|
| 920 | if (ilist) |
|---|
| 921 | { |
|---|
| 922 | cc = 0; |
|---|
| 923 | for( i = 0; i < nn; i++ ) |
|---|
| 924 | if( ilist[i] > cc ) cc = ilist[i]; |
|---|
| 925 | cc++; |
|---|
| 926 | } |
|---|
| 927 | else |
|---|
| 928 | cc = nn; |
|---|
| 929 | |
|---|
| 930 | osg::Vec4Array* osg_colors = new osg::Vec4Array(cc); |
|---|
| 931 | for( i = 0; i < cc; i++ ) |
|---|
| 932 | { |
|---|
| 933 | (*osg_colors)[i][0] = colors[i][0]; |
|---|
| 934 | (*osg_colors)[i][1] = colors[i][1]; |
|---|
| 935 | (*osg_colors)[i][2] = colors[i][2]; |
|---|
| 936 | (*osg_colors)[i][3] = colors[i][3]; |
|---|
| 937 | } |
|---|
| 938 | |
|---|
| 939 | geom->setColorArray(osg_colors); |
|---|
| 940 | |
|---|
| 941 | if(ilist) |
|---|
| 942 | { |
|---|
| 943 | geom->setColorIndices(new osg::UShortArray(nn,ilist)); |
|---|
| 944 | } |
|---|
| 945 | |
|---|
| 946 | } |
|---|
| 947 | } |
|---|
| 948 | else |
|---|
| 949 | { |
|---|
| 950 | } |
|---|
| 951 | |
|---|
| 952 | |
|---|
| 953 | |
|---|
| 954 | visitGeoState(geom,geoset->getGState()); |
|---|
| 955 | |
|---|
| 956 | if (flat_shaded_skip_per_primitive) |
|---|
| 957 | { |
|---|
| 958 | osg::StateSet* stateset = geom->getOrCreateStateSet(); |
|---|
| 959 | osg::ShadeModel* shademodel = dynamic_cast<osg::ShadeModel*>(stateset->getAttribute(osg::StateAttribute::SHADEMODEL)); |
|---|
| 960 | if (!shademodel) |
|---|
| 961 | { |
|---|
| 962 | shademodel = new osg::ShadeModel; |
|---|
| 963 | stateset->setAttribute(shademodel); |
|---|
| 964 | } |
|---|
| 965 | shademodel->setMode( osg::ShadeModel::FLAT ); |
|---|
| 966 | } |
|---|
| 967 | |
|---|
| 968 | osgGeode->addDrawable(geom); |
|---|
| 969 | registerPfObjectForOsgObject(geoset,geom); |
|---|
| 970 | |
|---|
| 971 | return geom; |
|---|
| 972 | } |
|---|
| 973 | |
|---|
| 974 | |
|---|
| 975 | osg::StateSet* ConvertFromPerformer::visitGeoState(osg::Drawable* osgDrawable,pfGeoState* geostate) |
|---|
| 976 | { |
|---|
| 977 | if (geostate==NULL) return NULL; |
|---|
| 978 | |
|---|
| 979 | osg::StateSet* osgStateSet = dynamic_cast<osg::StateSet*>(getOsgObject(geostate)); |
|---|
| 980 | if (osgStateSet) |
|---|
| 981 | { |
|---|
| 982 | if (osgDrawable) osgDrawable->setStateSet(osgStateSet); |
|---|
| 983 | return osgStateSet; |
|---|
| 984 | } |
|---|
| 985 | |
|---|
| 986 | osgStateSet = new osg::StateSet; |
|---|
| 987 | if (osgDrawable) osgDrawable->setStateSet(osgStateSet); |
|---|
| 988 | |
|---|
| 989 | registerPfObjectForOsgObject(geostate,osgStateSet); |
|---|
| 990 | |
|---|
| 991 | |
|---|
| 992 | unsigned int inherit = geostate->getInherit(); |
|---|
| 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 | if (!(inherit & PFSTATE_ALPHAFUNC)) { |
|---|
| 1027 | int mode = geostate->getMode(PFSTATE_ALPHAFUNC); |
|---|
| 1028 | float value = geostate->getVal(PFSTATE_ALPHAREF); |
|---|
| 1029 | osg::AlphaFunc* aFunc = new osg::AlphaFunc(); |
|---|
| 1030 | switch (mode) { |
|---|
| 1031 | case PFAF_ALWAYS: |
|---|
| 1032 | aFunc->setFunction(osg::AlphaFunc::ALWAYS); |
|---|
| 1033 | break; |
|---|
| 1034 | case PFAF_EQUAL: |
|---|
| 1035 | aFunc->setFunction(osg::AlphaFunc::EQUAL); |
|---|
| 1036 | break; |
|---|
| 1037 | case PFAF_GEQUAL: |
|---|
| 1038 | aFunc->setFunction(osg::AlphaFunc::GEQUAL); |
|---|
| 1039 | break; |
|---|
| 1040 | case PFAF_GREATER: |
|---|
| 1041 | aFunc->setFunction(osg::AlphaFunc::GREATER); |
|---|
| 1042 | break; |
|---|
| 1043 | case PFAF_LEQUAL: |
|---|
| 1044 | aFunc->setFunction(osg::AlphaFunc::LEQUAL); |
|---|
| 1045 | break; |
|---|
| 1046 | case PFAF_LESS: |
|---|
| 1047 | aFunc->setFunction(osg::AlphaFunc::LESS); |
|---|
| 1048 | break; |
|---|
| 1049 | case PFAF_NEVER: |
|---|
| 1050 | aFunc->setFunction(osg::AlphaFunc::NEVER); |
|---|
| 1051 | break; |
|---|
| 1052 | case PFAF_NOTEQUAL: |
|---|
| 1053 | aFunc->setFunction(osg::AlphaFunc::NOTEQUAL); |
|---|
| 1054 | break; |
|---|
| 1055 | |
|---|
| 1056 | |
|---|
| 1057 | default: |
|---|
| 1058 | break; |
|---|
| 1059 | } |
|---|
| 1060 | aFunc->setReferenceValue(value); |
|---|
| 1061 | if (mode != PFAF_OFF) { |
|---|
| 1062 | osgStateSet->setAttributeAndModes(aFunc); |
|---|
| 1063 | } else { |
|---|
| 1064 | osgStateSet->setAttributeAndModes(aFunc,osg::StateAttribute::OFF); |
|---|
| 1065 | } |
|---|
| 1066 | } |
|---|
| 1067 | |
|---|
| 1068 | if (inherit & PFSTATE_TRANSPARENCY) osgStateSet->setMode(GL_BLEND,osg::StateAttribute::INHERIT); |
|---|
| 1069 | else |
|---|
| 1070 | { |
|---|
| 1071 | int mode = geostate->getMode(PFSTATE_TRANSPARENCY); |
|---|
| 1072 | if (mode & PFTR_NO_OCCLUDE) { |
|---|
| 1073 | mode &= ~PFTR_NO_OCCLUDE; |
|---|
| 1074 | osg::Depth* depth = new osg::Depth(osg::Depth::LESS,0.0f,1.0f,false); |
|---|
| 1075 | osgStateSet->setAttributeAndModes(depth); |
|---|
| 1076 | } |
|---|
| 1077 | switch(mode) |
|---|
| 1078 | { |
|---|
| 1079 | case(PFTR_FAST): |
|---|
| 1080 | case(PFTR_HIGH_QUALITY): |
|---|
| 1081 | case(PFTR_BLEND_ALPHA): |
|---|
| 1082 | case(PFTR_MS_ALPHA): |
|---|
| 1083 | case(PFTR_MS_ALPHA_MASK): |
|---|
| 1084 | |
|---|
| 1085 | case(PFTR_ON): |
|---|
| 1086 | osgStateSet->setMode(GL_BLEND,osg::StateAttribute::ON); |
|---|
| 1087 | osgStateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 1088 | break; |
|---|
| 1089 | case(PFTR_OFF): osgStateSet->setMode(GL_BLEND,osg::StateAttribute::OFF);break; |
|---|
| 1090 | default: osgStateSet->setMode(GL_BLEND,osg::StateAttribute::INHERIT);break; |
|---|
| 1091 | } |
|---|
| 1092 | } |
|---|
| 1093 | |
|---|
| 1094 | if (inherit & PFSTATE_ENTEXTURE) osgStateSet->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::INHERIT); |
|---|
| 1095 | else |
|---|
| 1096 | { |
|---|
| 1097 | int mode = geostate->getMode(PFSTATE_ENTEXTURE); |
|---|
| 1098 | switch(mode) |
|---|
| 1099 | { |
|---|
| 1100 | case(PF_ON): osgStateSet->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::ON);break; |
|---|
| 1101 | case(PF_OFF): |
|---|
| 1102 | default: osgStateSet->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF);break; |
|---|
| 1103 | } |
|---|
| 1104 | } |
|---|
| 1105 | |
|---|
| 1106 | if (inherit & PFSTATE_CULLFACE) osgStateSet->setMode(GL_CULL_FACE,osg::StateAttribute::INHERIT); |
|---|
| 1107 | else |
|---|
| 1108 | { |
|---|
| 1109 | int mode = geostate->getMode(PFSTATE_CULLFACE); |
|---|
| 1110 | switch(mode) |
|---|
| 1111 | { |
|---|
| 1112 | case(PFCF_BACK): |
|---|
| 1113 | { |
|---|
| 1114 | osgStateSet->setMode(GL_CULL_FACE,osg::StateAttribute::ON); |
|---|
| 1115 | osg::CullFace *cf = new osg::CullFace; |
|---|
| 1116 | cf->setMode(osg::CullFace::BACK); |
|---|
| 1117 | osgStateSet->setAttribute(cf); |
|---|
| 1118 | } |
|---|
| 1119 | break; |
|---|
| 1120 | |
|---|
| 1121 | case(PFCF_FRONT): |
|---|
| 1122 | { |
|---|
| 1123 | osgStateSet->setMode(GL_CULL_FACE,osg::StateAttribute::ON); |
|---|
| 1124 | osg::CullFace *cf = new osg::CullFace; |
|---|
| 1125 | cf->setMode(osg::CullFace::FRONT); |
|---|
| 1126 | osgStateSet->setAttribute(cf); |
|---|
| 1127 | } |
|---|
| 1128 | break; |
|---|
| 1129 | case(PFCF_BOTH): |
|---|
| 1130 | { |
|---|
| 1131 | osgStateSet->setMode(GL_CULL_FACE,osg::StateAttribute::ON); |
|---|
| 1132 | osg::CullFace *cf = new osg::CullFace; |
|---|
| 1133 | cf->setMode(osg::CullFace::FRONT_AND_BACK); |
|---|
| 1134 | osgStateSet->setAttribute(cf); |
|---|
| 1135 | } |
|---|
| 1136 | break; |
|---|
| 1137 | case(PFCF_OFF): |
|---|
| 1138 | default: osgStateSet->setMode(GL_CULL_FACE,osg::StateAttribute::OFF);break; |
|---|
| 1139 | } |
|---|
| 1140 | } |
|---|
| 1141 | |
|---|
| 1142 | if (inherit & PFSTATE_ENLIGHTING) osgStateSet->setMode(GL_LIGHTING,osg::StateAttribute::INHERIT); |
|---|
| 1143 | else |
|---|
| 1144 | { |
|---|
| 1145 | int mode = geostate->getMode(PFSTATE_ENLIGHTING); |
|---|
| 1146 | switch(mode) |
|---|
| 1147 | { |
|---|
| 1148 | case(PF_ON): osgStateSet->setMode(GL_LIGHTING,osg::StateAttribute::ON);break; |
|---|
| 1149 | case(PF_OFF): |
|---|
| 1150 | default: osgStateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF);break; |
|---|
| 1151 | } |
|---|
| 1152 | } |
|---|
| 1153 | |
|---|
| 1154 | if (inherit & PFSTATE_ENFOG) osgStateSet->setMode(GL_FOG,osg::StateAttribute::INHERIT); |
|---|
| 1155 | else |
|---|
| 1156 | { |
|---|
| 1157 | int mode = geostate->getMode(PFSTATE_ENFOG); |
|---|
| 1158 | switch(mode) |
|---|
| 1159 | { |
|---|
| 1160 | case(PF_ON): osgStateSet->setMode(GL_FOG,osg::StateAttribute::ON);break; |
|---|
| 1161 | case(PF_OFF): |
|---|
| 1162 | default: osgStateSet->setMode(GL_FOG,osg::StateAttribute::OFF);break; |
|---|
| 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 | pfMaterial* front_mat = (pfMaterial*)geostate->getAttr(PFSTATE_FRONTMTL); |
|---|
| 1210 | pfMaterial* back_mat = (pfMaterial*)geostate->getAttr(PFSTATE_BACKMTL); |
|---|
| 1211 | visitMaterial(osgStateSet,front_mat,back_mat); |
|---|
| 1212 | |
|---|
| 1213 | pfTexture* tex = (pfTexture*)geostate->getAttr(PFSTATE_TEXTURE); |
|---|
| 1214 | visitTexture(osgStateSet,tex); |
|---|
| 1215 | |
|---|
| 1216 | pfTexEnv* texenv = (pfTexEnv*)geostate->getAttr(PFSTATE_TEXENV); |
|---|
| 1217 | |
|---|
| 1218 | if(texenv) |
|---|
| 1219 | { |
|---|
| 1220 | osg::TexEnv* osgTexEnv = new osg::TexEnv(); |
|---|
| 1221 | int mode = texenv->getMode(); |
|---|
| 1222 | |
|---|
| 1223 | float r,g,b,a; |
|---|
| 1224 | texenv->getBlendColor(&r, &g, &b, &a); |
|---|
| 1225 | |
|---|
| 1226 | switch(mode) |
|---|
| 1227 | { |
|---|
| 1228 | case(PFTE_MODULATE) : |
|---|
| 1229 | osgTexEnv->setMode(osg::TexEnv::MODULATE); |
|---|
| 1230 | osgTexEnv->setColor(osg::Vec4(r,g,b,a)); |
|---|
| 1231 | break; |
|---|
| 1232 | case(PFTE_DECAL) : |
|---|
| 1233 | osgTexEnv->setMode(osg::TexEnv::DECAL); |
|---|
| 1234 | osgTexEnv->setColor(osg::Vec4(r,g,b,a)); |
|---|
| 1235 | break; |
|---|
| 1236 | case(PFTE_BLEND) : |
|---|
| 1237 | osgTexEnv->setMode(osg::TexEnv::BLEND); |
|---|
| 1238 | osgTexEnv->setColor(osg::Vec4(r,g,b,a)); |
|---|
| 1239 | break; |
|---|
| 1240 | case(PFTE_REPLACE) : |
|---|
| 1241 | osgTexEnv->setMode(osg::TexEnv::REPLACE); |
|---|
| 1242 | osgTexEnv->setColor(osg::Vec4(r,g,b,a)); |
|---|
| 1243 | break; |
|---|
| 1244 | case(PFTE_ADD) : |
|---|
| 1245 | osgTexEnv->setMode(osg::TexEnv::ADD); |
|---|
| 1246 | osgTexEnv->setColor(osg::Vec4(r,g,b,a)); |
|---|
| 1247 | break; |
|---|
| 1248 | default: |
|---|
| 1249 | OSG_WARN << "TexEnv Mode "<<mode<<" not currently supported by the OSG."<<std::endl; |
|---|
| 1250 | break; |
|---|
| 1251 | } |
|---|
| 1252 | osgStateSet->setTextureAttribute(0,osgTexEnv); |
|---|
| 1253 | } |
|---|
| 1254 | |
|---|
| 1255 | pfTexGen* texgen = (pfTexGen*)geostate->getAttr(PFSTATE_TEXGEN); |
|---|
| 1256 | |
|---|
| 1257 | if (texgen) |
|---|
| 1258 | { |
|---|
| 1259 | osg::TexGen* osgTexGen = new osg::TexGen(); |
|---|
| 1260 | int mode = texgen->getMode(PF_S); |
|---|
| 1261 | |
|---|
| 1262 | |
|---|
| 1263 | float x, y, z, d; |
|---|
| 1264 | texgen->getPlane(PF_S, &x, &y, &z, &d); |
|---|
| 1265 | osgTexGen->setPlane(osg::TexGen::S, osg::Vec4(x,y,z,d)); |
|---|
| 1266 | texgen->getPlane(PF_T, &x, &y, &z, &d); |
|---|
| 1267 | osgTexGen->setPlane(osg::TexGen::T, osg::Vec4(x,y,z,d)); |
|---|
| 1268 | |
|---|
| 1269 | switch(mode) |
|---|
| 1270 | { |
|---|
| 1271 | case(PFTG_OBJECT_LINEAR) : |
|---|
| 1272 | osgTexGen->setMode(osg::TexGen::OBJECT_LINEAR); |
|---|
| 1273 | osgStateSet->setTextureAttribute(0,osgTexGen); |
|---|
| 1274 | osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 1275 | osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 1276 | break; |
|---|
| 1277 | case(PFTG_EYE_LINEAR_IDENT) : |
|---|
| 1278 | OSG_WARN << "TexGen Mode PFTG_EYE_LINEAR_IDENT not currently supported by the OSG,"<<std::endl; |
|---|
| 1279 | OSG_WARN << " assuming osg::TexGen::EYE_LINEAR."<<std::endl; |
|---|
| 1280 | case(PFTG_EYE_LINEAR) : |
|---|
| 1281 | osgTexGen->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 1282 | osgStateSet->setTextureAttribute(0,osgTexGen); |
|---|
| 1283 | osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 1284 | osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 1285 | break; |
|---|
| 1286 | case(PFTG_SPHERE_MAP) : |
|---|
| 1287 | osgTexGen->setMode(osg::TexGen::SPHERE_MAP); |
|---|
| 1288 | osgStateSet->setTextureAttribute(0,osgTexGen); |
|---|
| 1289 | osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 1290 | osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 1291 | break; |
|---|
| 1292 | case(PFTG_OFF) : |
|---|
| 1293 | osgStateSet->setAssociatedTextureModes(0,osgTexGen,osg::StateAttribute::OFF); |
|---|
| 1294 | break; |
|---|
| 1295 | case(PFTG_OBJECT_DISTANCE_TO_LINE) : |
|---|
| 1296 | OSG_WARN << "TexGen Mode PFTG_OBJECT_DISTANCE_TO_LINE not currently supported by the OSG."<<std::endl; |
|---|
| 1297 | osgStateSet->setAssociatedTextureModes(0,osgTexGen,osg::StateAttribute::OFF); |
|---|
| 1298 | break; |
|---|
| 1299 | case(PFTG_EYE_DISTANCE_TO_LINE) : |
|---|
| 1300 | OSG_WARN << "TexGen Mode PFTG_EYE_DISTANCE_TO_LINE not currently supported by the OSG."<<std::endl; |
|---|
| 1301 | osgStateSet->setAssociatedTextureModes(0,osgTexGen,osg::StateAttribute::OFF); |
|---|
| 1302 | break; |
|---|
| 1303 | default: |
|---|
| 1304 | OSG_WARN << "TexGen Mode "<<mode<<" not currently supported by the OSG."<<std::endl; |
|---|
| 1305 | osgStateSet->setAssociatedTextureModes(0,osgTexGen,osg::StateAttribute::OFF); |
|---|
| 1306 | break; |
|---|
| 1307 | } |
|---|
| 1308 | } |
|---|
| 1309 | |
|---|
| 1310 | pfMatrix* texmat = (pfMatrix*)geostate->getAttr(PFSTATE_TEXMAT); |
|---|
| 1311 | if (texmat) |
|---|
| 1312 | { |
|---|
| 1313 | osg::Matrix osgMatrix((*texmat)[0][0],(*texmat)[0][1],(*texmat)[0][2],(*texmat)[0][3], |
|---|
| 1314 | (*texmat)[1][0],(*texmat)[1][1],(*texmat)[1][2],(*texmat)[1][3], |
|---|
| 1315 | (*texmat)[2][0],(*texmat)[2][1],(*texmat)[2][2],(*texmat)[2][3], |
|---|
| 1316 | (*texmat)[3][0],(*texmat)[3][1],(*texmat)[3][2],(*texmat)[3][3]); |
|---|
| 1317 | |
|---|
| 1318 | osg::TexMat* osgTexMat = new osg::TexMat(); |
|---|
| 1319 | osgTexMat->setMatrix(osgMatrix); |
|---|
| 1320 | osgStateSet->setTextureAttribute(0,osgTexMat); |
|---|
| 1321 | } |
|---|
| 1322 | |
|---|
| 1323 | return osgStateSet; |
|---|
| 1324 | } |
|---|
| 1325 | |
|---|
| 1326 | |
|---|
| 1327 | osg::Material* ConvertFromPerformer::visitMaterial(osg::StateSet* osgStateSet,pfMaterial* front_mat,pfMaterial* back_mat) |
|---|
| 1328 | { |
|---|
| 1329 | if (front_mat==NULL && back_mat==NULL) return NULL; |
|---|
| 1330 | |
|---|
| 1331 | osg::Material* osgMaterial = new osg::Material; |
|---|
| 1332 | if (osgStateSet) osgStateSet->setAttribute(osgMaterial); |
|---|
| 1333 | |
|---|
| 1334 | pfMaterial* material = NULL; |
|---|
| 1335 | if (front_mat==back_mat) material = front_mat; |
|---|
| 1336 | else if (back_mat==NULL) material = front_mat; |
|---|
| 1337 | else if (front_mat==NULL) material = back_mat; |
|---|
| 1338 | |
|---|
| 1339 | if (material) |
|---|
| 1340 | { |
|---|
| 1341 | |
|---|
| 1342 | int colorMode = material->getColorMode(material->getSide()); |
|---|
| 1343 | |
|---|
| 1344 | switch(colorMode) |
|---|
| 1345 | { |
|---|
| 1346 | case(PFMTL_CMODE_AMBIENT_AND_DIFFUSE): osgMaterial->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE); break; |
|---|
| 1347 | case(PFMTL_CMODE_AMBIENT): osgMaterial->setColorMode(osg::Material::AMBIENT); break; |
|---|
| 1348 | case(PFMTL_CMODE_DIFFUSE): osgMaterial->setColorMode(osg::Material::DIFFUSE); break; |
|---|
| 1349 | case(PFMTL_CMODE_EMISSION): osgMaterial->setColorMode(osg::Material::EMISSION); break; |
|---|
| 1350 | case(PFMTL_CMODE_SPECULAR): osgMaterial->setColorMode(osg::Material::SPECULAR); break; |
|---|
| 1351 | case(PFMTL_CMODE_OFF): osgMaterial->setColorMode(osg::Material::OFF); break; |
|---|
| 1352 | } |
|---|
| 1353 | |
|---|
| 1354 | osgMaterial->setShininess(osg::Material::FRONT_AND_BACK,material->getShininess()); |
|---|
| 1355 | |
|---|
| 1356 | float a = material->getAlpha(); |
|---|
| 1357 | float r,g,b; |
|---|
| 1358 | |
|---|
| 1359 | material->getColor(PFMTL_AMBIENT,&r,&g,&b); |
|---|
| 1360 | osgMaterial->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a)); |
|---|
| 1361 | |
|---|
| 1362 | material->getColor(PFMTL_DIFFUSE,&r,&g,&b); |
|---|
| 1363 | osgMaterial->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a)); |
|---|
| 1364 | |
|---|
| 1365 | material->getColor(PFMTL_EMISSION,&r,&g,&b); |
|---|
| 1366 | osgMaterial->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a)); |
|---|
| 1367 | |
|---|
| 1368 | material->getColor(PFMTL_SPECULAR,&r,&g,&b); |
|---|
| 1369 | osgMaterial->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a)); |
|---|
| 1370 | } |
|---|
| 1371 | else |
|---|
| 1372 | { |
|---|
| 1373 | |
|---|
| 1374 | int colorMode = front_mat->getColorMode(front_mat->getSide()); |
|---|
| 1375 | |
|---|
| 1376 | switch(colorMode) |
|---|
| 1377 | { |
|---|
| 1378 | case(PFMTL_CMODE_AMBIENT_AND_DIFFUSE): osgMaterial->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE); break; |
|---|
| 1379 | case(PFMTL_CMODE_AMBIENT): osgMaterial->setColorMode(osg::Material::AMBIENT); break; |
|---|
| 1380 | case(PFMTL_CMODE_DIFFUSE): osgMaterial->setColorMode(osg::Material::DIFFUSE); break; |
|---|
| 1381 | case(PFMTL_CMODE_EMISSION): osgMaterial->setColorMode(osg::Material::EMISSION); break; |
|---|
| 1382 | case(PFMTL_CMODE_SPECULAR): osgMaterial->setColorMode(osg::Material::SPECULAR); break; |
|---|
| 1383 | case(PFMTL_CMODE_OFF): osgMaterial->setColorMode(osg::Material::OFF); break; |
|---|
| 1384 | } |
|---|
| 1385 | |
|---|
| 1386 | float a; |
|---|
| 1387 | float r,g,b; |
|---|
| 1388 | |
|---|
| 1389 | |
|---|
| 1390 | osgMaterial->setShininess(osg::Material::FRONT,front_mat->getShininess()); |
|---|
| 1391 | |
|---|
| 1392 | a = front_mat->getAlpha(); |
|---|
| 1393 | |
|---|
| 1394 | front_mat->getColor(PFMTL_AMBIENT,&r,&g,&b); |
|---|
| 1395 | osgMaterial->setAmbient(osg::Material::FRONT,osg::Vec4(r,g,b,a)); |
|---|
| 1396 | |
|---|
| 1397 | front_mat->getColor(PFMTL_DIFFUSE,&r,&g,&b); |
|---|
| 1398 | osgMaterial->setDiffuse(osg::Material::FRONT,osg::Vec4(r,g,b,a)); |
|---|
| 1399 | |
|---|
| 1400 | front_mat->getColor(PFMTL_EMISSION,&r,&g,&b); |
|---|
| 1401 | osgMaterial->setEmission(osg::Material::FRONT,osg::Vec4(r,g,b,a)); |
|---|
| 1402 | |
|---|
| 1403 | front_mat->getColor(PFMTL_SPECULAR,&r,&g,&b); |
|---|
| 1404 | osgMaterial->setSpecular(osg::Material::FRONT,osg::Vec4(r,g,b,a)); |
|---|
| 1405 | |
|---|
| 1406 | |
|---|
| 1407 | osgMaterial->setShininess(osg::Material::BACK,back_mat->getShininess()); |
|---|
| 1408 | |
|---|
| 1409 | a = back_mat->getAlpha(); |
|---|
| 1410 | |
|---|
| 1411 | back_mat->getColor(PFMTL_AMBIENT,&r,&g,&b); |
|---|
| 1412 | osgMaterial->setAmbient(osg::Material::BACK,osg::Vec4(r,g,b,a)); |
|---|
| 1413 | |
|---|
| 1414 | back_mat->getColor(PFMTL_DIFFUSE,&r,&g,&b); |
|---|
| 1415 | osgMaterial->setDiffuse(osg::Material::BACK,osg::Vec4(r,g,b,a)); |
|---|
| 1416 | |
|---|
| 1417 | back_mat->getColor(PFMTL_EMISSION,&r,&g,&b); |
|---|
| 1418 | osgMaterial->setEmission(osg::Material::BACK,osg::Vec4(r,g,b,a)); |
|---|
| 1419 | |
|---|
| 1420 | back_mat->getColor(PFMTL_SPECULAR,&r,&g,&b); |
|---|
| 1421 | osgMaterial->setSpecular(osg::Material::BACK,osg::Vec4(r,g,b,a)); |
|---|
| 1422 | |
|---|
| 1423 | } |
|---|
| 1424 | |
|---|
| 1425 | return osgMaterial; |
|---|
| 1426 | } |
|---|
| 1427 | |
|---|
| 1428 | |
|---|
| 1429 | static osg::Texture2D::FilterMode getTexfilter(int filter, int pftype) |
|---|
| 1430 | { |
|---|
| 1431 | if (filter == PFTEX_MINFILTER) |
|---|
| 1432 | { |
|---|
| 1433 | |
|---|
| 1434 | if (pftype & PFTEX_LINEAR) |
|---|
| 1435 | return osg::Texture2D::NEAREST_MIPMAP_LINEAR; |
|---|
| 1436 | else if (pftype & PFTEX_BILINEAR) |
|---|
| 1437 | return osg::Texture2D::LINEAR_MIPMAP_NEAREST; |
|---|
| 1438 | else if (pftype & PFTEX_TRILINEAR) |
|---|
| 1439 | return osg::Texture2D::LINEAR_MIPMAP_LINEAR; |
|---|
| 1440 | |
|---|
| 1441 | return osg::Texture2D::NEAREST_MIPMAP_LINEAR; |
|---|
| 1442 | |
|---|
| 1443 | } |
|---|
| 1444 | else |
|---|
| 1445 | { |
|---|
| 1446 | |
|---|
| 1447 | |
|---|
| 1448 | |
|---|
| 1449 | |
|---|
| 1450 | |
|---|
| 1451 | return osg::Texture2D::LINEAR; |
|---|
| 1452 | } |
|---|
| 1453 | } |
|---|
| 1454 | |
|---|
| 1455 | |
|---|
| 1456 | osg::Texture2D* ConvertFromPerformer::visitTexture(osg::StateSet* osgStateSet,pfTexture* tex) |
|---|
| 1457 | { |
|---|
| 1458 | if (tex==NULL) return NULL; |
|---|
| 1459 | |
|---|
| 1460 | osg::Texture2D* osgTexture = dynamic_cast<osg::Texture2D*>(getOsgObject(tex)); |
|---|
| 1461 | if (osgTexture) { |
|---|
| 1462 | if (osgStateSet) osgStateSet->setTextureAttribute(0,osgTexture); |
|---|
| 1463 | return osgTexture; |
|---|
| 1464 | } |
|---|
| 1465 | |
|---|
| 1466 | osgTexture = new osg::Texture2D; |
|---|
| 1467 | registerPfObjectForOsgObject(tex, osgTexture); |
|---|
| 1468 | |
|---|
| 1469 | if (osgStateSet) osgStateSet->setTextureAttribute(0,osgTexture); |
|---|
| 1470 | |
|---|
| 1471 | int repeat_r = tex->getRepeat(PFTEX_WRAP_R); |
|---|
| 1472 | int repeat_s = tex->getRepeat(PFTEX_WRAP_S); |
|---|
| 1473 | int repeat_t = tex->getRepeat(PFTEX_WRAP_T); |
|---|
| 1474 | |
|---|
| 1475 | if (repeat_r==PFTEX_CLAMP) |
|---|
| 1476 | osgTexture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::CLAMP); |
|---|
| 1477 | else |
|---|
| 1478 | osgTexture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::REPEAT); |
|---|
| 1479 | |
|---|
| 1480 | if (repeat_s==PFTEX_CLAMP) |
|---|
| 1481 | osgTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP); |
|---|
| 1482 | else |
|---|
| 1483 | osgTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT); |
|---|
| 1484 | |
|---|
| 1485 | if (repeat_t==PFTEX_CLAMP) |
|---|
| 1486 | osgTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP); |
|---|
| 1487 | else |
|---|
| 1488 | osgTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT); |
|---|
| 1489 | |
|---|
| 1490 | |
|---|
| 1491 | #if 1 |
|---|
| 1492 | osgTexture->setFilter(osg::Texture2D::MIN_FILTER, |
|---|
| 1493 | getTexfilter(PFTEX_MINFILTER, |
|---|
| 1494 | tex->getFilter(PFTEX_MINFILTER))); |
|---|
| 1495 | osgTexture->setFilter(osg::Texture2D::MAG_FILTER, |
|---|
| 1496 | getTexfilter(PFTEX_MAGFILTER, |
|---|
| 1497 | tex->getFilter(PFTEX_MAGFILTER))); |
|---|
| 1498 | #endif |
|---|
| 1499 | |
|---|
| 1500 | |
|---|
| 1501 | std::string texName = tex->getName(); |
|---|
| 1502 | |
|---|
| 1503 | if (_saveImagesAsRGB) { |
|---|
| 1504 | std::string strippedName = osgDB::getStrippedName(texName); |
|---|
| 1505 | |
|---|
| 1506 | pfList* imgList = tex->getList(); |
|---|
| 1507 | if (imgList) { |
|---|
| 1508 | |
|---|
| 1509 | char buf[8]; |
|---|
| 1510 | for (int i = 0; i < imgList->getNum(); i++) { |
|---|
| 1511 | pfTexture* t = (pfTexture*) imgList->get(i); |
|---|
| 1512 | if (t) { |
|---|
| 1513 | snprintf(buf, sizeof(buf)-1, "_%04d", i); |
|---|
| 1514 | texName = _saveImageDirectory+strippedName+buf+".rgb"; |
|---|
| 1515 | t->saveFile(texName.c_str()); |
|---|
| 1516 | } |
|---|
| 1517 | } |
|---|
| 1518 | } |
|---|
| 1519 | else { |
|---|
| 1520 | |
|---|
| 1521 | texName = _saveImageDirectory+strippedName+".rgb"; |
|---|
| 1522 | tex->saveFile(texName.c_str()); |
|---|
| 1523 | } |
|---|
| 1524 | } |
|---|
| 1525 | |
|---|
| 1526 | if (!_saveAbsoluteImagePath) texName = osgDB::getSimpleFileName(texName); |
|---|
| 1527 | |
|---|
| 1528 | int s=0; |
|---|
| 1529 | int t=0; |
|---|
| 1530 | int r=0; |
|---|
| 1531 | int comp=0; |
|---|
| 1532 | unsigned int* imageData = NULL; |
|---|
| 1533 | |
|---|
| 1534 | tex->getImage(&imageData,&comp,&s,&t,&r); |
|---|
| 1535 | |
|---|
| 1536 | int internalFormat = comp; |
|---|
| 1537 | |
|---|
| 1538 | unsigned int pixelFormat = |
|---|
| 1539 | comp == 1 ? GL_LUMINANCE : |
|---|
| 1540 | comp == 2 ? GL_LUMINANCE_ALPHA : |
|---|
| 1541 | comp == 3 ? GL_RGB : |
|---|
| 1542 | comp == 4 ? GL_RGBA : (GLenum)-1; |
|---|
| 1543 | |
|---|
| 1544 | unsigned int dataType = GL_UNSIGNED_BYTE; |
|---|
| 1545 | |
|---|
| 1546 | |
|---|
| 1547 | int size = s * t * r * comp; |
|---|
| 1548 | unsigned char* data = (unsigned char*) malloc(size); |
|---|
| 1549 | memcpy(data, imageData, size); |
|---|
| 1550 | |
|---|
| 1551 | osg::Image* image = new osg::Image; |
|---|
| 1552 | image->setFileName(texName.c_str()); |
|---|
| 1553 | image->setImage(s,t,r, |
|---|
| 1554 | internalFormat, |
|---|
| 1555 | pixelFormat, |
|---|
| 1556 | dataType,data, |
|---|
| 1557 | osg::Image::USE_MALLOC_FREE); |
|---|
| 1558 | |
|---|
| 1559 | osgTexture->setImage(image); |
|---|
| 1560 | |
|---|
| 1561 | return osgTexture; |
|---|
| 1562 | } |
|---|