Changeset 9053

Show
Ignore:
Timestamp:
10/25/08 15:17:22 (6 years ago)
Author:
robert
Message:

From Jim Vaughan,

- Matrix transform nodes were stripped out, and the
vertices and normals of each node were
transposed by the modelling matrix. My change preserves
the matrix transform nodes, so that
models can still be articulated by changing the matrices.

- Lights were copied from the COIN scenegraph to the OSG
scenegraph, but they were not associated
with a LightSource? node. My change for this creates a
Group and adds a LightSource? for each Light.

- If VRML textures have names, the name is now copied to

--

Form Robert Osfild, changed SbString? to std::string usage

M Inventor/ConvertFromInventor.h
M Inventor/ConvertFromInventor.cpp

Location:
OpenSceneGraph/trunk/src/osgPlugins/Inventor
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgPlugins/Inventor/ConvertFromInventor.cpp

    r7348 r9053  
    1818#include <osg/CullFace> 
    1919#include <osg/LightModel> 
     20#include <osg/LightSource> 
    2021#include <osg/ShadeModel> 
    2122#include <osg/LOD> 
     
    4041#include <Inventor/SoPrimitiveVertex.h> 
    4142#include <Inventor/SbLinear.h> 
     43#include <Inventor/nodes/SoTransform.h> 
     44#include <Inventor/nodes/SoInfo.h> 
    4245 
    4346#ifdef __COIN__ 
    4447#include <Inventor/VRMLnodes/SoVRMLImageTexture.h> 
     48#include <Inventor/VRMLnodes/SoVRMLTransform.h> 
     49#include <Inventor/VRMLnodes/SoVRMLAppearance.h> 
     50#include <Inventor/VRMLnodes/SoVRMLMaterial.h> 
    4551#endif 
    4652 
     
    5056#include <assert.h> 
    5157#include <math.h> 
     58#include <string.h> 
    5259#ifdef __linux 
    5360#include <values.h> 
     
    6269{ 
    6370    numPrimitives = 0; 
     71    transformInfoName = ""; 
     72    appearanceName = ""; 
     73    inAppearanceWithNoTexture = false; 
     74    lightGroup = NULL; 
    6475} 
    6576/////////////////////////////////////////// 
     
    99110    cbAction.addPreCallback(SoVRMLImageTexture::getClassTypeId(), 
    100111                            preVRMLImageTexture, this); 
     112    cbAction.addPreCallback(SoVRMLAppearance::getClassTypeId(), 
     113                            preVRMLAppearance, this); 
     114    cbAction.addPreCallback(SoInfo::getClassTypeId(), preInfo, this); 
    101115#endif 
    102116    cbAction.addPreCallback(SoLight::getClassTypeId(), preLight, this); 
     
    246260    osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry; 
    247261 
    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 
    253263    osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array(thisPtr->vertices.size()); 
    254264    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]; 
    257266    geometry->setVertexArray(coords.get()); 
    258267 
    259  
    260     // Normals need to be transformed using the transpose of the inverse  
    261     // modeling matrix 
    262     osg::Matrix invModelMat; 
    263     invModelMat.invert(modelMat); 
    264     thisPtr->transposeMatrix(invModelMat); 
    265  
    266     // Tranform the normals based on the modeling matrix 
    267268    osg::ref_ptr<osg::Vec3Array> norms = NULL; 
    268269    if (thisPtr->normalBinding == osg::Geometry::BIND_OVERALL) 
     
    271272        const SbVec3f &norm = action->getNormal(0); 
    272273        (*norms)[0].set(norm[0], norm[1], norm[2]); 
    273         (*norms)[0] = invModelMat.transform3x3((*norms)[0],invModelMat); 
    274         (*norms)[0].normalize(); 
    275274    } 
    276275    else 
     
    279278        for (unsigned int i = 0; i < thisPtr->normals.size(); i++) 
    280279        { 
    281             (*norms)[i] = invModelMat.transform3x3(thisPtr->normals[i],  
    282                                                 invModelMat); 
    283             (*norms)[i].normalize(); 
     280            (*norms)[i] = thisPtr->normals[i]; 
    284281        } 
    285282    } 
     
    361358    geode->addDrawable(geometry.get()); 
    362359 
     360    // copy name 
     361    std::string name = stateSet->getName(); 
     362    if (name != "") { 
     363        geode->setName(name); 
     364    } 
    363365    // Add geode to scenegraph 
    364366    thisPtr->groupStack.top()->addChild(geode.get()); 
     
    386388////////////////////////////////////////////////////////////////////////////////// 
    387389SoCallbackAction::Response  
     390ConvertFromInventor::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////////////////////////////////////////////////////////////////////////////////// 
     423SoCallbackAction::Response  
    388424ConvertFromInventor::preVRMLImageTexture(void* data, SoCallbackAction* action, 
    389425                                         const SoNode* node) 
     
    433469    osg::ref_ptr<osg::Light> osgLight = new osg::Light; 
    434470    osgLight->setLightNum(lightNum++); 
     471 
     472    const char* name = ivLight->getName().getString(); 
     473    osgLight->setName(name); 
    435474     
    436475    // Get color and intensity 
     
    487526        thisPtr->lightStack.push(lightList); 
    488527    } 
     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()); 
    489539 
    490540    return SoCallbackAction::CONTINUE; 
     
    519569         
    520570        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); 
    521576 
    522577        // Set the texture environment 
     
    668723 
    669724        stateSet->setAttributeAndModes(material.get(), osg::StateAttribute::ON); 
     725        stateSet->setName(appearanceName.getString()); 
    670726        stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON); 
    671727 
     
    709765    // Copy the texture image data from the inventor texture 
    710766    memcpy(osgImageData, soImageData, soSize[0] * soSize[1] * soNC); 
     767 
     768    // Copy the name 
     769    std::string name = soNode->getName().getString(); 
    711770 
    712771    // File name 
     
    740799    osg::Texture2D *osgTex = new osg::Texture2D; 
    741800    osgTex->setImage(osgImage.get()); 
     801    if (name != "") { 
     802        osgTex->setName(name); 
     803    } 
    742804 
    743805    static std::map<SoTexture2::Wrap, osg::Texture2D::WrapMode> texWrapMap; 
     
    776838/////////////////////////////////////////////////////////////////// 
    777839SoCallbackAction::Response  
    778 ConvertFromInventor::preGroup(void* data, SoCallbackAction*,  
     840ConvertFromInventor::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/////////////////////////////////////////////////////////////////// 
     855SoCallbackAction::Response  
     856ConvertFromInventor::preGroup(void* data, SoCallbackAction* action,  
    779857                              const SoNode* node) 
    780858{ 
     
    783861              << node->getTypeId().getName().getString() << std::endl; 
    784862#endif 
    785  
    786863    ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); 
    787864 
    788865    // Create a new Group or LOD and add it to the stack 
    789866    osg::ref_ptr<osg::Group> group; 
    790     if (node->isOfType(SoLOD::getClassTypeId())) 
     867    if (node->isOfType(SoLOD::getClassTypeId())) { 
    791868        group = new osg::LOD; 
    792     else 
     869    } 
     870    else { 
    793871        group = new osg::Group; 
     872    } 
     873 
    794874    thisPtr->groupStack.top()->addChild(group.get()); 
    795875    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 
    797915    if (node->isOfType(SoSeparator::getClassTypeId())) 
    798916    { 
     
    823941              << node->getTypeId().getName().getString() << std::endl; 
    824942#endif 
    825  
    826943    ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); 
    827944 
     
    10471164} 
    10481165//////////////////////////////////////////////////////////////////////////// 
     1166void 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//////////////////////////////////////////////////////////////////////////// 
    10491224void ConvertFromInventor::addTriangleCB(void* data, SoCallbackAction* action, 
    10501225                                        const SoPrimitiveVertex* v0, 
  • OpenSceneGraph/trunk/src/osgPlugins/Inventor/ConvertFromInventor.h

    r6543 r9053  
    88#include <osg/Light> 
    99#include <Inventor/actions/SoCallbackAction.h> 
     10#include <Inventor/SbLinear.h> 
    1011#include <vector> 
    1112#include <stack> 
     
    5051        static SoCallbackAction::Response preVRMLImageTexture(void* data, 
    5152                                 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); 
    5259 
    5360        static void addTriangleCB(void* data, SoCallbackAction* action, 
     
    6269 
    6370    private: 
     71        SbString transformInfoName; 
     72        SbName appearanceName; 
     73        bool inAppearanceWithNoTexture; 
    6474 
     75        void addMatrixTransform(const std::string& name, SbVec3f axis, float angle, SbVec3f center, SbVec3f trans, SbVec3f scale); 
    6576        void addVertex(SoCallbackAction* action, const SoPrimitiveVertex* v,  
    6677                       int index); 
     
    113124 
    114125        osg::ref_ptr<osg::MatrixTransform> _root;///<The root node; 
     126 
     127        osg::ref_ptr<osg::Group> lightGroup; 
    115128}; 
    116129