fixed: gcc8 compile issues
[opensg.git] / Examples / Tutorial / 06solarsystem3.cpp
blob61b10393b9512a94577f4f1e2b6e405a2c5a15d1
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>
8 #else
9 #include <OpenSG/OSGGLUT.h>
10 #include <OpenSG/OSGConfig.h>
11 #include <OpenSG/OSGSimpleGeometry.h>
12 #include <OpenSG/OSGGLUTWindow.h>
13 #include <OpenSG/OSGSimpleSceneManager.h>
14 #endif
16 OSG::SimpleSceneManagerRefPtr mgr;
17 OSG::NodeRecPtr scene;
18 OSG::TransformRecPtr planetTransform;
19 OSG::TransformRecPtr moonTransform;
21 int setupGLUT( int *argc, char *argv[] );
23 OSG::NodeRecPtr createScenegraph(void)
25 //create sun, planet & moon geometry
27 OSG::GeometryRecPtr sun = OSG::makeSphereGeo(3, 6);
28 OSG::NodeRecPtr planet = OSG::makeSphere (3, 3);
29 OSG::NodeRecPtr moon = OSG::makeSphere (2, 1);
31 //the root node will be the sun
32 OSG::NodeRecPtr root = OSG::Node::create();
33 root->setCore(sun);
35 OSG::NodeRecPtr planetTransformNode = OSG::Node::create();
36 OSG::NodeRecPtr moonTransformNode = OSG::Node::create();
38 // these were declared globally
39 planetTransform = OSG::Transform::create();
40 moonTransform = OSG::Transform::create();
42 // Now we need to fill it with live
43 // We want to have the planet some distance away from the sun,
44 // but initial with no rotation. The same aplies to the moon
45 OSG::Matrix m,n;
47 m.setIdentity();
48 n.setIdentity();
50 m.setTranslate(20, 0, 0);
51 n.setTranslate( 8, 0, 0);
53 planetTransform->setMatrix(m);
54 moonTransform ->setMatrix(n);
56 //Insert the cores into the apropiate nodes and add the geometry
57 planetTransformNode->setCore (planetTransform);
58 planetTransformNode->addChild(planet );
60 moonTransformNode->setCore (moonTransform);
61 moonTransformNode->addChild(moon );
63 //add the planet to the sun
64 root->addChild(planetTransformNode);
65 root->addChild(moonTransformNode );
67 //now we are done
68 return OSG::NodeTransitPtr(root);
72 int main(int argc, char **argv)
74 OSG::osgInit(argc,argv);
77 int winid = setupGLUT(&argc, argv);
78 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
79 gwin->setGlutId(winid);
80 gwin->init();
82 scene = createScenegraph();
84 mgr = OSG::SimpleSceneManager::create();
85 mgr->setWindow(gwin );
86 mgr->setRoot (scene);
87 mgr->showAll();
89 OSG::commitChanges();
92 glutMainLoop();
94 return 0;
97 void reshape(int w, int h)
99 mgr->resize(w, h);
100 glutPostRedisplay();
103 void display(void)
105 OSG::Real32 time = glutGet(GLUT_ELAPSED_TIME );
107 //create the Quaternion the describes the rotation of
108 //the planet around the sun
109 OSG::Quaternion planetRot = OSG::Quaternion(OSG::Vec3f(0,1,0),
110 time/float(1000));
112 //now the rotation of the moon around the planet
113 //the division by 12 speeds up the rotation by 12 compared to the
114 //planet rotation
115 OSG::Quaternion moonRot = OSG::Quaternion(OSG::Vec3f(0,1,0),
116 time/float(1000/12));
118 //generate the Matrices
119 OSG::Matrix p,m,t1,r1,t2,r2;
121 t1.setTransform(OSG::Vec3f(20,0,0));
122 r1.setTransform(planetRot);
123 r1.mult(t1);
124 p.setValue(r1);
126 t2.setTransform(OSG::Vec3f(8,0,0));
127 r2.setTransform(moonRot);
128 r2.mult(t2);
129 r1.mult(r2);
130 m.setValue(r1);
132 planetTransform->setMatrix(p);
133 moonTransform ->setMatrix(m);
135 mgr->redraw();
138 void mouse(int button, int state, int x, int y)
140 if (state)
141 mgr->mouseButtonRelease(button, x, y);
142 else
143 mgr->mouseButtonPress(button, x, y);
145 glutPostRedisplay();
148 void motion(int x, int y)
150 mgr->mouseMove(x, y);
151 glutPostRedisplay();
154 int setupGLUT(int *argc, char *argv[])
156 glutInit(argc, argv);
157 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
159 int winid = glutCreateWindow("OpenSG First Application");
161 glutDisplayFunc(display);
162 glutMouseFunc(mouse);
163 glutMotionFunc(motion);
164 glutReshapeFunc(reshape);
165 glutIdleFunc(display);
167 return winid;