root/branches/Dirk_CPtr/Tutorials/04hiertransform.cpp

Revision 637, 4.7 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: Hierarchical Transformation
2 //
3 // This example demonstrates how transformations accumulate through the graph.
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 #include <OpenSG/OSGBaseFunctions.h>
13 #include <OpenSG/OSGTransform.h>
14 #include <OpenSG/OSGGroup.h>
15
16
17 // Activate the OpenSG namespace
18 OSG_USING_NAMESPACE
19
20 // number of copies to create
21 const UInt16 ncopies = 10;
22
23 // just use a single transformation that is shared
24 TransformPtr trans;
25
26
27 // The SimpleSceneManager to manage simple applications
28 SimpleSceneManager *mgr;
29
30 // forward declaration so we can have the interesting stuff upfront
31 int setupGLUT( int *argc, char *argv[] );
32
33 // redraw the window
34 void display( void )
35 {
36     Matrix m;
37     Real32 t = glutGet(GLUT_ELAPSED_TIME );
38    
39     m.setTransform(Vec3f(0, .9, 0),
40                    Quaternion( Vec3f(1,1,0), osgSin(t / 1000.f) / 2.f));
41    
42     // set the transform's matrix
43     trans->setMatrix(m);
44
45     commitChanges();
46      
47     mgr->redraw();
48 }
49
50 // Initialize GLUT & OpenSG and set up the scene
51 int main(int argc, char **argv)
52 {
53     // OSG init
54     osgInit(argc,argv);
55
56     // GLUT init
57     int winid = setupGLUT(&argc, argv);
58
59     // the connection between GLUT and OpenSG
60     GLUTWindowPtr gwin= GLUTWindow::create();
61     gwin->setId(winid);
62     gwin->init();
63
64     // create the scene
65
66     /*
67         Transformation accumulate through the graph, i.e. all nodes below
68         a Transformation are influenced by it, even other Transformations.
69         
70         This can be used to create models of objects that move together and
71         in relation to each other, the prime examples being a robot arm and
72         a planetary system. This example does something not quite unlike a
73         robot arm.
74     */   
75
76     // create the scene
77     
78     /*
79        This time the graph is not wide, but deep, i.e. every Transformation
80        only has two children, a Geometry and another transformation.
81        The end resulting motion of the geometry is the accumulation of
82        all the Transformations above it.
83     */
84      
85     // use a cylinder this time
86     GeometryPtr cyl = makeCylinderGeo( 1, .3, 8, true, true, true );
87    
88     // the single transformation Core used
89     trans = Transform::create();
90    
91     // setup an intial transformation
92     Matrix m;
93     m.setTransform(Vec3f(0, .9, 0));
94
95     trans->setMatrix(m);
96    
97     /*
98        NullFC is the generic NULL value for FieldContainer pointer.
99     */
100     NodePtr last = NullFC;
101    
102     // create the copied transformations and their geometry nodes
103     for(UInt16 i = 1; i < ncopies; ++i)
104     {
105         // create the shared Geometry
106         NodePtr geonode = Node::create();
107         geonode->setCore(cyl);
108
109         // add a transformation to the Geometry
110         NodePtr transnode = Node::create();
111
112         transnode->setCore (trans);
113         transnode->addChild(geonode );
114         if(last != NullFC)
115         {
116             transnode->addChild(last);       
117         }
118        
119         last = transnode;
120     }
121  
122     NodePtr scene = last;
123
124     commitChanges();
125
126     // create the SimpleSceneManager helper
127     mgr = new SimpleSceneManager;
128
129     // tell the manager what to manage
130     mgr->setWindow(gwin );
131     mgr->setRoot  (scene);
132
133     // show the whole scene
134     mgr->showAll();
135
136     // GLUT main loop
137     glutMainLoop();
138
139     return 0;
140 }
141
142 //
143 // GLUT callback functions
144 //
145
146 // react to size changes
147 void reshape(int w, int h)
148 {
149     mgr->resize(w, h);
150     glutPostRedisplay();
151 }
152
153 // react to mouse button presses
154 void mouse(int button, int state, int x, int y)
155 {
156     if (state)
157         mgr->mouseButtonRelease(button, x, y);
158     else
159         mgr->mouseButtonPress(button, x, y);
160        
161     glutPostRedisplay();
162 }
163
164 // react to mouse motions with pressed buttons
165 void motion(int x, int y)
166 {
167     mgr->mouseMove(x, y);
168     glutPostRedisplay();
169 }
170
171 // react to keys
172 void keyboard(unsigned char k, int x, int y)
173 {
174     switch(k)
175     {
176         case 27:   
177         {
178             OSG::osgExit();
179             exit(0);
180         }
181         break;
182
183         case 's':
184         {
185             mgr->setStatistics(!mgr->getStatistics());
186         }
187         break;
188     }
189 }
190
191 // setup the GLUT library which handles the windows for us
192 int setupGLUT(int *argc, char *argv[])
193 {
194     glutInit(argc, argv);
195     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
196    
197     int winid = glutCreateWindow("OpenSG");
198    
199     glutReshapeFunc(reshape);
200     glutDisplayFunc(display);
201     glutMouseFunc(mouse);
202     glutMotionFunc(motion);
203     glutKeyboardFunc(keyboard);
204
205     // call the redraw function whenever there's nothing else to do
206     glutIdleFunc(display);
207
208     return winid;
209 }
Note: See TracBrowser for help on using the browser.