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

Revision 13557, 9.8 kB (checked in by robert, 18 hours ago)

Changed the osgUI behaviour so that events are set to be handled by Widgets that have focus even if they don't directly use them.

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