changed: gcc8 base update
[opensg.git] / Examples / Tutorial / 02movingTorus.cpp
blobd780421b5eeb5b1987c60ec0f13263f8fbad4b9c
1 // what follows here is the smallest OpenSG programm possible
2 // most things used here are explained now or on the next few pages, so don't
3 // worry if not everythings clear right at the beginning...
5 // Some needed inlcude files - these will become more, believe me ;)
6 #ifdef OSG_BUILD_ACTIVE
7 #include <OSGGLUT.h>
8 #include <OSGConfig.h>
9 #include <OSGSimpleGeometry.h>
10 #include <OSGGLUTWindow.h>
11 #include <OSGSimpleSceneManager.h>
12 #else
13 #include <OpenSG/OSGGLUT.h>
14 #include <OpenSG/OSGConfig.h>
15 #include <OpenSG/OSGSimpleGeometry.h>
16 #include <OpenSG/OSGGLUTWindow.h>
17 #include <OpenSG/OSGSimpleSceneManager.h>
18 #endif
20 // The SimpleSceneManager is a little usefull class which helps us to
21 // manage little scenes. It will be discussed in detail later on
22 OSG::SimpleSceneManagerRefPtr mgr;
23 OSG::TransformRecPtr transCore;
25 // we have a forward declarion here, just to sort the code
26 int setupGLUT( int *argc, char *argv[] );
28 int main(int argc, char **argv)
30 // Init the OpenSG subsystem
31 OSG::osgInit(argc,argv);
34 // We create a GLUT Window (that is almost the same for most applications)
35 int winid = setupGLUT(&argc, argv);
36 OSG::GLUTWindowRecPtr gwin= OSG::GLUTWindow::create();
37 gwin->setGlutId(winid);
38 gwin->init();
40 // That will be our whole scene for now : an incredible Torus
42 OSG::NodeRecPtr scene;
44 // create all that stuff we will need:
45 //one geometry and one transform node
47 OSG::NodeRecPtr torus = OSG::makeTorus(.5, 2, 16, 16);
48 OSG::NodeRecPtr transNode = OSG::Node::create();
50 transCore = OSG::Transform::create();
51 OSG::Matrix m;
53 // now provide some data...
55 // no rotation at the beginning
56 m.setIdentity();
58 // set the core to the matrix we created
59 transCore->setMatrix(m);
61 // now "insert" the core into the node
62 transNode->setCore(transCore);
63 // add the torus as a child to
64 // the transformation node
65 transNode->addChild(torus);
67 // "declare" the transformation as root
68 scene = transNode;
70 // Create and setup our little friend - the SSM
71 mgr = OSG::SimpleSceneManager::create();
72 mgr->setWindow(gwin );
73 mgr->setRoot (scene);
74 mgr->showAll();
76 OSG::commitChanges();
79 // Give Control to the GLUT Main Loop
80 glutMainLoop();
82 return 0;
85 // react to size changes
86 void reshape(int w, int h)
88 mgr->resize(w, h);
89 glutPostRedisplay();
92 // just redraw our scene if this GLUT callback is invoked
93 void display(void)
95 //--------------------------BEGIN_ADD
96 OSG::Matrix m;
98 // get the time since the apllication startet
99 OSG::Real32 time = glutGet(GLUT_ELAPSED_TIME );
101 // set the rotation
102 m.setRotate(OSG::Quaternion(OSG::Vec3f(0,1,0), time/1000.f));
104 //apply the new matrix to our transform core
105 transCore->setMatrix(m);
107 // Ordinarily a call to commitChanges should be placed here, since we
108 // modify the scene and therefore invalidate bounding volumes on which
109 // rendering depends.
110 // Since we are using the SimpleSceneManager though, this is not needed,
111 // it calls commitChanges internally (adding the call here anyways would
112 // not be a big issue, just slightly inefficient).
114 //--------------------------END_ADD
116 mgr->redraw();
119 // react to mouse button presses
120 void mouse(int button, int state, int x, int y)
122 if (state)
123 mgr->mouseButtonRelease(button, x, y);
124 else
125 mgr->mouseButtonPress(button, x, y);
127 glutPostRedisplay();
130 // react to mouse motions with pressed buttons
131 void motion(int x, int y)
133 mgr->mouseMove(x, y);
134 glutPostRedisplay();
137 //The GLUT subsystem is set up here. This is very similar to other GLUT applications
138 //If you have worked with GLUT before, you may have the feeling of meeting old friends again,
139 //if you have not used GLUT before that is no problem. GLUT will be introduced shortly on the
140 //next section
142 int setupGLUT(int *argc, char *argv[])
144 glutInit(argc, argv);
145 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
147 int winid = glutCreateWindow("OpenSG First Application");
149 glutDisplayFunc(display);
150 glutMouseFunc(mouse);
151 glutMotionFunc(motion);
152 glutReshapeFunc(reshape);
153 glutIdleFunc(display);
155 return winid;