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
45 //method that will be called when entering
47 OSG::Action::ResultE
enter(OSG::Node
* const)
50 return OSG::Action::Continue
;
53 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
;
109 OSG::NodeTransitPtr
createScenegraph(const char* filename
)
111 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
113 // define the Graph Op Sequence here
114 OSG::GraphOpSeqRefPtr graphOperator
= OSG::GraphOpSeq::create();
115 OSG::GraphOpRefPtr go
;
117 //first we verify the geometry
118 go
= OSG::VerifyGeoGraphOp::create();
119 graphOperator
->addGraphOp(go
);
120 //merge identical field containers
121 go
= OSG::SharePtrGraphOp::create();
122 graphOperator
->addGraphOp(go
);
124 go
= OSG::VerifyGeoGraphOp::create();
125 graphOperator
->addGraphOp(go
);
127 std::cout
<< "Loading " << filename
<< " now" << std::endl
;
130 OSG::SceneFileHandler::the()->read(filename
, graphOperator
);
132 //we check the result
135 std::cout
<< "Loading the specified file was not possible!"
137 return OSG::NodeTransitPtr();
140 return OSG::NodeTransitPtr(n
);
143 int main(int argc
, char **argv
)
145 OSG::osgInit(argc
,argv
);
148 int winid
= setupGLUT(&argc
, argv
);
149 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
150 gwin
->setGlutId(winid
);
154 scene
= createScenegraph(argv
[1]);
156 scene
= createScenegraph("Data/brick_quads.wrl");
158 mgr
= OSG::SimpleSceneManager::create();
159 mgr
->setWindow(gwin
);
160 mgr
->setRoot (scene
);
163 OSG::commitChanges();
171 void reshape(int w
, int h
)
182 void mouse(int button
, int state
, int x
, int y
)
185 mgr
->mouseButtonRelease(button
, x
, y
);
187 mgr
->mouseButtonPress(button
, x
, y
);
192 void motion(int x
, int y
)
194 mgr
->mouseMove(x
, y
);
198 void keyboard(unsigned char k
, int x
, int y
)
204 // clean up global variables
213 // this will print the names of all nodes
214 // in the whole graph
217 std::cout
<< std::endl
<< std::endl
;
218 std::cout
<< "Printing all node names";
219 std::cout
<< "---------------------------------------";
220 std::cout
<< std::endl
<< std::endl
;
222 // now we invoke the traversal
223 traverse(scene
, enter
);
227 // this will only print the names of nodes
228 // which have a geometry core
231 std::cout
<< std::endl
<< std::endl
;
232 std::cout
<< "Printing all geometry nodes";
233 std::cout
<< "---------------------------------------";
234 std::cout
<< std::endl
<< std::endl
;
236 // traverse the graph
237 traverse(scene
, isGeometry
);
243 std::cout
<< "Splitting Graph now...";
247 traverse(scene
, boost::bind(&counter::enter
, &c
, _1
));
249 std::cout
<< "Number of nodes before splitting: " << c
.getCount()
252 OSG::SplitGraphOpRefPtr spo
= OSG::SplitGraphOp::create();
253 spo
->setMaxPolygons(50);
254 spo
->traverse(scene
);
256 std::cout
<< "done" << std::endl
;
260 traverse(scene
, boost::bind(&counter::enter
, &c
, _1
));
262 std::cout
<< "Number of nodes after splitting: " << c
.getCount()
269 int setupGLUT(int *argc
, char *argv
[])
271 glutInit(argc
, argv
);
272 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
274 int winid
= glutCreateWindow("OpenSG First Application");
276 glutDisplayFunc(display
);
277 glutMouseFunc(mouse
);
278 glutMotionFunc(motion
);
279 glutReshapeFunc(reshape
);
280 glutIdleFunc(display
);
281 glutKeyboardFunc(keyboard
);