#ifndef _CONVERTFROMINVENTOR_H_ #define _CONVERTFROMINVENTOR_H_ #include #include #include #include #include #include #include #include #include #include class ConvertFromInventor { public: ConvertFromInventor(); ~ConvertFromInventor(); /// Conversts from IV to OSG scene graph osg::Node* convert(SoNode* rootIVNode); /** * Preprocessing restructure the scene for the convertor * to be able to convert some peculiar scene constructions. * Resulting scene is geometrically equivalent to the source * scene. The preprocessing is related to grouping nodes only * (SoSeparator, SoGroup, SoLOD, SoSwitch,...) and their * treatment with traversal state. */ void preprocess(SoNode *root); private: // Callback functions for converting inventor scene graph to osg // scene graph static SoCallbackAction::Response preNode(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response postNode(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response preTransformSeparator(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response postTransformSeparator(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response preLOD(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response postLOD(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response preShape(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response postShape(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response postTexture(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response preLight(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response preEnvironment(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response preShaderProgram(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response preRotor(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response prePendulum(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response preShuttle(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response preInfo(void* data, SoCallbackAction* action, const SoNode* node); static void addTriangleCB(void* data, SoCallbackAction* action, const SoPrimitiveVertex *v0, const SoPrimitiveVertex *v1, const SoPrimitiveVertex *v2); static void addLineSegmentCB(void* data, SoCallbackAction* action, const SoPrimitiveVertex *v0, const SoPrimitiveVertex *v1); static void addPointCB(void* data, SoCallbackAction* action, const SoPrimitiveVertex *v0); static SoCallbackAction::Response restructure(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response restructurePreNode(void* data, SoCallbackAction* action, const SoNode* node); static SoCallbackAction::Response restructurePostNode(void* data, SoCallbackAction* action, const SoNode* node); private: SbString transformInfoName; SbName appearanceName; void addVertex(SoCallbackAction* action, const SoPrimitiveVertex* v, int index); osg::ref_ptr getStateSet(SoCallbackAction* action); osg::Texture2D* convertIVTexToOSGTex(const SoNode* soNode, SoCallbackAction* action); void transformLight(SoCallbackAction* action, const SbVec3f& vec, osg::Vec3& transVec); // OSG doesn't seem to have a transpose function for matrices void transposeMatrix(osg::Matrix& mat); private: // Normal and color binding osg::Geometry::AttributeBinding normalBinding; osg::Geometry::AttributeBinding colorBinding; // List of vertices, normals, colors and texture coordinates std::vector vertices; std::vector normals; std::vector colors; std::vector textureCoords; // Num of primitive and primitive type int numPrimitives; osg::PrimitiveSet::Mode primitiveType; // Vertex ordering enum VertexOrder { CLOCKWISE, COUNTER_CLOCKWISE }; VertexOrder vertexOrder; // Mapping from SoTexture2 and SoVRMLImageTexture to already converted // osg::Texture2D - avoids duplication of same texture objects std::map ivToOsgTexMap; osg::ref_ptr _root;/// > inheritedLights; std::vector > currentLights; // Active OpenGL glProgram and associated shaders osg::ref_ptr inheritedGLProgram; osg::ref_ptr currentGLProgram; // Ambient light (of SoEnvironment) SbColor inheritedAmbientLight; SbColor currentAmbientLight; // Generated OSG graph osg::ref_ptr osgStateRoot; // Extra variables const SoNode *keepChildrenOrderParent; IvStateItem(const SoNode *initiator, osg::Group *root = NULL) : flags(IvStateItem::DEFAULT_FLAGS), pushInitiator(initiator), inheritedTransformation(SbMatrix::identity()), lastUsedTransformation(SbMatrix::identity()), inheritedTexture(NULL), currentTexture(NULL), inheritedLights(), currentLights(), inheritedGLProgram(NULL), currentGLProgram(NULL), inheritedAmbientLight(SbColor(0.2f,0.2f,0.2f)), currentAmbientLight(SbColor(0.2f,0.2f,0.2f)), osgStateRoot(root ? root : new osg::Group) {} IvStateItem(const IvStateItem& i, const SoCallbackAction *action, const SoNode *initiator, const int f, osg::Group *root) : flags(f), pushInitiator(initiator), inheritedTransformation(action->getModelMatrix()), lastUsedTransformation(action->getModelMatrix()), inheritedTexture(i.currentTexture), currentTexture(i.currentTexture), inheritedLights(i.currentLights), currentLights(i.currentLights), inheritedGLProgram(i.currentGLProgram), currentGLProgram(i.currentGLProgram), inheritedAmbientLight(i.inheritedAmbientLight), currentAmbientLight(i.currentAmbientLight), osgStateRoot(root) {} }; /// State stack for Inventor scene traversal std::stack ivStateStack; void ivPushState(const SoCallbackAction *action, const SoNode *initiator, const int flags = IvStateItem::DEFAULT_FLAGS, osg::Group *root = new osg::Group); void ivPopState(const SoCallbackAction *action, const SoNode *initator); void appendNode(osg::Node *n, const SoCallbackAction *action); }; #endif