root/branches/Carsten_PtrWork2/Tutorials/03share.cpp

Revision 1039, 5.6 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: 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 TransformGlobalRefPtr 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     GLUTWindowGlobalRefPtr gwin= GLUTWindow::create();
73     gwin->setGlutId(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     GeometryGlobalRefPtr 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     NodeGlobalRefPtr  scene = Node::create();
113     GroupGlobalRefPtr 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         NodeRefPtr geonode = Node::create();
122        
123         // assign the Core to the Node
124         geonode->setCore(torus);
125
126         // add a transformation for every Geometry
127         NodeRefPtr 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             delete mgr;
192        
193             OSG::osgExit();
194             exit(0);
195         }
196         break;
197
198         case 's':
199         {
200             mgr->setStatistics(!mgr->getStatistics());
201         }
202         break;
203     }
204 }
205
206 // setup the GLUT library which handles the windows for us
207 int setupGLUT(int *argc, char *argv[])
208 {
209     glutInit(argc, argv);
210     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
211    
212     int winid = glutCreateWindow("OpenSG");
213    
214     glutReshapeFunc(reshape);
215     glutDisplayFunc(display);
216     glutMouseFunc(mouse);
217     glutMotionFunc(motion);
218     glutKeyboardFunc(keyboard);
219
220     // call the redraw function whenever there's nothing else to do
221     glutIdleFunc(display);
222
223     return winid;
224 }
Note: See TracBrowser for help on using the browser.