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

Revision 1039, 3.7 kB (checked in by cneumann, 11 months ago)

changed: - factory functions return a TransitPtr? that can not be implicitly

converted to C Ptr. Should help with porting.

added: - GlobalRefPtr?, needed for cases where upon return from main

a RefPtr? goes out of scope (it would attempt to access the
FCFactory which is already shutdown at that point).

status: - vrml loader does not compile (needs porting to ref ptr)

  • tutorials compile, run and exit cleanly
  • multithreading and cluster are untested, yet
  • 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 TransformGlobalRefPtr 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     GLUTWindowGlobalRefPtr gwin = GLUTWindow::create();
68     gwin->setGlutId(winid);
69     gwin->init();
70
71     // create the scene
72
73     NodeGlobalRefPtr 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     NodeGlobalRefPtr 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             delete mgr;
145        
146             OSG::osgExit();
147             exit(0);
148         }
149         break;
150
151         case 's':
152         {
153             mgr->setStatistics(!mgr->getStatistics());
154         }
155         break;
156     }
157 }
158
159 // setup the GLUT library which handles the windows for us
160 int setupGLUT(int *argc, char *argv[])
161 {
162     glutInit(argc, argv);
163     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
164    
165     int winid = glutCreateWindow("OpenSG");
166    
167     glutReshapeFunc(reshape);
168     glutDisplayFunc(display);
169     glutMouseFunc(mouse);
170     glutMotionFunc(motion);
171     glutKeyboardFunc(keyboard);
172
173     // call the redraw function whenever there's nothing else to do
174     glutIdleFunc(display);
175
176     return winid;
177 }
Note: See TracBrowser for help on using the browser.