root/branches/mixin-collappse/Tutorials/03share.cpp

Revision 637, 5.5 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: Share
2 //
3 // This example shows how to use core sharing to create copies
4 // of an object, e.g. a geometry, which take up less memory.
5 //
6
7 // Headers
8 #include <OpenSG/OSGGLUT.h>
9 #include <OpenSG/OSGConfig.h>
10 #include <OpenSG/OSGSimpleGeometry.h>
11 #include <OpenSG/OSGGLUTWindow.h>
12 #include <OpenSG/OSGSimpleSceneManager.h>
13 #include <OpenSG/OSGBaseFunctions.h>
14 #include <OpenSG/OSGTransform.h>
15
16 // new headers:
17
18 // the Group node core
19 #include <OpenSG/OSGGroup.h>
20
21
22 // Activate the OpenSG namespace
23 OSG_USING_NAMESPACE
24
25 // number of copies to create
26 const UInt16 ncopies = 20;
27
28 // a seprate transformation for every copy
29 TransformPtr trans[ncopies];
30
31
32 // The SimpleSceneManager to manage simple applications
33 SimpleSceneManager *mgr;
34
35 // forward declaration so we can have the interesting stuff upfront
36 int setupGLUT( int *argc, char *argv[] );
37
38 // redraw the window
39 void display( void )
40 {
41     // create the matrix
42     Matrix m;
43     Real32 t = glutGet(GLUT_ELAPSED_TIME );
44    
45     // set the transforms' matrices
46     for(UInt16 i=0; i<ncopies; ++i)
47     {
48         m.setTransform(Vec3f(      osgSin(t / 1000.f + i * 4 * ncopies / Pi),
49                                    osgCos(t / 1000.f + i * 6 * ncopies / Pi),
50                                    osgSin(t / 1000.f + i * 7 * ncopies / Pi)),
51                        Quaternion( Vec3f (1,1,0),
52                                           t / 1000.f + i * 4 * ncopies / Pi));
53    
54         trans[i]->setMatrix(m);
55     }
56
57     commitChanges();
58    
59     mgr->redraw();
60 }
61
62 // Initialize GLUT & OpenSG and set up the scene
63 int main(int argc, char **argv)
64 {
65     // OSG init
66     osgInit(argc,argv);
67
68     // GLUT init
69     int winid = setupGLUT(&argc, argv);
70
71     // the connection between GLUT and OpenSG
72     GLUTWindowPtr gwin= GLUTWindow::create();
73     gwin->setId(winid);
74     gwin->init();
75
76     // create the scene
77     
78     /*
79        Scenegraph nodes in OpenSG consist of two parts: the Node and its Core.
80       
81        The Node contains the general information that anchors an object in the
82        graph: the parent, a list of children, a bounding volume. Note that the
83        Node contains a single parent, a Node can only be connected to a graph
84        in one place.
85       
86        There is only one Node class, all nodes in the scenegraph use the same
87        Node. The specific information that distinguishes node kinds from each
88        other is stored in the NodeCore.
89
90        Consequently there is a different kind of NodeCore for the different
91        kinds of functions a node can have. The NodeCore contains all the
92        information that the Node doesn't.
93       
94        NodeCores can be used by multiple Nodes. In this example the Geometry
95        NodeCore of the torus is used to display multiple tori.
96     */
97    
98     // this time, create just the core of the geometry
99     GeometryPtr torus = makeTorusGeo( .5, 2, 8, 12 );
100
101     // create the scene
102     // the scene has a single group with ncopies transformations below,
103     // each of these carries a Node that shares the geometry
104     
105     /*
106        The Group NodeCore is the basic type to create the hierarchical graph.
107        It does very little to its children, just calls all of them when asked
108        to do anything, and collecting their information if necessary.
109     */
110    
111     // create the root Group node
112     NodePtr  scene = Node::create();
113     GroupPtr g     = Group::create();
114    
115     scene->setCore(g);
116    
117     // create the copied geometry nodes and their transformations
118     for(UInt16 i = 0; i<ncopies; ++i)
119     {
120         // create the nodes for the shared Geometry core
121         NodePtr geonode = Node::create();
122        
123         // assign the Core to the Node
124         geonode->setCore(torus);
125
126         // add a transformation for every Geometry
127         NodePtr transnode = Node::create();
128        
129         trans[i] = Transform::create();
130          
131         transnode->setCore (trans[i]);
132         transnode->addChild(geonode );
133        
134         scene->addChild(transnode);       
135     }
136
137     commitChanges();
138
139     // create the SimpleSceneManager helper
140     mgr = new SimpleSceneManager;
141
142     // tell the manager what to manage
143     mgr->setWindow(gwin );
144     mgr->setRoot  (scene);
145
146     // show the whole scene
147     mgr->showAll();
148
149     // GLUT main loop
150     glutMainLoop();
151
152     return 0;
153 }
154
155 //
156 // GLUT callback functions
157 //
158
159 // react to size changes
160 void reshape(int w, int h)
161 {
162     mgr->resize(w, h);
163     glutPostRedisplay();
164 }
165
166 // react to mouse button presses
167 void mouse(int button, int state, int x, int y)
168 {
169     if (state)
170         mgr->mouseButtonRelease(button, x, y);
171     else
172         mgr->mouseButtonPress(button, x, y);
173        
174     glutPostRedisplay();
175 }
176
177 // react to mouse motions with pressed buttons
178 void motion(int x, int y)
179 {
180     mgr->mouseMove(x, y);
181     glutPostRedisplay();
182 }
183
184 // react to keys
185 void keyboard(unsigned char k, int x, int y)
186 {
187     switch(k)
188     {
189         case 27:   
190         {
191             OSG::osgExit();
192             exit(0);
193         }
194         break;
195
196         case 's':
197         {
198             mgr->setStatistics(!mgr->getStatistics());
199         }
200         break;
201     }
202 }
203
204 // setup the GLUT library which handles the windows for us
205 int setupGLUT(int *argc, char *argv[])
206 {
207     glutInit(argc, argv);
208     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
209    
210     int winid = glutCreateWindow("OpenSG");
211    
212     glutReshapeFunc(reshape);
213     glutDisplayFunc(display);
214     glutMouseFunc(mouse);
215     glutMotionFunc(motion);
216     glutKeyboardFunc(keyboard);
217
218     // call the redraw function whenever there's nothing else to do
219     glutIdleFunc(display);
220
221     return winid;
222 }
Note: See TracBrowser for help on using the browser.