fixed: compile issue
[opensg.git] / Examples / Tutorial / 08coresdemo1.cpp
blob1a9c7168ffbecf183e1804cacaf5578e7d2a2482
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 #else
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>
26 #endif
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
40 if (getName(n))
42 //check if it is the name we are looking for
43 if (getName(n) == std::string("FACESET_Woman"))
44 // We got the node!
45 return n;
48 //check all children
49 for(OSG::UInt32 i = 0; i < children; i++)
51 OSG::Node *r = checkName(n->getChild(i));
52 if(r != NULL)
53 // if it is not NULL it is the node we are looking for
54 // so just pass it through
55 return r;
58 // no children's name matches or there are no more childs
59 // so return NULL, indicating that the node was not found yet
60 return NULL;
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"
79 << std::endl;
80 return OSG::NodeTransitPtr();
83 // now the LOD core
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
100 sw->setChoice(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();
120 woman->setCore(geo);
122 //translate it a bit to see both women
123 OSG::NodeRecPtr womanTrans = OSG::Node ::create();
124 OSG::TransformRecPtr t = OSG::Transform::create();
125 OSG::Matrix m;
126 m.setIdentity();
127 m.setTranslate(OSG::Vec3f(0,0,200));
128 t->setMatrix(m);
130 womanTrans->setCore(t);
131 womanTrans->addChild(woman);
133 //add it to the root
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);
147 gwin->init();
149 scene = createScenegraph();
151 mgr = OSG::SimpleSceneManager::create();
152 mgr->setWindow(gwin );
153 mgr->setRoot (scene);
154 mgr->showAll();
156 OSG::commitChanges();
159 glutMainLoop();
161 return 0;
164 void reshape(int w, int h)
166 mgr->resize(w, h);
167 glutPostRedisplay();
170 void display(void)
172 mgr->redraw();
175 void mouse(int button, int state, int x, int y)
177 if (state)
178 mgr->mouseButtonRelease(button, x, y);
179 else
180 mgr->mouseButtonPress(button, x, y);
182 glutPostRedisplay();
185 void motion(int x, int y)
187 mgr->mouseMove(x, y);
188 glutPostRedisplay();
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);
204 return winid;