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>
14 #include <OSGComponentTransform.h>
16 #include <OpenSG/OSGGLUT.h>
17 #include <OpenSG/OSGConfig.h>
18 #include <OpenSG/OSGSimpleGeometry.h>
19 #include <OpenSG/OSGGLUTWindow.h>
20 #include <OpenSG/OSGSimpleSceneManager.h>
22 //additional headder files
23 #include <OpenSG/OSGSceneFileHandler.h>
24 #include <OpenSG/OSGDistanceLOD.h>
25 #include <OpenSG/OSGSwitch.h>
26 #include <OpenSG/OSGNameAttachment.h>
27 #include <OpenSG/OSGComponentTransform.h>
30 OSG::SimpleSceneManagerRefPtr mgr
;
31 OSG::NodeRecPtr scene
;
32 OSG::ComponentTransformRecPtr ct
;
33 OSG::UInt32 frame
= 0;
39 int setupGLUT(int *argc
, char *argv
[]);
42 const char *getNodeName(OSG::Node
const *node
)
48 OSG::NameRecPtr nodename
=
49 dynamic_cast<OSG::Name
*>(node
->findAttachment(OSG::Name::getClassType()));
51 // no node name, try core name
52 if(nodename
== NULL
&& node
->getCore() != NULL
)
55 dynamic_cast<OSG::Name
*>(
56 node
->getCore()->findAttachment(OSG::Name::getClassType()));
60 return nodename
->getFieldPtr()->getValue().c_str();
63 if(node
->getCore() != NULL
)
64 return node
->getCore()->getType().getName().c_str();
70 // this function will return the node named "FACESET_Woman"
71 // if there is no such node NullFC will be returned
72 OSG::Node
*checkName(OSG::Node
*n
)
74 OSG::UInt32 children
= n
->getNChildren();
76 //make sure a name existes
79 //check if it is the name we are looking for
80 if (getName(n
)== std::string("FACESET_Woman"))
86 for(OSG::UInt32 i
= 0; i
< children
; i
++)
88 OSG::Node
*r
= checkName(n
->getChild(i
));
90 // if it is not NULL it is the node we are looking for
91 // so just pass it through
95 // no children's name matches or there are no more childs
96 // so return NULL, indicating that the node was not found yet
100 OSG::NodeTransitPtr
createScenegraph(void)
102 // At first we load all needed models from file
103 OSG::NodeRecPtr w_high
=
104 OSG::SceneFileHandler::the()->read("Data/woman_high.wrl");
106 OSG::NodeRecPtr w_medium
=
107 OSG::SceneFileHandler::the()->read("Data/woman_medium.wrl");
109 OSG::NodeRecPtr w_low
=
110 OSG::SceneFileHandler::the()->read("Data/woman_low.wrl");
112 // we check the result
113 // we check the result
114 if((w_high
== NULL
) || (w_medium
== NULL
)|| (w_low
== NULL
))
116 std::cout
<< "It was not possible to load all needed models from file"
118 return OSG::NodeTransitPtr();
122 OSG::DistanceLODRecPtr lod
= OSG::DistanceLOD::create();
123 lod
->editSFCenter()->setValue(OSG::Pnt3f(0,0,0));
124 lod
->editMFRange()->push_back(200);
125 lod
->editMFRange()->push_back(500);
127 // the node containing the LOD core. The three models will be
128 // added as its children
129 OSG::NodeRecPtr lodNode
= OSG::Node::create();
130 lodNode
->setCore(lod
);
131 lodNode
->addChild(w_high
);
132 lodNode
->addChild(w_medium
);
133 lodNode
->addChild(w_low
);
135 // create the node with switch core ********************
136 OSG::SwitchRecPtr sw
= OSG::Switch::create();
137 //Notice: the first choice is 0
140 OSG::NodeRecPtr switchNode
= OSG::Node::create();
141 switchNode
->setCore(sw
);
142 switchNode
->addChild(lodNode
);
144 //end witch creation **********************************
146 OSG::NodeRecPtr root
= OSG::Node::create();
147 root
->setCore(OSG::Group::create());
148 root
->addChild(switchNode
);
150 // we know want to extract the mesh geometry out of the graph
151 // it is sufficent to pass the model only as root for searching
152 OSG::NodeRecPtr womanGeometry
= checkName(w_high
);
153 if(womanGeometry
== NULL
)
155 std::cout
<< "Couldn't find geometry node 'FACESET_Woman'!"
157 return OSG::NodeTransitPtr();
160 OSG::GeometryRecPtr geo
=
161 dynamic_cast<OSG::Geometry
*>(womanGeometry
->getCore());
165 std::cout
<< "Node 'FACESET_Woman' is not a geometry node!"
167 return OSG::NodeTransitPtr();
170 // generating a material *********************************
172 OSG::SimpleMaterialRecPtr mat
= OSG::SimpleMaterial::create();
173 mat
->setAmbient(OSG::Color3f(0.2,0.2,0.2));
174 mat
->setDiffuse(OSG::Color3f(0.6,0.3,0.1));
175 mat
->setSpecular(OSG::Color3f(1,1,1));
176 mat
->setShininess(0.8);
178 geo
->setMaterial(mat
);
180 // end material generation *******************************
182 //new node with "old" geometry core referenced
183 OSG::NodeRecPtr woman
= OSG::Node::create();
186 /* the old transformation is not needed any longer
187 //translate it a bit to see both women
188 NodeRecPtr womanTrans = Node::create();
189 TransformRecPtr t = Transform::create();
192 m.setTranslate(Vec3f(0,0,200));
195 womanTrans->setCore(t);
196 womanTrans->addChild(woman);
199 // component transform ************************************
200 OSG::NodeRecPtr ctNode
= OSG::Node::create();
202 //this one is declared globally
203 ct
= OSG::ComponentTransform::create();
205 ct
->setTranslation(OSG::Vec3f(0,0,200));
206 ct
->setScale(OSG::Vec3f(1,1,1));
207 ct
->setRotation(OSG::Quaternion(OSG::Vec3f(0,1,0),0));
210 ctNode
->addChild(woman
);
211 // end component transform ********************************
214 root
->addChild(ctNode
);
216 return OSG::NodeTransitPtr(root
);
219 int main(int argc
, char **argv
)
221 OSG::osgInit(argc
,argv
);
224 int winid
= setupGLUT(&argc
, argv
);
225 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
226 gwin
->setGlutId(winid
);
229 scene
= createScenegraph();
234 mgr
= OSG::SimpleSceneManager::create();
235 mgr
->setWindow(gwin
);
236 mgr
->setRoot (scene
);
239 OSG::commitChanges();
247 void reshape(int w
, int h
)
256 OSG::Real32 time
= glutGet(GLUT_ELAPSED_TIME
);
261 ct
->setTranslation(OSG::Vec3f(0,cos(time
/2000.f
)*100,200));
264 ct
->setRotation(OSG::Quaternion(OSG::Vec3f(0,1,0), time
/2000));
267 ct
->setScale(OSG::Vec3f(cos(time
/2000),
277 void mouse(int button
, int state
, int x
, int y
)
280 mgr
->mouseButtonRelease(button
, x
, y
);
282 mgr
->mouseButtonPress(button
, x
, y
);
287 void motion(int x
, int y
)
289 mgr
->mouseMove(x
, y
);
293 void keyboard(unsigned char k
, int x
, int y
){
296 case 't' : mode
= 0; break;
297 case 'r' : mode
= 1; break;
298 case 's' : mode
= 2; break;
303 int setupGLUT(int *argc
, char *argv
[])
305 glutInit(argc
, argv
);
306 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
308 int winid
= glutCreateWindow("OpenSG First Application");
310 glutDisplayFunc(display
);
311 glutMouseFunc(mouse
);
312 glutMotionFunc(motion
);
313 glutReshapeFunc(reshape
);
314 glutIdleFunc(display
);
315 glutKeyboardFunc(keyboard
);