1 // all needed include files
2 #ifdef OSG_BUILD_ACTIVE
5 #include <OSGSimpleGeometry.h>
6 #include <OSGGLUTWindow.h>
7 #include <OSGSimpleSceneManager.h>
9 //additional headder files
10 #include <OSGSceneFileHandler.h>
11 #include <OSGDistanceLOD.h>
12 #include <OSGSwitch.h>
13 #include <OSGNameAttachment.h>
15 #include <OpenSG/OSGGLUT.h>
16 #include <OpenSG/OSGConfig.h>
17 #include <OpenSG/OSGSimpleGeometry.h>
18 #include <OpenSG/OSGGLUTWindow.h>
19 #include <OpenSG/OSGSimpleSceneManager.h>
21 //additional headder files
22 #include <OpenSG/OSGSceneFileHandler.h>
23 #include <OpenSG/OSGDistanceLOD.h>
24 #include <OpenSG/OSGSwitch.h>
25 #include <OpenSG/OSGNameAttachment.h>
28 OSG::SimpleSceneManagerRefPtr mgr
;
29 OSG::NodeRecPtr scene
;
31 int setupGLUT(int *argc
, char *argv
[]);
33 // this function will return the node named "FACESET_Woman"
34 // if there is no such node NULL will be returned
35 OSG::Node
*checkName(OSG::Node
*n
)
37 OSG::UInt32 children
= n
->getNChildren();
39 //make sure a name existes
42 //check if it is the name we are looking for
43 if (getName(n
) == std::string("FACESET_Woman"))
49 for(OSG::UInt32 i
= 0; i
< children
; i
++)
51 OSG::Node
*r
= checkName(n
->getChild(i
));
53 // if it is not NULL it is the node we are looking for
54 // so just pass it through
58 // no children's name matches or there are no more childs
59 // so return NULL, indicating that the node was not found yet
63 OSG::NodeTransitPtr
createScenegraph(void)
65 // At first we load all needed models from file
66 OSG::NodeRecPtr w_high
=
67 OSG::SceneFileHandler::the()->read("Data/woman_high.wrl");
69 OSG::NodeRecPtr w_medium
=
70 OSG::SceneFileHandler::the()->read("Data/woman_medium.wrl");
72 OSG::NodeRecPtr w_low
=
73 OSG::SceneFileHandler::the()->read("Data/woman_low.wrl");
75 // we check the result
76 if((w_high
== NULL
) || (w_medium
== NULL
)|| (w_low
== NULL
))
78 std::cout
<< "It was not possible to load all needed models from file"
80 return OSG::NodeTransitPtr();
84 OSG::DistanceLODRecPtr lod
= OSG::DistanceLOD::create();
85 lod
->editSFCenter()->setValue(OSG::Pnt3f(0,0,0));
86 lod
->editMFRange()->push_back(200);
87 lod
->editMFRange()->push_back(500);
89 // the node containing the LOD core. The three models will be
90 // added as its children
91 OSG::NodeRecPtr lodNode
= OSG::Node::create();
92 lodNode
->setCore(lod
);
93 lodNode
->addChild(w_high
);
94 lodNode
->addChild(w_medium
);
95 lodNode
->addChild(w_low
);
97 // create the node with switch core ********************
98 OSG::SwitchRecPtr sw
= OSG::Switch::create();
99 //Notice: the first choice is 0
102 OSG::NodeRecPtr switchNode
= OSG::Node::create();
103 switchNode
->setCore(sw
);
104 switchNode
->addChild(lodNode
);
106 //end switch creation **********************************
108 OSG::NodeRecPtr root
= OSG::Node::create();
109 root
->setCore(OSG::Group::create());
110 root
->addChild(switchNode
);
112 // we know want to extract the mesh geometry out of the graph
113 // it is sufficent to pass the model only as root for searching
114 OSG::NodeRecPtr womanGeometry
= checkName(w_high
);
115 OSG::GeometryRecPtr geo
=
116 dynamic_cast<OSG::Geometry
*>(womanGeometry
->getCore());
118 //new node with "old" geometry core referenced
119 OSG::NodeRecPtr woman
= OSG::Node::create();
122 //translate it a bit to see both women
123 OSG::NodeRecPtr womanTrans
= OSG::Node ::create();
124 OSG::TransformRecPtr t
= OSG::Transform::create();
127 m
.setTranslate(OSG::Vec3f(0,0,200));
130 womanTrans
->setCore(t
);
131 womanTrans
->addChild(woman
);
134 root
->addChild(womanTrans
);
136 return OSG::NodeTransitPtr(root
);
139 int main(int argc
, char **argv
)
141 OSG::osgInit(argc
,argv
);
144 int winid
= setupGLUT(&argc
, argv
);
145 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
146 gwin
->setGlutId(winid
);
149 scene
= createScenegraph();
151 mgr
= OSG::SimpleSceneManager::create();
152 mgr
->setWindow(gwin
);
153 mgr
->setRoot (scene
);
156 OSG::commitChanges();
164 void reshape(int w
, int h
)
175 void mouse(int button
, int state
, int x
, int y
)
178 mgr
->mouseButtonRelease(button
, x
, y
);
180 mgr
->mouseButtonPress(button
, x
, y
);
185 void motion(int x
, int y
)
187 mgr
->mouseMove(x
, y
);
191 int setupGLUT(int *argc
, char *argv
[])
193 glutInit(argc
, argv
);
194 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
196 int winid
= glutCreateWindow("OpenSG First Application");
198 glutDisplayFunc(display
);
199 glutMouseFunc(mouse
);
200 glutMotionFunc(motion
);
201 glutReshapeFunc(reshape
);
202 glutIdleFunc(display
);