fixed: push transparency enforcement to state so the backend is aware of it
[opensg.git] / Examples / Simple / deepclone.cpp
blob2f1355c09d9c6d2494632e8fe7a51df3b1b23424
1 // OpenSG Tutorial Example: Deep Clones of scenes
2 //
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
7 // Headers
8 #include <OSGGLUT.h>
9 #include <OSGConfig.h>
10 #include <OSGSimpleGeometry.h>
11 #include <OSGComponentTransform.h>
12 #include <OSGBaseFunctions.h>
13 #include <OSGGLUTWindow.h>
14 #include <OSGSimpleSceneManager.h>
15 #include <OSGSceneFileHandler.h>
16 #else
17 // Headers
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>
26 #endif
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());
40 if(geo == NULL)
41 return OSG::Action::Continue;
44 OSG::GeoColor3fPropertyRefPtr col = dynamic_cast<OSG::GeoColor3fProperty *>(geo->getColors());
45 if(col == NULL)
47 col = OSG::GeoColor3fProperty::create();
49 col->resize(geo->getPositions()->size());
51 // Change the geometry to use the new colors
52 geo->setColors(col);
53 // If multi-indexed, make the colors use the same index as
54 // the geometry
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)
65 OSG::Color3f c;
66 c[0] = 0.0f;
67 c[1] = static_cast<OSG::Real32>(i) / size;
68 c[2] = 0.0f;
69 col->setValue(c, i);
72 return OSG::Action::Continue;
75 // Initialize GLUT & OpenSG and set up the scene
76 int main(int argc, char **argv)
78 // OSG init
79 OSG::osgInit(argc,argv);
81 // GLUT init
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);
91 gwin->init();
93 // load the scene
94 OSG::NodeRefPtr scene;
96 if(argc < 2)
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();
106 ++it)
108 FWARNING(("%s\n", *it));
111 scene = OSG::makeTorus(.5, 2, 16, 16);
113 else
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
124 OSG::Vec3f min, max;
125 OSG::BoxVolume vol;
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);
157 root->addChild(tn1);
158 root->addChild(tn2);
160 OSG::commitChanges();
162 // create the SimpleSceneManager helper
163 mgr = OSG::SimpleSceneManager::create();
165 // tell the manager what to manage
166 mgr->setWindow(gwin );
167 mgr->setRoot (root);
169 // show the whole scene
170 mgr->showAll();
173 // GLUT main loop
174 glutMainLoop();
176 return 0;
180 // GLUT callback functions
183 // redraw the window
184 void display(void)
186 mgr->redraw();
189 // react to size changes
190 void reshape(int w, int h)
192 mgr->resize(w, h);
193 glutPostRedisplay();
196 // react to mouse button presses
197 void mouse(int button, int state, int x, int y)
199 if (state)
200 mgr->mouseButtonRelease(button, x, y);
201 else
202 mgr->mouseButtonPress(button, x, y);
204 glutPostRedisplay();
207 // react to mouse motions with pressed buttons
208 void motion(int x, int y)
210 mgr->mouseMove(x, y);
211 glutPostRedisplay();
214 // react to keys
215 void keyboard(unsigned char k, int x, int y)
217 switch(k)
219 case 27:
221 // clean up global variables
222 _geo = NULL;
223 _pos = NULL;
224 mgr = NULL;
226 OSG::osgExit();
227 exit(0);
229 break;
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);
247 return winid;