1 // OpenSG Tutorial Example: Multiindex Geometry
3 // This example shows how to use an index mapping to use different indices
4 // for different attributes.
7 #ifdef OSG_BUILD_ACTIVE
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>
18 #include <OSGGeometry.h>
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>
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
[] );
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
59 // Initialize GLUT & OpenSG and set up the scene
60 int main(int argc
, char **argv
)
63 OSG::osgInit(argc
,argv
);
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
);
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();
104 OSG::GeoPnt3fPropertyRefPtr pnts
= OSG::GeoPnt3fProperty::create();
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));
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));
118 pnts
->addValue(OSG::Pnt3f( 0, 1, -1));
119 pnts
->addValue(OSG::Pnt3f( 0, 1, 1));
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
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
161 ind1
->push_back(7); // triangle 1
164 ind1
->push_back(5); // triangle 2
168 ind1
->push_back(1); // quad 1
172 ind1
->push_back(3); // quad 2
177 // fill second index (will be used for colors/normals)
178 ind2
->push_back(3); // polygon
183 ind2
->push_back(4); // triangle 1
186 ind2
->push_back(5); // triangle 2
190 ind2
->push_back(5); // quad 1
194 ind2
->push_back(4); // quad 2
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();
229 // add a transformation to make it move
230 OSG::NodeRefPtr scene
= OSG::Node::create();
231 trans
= OSG::Transform::create();
232 scene
->setCore(trans
);
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
255 // GLUT callback functions
258 // react to size changes
259 void reshape(int w
, int h
)
265 // react to mouse button presses
266 void mouse(int button
, int state
, int x
, int y
)
269 mgr
->mouseButtonRelease(button
, x
, y
);
271 mgr
->mouseButtonPress(button
, x
, y
);
276 // react to mouse motions with pressed buttons
277 void motion(int x
, int y
)
279 mgr
->mouseMove(x
, y
);
284 void keyboard(unsigned char k
, int x
, int y
)
290 // clean up global variables
301 mgr
->setStatistics(!mgr
->getStatistics());
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
);