fixed: compile issue
[opensg.git] / Examples / Tutorial / 06solarsystem2.cpp
blobf1ae222f9e5531a1e1e7737b496c4021aada66fa
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::NodeTransitPtr 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;
46 m.setIdentity();
47 n.setIdentity();
49 m.setTranslate(20, 0, 0);
50 n.setTranslate( 8, 0, 0);
52 planetTransform->setMatrix(m);
53 moonTransform ->setMatrix(n);
55 //Insert the cores into the apropiate nodes and add the geometry
56 planetTransformNode->setCore (planetTransform);
57 planetTransformNode->addChild(planet );
59 moonTransformNode->setCore (moonTransform);
60 moonTransformNode->addChild(moon );
62 //add the planet to the sun
63 root->addChild(planetTransformNode);
65 //add the moon to the planet
66 planet->addChild(moonTransformNode);
68 //now we are done
69 return OSG::NodeTransitPtr(root);
73 int main(int argc, char **argv)
75 OSG::osgInit(argc,argv);
78 int winid = setupGLUT(&argc, argv);
79 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
80 gwin->setGlutId(winid);
81 gwin->init();
83 scene = createScenegraph();
85 mgr = OSG::SimpleSceneManager::create();
86 mgr->setWindow(gwin );
87 mgr->setRoot (scene);
88 mgr->showAll();
90 OSG::commitChanges();
93 glutMainLoop();
95 return 0;
98 void reshape(int w, int h)
100 mgr->resize(w, h);
101 glutPostRedisplay();
104 void display(void)
106 OSG::Real32 time = glutGet(GLUT_ELAPSED_TIME );
108 //create the Quaternion the describes the rotation of
109 //the planet around the sun
110 OSG::Quaternion planetRot = OSG::Quaternion(OSG::Vec3f(0,1,0),
111 time/float(1000));
113 //now the rotation of the moon around the planet
114 //the division by 12 speeds up the rotation by 12 compared to the
115 //planet rotation
116 OSG::Quaternion moonRot = OSG::Quaternion(OSG::Vec3f(0,1,0),
117 time/float(1000/12));
119 //generate the Matrices
120 OSG::Matrix p,m,t,r;
122 t.setTransform(OSG::Vec3f(20,0,0));
123 r.setTransform(planetRot);
124 r.mult(t);
125 p.setValue(r);
127 t.setTransform(OSG::Vec3f(8,0,0));
128 r.setTransform(moonRot);
129 r.mult(t);
130 m.setValue(r);
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;