1 // all needed include files
2 #ifdef OSG_BUILD_ACTIVE
5 #include <OSGSimpleGeometry.h>
6 #include <OSGGLUTWindow.h>
7 #include <OSGSimpleSceneManager.h>
9 #include <OSGSceneFileHandler.h>
10 #include <OSGNameAttachment.h>
12 #include <OSGGraphOpSeq.h>
13 #include <OSGSplitGraphOp.h>
14 #include <OSGVerifyGeoGraphOp.h>
15 #include <OSGSharePtrGraphOp.h>
17 #include <OpenSG/OSGGLUT.h>
18 #include <OpenSG/OSGConfig.h>
19 #include <OpenSG/OSGSimpleGeometry.h>
20 #include <OpenSG/OSGGLUTWindow.h>
21 #include <OpenSG/OSGSimpleSceneManager.h>
23 #include <OpenSG/OSGSceneFileHandler.h>
24 #include <OpenSG/OSGNameAttachment.h>
26 #include <OpenSG/OSGGraphOpSeq.h>
27 #include <OpenSG/OSGSplitGraphOp.h>
28 #include <OpenSG/OSGVerifyGeoGraphOp.h>
29 #include <OpenSG/OSGSharePtrGraphOp.h>
32 OSG::SimpleSceneManagerRefPtr mgr
;
33 OSG::NodeRecPtr scene
;
35 // A simple class that counts the number of entered nodes
40 counter(void) : mCount(0)
44 //method that will be called when entering
46 OSG::Action::ResultE
enter(OSG::Node
* const)
49 return OSG::Action::Continue
;
52 OSG::UInt16
getCount(void)
67 int setupGLUT( int *argc
, char *argv
[] );
69 //This is the function that will be called when a node
70 //is entered during traversal.
71 OSG::Action::ResultE
enter(OSG::Node
* const node
)
75 std::cout
<< getName(node
) << std::endl
;
79 std::cout
<< "No name was set!" << std::endl
;
82 return OSG::Action::Continue
;
85 //This function will test if the core is of type
86 //geometry and if it is, it will print the node's
88 OSG::Action::ResultE
isGeometry(OSG::Node
* const node
)
90 // this tests if the core is derived from geometry
91 if (node
->getCore()->getType().isDerivedFrom(OSG::Geometry::getClassType()))
95 std::cout
<< "Found a geometry core stored in " << getName(node
)
100 std::cout
<< "Found a geometry core but node has no name"
105 return OSG::Action::Continue
;
108 OSG::NodeTransitPtr
createScenegraph(const char* filename
)
110 OSG::NodeRecPtr n
= OSG::SceneFileHandler::the()->read(filename
);
112 //we check the result
115 std::cout
<< "Loading the specified file was not possible!"
117 return OSG::NodeTransitPtr();
120 return OSG::NodeTransitPtr(n
);
123 int main(int argc
, char **argv
)
125 OSG::osgInit(argc
,argv
);
128 int winid
= setupGLUT(&argc
, argv
);
129 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
130 gwin
->setGlutId(winid
);
134 scene
= createScenegraph(argv
[1]);
136 scene
= createScenegraph("Data/brick_quads.wrl");
138 mgr
= OSG::SimpleSceneManager::create();
139 mgr
->setWindow(gwin
);
140 mgr
->setRoot (scene
);
143 OSG::commitChanges();
151 void reshape(int w
, int h
)
162 void mouse(int button
, int state
, int x
, int y
)
166 mgr
->mouseButtonRelease(button
, x
, y
);
170 mgr
->mouseButtonPress(button
, x
, y
);
172 OSG::Line ray
= mgr
->calcViewRay(x
, y
);
173 OSG::IntersectActionRefPtr iAct
= OSG::IntersectAction::create();
179 OSG::Pnt3f p
= iAct
->getHitPoint();
180 std::cout
<< "Hit point : " << p
[0] << " " << p
[1] << " " << p
[2]
182 OSG::NodeRecPtr n
= iAct
->getHitObject();
183 OSG::NodeRecPtr parent
= n
->getParent();
194 void motion(int x
, int y
)
196 mgr
->mouseMove(x
, y
);
200 void keyboard(unsigned char k
, int x
, int y
)
206 // clean up global variables
215 // this will print the names of all nodes
216 // in the whole graph
219 std::cout
<< std::endl
<< std::endl
;
220 std::cout
<< "Printing all node names";
221 std::cout
<< "---------------------------------------";
222 std::cout
<< std::endl
<< std::endl
;
224 // now we invoke the traversal
225 traverse(scene
, enter
);
229 // this will only print the names of nodes
230 // which have a geometry core
233 std::cout
<< std::endl
<< std::endl
;
234 std::cout
<< "Printing all geometry nodes";
235 std::cout
<< "---------------------------------------";
236 std::cout
<< std::endl
<< std::endl
;
238 // traverse the graph
239 traverse(scene
, isGeometry
);
244 std::cout
<< "Splitting Graph now...";
248 traverse(scene
, boost::bind(&counter::enter
, &c
, _1
));
250 std::cout
<< "Number of nodes before splitting: " << c
.getCount()
253 OSG::SplitGraphOpRefPtr spo
= OSG::SplitGraphOp::create();
254 spo
->setMaxPolygons(50);
255 spo
->traverse(scene
);
257 std::cout
<< "done" << std::endl
;
261 traverse(scene
, boost::bind(&counter::enter
, &c
, _1
));
263 std::cout
<< "Number of nodes after splitting: " << c
.getCount()
270 int setupGLUT(int *argc
, char *argv
[])
272 glutInit(argc
, argv
);
273 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
275 int winid
= glutCreateWindow("OpenSG First Application");
277 glutDisplayFunc(display
);
278 glutMouseFunc(mouse
);
279 glutMotionFunc(motion
);
280 glutReshapeFunc(reshape
);
281 glutIdleFunc(display
);
282 glutKeyboardFunc(keyboard
);