1 // OpenSG Tutorial Example: Indexed Geometry
3 // This example shows how to use Indices to reuse data within a Geometry
6 #ifdef OSG_BUILD_ACTIVE
10 #include <OSGSimpleGeometry.h>
11 #include <OSGGeoProperties.h>
12 #include <OSGGLUTWindow.h>
13 #include <OSGSimpleSceneManager.h>
14 #include <OSGBaseFunctions.h>
15 #include <OSGTransform.h>
17 #include <OSGGeometry.h>
20 #include <OpenSG/OSGGLUT.h>
21 #include <OpenSG/OSGConfig.h>
22 #include <OpenSG/OSGSimpleGeometry.h>
23 #include <OpenSG/OSGGeoProperties.h>
24 #include <OpenSG/OSGGLUTWindow.h>
25 #include <OpenSG/OSGSimpleSceneManager.h>
26 #include <OpenSG/OSGBaseFunctions.h>
27 #include <OpenSG/OSGTransform.h>
28 #include <OpenSG/OSGGroup.h>
29 #include <OpenSG/OSGGeometry.h>
32 // The pointer to the transformation
33 OSG::TransformRefPtr trans
;
35 // The SimpleSceneManager to manage simple applications
36 OSG::SimpleSceneManagerRefPtr mgr
;
38 // forward declaration so we can have the interesting stuff upfront
39 int setupGLUT( int *argc
, char *argv
[] );
46 OSG::Real32 t
= glutGet(GLUT_ELAPSED_TIME
);
48 m
.setTransform(OSG::Quaternion( OSG::Vec3f(0,1,0),
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
);
68 // open a new scope, because the pointers below should go out of scope
69 // before entering glutMainLoop.
70 // Otherwise OpenSG will complain about objects being alive after shutdown.
72 // the connection between GLUT and OpenSG
73 OSG::GLUTWindowRefPtr gwin
= OSG::GLUTWindow::create();
74 gwin
->setGlutId(winid
);
80 Some of the positions in the Geometry example were added to it
81 multiple times, when they were used by multiple primitives.
83 For large objects that's very inefficient memorywise, thus it is
84 possible to reuse the positions by using an index.
88 The initial setup is the same as in the geometry...
90 OSG::GeoUInt8PropertyRefPtr type
= OSG::GeoUInt8Property::create();
91 type
->addValue(GL_POLYGON
);
92 type
->addValue(GL_TRIANGLES
);
93 type
->addValue(GL_QUADS
);
95 OSG::GeoUInt32PropertyRefPtr lens
= OSG::GeoUInt32Property::create();
101 This time, only unique positions are stored.
103 OSG::GeoPnt3fPropertyRefPtr pnts
= OSG::GeoPnt3fProperty::create();
105 pnts
->addValue(OSG::Pnt3f(-1, -1, -1));
106 pnts
->addValue(OSG::Pnt3f(-1, -1, 1));
107 pnts
->addValue(OSG::Pnt3f( 1, -1, 1));
108 pnts
->addValue(OSG::Pnt3f( 1, -1, -1));
111 pnts
->addValue(OSG::Pnt3f(-1, 0, -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));
117 pnts
->addValue(OSG::Pnt3f( 0, 1, -1));
118 pnts
->addValue(OSG::Pnt3f( 0, 1, 1));
121 The first new part: Colors.
123 In parallel to the Positions every vertex can also have a separate
126 Colors also have their own types, they are neither Points nor Vectors.
128 OSG::GeoVec3fPropertyRefPtr colors
= OSG::GeoVec3fProperty::create();
130 colors
->addValue(OSG::Color3f(1, 1, 0));
131 colors
->addValue(OSG::Color3f(1, 0, 0));
132 colors
->addValue(OSG::Color3f(1, 0, 0));
133 colors
->addValue(OSG::Color3f(1, 1, 0));
136 colors
->addValue(OSG::Color3f(0, 1, 1));
137 colors
->addValue(OSG::Color3f(1, 0, 1));
138 colors
->addValue(OSG::Color3f(1, 0, 1));
139 colors
->addValue(OSG::Color3f(0, 1, 1));
142 colors
->addValue(OSG::Color3f( 0, 1, 1));
143 colors
->addValue(OSG::Color3f( 1, 1, 0));
146 The second new part: Indices.
148 The Indices are positioned between the primitives and the positions
149 (and other attribute data like colors). So in this example the polygon
150 does not use the first 4 elements from the positions property, it used
151 the first 4 elements from the indices property, which define the
152 positions to be used. The same 4 indices are used to select the colors
155 OSG::GeoUInt32PropertyRefPtr indices
= OSG::GeoUInt32Property::create();
156 // indices for the polygon
157 indices
->addValue(0);
158 indices
->addValue(1);
159 indices
->addValue(2);
160 indices
->addValue(3);
162 // indices for the triangles
163 indices
->addValue(7);
164 indices
->addValue(4);
165 indices
->addValue(8);
167 indices
->addValue(5);
168 indices
->addValue(6);
169 indices
->addValue(9);
171 // indices for the quads
172 indices
->addValue(1);
173 indices
->addValue(2);
174 indices
->addValue(6);
175 indices
->addValue(5);
177 indices
->addValue(3);
178 indices
->addValue(0);
179 indices
->addValue(4);
180 indices
->addValue(7);
183 Put it all together into a Geometry NodeCore.
185 OSG::GeometryRefPtr geo
= OSG::Geometry::create();
186 geo
->setTypes (type
);
187 geo
->setLengths (lens
);
188 geo
->setIndices (indices
);
189 geo
->setPositions(pnts
);
190 geo
->setColors (colors
);
191 geo
->setMaterial (OSG::getDefaultMaterial());
193 // put the geometry core into a node
194 OSG::NodeRefPtr n
= OSG::Node::create();
197 // add a transformation to make it move
198 OSG::NodeRefPtr scene
= OSG::Node::create();
199 trans
= OSG::Transform::create();
200 scene
->setCore(trans
);
203 OSG::commitChanges();
205 // create the SimpleSceneManager helper
206 mgr
= OSG::SimpleSceneManager::create();
208 // tell the manager what to manage
209 mgr
->setWindow(gwin
);
210 mgr
->setRoot (scene
);
212 // show the whole scene
223 // GLUT callback functions
226 // react to size changes
227 void reshape(int w
, int h
)
233 // react to mouse button presses
234 void mouse(int button
, int state
, int x
, int y
)
237 mgr
->mouseButtonRelease(button
, x
, y
);
239 mgr
->mouseButtonPress(button
, x
, y
);
244 // react to mouse motions with pressed buttons
245 void motion(int x
, int y
)
247 mgr
->mouseMove(x
, y
);
252 void keyboard(unsigned char k
, int x
, int y
)
258 // clean up global variables
269 mgr
->setStatistics(!mgr
->getStatistics());
275 // setup the GLUT library which handles the windows for us
276 int setupGLUT(int *argc
, char *argv
[])
278 glutInit(argc
, argv
);
279 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
281 int winid
= glutCreateWindow("OpenSG");
283 glutReshapeFunc(reshape
);
284 glutDisplayFunc(display
);
285 glutMouseFunc(mouse
);
286 glutMotionFunc(motion
);
287 glutKeyboardFunc(keyboard
);
289 // call the redraw function whenever there's nothing else to do
290 glutIdleFunc(display
);