root/OpenSceneGraph/trunk/src/osgPlugins/Inventor/ConvertFromInventor.h @ 11032

Revision 11032, 9.7 kB (checked in by robert, 5 years ago)

From Jan Peciva, "I am sending improved version of Inventor plugin. Attaching just
modified files, while GroupSoLOD.h and .cpp was deleted. Please, delete
it from repository, it is not used any longer and I doubt if it is
probably not used for anything meaningful for a while. In the new code,
there is no GroupSoLOD. Please, delete it.

I am using new plugin version for about 1.5 month so I consider it
stable by myself.

List of changes:
- rewritten Inventor state stack
- shaders support
- light attenuation support
- support for reading from stream (readNode(std::istream& fin, options))
- improved grouping node handling (SoSeparator?, SoGroup?,...)
- fixed transformation bug when two SoShapes/Drawables? with different transformations are placed bellow one grouping node
- introduced preprocessing to handle more advanced usage schemes of SoLOD and SoSwitch? nodes
- unused code clean up
- improved notify messages
- animation callbacks fixes
- FindInventor?.cmake improved finding routines, support for Coin3 and Coin4"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[2225]1#ifndef _CONVERTFROMINVENTOR_H_
2#define _CONVERTFROMINVENTOR_H_
3
4#include <osg/Group>
5#include <osg/Geometry>
6#include <osg/PrimitiveSet>
7#include <osg/Texture2D>
8#include <osg/Light>
9#include <Inventor/actions/SoCallbackAction.h>
[9053]10#include <Inventor/SbLinear.h>
[2225]11#include <vector>
12#include <stack>
[11032]13#include <assert.h>
[2225]14
[6543]15class ConvertFromInventor
[2225]16{
17    public:
18        ConvertFromInventor();
19        ~ConvertFromInventor();
[6543]20
[11032]21        /// Conversts from IV to OSG scene graph
[6543]22        osg::Node* convert(SoNode* rootIVNode);
23
[11032]24        /**
25         * Preprocessing restructure the scene for the convertor
26         * to be able to convert some peculiar scene constructions.
27         * Resulting scene is geometrically equivalent to the source
28         * scene. The preprocessing is related to grouping nodes only
29         * (SoSeparator, SoGroup, SoLOD, SoSwitch,...) and their
30         * treatment with traversal state.
31         */
32        void preprocess(SoNode *root);
33
[2225]34    private:
35
[11032]36        // Callback functions for converting inventor scene graph to osg
[2225]37        // scene graph
38
[11032]39        static SoCallbackAction::Response preNode(void* data,
[2225]40                                 SoCallbackAction* action, const SoNode* node);
[11032]41        static SoCallbackAction::Response postNode(void* data,
[2225]42                                 SoCallbackAction* action, const SoNode* node);
[11032]43        static SoCallbackAction::Response preTransformSeparator(void* data,
[2225]44                                 SoCallbackAction* action, const SoNode* node);
[11032]45        static SoCallbackAction::Response postTransformSeparator(void* data,
[2225]46                                 SoCallbackAction* action, const SoNode* node);
[11032]47        static SoCallbackAction::Response preLOD(void* data,
[2225]48                                 SoCallbackAction* action, const SoNode* node);
[11032]49        static SoCallbackAction::Response postLOD(void* data,
[2225]50                                 SoCallbackAction* action, const SoNode* node);
[11032]51        static SoCallbackAction::Response preShape(void* data,
[2225]52                                 SoCallbackAction* action, const SoNode* node);
[11032]53        static SoCallbackAction::Response postShape(void* data,
[2225]54                                 SoCallbackAction* action, const SoNode* node);
[11032]55        static SoCallbackAction::Response postTexture(void* data,
[2225]56                                 SoCallbackAction* action, const SoNode* node);
[11032]57        static SoCallbackAction::Response preLight(void* data,
[2225]58                                 SoCallbackAction* action, const SoNode* node);
[11032]59        static SoCallbackAction::Response preEnvironment(void* data,
[6543]60                                 SoCallbackAction* action, const SoNode* node);
[11032]61        static SoCallbackAction::Response preShaderProgram(void* data,
[9053]62                                 SoCallbackAction* action, const SoNode* node);
[11032]63        static SoCallbackAction::Response preRotor(void* data,
[9053]64                                 SoCallbackAction* action, const SoNode* node);
[11032]65        static SoCallbackAction::Response prePendulum(void* data,
66                                 SoCallbackAction* action, const SoNode* node);
67        static SoCallbackAction::Response preShuttle(void* data,
68                                 SoCallbackAction* action, const SoNode* node);
[9053]69        static SoCallbackAction::Response preInfo(void* data,
70                                 SoCallbackAction* action, const SoNode* node);
[2225]71
72        static void addTriangleCB(void* data, SoCallbackAction* action,
[5802]73                              const SoPrimitiveVertex *v0,
[2225]74                                  const SoPrimitiveVertex *v1,
75                                  const SoPrimitiveVertex *v2);
76        static void addLineSegmentCB(void* data, SoCallbackAction* action,
77                                     const SoPrimitiveVertex *v0,
78                                     const SoPrimitiveVertex *v1);
79        static void addPointCB(void* data, SoCallbackAction* action,
80                               const SoPrimitiveVertex *v0);
[6543]81
[11032]82        static SoCallbackAction::Response restructure(void* data,
83                                 SoCallbackAction* action, const SoNode* node);
84        static SoCallbackAction::Response restructurePreNode(void* data,
85                                 SoCallbackAction* action, const SoNode* node);
86        static SoCallbackAction::Response restructurePostNode(void* data,
87                                 SoCallbackAction* action, const SoNode* node);
88
[2225]89    private:
[9053]90        SbString transformInfoName;
91        SbName appearanceName;
[2225]92
[11032]93        void addVertex(SoCallbackAction* action, const SoPrimitiveVertex* v,
[2225]94                       int index);
95
[6543]96        osg::ref_ptr<osg::StateSet> getStateSet(SoCallbackAction* action);
[2225]97
[11032]98        osg::Texture2D* convertIVTexToOSGTex(const SoNode* soNode,
[6543]99                                             SoCallbackAction* action);
[2225]100
101        void transformLight(SoCallbackAction* action, const SbVec3f& vec,
102                            osg::Vec3& transVec);
103
104        // OSG doesn't seem to have a transpose function for matrices
105        void transposeMatrix(osg::Matrix& mat);
[6543]106
[2225]107    private:
108
109        // Normal and color binding
110        osg::Geometry::AttributeBinding normalBinding;
111        osg::Geometry::AttributeBinding colorBinding;
112
113        // List of vertices, normals, colors and texture coordinates
114        std::vector<osg::Vec3> vertices;
115        std::vector<osg::Vec3> normals;
116        std::vector<osg::Vec4> colors;
117        std::vector<osg::Vec2> textureCoords;
118
[11032]119        // Num of primitive and primitive type
[2225]120        int numPrimitives;
121        osg::PrimitiveSet::Mode primitiveType;
122
123        // Vertex ordering
124        enum VertexOrder { CLOCKWISE, COUNTER_CLOCKWISE };
125        VertexOrder vertexOrder;
126
[6543]127        // Mapping from SoTexture2 and SoVRMLImageTexture to already converted
128        // osg::Texture2D - avoids duplication of same texture objects
129        std::map<const SoNode*, osg::Texture2D*> ivToOsgTexMap;
[2225]130
[5802]131        osg::ref_ptr<osg::MatrixTransform> _root;///<The root node;
[9053]132
[11032]133        /**
134         * IvStateItem aids lack of some state retrieval methods
135         * of SoCallbackAction. State is maintained in stack
136         * manner separately from Open Inventor.
137         */
138        class IvStateItem {
139        public:
140
141            // Pop flags and node caused the push
142            enum Flags {
143                DEFAULT_FLAGS = 0,
144                MULTI_POP = 1,
145                KEEP_CHILDREN_ORDER = 2,
146                APPEND_AT_PUSH = 4,
147                UPDATE_STATE = 8,
148                UPDATE_STATE_EXCEPT_TRANSFORM = 0x10 // this has the same
149                          // effect as UPDATE_STATE at the present time
150            };
151            int flags;
152            const SoNode *pushInitiator;
153
154            // Tracking of model transformation
155            SbMatrix inheritedTransformation;
156            SbMatrix lastUsedTransformation;
157
158            // Active texture node (used for attaching the right texture to the
159            // geosets). Supported types are SoTexture2 and SoVRMLImageTexture for now.
160            // No multitexturing yet.
161            const SoNode* inheritedTexture;
162            const SoNode* currentTexture;
163
164            // List of active lights
165            std::vector<osg::ref_ptr<osg::Light> > inheritedLights;
166            std::vector<osg::ref_ptr<osg::Light> > currentLights;
167
168            // Active OpenGL glProgram and associated shaders
169            osg::ref_ptr<osg::Program> inheritedGLProgram;
170            osg::ref_ptr<osg::Program> currentGLProgram;
171
172            // Ambient light (of SoEnvironment)
173            SbColor inheritedAmbientLight;
174            SbColor currentAmbientLight;
175
176            // Generated OSG graph
177            osg::ref_ptr<osg::Group> osgStateRoot;
178
179            // Extra variables
180            const SoNode *keepChildrenOrderParent;
181
182            IvStateItem(const SoNode *initiator, osg::Group *root = NULL) :
183                flags(IvStateItem::DEFAULT_FLAGS),
184                pushInitiator(initiator),
185                inheritedTransformation(SbMatrix::identity()),
186                lastUsedTransformation(SbMatrix::identity()),
187                inheritedTexture(NULL),
188                currentTexture(NULL),
189                inheritedLights(),
190                currentLights(),
191                inheritedGLProgram(NULL),
192                currentGLProgram(NULL),
193                inheritedAmbientLight(SbColor(0.2f,0.2f,0.2f)),
194                currentAmbientLight(SbColor(0.2f,0.2f,0.2f)),
195                osgStateRoot(root ? root : new osg::Group) {}
196
197            IvStateItem(const IvStateItem& i, const SoCallbackAction *action,
198                        const SoNode *initiator, const int f,
199                        osg::Group *root) :
200                flags(f),
201                pushInitiator(initiator),
202                inheritedTransformation(action->getModelMatrix()),
203                lastUsedTransformation(action->getModelMatrix()),
204                inheritedTexture(i.currentTexture),
205                currentTexture(i.currentTexture),
206                inheritedLights(i.currentLights),
207                currentLights(i.currentLights),
208                inheritedGLProgram(i.currentGLProgram),
209                currentGLProgram(i.currentGLProgram),
210                inheritedAmbientLight(i.inheritedAmbientLight),
211                currentAmbientLight(i.currentAmbientLight),
212                osgStateRoot(root) {}
213        };
214
215        /// State stack for Inventor scene traversal
216        std::stack<IvStateItem> ivStateStack;
217
218        void ivPushState(const SoCallbackAction *action,
219                         const SoNode *initiator, const int flags = IvStateItem::DEFAULT_FLAGS,
220                         osg::Group *root = new osg::Group);
221        void ivPopState(const SoCallbackAction *action, const SoNode *initator);
222
223        void appendNode(osg::Node *n, const SoCallbackAction *action);
224
[2225]225};
226
227#endif
Note: See TracBrowser for help on using the browser.