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)
66 int setupGLUT(int *argc
, char *argv
[]);
68 //This is the function that will be called when a node
69 //is entered during traversal.
70 OSG::Action::ResultE
enter(OSG::Node
* const node
)
74 std::cout
<< getName(node
) << std::endl
;
78 std::cout
<< "No name was set!" << std::endl
;
81 return OSG::Action::Continue
;
84 //This function will test if the core is of type
85 //geometry and if it is, it will print the node's
87 OSG::Action::ResultE
isGeometry(OSG::Node
* const node
)
89 // this tests if the core is derived from geometry
90 if (node
->getCore()->getType().isDerivedFrom(OSG::Geometry::getClassType()))
94 std::cout
<< "Found a geometry core stored in " << getName(node
)
99 std::cout
<< "Found a geometry core but node has no name"
104 return OSG::Action::Continue
;
108 OSG::NodeTransitPtr
createScenegraph(const char* filename
)
110 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
112 // define the Graph Op Sequence here
113 OSG::GraphOpSeqRefPtr graphOperator
= OSG::GraphOpSeq::create();
114 OSG::GraphOpRefPtr go
;
116 //first we verify the geometry
117 go
= OSG::VerifyGeoGraphOp::create();
118 graphOperator
->addGraphOp(go
);
119 //merge identical field containers
120 go
= OSG::SharePtrGraphOp::create();
121 graphOperator
->addGraphOp(go
);
123 go
= OSG::VerifyGeoGraphOp::create();
124 graphOperator
->addGraphOp(go
);
126 std::cout
<< "Loading " << filename
<< " now" << std::endl
;
129 OSG::SceneFileHandler::the()->read(filename
, graphOperator
);
131 //we check the result
134 std::cout
<< "Loading the specified file was not possible!"
136 return OSG::NodeTransitPtr();
139 return OSG::NodeTransitPtr(n
);
142 int main(int argc
, char **argv
)
144 OSG::osgInit(argc
,argv
);
147 int winid
= setupGLUT(&argc
, argv
);
148 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
149 gwin
->setGlutId(winid
);
153 scene
= createScenegraph(argv
[1]);
155 scene
= createScenegraph("Data/brick_quads.wrl");
157 mgr
= OSG::SimpleSceneManager::create();
158 mgr
->setWindow(gwin
);
159 mgr
->setRoot (scene
);
162 OSG::commitChanges();
170 void reshape(int w
, int h
)
181 void mouse(int button
, int state
, int x
, int y
)
184 mgr
->mouseButtonRelease(button
, x
, y
);
186 mgr
->mouseButtonPress(button
, x
, y
);
191 void motion(int x
, int y
)
193 mgr
->mouseMove(x
, y
);
197 void keyboard(unsigned char k
, int x
, int y
)
203 // clean up global variables
212 // this will print the names of all nodes
213 // in the whole graph
216 std::cout
<< std::endl
<< std::endl
;
217 std::cout
<< "Printing all node names";
218 std::cout
<< "---------------------------------------";
219 std::cout
<< std::endl
<< std::endl
;
221 // now we invoke the traversal
222 traverse(scene
, enter
);
226 // this will only print the names of nodes
227 // which have a geometry core
230 std::cout
<< std::endl
<< std::endl
;
231 std::cout
<< "Printing all geometry nodes";
232 std::cout
<< "---------------------------------------";
233 std::cout
<< std::endl
<< std::endl
;
235 // traverse the graph
236 traverse(scene
, isGeometry
);
242 std::cout
<< "Splitting Graph now...";
246 traverse(scene
, boost::bind(&counter::enter
, &c
, _1
));
248 std::cout
<< "Number of nodes before splitting: " << c
.getCount()
251 OSG::SplitGraphOpRefPtr spo
= OSG::SplitGraphOp::create();
252 spo
->setMaxPolygons(50);
253 spo
->traverse(scene
);
255 std::cout
<< "done" << std::endl
;
259 traverse(scene
, boost::bind(&counter::enter
, &c
, _1
));
261 std::cout
<< "Number of nodes after splitting: " << c
.getCount()
268 int setupGLUT(int *argc
, char *argv
[])
270 glutInit(argc
, argv
);
271 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
273 int winid
= glutCreateWindow("OpenSG First Application");
275 glutDisplayFunc(display
);
276 glutMouseFunc(mouse
);
277 glutMotionFunc(motion
);
278 glutReshapeFunc(reshape
);
279 glutIdleFunc(display
);
280 glutKeyboardFunc(keyboard
);