root/branches/Dirk_CPtr/Tutorials/02move.cpp

Revision 637, 3.6 kB (checked in by vossg, 2 years ago)

fixed : added statistics key (s)

  • Property svn:eol-style set to native
Line 
1 // OpenSG Tutorial Example: Move An Object
2 //
3 // This example shows how to use Transform nodes to move objects aroung
4 //
5
6 // Headers
7 #include <OpenSG/OSGGLUT.h>
8 #include <OpenSG/OSGConfig.h>
9 #include <OpenSG/OSGSimpleGeometry.h>
10 #include <OpenSG/OSGGLUTWindow.h>
11 #include <OpenSG/OSGSimpleSceneManager.h>
12
13 // new headers:
14
15 // some wrappers for standard functions for platform independence, e.g.
16 // osgSin, osgCos, osgTan
17 #include <OpenSG/OSGBaseFunctions.h>
18
19 // the transformation node core
20 #include <OpenSG/OSGTransform.h>
21
22 // Activate the OpenSG namespace
23 OSG_USING_NAMESPACE
24
25 // The pointer to the transformation
26 TransformPtr trans;
27
28
29 // The SimpleSceneManager to manage simple applications
30 SimpleSceneManager *mgr;
31
32 // forward declaration so we can have the interesting stuff upfront
33 int setupGLUT( int *argc, char *argv[] );
34
35 // redraw the window
36 void display( void )
37 {
38     // create the matrix
39     Matrix m;
40     Real32 t = glutGet(GLUT_ELAPSED_TIME );
41    
42     m.setTransform(Vec3f(      osgSin(t / 1000.f),
43                                osgCos(t / 1000.f),
44                                osgSin(t / 1000.f)),
45                    Quaternion( Vec3f(0,1,0),
46                                t / 1000.f));
47    
48     // set the transform's matrix
49     
50     trans->setMatrix(m);
51
52     commitChanges();
53
54     mgr->redraw();
55 }
56
57 // Initialize GLUT & OpenSG and set up the scene
58 int main(int argc, char **argv)
59 {
60     // OSG init
61     osgInit(argc,argv);
62
63     // GLUT init
64     int winid = setupGLUT(&argc, argv);
65
66     // the connection between GLUT and OpenSG
67     GLUTWindowPtr gwin= GLUTWindow::create();
68     gwin->setId(winid);
69     gwin->init();
70
71     // create the scene
72
73     NodePtr torus = makeTorus( .5, 2, 16, 32 );
74
75     // create the transformation node
76     // scenegraph nodes are split into 2 parts: the node and its core
77     
78     // 1. create the Node
79     NodePtr scene = Node::create();
80    
81     // 2. create the core
82     trans = Transform::create();
83    
84     // 3. associate the core with the node
85  
86     scene->setCore(trans);
87     // add the torus as a child
88     scene->addChild(torus);
89    
90     commitChanges();
91
92     // create the SimpleSceneManager helper
93     mgr = new SimpleSceneManager;
94
95     // tell the manager what to manage
96     mgr->setWindow(gwin );
97     mgr->setRoot  (scene);
98
99     // show the whole scene
100     mgr->showAll();
101
102     // GLUT main loop
103     glutMainLoop();
104
105     return 0;
106 }
107
108 //
109 // GLUT callback functions
110 //
111
112 // react to size changes
113 void reshape(int w, int h)
114 {
115     mgr->resize(w, h);
116     glutPostRedisplay();
117 }
118
119 // react to mouse button presses
120 void mouse(int button, int state, int x, int y)
121 {
122     if (state)
123         mgr->mouseButtonRelease(button, x, y);
124     else
125         mgr->mouseButtonPress(button, x, y);
126        
127     glutPostRedisplay();
128 }
129
130 // react to mouse motions with pressed buttons
131 void motion(int x, int y)
132 {
133     mgr->mouseMove(x, y);
134     glutPostRedisplay();
135 }
136
137 // react to keys
138 void keyboard(unsigned char k, int x, int y)
139 {
140     switch(k)
141     {
142         case 27
143         {
144             OSG::osgExit();
145             exit(0);
146         }
147         break;
148
149         case 's':
150         {
151             mgr->setStatistics(!mgr->getStatistics());
152         }
153         break;
154     }
155 }
156
157 // setup the GLUT library which handles the windows for us
158 int setupGLUT(int *argc, char *argv[])
159 {
160     glutInit(argc, argv);
161     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
162    
163     int winid = glutCreateWindow("OpenSG");
164    
165     glutReshapeFunc(reshape);
166     glutDisplayFunc(display);
167     glutMouseFunc(mouse);
168     glutMotionFunc(motion);
169     glutKeyboardFunc(keyboard);
170
171     // call the redraw function whenever there's nothing else to do
172     glutIdleFunc(display);
173
174     return winid;
175 }
Note: See TracBrowser for help on using the browser.