1 // OpenSG Tutorial Example: Deep Clones of scenes
3 // This tutorial demonstrates how to create independent (deep) copies of a
4 // scene and how to control what types should be shared.
6 #ifdef OSG_BUILD_ACTIVE
10 #include <OSGSimpleGeometry.h>
11 #include <OSGComponentTransform.h>
12 #include <OSGBaseFunctions.h>
13 #include <OSGGLUTWindow.h>
14 #include <OSGSimpleSceneManager.h>
15 #include <OSGSceneFileHandler.h>
18 #include <OpenSG/OSGGLUT.h>
19 #include <OpenSG/OSGConfig.h>
20 #include <OpenSG/OSGSimpleGeometry.h>
21 #include <OpenSG/OSGComponentTransform.h>
22 #include <OpenSG/OSGBaseFunctions.h>
23 #include <OpenSG/OSGGLUTWindow.h>
24 #include <OpenSG/OSGSimpleSceneManager.h>
25 #include <OpenSG/OSGSceneFileHandler.h>
28 OSG::SimpleSceneManagerRefPtr mgr
;
30 OSG::GeometryRefPtr _geo
;
31 OSG::GeoPnt3fPropertyRefPtr _pos
;
33 // forward declaration so we can have the interesting stuff upfront
34 int setupGLUT( int *argc
, char *argv
[] );
36 OSG::Action::ResultE
changeGeo(OSG::Node
*node
)
38 OSG::Geometry
*geo
= dynamic_cast<OSG::Geometry
*>(node
->getCore());
41 return OSG::Action::Continue
;
44 OSG::GeoColor3fPropertyRefPtr col
= dynamic_cast<OSG::GeoColor3fProperty
*>(geo
->getColors());
47 col
= OSG::GeoColor3fProperty::create();
49 col
->resize(geo
->getPositions()->size());
51 // Change the geometry to use the new colors
53 // If multi-indexed, make the colors use the same index as
55 if(geo
->getIndex(OSG::Geometry::PositionsIndex
) != NULL
)
57 geo
->setIndex(geo
->getIndex(OSG::Geometry::PositionsIndex
),
58 OSG::Geometry::ColorsIndex
);
62 OSG::Real32 size
= col
->size();
63 for(OSG::UInt32 i
= 0; i
< size
; ++i
)
67 c
[1] = static_cast<OSG::Real32
>(i
) / size
;
72 return OSG::Action::Continue
;
75 // Initialize GLUT & OpenSG and set up the scene
76 int main(int argc
, char **argv
)
79 OSG::osgInit(argc
,argv
);
82 int winid
= setupGLUT(&argc
, argv
);
84 // open a new scope, because the pointers below should go out of scope
85 // before entering glutMainLoop.
86 // Otherwise OpenSG will complain about objects being alive after shutdown.
88 // the connection between GLUT and OpenSG
89 OSG::GLUTWindowRefPtr gwin
= OSG::GLUTWindow::create();
90 gwin
->setGlutId(winid
);
94 OSG::NodeRefPtr scene
;
98 FWARNING(("No file given!\n"));
99 FWARNING(("Supported file formats:\n"));
101 std::list
<const char*> suffixes
;
102 OSG::SceneFileHandler::the()->getSuffixList(suffixes
, OSG::SceneFileType::OSG_READ_SUPPORTED
);
104 for(std::list
<const char*>::iterator it
= suffixes
.begin();
105 it
!= suffixes
.end();
108 FWARNING(("%s\n", *it
));
111 scene
= OSG::makeTorus(.5, 2, 16, 16);
116 All scene file loading is handled via the SceneFileHandler.
118 scene
= OSG::SceneFileHandler::the()->read(argv
[1]);
121 OSG::commitChanges();
123 // calc size of the scene
126 scene
->getWorldVolume(vol
);
127 vol
.getBounds(min
, max
);
129 OSG::Vec3f d
= max
- min
;
130 OSG::Real32 offset
= d
.length() / 2.0f
;
132 // now create a deep clone
133 OSG::NodeRefPtr sceneClone
= OSG::deepCloneTree(scene
);
135 // this clones all nodes but the cores of type Material and Transform are shared.
136 //NodePtr sceneClone = deepCloneTree(scene, "Material, Transform");
138 // now change all geometries from the cloned scene just to show
139 // that it is a real deep copy.
140 traverse(sceneClone
, &changeGeo
);
142 // create a small scene graph with two transformation nodes.
143 OSG::NodeRefPtr root
= OSG::makeCoredNode
<OSG::Group
>();
144 OSG::ComponentTransformRefPtr t1
;
145 OSG::NodeRefPtr tn1
=
146 OSG::makeCoredNode
<OSG::ComponentTransform
>(&t1
);
147 OSG::ComponentTransformRefPtr t2
;
148 OSG::NodeRefPtr tn2
=
149 OSG::makeCoredNode
<OSG::ComponentTransform
>(&t2
);
151 t1
->setTranslation(OSG::Vec3f(- offset
, 0.0f
, 0.0f
));
152 t2
->setTranslation(OSG::Vec3f(offset
, 0.0f
, 0.0f
));
154 tn1
->addChild(scene
);
155 tn2
->addChild(sceneClone
);
160 OSG::commitChanges();
162 // create the SimpleSceneManager helper
163 mgr
= OSG::SimpleSceneManager::create();
165 // tell the manager what to manage
166 mgr
->setWindow(gwin
);
169 // show the whole scene
180 // GLUT callback functions
189 // react to size changes
190 void reshape(int w
, int h
)
196 // react to mouse button presses
197 void mouse(int button
, int state
, int x
, int y
)
200 mgr
->mouseButtonRelease(button
, x
, y
);
202 mgr
->mouseButtonPress(button
, x
, y
);
207 // react to mouse motions with pressed buttons
208 void motion(int x
, int y
)
210 mgr
->mouseMove(x
, y
);
215 void keyboard(unsigned char k
, int x
, int y
)
221 // clean up global variables
233 // setup the GLUT library which handles the windows for us
234 int setupGLUT(int *argc
, char *argv
[])
236 glutInit(argc
, argv
);
237 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
239 int winid
= glutCreateWindow("OpenSG");
241 glutReshapeFunc(reshape
);
242 glutDisplayFunc(display
);
243 glutMouseFunc(mouse
);
244 glutMotionFunc(motion
);
245 glutKeyboardFunc(keyboard
);