root/branches/Carsten_PtrWork2/Tutorials/06indexgeometry.cpp

Revision 1039, 6.8 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: Indexed Geometry
2 //
3 // This example shows how to use Indices to reuse data within a Geometry
4 //
5
6 // Headers
7 #include <OpenSG/OSGGLUT.h>
8 #include <OpenSG/OSGConfig.h>
9 #include <OpenSG/OSGSimpleGeometry.h>
10 #include <OpenSG/OSGGeoProperties.h>
11 #include <OpenSG/OSGGLUTWindow.h>
12 #include <OpenSG/OSGSimpleSceneManager.h>
13 #include <OpenSG/OSGBaseFunctions.h>
14 #include <OpenSG/OSGTransform.h>
15 #include <OpenSG/OSGGroup.h>
16 #include <OpenSG/OSGGeometry.h>
17
18 // Activate the OpenSG namespace
19 OSG_USING_NAMESPACE
20
21 // The pointer to the transformation
22 TransformGlobalRefPtr trans;
23
24 // The SimpleSceneManager to manage simple applications
25 SimpleSceneManager *mgr;
26
27 // forward declaration so we can have the interesting stuff upfront
28 int setupGLUT( int *argc, char *argv[] );
29
30 // redraw the window
31 void display( void )
32 {
33     // create the matrix
34     Matrix m;
35     Real32 t = glutGet(GLUT_ELAPSED_TIME );
36    
37     m.setTransform(Quaternion( Vec3f(0,1,0),
38                                t / 1000.f));
39    
40     // set the transform's matrix
41     trans->setMatrix(m);
42
43     commitChanges();
44    
45     mgr->redraw();
46 }
47
48 // Initialize GLUT & OpenSG and set up the scene
49 int main(int argc, char **argv)
50 {
51     // OSG init
52     osgInit(argc,argv);
53
54     // GLUT init
55     int winid = setupGLUT(&argc, argv);
56
57     // the connection between GLUT and OpenSG
58     GLUTWindowGlobalRefPtr gwin= GLUTWindow::create();
59     gwin->setGlutId(winid);
60     gwin->init();
61
62     // create the scene
63     
64     /*
65         Some of the positions in the Geometry example were added to it
66         multiple times, when they were used by multiple primitives.
67         
68         For large objects that's very inefficient memorywise, thus it is
69         possible to reuse the positions by using an index.
70     */
71    
72     /*
73         The initial setup is the same as in the geometry...
74     */
75     GeoUInt8PropertyGlobalRefPtr type = GeoUInt8Property::create();
76     type->addValue(GL_POLYGON  );
77     type->addValue(GL_TRIANGLES);
78     type->addValue(GL_QUADS    );
79
80     GeoUInt32PropertyGlobalRefPtr lens = GeoUInt32Property::create();
81     lens->addValue(4);
82     lens->addValue(6);
83     lens->addValue(8);
84        
85     /*
86         This time, only unique positions are stored.
87     */
88     GeoPnt3fPropertyGlobalRefPtr pnts = GeoPnt3fProperty::create();
89     // the base
90     pnts->addValue(Pnt3f(-1, -1, -1));
91     pnts->addValue(Pnt3f(-1, -1,  1));
92     pnts->addValue(Pnt3f( 1, -1,  1));
93     pnts->addValue(Pnt3f( 1, -1, -1));
94
95     // the roof base
96     pnts->addValue(Pnt3f(-1,  0, -1));
97     pnts->addValue(Pnt3f(-1,  0,  1));
98     pnts->addValue(Pnt3f( 1,  0,  1));
99     pnts->addValue(Pnt3f( 1,  0, -1));
100
101     // the gable
102     pnts->addValue(Pnt3f( 0,  1, -1));
103     pnts->addValue(Pnt3f( 0,  1,  1));
104    
105     /*
106         The first new part: Colors.
107         
108         In parallel to the Positions every vertex can also have a separate
109         color.
110         
111         Colors also have their own types, they are neither Points nor Vectors.
112     */
113     GeoVec3fPropertyGlobalRefPtr colors = GeoVec3fProperty::create();
114     // the base
115     colors->addValue(Color3f(1, 1, 0));
116     colors->addValue(Color3f(1, 0, 0));
117     colors->addValue(Color3f(1, 0, 0));
118     colors->addValue(Color3f(1, 1, 0));
119
120     // the roof base
121     colors->addValue(Color3f(0, 1, 1));
122     colors->addValue(Color3f(1, 0, 1));
123     colors->addValue(Color3f(1, 0, 1));
124     colors->addValue(Color3f(0, 1, 1));
125
126     // the gable
127     colors->addValue(Color3f( 0,  1,  1));
128     colors->addValue(Color3f( 1,  1,  0));
129  
130     /*
131         The second new part: Indices.
132         
133         The Indices are positioned between the primitives and the positions
134         (and other attribute data like colors). So in this example the polygon
135         does not use the first 4 elements from the positions property, it used
136         the first 4 elements from the indices property, which define the
137         positions to be used. The same 4 indices are used to select the colors
138         for the vertices.
139     */
140     GeoUInt32PropertyGlobalRefPtr indices = GeoUInt32Property::create();
141     // indices for the polygon
142     indices->addValue(0);
143     indices->addValue(1);
144     indices->addValue(2);
145     indices->addValue(3);
146        
147     // indices for the triangles
148     indices->addValue(7);
149     indices->addValue(4);
150     indices->addValue(8);
151
152     indices->addValue(5);
153     indices->addValue(6);
154     indices->addValue(9);
155        
156     // indices for the quads
157     indices->addValue(1);
158     indices->addValue(2);
159     indices->addValue(6);
160     indices->addValue(5);
161
162     indices->addValue(3);
163     indices->addValue(0);
164     indices->addValue(4);
165     indices->addValue(7);
166    
167     /*
168        Put it all together into a Geometry NodeCore.
169     */
170     GeometryGlobalRefPtr geo = Geometry::create();
171     geo->setTypes    (type);
172     geo->setLengths  (lens);
173     geo->setIndices  (indices);
174     geo->setPositions(pnts);
175     geo->setColors   (colors);
176     geo->setMaterial (getDefaultMaterial());   
177    
178     // put the geometry core into a node
179     NodeGlobalRefPtr n = Node::create();
180     n->setCore(geo);
181    
182     // add a transformation to make it move     
183     NodeGlobalRefPtr scene = Node::create(); 
184     trans = Transform::create();
185     scene->setCore(trans);
186     scene->addChild(n);
187
188     commitChanges();
189
190     // create the SimpleSceneManager helper
191     mgr = new SimpleSceneManager;
192
193     // tell the manager what to manage
194     mgr->setWindow(gwin );
195     mgr->setRoot  (scene);
196
197     // show the whole scene
198     mgr->showAll();
199
200     // GLUT main loop
201     glutMainLoop();
202
203     return 0;
204 }
205
206 //
207 // GLUT callback functions
208 //
209
210 // react to size changes
211 void reshape(int w, int h)
212 {
213     mgr->resize(w, h);
214     glutPostRedisplay();
215 }
216
217 // react to mouse button presses
218 void mouse(int button, int state, int x, int y)
219 {
220     if (state)
221         mgr->mouseButtonRelease(button, x, y);
222     else
223         mgr->mouseButtonPress(button, x, y);
224        
225     glutPostRedisplay();
226 }
227
228 // react to mouse motions with pressed buttons
229 void motion(int x, int y)
230 {
231     mgr->mouseMove(x, y);
232     glutPostRedisplay();
233 }
234
235 // react to keys
236 void keyboard(unsigned char k, int x, int y)
237 {
238     switch(k)
239     {
240         case 27:   
241         {
242             delete mgr;
243        
244             OSG::osgExit();
245             exit(0);
246         }
247         break;
248
249         case 's':
250         {
251             mgr->setStatistics(!mgr->getStatistics());
252         }
253         break;
254     }
255 }
256
257 // setup the GLUT library which handles the windows for us
258 int setupGLUT(int *argc, char *argv[])
259 {
260     glutInit(argc, argv);
261     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
262    
263     int winid = glutCreateWindow("OpenSG");
264    
265     glutReshapeFunc(reshape);
266     glutDisplayFunc(display);
267     glutMouseFunc(mouse);
268     glutMotionFunc(motion);
269     glutKeyboardFunc(keyboard);
270
271     // call the redraw function whenever there's nothing else to do
272     glutIdleFunc(display);
273
274     return winid;
275 }
Note: See TracBrowser for help on using the browser.