Changeset 9053
- Timestamp:
- 10/25/08 15:17:22 (5 years ago)
- Location:
- OpenSceneGraph/trunk/src/osgPlugins/Inventor
- Files:
-
- 2 modified
-
ConvertFromInventor.cpp (modified) (20 diffs)
-
ConvertFromInventor.h (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgPlugins/Inventor/ConvertFromInventor.cpp
r7348 r9053 18 18 #include <osg/CullFace> 19 19 #include <osg/LightModel> 20 #include <osg/LightSource> 20 21 #include <osg/ShadeModel> 21 22 #include <osg/LOD> … … 40 41 #include <Inventor/SoPrimitiveVertex.h> 41 42 #include <Inventor/SbLinear.h> 43 #include <Inventor/nodes/SoTransform.h> 44 #include <Inventor/nodes/SoInfo.h> 42 45 43 46 #ifdef __COIN__ 44 47 #include <Inventor/VRMLnodes/SoVRMLImageTexture.h> 48 #include <Inventor/VRMLnodes/SoVRMLTransform.h> 49 #include <Inventor/VRMLnodes/SoVRMLAppearance.h> 50 #include <Inventor/VRMLnodes/SoVRMLMaterial.h> 45 51 #endif 46 52 … … 50 56 #include <assert.h> 51 57 #include <math.h> 58 #include <string.h> 52 59 #ifdef __linux 53 60 #include <values.h> … … 62 69 { 63 70 numPrimitives = 0; 71 transformInfoName = ""; 72 appearanceName = ""; 73 inAppearanceWithNoTexture = false; 74 lightGroup = NULL; 64 75 } 65 76 /////////////////////////////////////////// … … 99 110 cbAction.addPreCallback(SoVRMLImageTexture::getClassTypeId(), 100 111 preVRMLImageTexture, this); 112 cbAction.addPreCallback(SoVRMLAppearance::getClassTypeId(), 113 preVRMLAppearance, this); 114 cbAction.addPreCallback(SoInfo::getClassTypeId(), preInfo, this); 101 115 #endif 102 116 cbAction.addPreCallback(SoLight::getClassTypeId(), preLight, this); … … 246 260 osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry; 247 261 248 // Get the modeling matrix 249 osg::Matrix modelMat; 250 modelMat.set((float *)action->getModelMatrix().getValue()); 251 252 // Tranform the vertices based on the modeling matrix 262 253 263 osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array(thisPtr->vertices.size()); 254 264 for (unsigned int i = 0; i < thisPtr->vertices.size(); i++) 255 (*coords)[i] = modelMat.preMult(thisPtr->vertices[i]); 256 265 (*coords)[i] = thisPtr->vertices[i]; 257 266 geometry->setVertexArray(coords.get()); 258 267 259 260 // Normals need to be transformed using the transpose of the inverse261 // modeling matrix262 osg::Matrix invModelMat;263 invModelMat.invert(modelMat);264 thisPtr->transposeMatrix(invModelMat);265 266 // Tranform the normals based on the modeling matrix267 268 osg::ref_ptr<osg::Vec3Array> norms = NULL; 268 269 if (thisPtr->normalBinding == osg::Geometry::BIND_OVERALL) … … 271 272 const SbVec3f &norm = action->getNormal(0); 272 273 (*norms)[0].set(norm[0], norm[1], norm[2]); 273 (*norms)[0] = invModelMat.transform3x3((*norms)[0],invModelMat);274 (*norms)[0].normalize();275 274 } 276 275 else … … 279 278 for (unsigned int i = 0; i < thisPtr->normals.size(); i++) 280 279 { 281 (*norms)[i] = invModelMat.transform3x3(thisPtr->normals[i], 282 invModelMat); 283 (*norms)[i].normalize(); 280 (*norms)[i] = thisPtr->normals[i]; 284 281 } 285 282 } … … 361 358 geode->addDrawable(geometry.get()); 362 359 360 // copy name 361 std::string name = stateSet->getName(); 362 if (name != "") { 363 geode->setName(name); 364 } 363 365 // Add geode to scenegraph 364 366 thisPtr->groupStack.top()->addChild(geode.get()); … … 386 388 ////////////////////////////////////////////////////////////////////////////////// 387 389 SoCallbackAction::Response 390 ConvertFromInventor::preVRMLAppearance(void* data, SoCallbackAction* action, 391 const SoNode* node) 392 { 393 #ifdef DEBUG_IV_PLUGIN 394 osg::notify(osg::INFO) << "preVRMLAppearance() " 395 << node->getTypeId().getName().getString() << std::endl; 396 #endif 397 398 #ifdef __COIN__ 399 ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); 400 401 // If there is a VRML appearance node without a texture node, then 402 // we push a NULL texture onto the stack 403 bool foundTex = false; 404 SoChildList *kids = node->getChildren(); 405 for (int i=0; i<kids->getLength(); i++) { 406 SoNode* kid = (SoNode*)kids->get(i); 407 if (kid->isOfType(SoVRMLMaterial::getClassTypeId())) { 408 thisPtr->appearanceName = kid->getName(); 409 } 410 if (kid->isOfType(SoVRMLTexture::getClassTypeId())) { 411 foundTex = true; 412 } 413 } 414 if (!foundTex) { 415 thisPtr->soTexStack.push(NULL); 416 thisPtr->inAppearanceWithNoTexture = true; 417 } 418 #endif 419 return SoCallbackAction::CONTINUE; 420 } 421 422 ////////////////////////////////////////////////////////////////////////////////// 423 SoCallbackAction::Response 388 424 ConvertFromInventor::preVRMLImageTexture(void* data, SoCallbackAction* action, 389 425 const SoNode* node) … … 433 469 osg::ref_ptr<osg::Light> osgLight = new osg::Light; 434 470 osgLight->setLightNum(lightNum++); 471 472 const char* name = ivLight->getName().getString(); 473 osgLight->setName(name); 435 474 436 475 // Get color and intensity … … 487 526 thisPtr->lightStack.push(lightList); 488 527 } 528 529 // add a light source node to the scene graph 530 osg::ref_ptr<osg::LightSource> ls = new osg::LightSource(); 531 ls->setLight(osgLight.get()); 532 ls->setName(ivLight->getName().getString()); 533 if (thisPtr->lightGroup == NULL) { 534 thisPtr->lightGroup = new osg::Group; 535 thisPtr->lightGroup->setName("IvLightGroup"); 536 thisPtr->_root->addChild(thisPtr->lightGroup.get()); 537 } 538 thisPtr->lightGroup->addChild(ls.get()); 489 539 490 540 return SoCallbackAction::CONTINUE; … … 519 569 520 570 stateSet->setTextureAttributeAndModes(0, texture.get(), osg::StateAttribute::ON); 571 572 // propogate name 573 std::string name = texture->getName(); 574 if (name != "") 575 stateSet->setName(name); 521 576 522 577 // Set the texture environment … … 668 723 669 724 stateSet->setAttributeAndModes(material.get(), osg::StateAttribute::ON); 725 stateSet->setName(appearanceName.getString()); 670 726 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON); 671 727 … … 709 765 // Copy the texture image data from the inventor texture 710 766 memcpy(osgImageData, soImageData, soSize[0] * soSize[1] * soNC); 767 768 // Copy the name 769 std::string name = soNode->getName().getString(); 711 770 712 771 // File name … … 740 799 osg::Texture2D *osgTex = new osg::Texture2D; 741 800 osgTex->setImage(osgImage.get()); 801 if (name != "") { 802 osgTex->setName(name); 803 } 742 804 743 805 static std::map<SoTexture2::Wrap, osg::Texture2D::WrapMode> texWrapMap; … … 776 838 /////////////////////////////////////////////////////////////////// 777 839 SoCallbackAction::Response 778 ConvertFromInventor::preGroup(void* data, SoCallbackAction*, 840 ConvertFromInventor::preInfo(void* data, SoCallbackAction* action, 841 const SoNode* node) 842 { 843 #ifdef DEBUG_IV_PLUGIN 844 osg::notify(osg::INFO) << "preInfo() " 845 << node->getTypeId().getName().getString() << std::endl; 846 #endif 847 ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); 848 SoInfo* info = (SoInfo*)node; 849 thisPtr->transformInfoName = info->string.getValue(); 850 851 return SoCallbackAction::CONTINUE; 852 } 853 854 /////////////////////////////////////////////////////////////////// 855 SoCallbackAction::Response 856 ConvertFromInventor::preGroup(void* data, SoCallbackAction* action, 779 857 const SoNode* node) 780 858 { … … 783 861 << node->getTypeId().getName().getString() << std::endl; 784 862 #endif 785 786 863 ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); 787 864 788 865 // Create a new Group or LOD and add it to the stack 789 866 osg::ref_ptr<osg::Group> group; 790 if (node->isOfType(SoLOD::getClassTypeId())) 867 if (node->isOfType(SoLOD::getClassTypeId())) { 791 868 group = new osg::LOD; 792 else 869 } 870 else { 793 871 group = new osg::Group; 872 } 873 794 874 thisPtr->groupStack.top()->addChild(group.get()); 795 875 thisPtr->groupStack.push(group.get()); 796 876 877 // handle transform nodes 878 if (node->isOfType(SoTransform::getClassTypeId())) { 879 SoTransform* t = (SoTransform*)node; 880 SbVec3f axis, center, trans, scale; 881 float angle; 882 883 center = t->center.getValue(); 884 t->rotation.getValue(axis, angle); 885 trans = t->translation.getValue(); 886 scale = t->scaleFactor.getValue(); 887 std::string name = t->getName().getString(); 888 889 thisPtr->addMatrixTransform(name, axis, angle, center, trans, scale); 890 } 891 #ifdef __COIN__ 892 if (node->isOfType(SoVRMLTransform::getClassTypeId())) { 893 std::string name; 894 if (thisPtr->transformInfoName != "") { 895 name = std::string("INFO_"); 896 name += thisPtr->transformInfoName.getString(); 897 name += "_trans"; 898 } 899 else { 900 name = node->getName(); 901 } 902 903 SoVRMLTransform* vt = (SoVRMLTransform*)node; 904 SbVec3f axis, center, trans, scale; 905 float angle; 906 907 center = vt->center.getValue(); 908 vt->rotation.getValue(axis, angle); 909 trans = vt->translation.getValue(); 910 scale = vt->scale.getValue(); 911 912 thisPtr->addMatrixTransform(name, axis, angle, center, trans, scale); 913 } 914 #endif 797 915 if (node->isOfType(SoSeparator::getClassTypeId())) 798 916 { … … 823 941 << node->getTypeId().getName().getString() << std::endl; 824 942 #endif 825 826 943 ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); 827 944 … … 1047 1164 } 1048 1165 //////////////////////////////////////////////////////////////////////////// 1166 void ConvertFromInventor::addMatrixTransform(const std::string& name, SbVec3f axis, float angle, SbVec3f center, SbVec3f trans, SbVec3f scale) 1167 { 1168 osg::Matrix mat; 1169 if (trans.length() != 0.0 || name != "") 1170 { 1171 mat.makeIdentity(); 1172 mat.setTrans(trans[0], trans[1], trans[2]); 1173 osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform(mat); 1174 if (name != "") { 1175 std::string name2 = name; 1176 name2 += "_t"; 1177 mt->setName(name2); 1178 } 1179 groupStack.top()->addChild(mt.get()); 1180 groupStack.push(mt.get()); 1181 } 1182 1183 if (center.length() != 0.0) { 1184 mat.makeIdentity(); 1185 mat.setTrans(center[0], center[1], center[2]); 1186 osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform(mat); 1187 groupStack.top()->addChild(mt.get()); 1188 groupStack.push(mt.get()); 1189 } 1190 1191 if (angle != 0.0 || name != "") 1192 { 1193 osg::Quat q(angle, osg::Vec3f(axis[0], axis[1], axis[2])); 1194 mat.makeIdentity(); 1195 mat.setRotate(q); 1196 osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform(mat); 1197 if (name != "") { 1198 std::string name2 = name; 1199 name2 += "_r"; 1200 mt->setName(name2); 1201 } 1202 groupStack.top()->addChild(mt.get()); 1203 groupStack.push(mt.get()); 1204 } 1205 1206 if (center.length() != 0.0) { 1207 center.negate(); 1208 mat.makeIdentity(); 1209 mat.setTrans(center[0], center[1], center[2]); 1210 osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform(mat); 1211 groupStack.top()->addChild(mt.get()); 1212 groupStack.push(mt.get()); 1213 } 1214 1215 if (scale[0] != 1.0 || scale[1] != 1.0 || scale[2] != 1.0) { 1216 mat.makeIdentity(); 1217 mat.makeScale(scale[0], scale[1], scale[2]); 1218 osg::ref_ptr<osg::MatrixTransform> smt = new osg::MatrixTransform(mat); 1219 groupStack.top()->addChild(smt.get()); 1220 groupStack.push(smt.get()); 1221 } 1222 } 1223 //////////////////////////////////////////////////////////////////////////// 1049 1224 void ConvertFromInventor::addTriangleCB(void* data, SoCallbackAction* action, 1050 1225 const SoPrimitiveVertex* v0, -
OpenSceneGraph/trunk/src/osgPlugins/Inventor/ConvertFromInventor.h
r6543 r9053 8 8 #include <osg/Light> 9 9 #include <Inventor/actions/SoCallbackAction.h> 10 #include <Inventor/SbLinear.h> 10 11 #include <vector> 11 12 #include <stack> … … 50 51 static SoCallbackAction::Response preVRMLImageTexture(void* data, 51 52 SoCallbackAction* action, const SoNode* node); 53 static SoCallbackAction::Response preVRMLAppearance(void* data, 54 SoCallbackAction* action, const SoNode* node); 55 static SoCallbackAction::Response postVRMLAppearance(void* data, 56 SoCallbackAction* action, const SoNode* node); 57 static SoCallbackAction::Response preInfo(void* data, 58 SoCallbackAction* action, const SoNode* node); 52 59 53 60 static void addTriangleCB(void* data, SoCallbackAction* action, … … 62 69 63 70 private: 71 SbString transformInfoName; 72 SbName appearanceName; 73 bool inAppearanceWithNoTexture; 64 74 75 void addMatrixTransform(const std::string& name, SbVec3f axis, float angle, SbVec3f center, SbVec3f trans, SbVec3f scale); 65 76 void addVertex(SoCallbackAction* action, const SoPrimitiveVertex* v, 66 77 int index); … … 113 124 114 125 osg::ref_ptr<osg::MatrixTransform> _root;///<The root node; 126 127 osg::ref_ptr<osg::Group> lightGroup; 115 128 }; 116 129
