fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Simple / 07multiindexgeometry.cpp
blobd334b1cc80d119e7d91fd50d9b6b8bfac7f52a46
1 // OpenSG Tutorial Example: Multiindex Geometry
2 //
3 // This example shows how to use an index mapping to use different indices
4 // for different attributes.
5 //
7 #ifdef OSG_BUILD_ACTIVE
8 // Headers
9 #include <OSGGLUT.h>
10 #include <OSGConfig.h>
11 #include <OSGSimpleGeometry.h>
12 #include <OSGGeoProperties.h>
13 #include <OSGGLUTWindow.h>
14 #include <OSGSimpleSceneManager.h>
15 #include <OSGBaseFunctions.h>
16 #include <OSGTransform.h>
17 #include <OSGGroup.h>
18 #include <OSGGeometry.h>
19 #else
20 // Headers
21 #include <OpenSG/OSGGLUT.h>
22 #include <OpenSG/OSGConfig.h>
23 #include <OpenSG/OSGSimpleGeometry.h>
24 #include <OpenSG/OSGGeoProperties.h>
25 #include <OpenSG/OSGGLUTWindow.h>
26 #include <OpenSG/OSGSimpleSceneManager.h>
27 #include <OpenSG/OSGBaseFunctions.h>
28 #include <OpenSG/OSGTransform.h>
29 #include <OpenSG/OSGGroup.h>
30 #include <OpenSG/OSGGeometry.h>
31 #endif
33 // The pointer to the transformation
34 OSG::TransformRefPtr trans;
36 // The SimpleSceneManager to manage simple applications
37 OSG::SimpleSceneManagerRefPtr mgr;
39 // forward declaration so we can have the interesting stuff upfront
40 int setupGLUT( int *argc, char *argv[] );
42 // redraw the window
43 void display( void )
45 // create the matrix
46 OSG::Matrix m;
47 OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME );
49 m.setTransform(OSG::Quaternion(OSG::Vec3f(0,1,0), t / 1000.f));
51 // set the transform's matrix
52 trans->setMatrix(m);
54 OSG::commitChanges();
56 mgr->redraw();
59 // Initialize GLUT & OpenSG and set up the scene
60 int main(int argc, char **argv)
62 // OSG init
63 OSG::osgInit(argc,argv);
65 // GLUT init
66 int winid = setupGLUT(&argc, argv);
69 open a new scope, because the pointers below should go out of scope
70 before entering glutMainLoop.
71 Otherwise OpenSG will complain about objects being alive after shutdown.
74 // the connection between GLUT and OpenSG
75 OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
76 gwin->setGlutId(winid);
77 gwin->init();
80 create the scene
82 In the previous example, the colors and positions used the same
83 indices. That might not always be the preferred way, and it might not
84 make sense for other properties, e.g. normals.
86 It is possible to assign a different index for every property. See the
87 indices section below for details.
90 The initial setup is the same as in 06indexgeometry
93 OSG::GeoUInt8PropertyRefPtr type = OSG::GeoUInt8Property::create();
94 type->addValue(GL_POLYGON );
95 type->addValue(GL_TRIANGLES);
96 type->addValue(GL_QUADS );
98 OSG::GeoUInt32PropertyRefPtr lens = OSG::GeoUInt32Property::create();
99 lens->addValue(4);
100 lens->addValue(6);
101 lens->addValue(8);
103 // positions
104 OSG::GeoPnt3fPropertyRefPtr pnts = OSG::GeoPnt3fProperty::create();
105 // the base
106 pnts->addValue(OSG::Pnt3f(-1, -1, -1));
107 pnts->addValue(OSG::Pnt3f(-1, -1, 1));
108 pnts->addValue(OSG::Pnt3f( 1, -1, 1));
109 pnts->addValue(OSG::Pnt3f( 1, -1, -1));
111 // the roof base
112 pnts->addValue(OSG::Pnt3f(-1, 0, -1));
113 pnts->addValue(OSG::Pnt3f(-1, 0, 1));
114 pnts->addValue(OSG::Pnt3f( 1, 0, 1));
115 pnts->addValue(OSG::Pnt3f( 1, 0, -1));
117 // the gable
118 pnts->addValue(OSG::Pnt3f( 0, 1, -1));
119 pnts->addValue(OSG::Pnt3f( 0, 1, 1));
121 // colors
122 OSG::GeoVec3fPropertyRefPtr colors = OSG::GeoVec3fProperty::create();
123 colors->push_back(OSG::Color3f(1, 1, 0));
124 colors->push_back(OSG::Color3f(1, 0, 0));
125 colors->push_back(OSG::Color3f(1, 0, 0));
126 colors->push_back(OSG::Color3f(1, 1, 0));
127 colors->push_back(OSG::Color3f(0, 1, 1));
128 colors->push_back(OSG::Color3f(1, 0, 1));
131 A new property: normals.
133 They are used for lighting calculations and have to point away from the
134 surface. Normals are standard vectors.
137 OSG::GeoVec3fPropertyRefPtr norms = OSG::GeoVec3fProperty::create();
138 norms->push_back(OSG::Vec3f(-1, 0, 0));
139 norms->push_back(OSG::Vec3f( 1, 0, 0));
140 norms->push_back(OSG::Vec3f( 0, -1, 0));
141 norms->push_back(OSG::Vec3f( 0, 1, 0));
142 norms->push_back(OSG::Vec3f( 0, 0, -1));
143 norms->push_back(OSG::Vec3f( 0, 0, 1));
146 To use more than one index for a geometry, create multiple
147 GeoUInt32Property (or GeoUInt8Property or GeoUInt16Property) objects
148 and add them as index for the corresponding property you want to
149 index.
152 OSG::GeoUInt32PropertyRefPtr ind1 = OSG::GeoUInt32Property::create();
153 OSG::GeoUInt32PropertyRefPtr ind2 = OSG::GeoUInt32Property::create();
155 // fill first index (will be used for positions)
156 ind1->push_back(0); // polygon
157 ind1->push_back(1);
158 ind1->push_back(2);
159 ind1->push_back(3);
161 ind1->push_back(7); // triangle 1
162 ind1->push_back(4);
163 ind1->push_back(8);
164 ind1->push_back(5); // triangle 2
165 ind1->push_back(6);
166 ind1->push_back(9);
168 ind1->push_back(1); // quad 1
169 ind1->push_back(2);
170 ind1->push_back(6);
171 ind1->push_back(5);
172 ind1->push_back(3); // quad 2
173 ind1->push_back(0);
174 ind1->push_back(4);
175 ind1->push_back(7);
177 // fill second index (will be used for colors/normals)
178 ind2->push_back(3); // polygon
179 ind2->push_back(3);
180 ind2->push_back(3);
181 ind2->push_back(3);
183 ind2->push_back(4); // triangle 1
184 ind2->push_back(4);
185 ind2->push_back(4);
186 ind2->push_back(5); // triangle 2
187 ind2->push_back(5);
188 ind2->push_back(5);
190 ind2->push_back(5); // quad 1
191 ind2->push_back(5);
192 ind2->push_back(5);
193 ind2->push_back(5);
194 ind2->push_back(4); // quad 2
195 ind2->push_back(4);
196 ind2->push_back(4);
197 ind2->push_back(4);
201 Put it all together into a Geometry NodeCore.
203 OSG::GeometryRefPtr geo = OSG::Geometry::create();
204 geo->setTypes (type);
205 geo->setLengths (lens);
208 Set the properties and indices used to index them.
209 Calling geo->setProperty(pnts, Geometry::PositionsIndex) is the
210 same as calling geo->setPositions(pnts), but this way it is
211 more obvious which properties and indices go together.
214 geo->setProperty(pnts, OSG::Geometry::PositionsIndex);
215 geo->setIndex (ind1, OSG::Geometry::PositionsIndex);
217 geo->setProperty(norms, OSG::Geometry::NormalsIndex );
218 geo->setIndex (ind2, OSG::Geometry::NormalsIndex );
220 geo->setProperty(colors, OSG::Geometry::ColorsIndex );
221 geo->setIndex (ind2, OSG::Geometry::ColorsIndex );
223 geo->setMaterial (OSG::getDefaultMaterial());
225 // put the geometry core into a node
226 OSG::NodeRefPtr n = OSG::Node::create();
227 n->setCore(geo);
229 // add a transformation to make it move
230 OSG::NodeRefPtr scene = OSG::Node::create();
231 trans = OSG::Transform::create();
232 scene->setCore(trans);
233 scene->addChild(n);
235 OSG::commitChanges();
237 // create the SimpleSceneManager helper
238 mgr = OSG::SimpleSceneManager::create();
240 // tell the manager what to manage
241 mgr->setWindow(gwin );
242 mgr->setRoot (scene);
244 // show the whole scene
245 mgr->showAll();
248 // GLUT main loop
249 glutMainLoop();
251 return 0;
255 // GLUT callback functions
258 // react to size changes
259 void reshape(int w, int h)
261 mgr->resize(w, h);
262 glutPostRedisplay();
265 // react to mouse button presses
266 void mouse(int button, int state, int x, int y)
268 if (state)
269 mgr->mouseButtonRelease(button, x, y);
270 else
271 mgr->mouseButtonPress(button, x, y);
273 glutPostRedisplay();
276 // react to mouse motions with pressed buttons
277 void motion(int x, int y)
279 mgr->mouseMove(x, y);
280 glutPostRedisplay();
283 // react to keys
284 void keyboard(unsigned char k, int x, int y)
286 switch(k)
288 case 27:
290 // clean up global variables
291 trans = NULL;
292 mgr = NULL;
294 OSG::osgExit();
295 exit(0);
297 break;
299 case 's':
301 mgr->setStatistics(!mgr->getStatistics());
303 break;
307 // setup the GLUT library which handles the windows for us
308 int setupGLUT(int *argc, char *argv[])
310 glutInit(argc, argv);
311 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
313 int winid = glutCreateWindow("OpenSG");
315 glutReshapeFunc(reshape);
316 glutDisplayFunc(display);
317 glutMouseFunc(mouse);
318 glutMotionFunc(motion);
319 glutKeyboardFunc(keyboard);
321 // call the redraw function whenever there's nothing else to do
322 glutIdleFunc(display);
324 return winid;