fixed: compile issue
[opensg.git] / Examples / Tutorial / 08coresdemo3.cpp
blob3ab2f3d9787006711fb572125998062699ef2011
1 // all needed include files
2 #ifdef OSG_BUILD_ACTIVE
3 #include <OSGGLUT.h>
4 #include <OSGConfig.h>
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>
15 #else
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>
28 #endif
30 OSG::SimpleSceneManagerRefPtr mgr;
31 OSG::NodeRecPtr scene;
32 OSG::ComponentTransformRecPtr ct;
33 OSG::UInt32 frame = 0;
34 // 0 = translation
35 // 1 = rotation
36 // 2 = scalation
37 OSG::UInt8 mode = 0;
39 int setupGLUT(int *argc, char *argv[]);
42 const char *getNodeName(OSG::Node const *node)
44 if(node == NULL)
45 return NULL;
47 // get node name
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)
54 nodename =
55 dynamic_cast<OSG::Name *>(
56 node->getCore()->findAttachment(OSG::Name::getClassType()));
59 if(nodename != NULL)
60 return nodename->getFieldPtr()->getValue().c_str();
61 else
63 if(node->getCore() != NULL)
64 return node->getCore()->getType().getName().c_str();
67 return NULL;
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
77 if (getName(n))
79 //check if it is the name we are looking for
80 if (getName(n)== std::string("FACESET_Woman"))
81 // We got the node!
82 return n;
85 //check all children
86 for(OSG::UInt32 i = 0; i < children; i++)
88 OSG::Node *r = checkName(n->getChild(i));
89 if(r != NULL)
90 // if it is not NULL it is the node we are looking for
91 // so just pass it through
92 return r;
95 // no children's name matches or there are no more childs
96 // so return NULL, indicating that the node was not found yet
97 return NULL;
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"
117 << std::endl;
118 return OSG::NodeTransitPtr();
121 // now the LOD core
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
138 sw->setChoice(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'!"
156 << std::endl;
157 return OSG::NodeTransitPtr();
160 OSG::GeometryRecPtr geo =
161 dynamic_cast<OSG::Geometry *>(womanGeometry->getCore());
163 if(geo == NULL)
165 std::cout << "Node 'FACESET_Woman' is not a geometry node!"
166 << std::endl;
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();
184 woman->setCore(geo);
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();
190 Matrix m;
191 m.setIdentity();
192 m.setTranslate(Vec3f(0,0,200));
193 t->setMatrix(m);
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));
209 ctNode->setCore(ct);
210 ctNode->addChild(woman);
211 // end component transform ********************************
213 //add it to the root
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);
227 gwin->init();
229 scene = createScenegraph();
231 if(scene == NULL)
232 return 1;
234 mgr = OSG::SimpleSceneManager::create();
235 mgr->setWindow(gwin );
236 mgr->setRoot (scene);
237 mgr->showAll();
239 OSG::commitChanges();
242 glutMainLoop();
244 return 0;
247 void reshape(int w, int h)
249 mgr->resize(w, h);
250 glutPostRedisplay();
253 void display(void)
255 frame++;
256 OSG::Real32 time = glutGet(GLUT_ELAPSED_TIME);
258 switch(mode)
260 case 0 :
261 ct->setTranslation(OSG::Vec3f(0,cos(time/2000.f)*100,200));
262 break;
263 case 1 :
264 ct->setRotation(OSG::Quaternion(OSG::Vec3f(0,1,0), time/2000));
265 break;
266 case 2 :
267 ct->setScale(OSG::Vec3f(cos(time/2000),
268 sin(time/2000),
269 tan(time/2000)));
270 break;
274 mgr->redraw();
277 void mouse(int button, int state, int x, int y)
279 if (state)
280 mgr->mouseButtonRelease(button, x, y);
281 else
282 mgr->mouseButtonPress(button, x, y);
284 glutPostRedisplay();
287 void motion(int x, int y)
289 mgr->mouseMove(x, y);
290 glutPostRedisplay();
293 void keyboard(unsigned char k, int x, int y){
294 switch (k)
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);
317 return winid;