changed: gcc8 base update
[opensg.git] / Examples / Tutorial / 08coresdemo2.cpp
blob6f43510d0fb8a7ca170f91f48d9b3d89668ba7cc
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 NullFC 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 if(womanGeometry !=NULL)
118 geo = dynamic_cast<OSG::Geometry *>(womanGeometry->getCore());
119 if (geo == NULL)
120 std::cout << "Casting failed!" << std::endl;
122 else
124 std::cout << "No correct geometry node found!" << std::endl;
125 //create a dummy object
126 geo = OSG::makeBoxGeo(0.5,0.5,0.5,1,1,1);
129 // generating a material *********************************
131 OSG::SimpleMaterialRecPtr mat = OSG::SimpleMaterial::create();
132 mat->setAmbient(OSG::Color3f(0.2,0.2,0.2));
133 mat->setDiffuse(OSG::Color3f(0.6,0.3,0.1));
134 mat->setSpecular(OSG::Color3f(1,1,1));
135 mat->setShininess(0.8);
137 geo->setMaterial(mat);
139 // end material generation *******************************
141 //new node with "old" geometry core referenced
142 OSG::NodeRecPtr woman = OSG::Node::create();
143 woman->setCore(geo);
145 //translate it a bit to see both women
146 OSG::NodeRecPtr womanTrans = OSG::Node::create();
147 OSG::TransformRecPtr t = OSG::Transform::create();
149 OSG::Matrix m;
150 m.setIdentity();
151 m.setTranslate(OSG::Vec3f(0,0,200));
152 t->setMatrix(m);
154 womanTrans->setCore(t);
155 womanTrans->addChild(woman);
157 //add it to the root
158 root->addChild(womanTrans);
160 return OSG::NodeTransitPtr(root);
163 int main(int argc, char **argv)
165 OSG::osgInit(argc,argv);
168 int winid = setupGLUT(&argc, argv);
169 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
170 gwin->setGlutId(winid);
171 gwin->init();
173 scene = createScenegraph();
175 mgr = OSG::SimpleSceneManager::create();
176 mgr->setWindow(gwin );
177 mgr->setRoot (scene);
178 mgr->showAll();
180 OSG::commitChanges();
183 glutMainLoop();
185 return 0;
188 void reshape(int w, int h)
190 mgr->resize(w, h);
191 glutPostRedisplay();
194 void display(void)
196 mgr->redraw();
199 void mouse(int button, int state, int x, int y)
201 if (state)
202 mgr->mouseButtonRelease(button, x, y);
203 else
204 mgr->mouseButtonPress(button, x, y);
206 glutPostRedisplay();
209 void motion(int x, int y)
211 mgr->mouseMove(x, y);
212 glutPostRedisplay();
215 int setupGLUT(int *argc, char *argv[])
217 glutInit(argc, argv);
218 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
220 int winid = glutCreateWindow("OpenSG First Application");
222 glutDisplayFunc(display);
223 glutMouseFunc(mouse);
224 glutMotionFunc(motion);
225 glutReshapeFunc(reshape);
226 glutIdleFunc(display);
228 return winid;