root/branches/Carsten_PtrWork2/Tutorials/05geometry.cpp

Revision 1039, 7.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: Simple Geometry
2 //
3 // This example shows how to built a simple Geometry NodeCore.
4 //
5 // It also shows how to manipulate the geometry and what to watch out for. This
6 // is done in the display() function.
7 //
8
9 // Headers
10 #include <OpenSG/OSGGLUT.h>
11 #include <OpenSG/OSGConfig.h>
12 #include <OpenSG/OSGSimpleGeometry.h>
13 #include <OpenSG/OSGGeoProperties.h>
14 #include <OpenSG/OSGGLUTWindow.h>
15 #include <OpenSG/OSGSimpleSceneManager.h>
16 #include <OpenSG/OSGBaseFunctions.h>
17 #include <OpenSG/OSGTransform.h>
18 #include <OpenSG/OSGGroup.h>
19
20 // New headers:
21
22 // the geometry node core
23 #include <OpenSG/OSGGeometry.h>
24
25 // Activate the OpenSG namespace
26 OSG_USING_NAMESPACE
27
28 // The pointer to the transformation
29 TransformGlobalRefPtr trans;
30
31 // The pointer to the geometry core
32 GeometryGlobalRefPtr geo;
33
34
35 // The SimpleSceneManager to manage simple applications
36 SimpleSceneManager *mgr;
37
38 // forward declaration so we can have the interesting stuff upfront
39 int setupGLUT( int *argc, char *argv[] );
40
41 // Initialize GLUT & OpenSG and set up the scene
42 int main(int argc, char **argv)
43 {
44     // OSG init
45     osgInit(argc,argv);
46
47     // GLUT init
48     int winid = setupGLUT(&argc, argv);
49
50     // the connection between GLUT and OpenSG
51     GLUTWindowGlobalRefPtr gwin = GLUTWindow::create();
52     gwin->setGlutId(winid);
53     gwin->init();
54
55     // create the scene
56     
57     /*
58         Geometry data in OpenSG is stored in several separate vectors.
59         
60         These vectors are not a direct part of the Geometry Core but
61         rather split up into multiple separate classes.
62         
63         These classes, the GeoProperties, contain a single field containg
64         their values, which can be accessed directly, see the docs for
65         GeoProperty for the whole interface.
66     */
67    
68     /*
69         The first part: the primtive types.
70         These are taken from OpenGL, any values that can be passed to
71         glBegin(); are legal. Different types can be freely mixed.
72         
73         All properties have only one field, which has the same name for every
74         property, thus the mask is also called the same for each property.
75     */
76     GeoUInt8PropertyGlobalRefPtr type = GeoUInt8Property::create();
77     type->addValue(GL_POLYGON  );
78     type->addValue(GL_TRIANGLES);
79     type->addValue(GL_QUADS    );
80    
81     /*
82         The second part: the primtive lengths.
83         These define the number of vertices to be passed to OpenGL for each
84         primitive. Thus there have to be at least as many entries as in the
85         types property.
86     */
87     GeoUInt32PropertyGlobalRefPtr lens = GeoUInt32Property::create();
88     lens->addValue(4);
89     lens->addValue(6);
90     lens->addValue(8);
91        
92     /*
93         The third part: the vertex positions.
94         
95         OpenSG uses different types for vectors and points.
96         
97         Points (e.g. Pnt3f) are just positions in space, they have a limited
98         set of operations they can handle. Vectors (e.g. Vec3f) are the more
99         general kind.
100     */
101     GeoPnt3fPropertyGlobalRefPtr  pnts = OSG::GeoPnt3fProperty::create();
102     // the 4 points of the polygon
103     pnts->addValue(Pnt3f(-1, -1, -1));
104     pnts->addValue(Pnt3f(-1, -1,  1));
105     pnts->addValue(Pnt3f( 1, -1,  1));
106     pnts->addValue(Pnt3f( 1, -1, -1));
107
108     // the 6 points of the two triangles
109     pnts->addValue(Pnt3f( 1,  0, -1));
110     pnts->addValue(Pnt3f(-1,  0, -1));
111     pnts->addValue(Pnt3f( 0,  1, -1));
112
113     pnts->addValue(Pnt3f(-1,  0,  1));
114     pnts->addValue(Pnt3f( 1,  0,  1));
115     pnts->addValue(Pnt3f( 0,  1,  1));
116
117     // the 8 points of the two quads
118     pnts->addValue(Pnt3f(-1,  -1,  1));   
119     pnts->addValue(Pnt3f( 1,  -1,  1));   
120     pnts->addValue(Pnt3f( 1,   0,  1));   
121     pnts->addValue(Pnt3f(-1,   0,  1));   
122
123     pnts->addValue(Pnt3f( 1,  -1, -1));   
124     pnts->addValue(Pnt3f(-1,  -1, -1));   
125     pnts->addValue(Pnt3f(-1,   0, -1));   
126     pnts->addValue(Pnt3f( 1,   0, -1));   
127    
128     /*
129        Put it all together into a Geometry NodeCore.
130     */
131     geo = Geometry::create();
132     geo->setTypes    (type);
133     geo->setLengths  (lens);
134     geo->setPositions(pnts);
135
136     // assign a material to the geometry to make it visible. The details
137     // of materials are defined later.
138     geo->setMaterial(getDefaultUnlitMaterial());   
139    
140     // put the geometry core into a node
141     NodeGlobalRefPtr n = Node::create();
142     n->setCore(geo);
143    
144     // add a transformation to make it move     
145     NodeGlobalRefPtr scene = Node::create(); 
146     trans = Transform::create();
147     scene->setCore(trans);
148     scene->addChild(n);
149
150     commitChanges();
151
152     // create the SimpleSceneManager helper
153     mgr = new SimpleSceneManager;
154
155     // tell the manager what to manage
156     mgr->setWindow(gwin );
157     mgr->setRoot  (scene);
158
159     // show the whole scene
160     mgr->showAll();
161
162     // GLUT main loop
163     glutMainLoop();
164
165     return 0;
166 }
167
168 //
169 // GLUT callback functions
170 //
171
172 // redraw the window
173 void display( void )
174 {
175     // create the matrix
176     Matrix m;
177     Real32 t = glutGet(GLUT_ELAPSED_TIME );
178    
179     m.setTransform(Quaternion( Vec3f(0,1,0),
180                                t / 1000.f));
181    
182     // set the transform's matrix
183     trans->setMatrix(m);
184    
185     /*
186         Manipulate the geometry.
187         
188         The OpenSG geometry structure is pretty flexible.
189         
190         The disadvantage of all this flexibility is that it can be hard to
191         write generic tools, as pretty much all the used types can be one of a
192         number of variants.
193         
194         To simplify that, every kind of GeoProperty has a generic type, e.g.
195         the generic type for positions is Pnt3f, for colors it's Color3f.
196         
197         No matter the internal data representation looks like, all
198         GeoProperties have the generic interface. As does the abstract parent
199         class of every kind of property. Thus it's possible to access the data
200         of an arbitrary geometry using the generic interface.
201     */
202    
203     // note that this is the abstract parent class, it doesn't have a specific
204     // type
205     GeoVectorPropertyPtr pos = geo->getPositions();
206    
207     for(UInt32 i = 0; i < pos->getSize(); i++)
208     {
209         Pnt3f p;     
210         pos->getValue(p, i);
211        
212         p[0] += osgSin(t / 300) * p[1] / 100;
213         p[1] += osgSin(t / 300) * p[2] / 100;
214         p[2] += osgSin(t / 300) * p[0] / 100;
215        
216         pos->setValue(p, i);
217     }
218
219     commitChanges();
220    
221     mgr->redraw();
222 }
223
224 // react to size changes
225 void reshape(int w, int h)
226 {
227     mgr->resize(w, h);
228     glutPostRedisplay();
229 }
230
231 // react to mouse button presses
232 void mouse(int button, int state, int x, int y)
233 {
234     if (state)
235         mgr->mouseButtonRelease(button, x, y);
236     else
237         mgr->mouseButtonPress(button, x, y);
238        
239     glutPostRedisplay();
240 }
241
242 // react to mouse motions with pressed buttons
243 void motion(int x, int y)
244 {
245     mgr->mouseMove(x, y);
246     glutPostRedisplay();
247 }
248
249 // react to keys
250 void keyboard(unsigned char k, int x, int y)
251 {
252     switch(k)
253     {
254         case 27:   
255         {
256             delete mgr;
257        
258             OSG::osgExit();
259             exit(0);
260         }
261         break;
262
263         case 's':
264         {
265             mgr->setStatistics(!mgr->getStatistics());
266         }
267         break;
268     }
269 }
270
271 // setup the GLUT library which handles the windows for us
272 int setupGLUT(int *argc, char *argv[])
273 {
274     glutInit(argc, argv);
275     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
276    
277     int winid = glutCreateWindow("OpenSG");
278    
279     glutReshapeFunc(reshape);
280     glutDisplayFunc(display);
281     glutMouseFunc(mouse);
282     glutMotionFunc(motion);
283     glutKeyboardFunc(keyboard);
284
285     // call the redraw function whenever there's nothing else to do
286     glutIdleFunc(display);
287
288     return winid;
289 }
Note: See TracBrowser for help on using the browser.