fixed: push transparency enforcement to state so the backend is aware of it
[opensg.git] / Examples / Simple / 06indexgeometry.cpp
blobfde4fad703954dc2d0e48b45aedf598f03cb117e
1 // OpenSG Tutorial Example: Indexed Geometry
2 //
3 // This example shows how to use Indices to reuse data within a Geometry
4 //
6 #ifdef OSG_BUILD_ACTIVE
7 // Headers
8 #include <OSGGLUT.h>
9 #include <OSGConfig.h>
10 #include <OSGSimpleGeometry.h>
11 #include <OSGGeoProperties.h>
12 #include <OSGGLUTWindow.h>
13 #include <OSGSimpleSceneManager.h>
14 #include <OSGBaseFunctions.h>
15 #include <OSGTransform.h>
16 #include <OSGGroup.h>
17 #include <OSGGeometry.h>
18 #else
19 // Headers
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>
30 #endif
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[] );
41 // redraw the window
42 void display( void )
44 // create the matrix
45 OSG::Matrix m;
46 OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME );
48 m.setTransform(OSG::Quaternion( OSG::Vec3f(0,1,0),
49 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);
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);
75 gwin->init();
77 // create the scene
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();
96 lens->addValue(4);
97 lens->addValue(6);
98 lens->addValue(8);
101 This time, only unique positions are stored.
103 OSG::GeoPnt3fPropertyRefPtr pnts = OSG::GeoPnt3fProperty::create();
104 // the base
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));
110 // the roof base
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));
116 // the gable
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
124 color.
126 Colors also have their own types, they are neither Points nor Vectors.
128 OSG::GeoVec3fPropertyRefPtr colors = OSG::GeoVec3fProperty::create();
129 // the base
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));
135 // the roof base
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));
141 // the gable
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
153 for the vertices.
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();
195 n->setCore(geo);
197 // add a transformation to make it move
198 OSG::NodeRefPtr scene = OSG::Node::create();
199 trans = OSG::Transform::create();
200 scene->setCore(trans);
201 scene->addChild(n);
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
213 mgr->showAll();
216 // GLUT main loop
217 glutMainLoop();
219 return 0;
223 // GLUT callback functions
226 // react to size changes
227 void reshape(int w, int h)
229 mgr->resize(w, h);
230 glutPostRedisplay();
233 // react to mouse button presses
234 void mouse(int button, int state, int x, int y)
236 if (state)
237 mgr->mouseButtonRelease(button, x, y);
238 else
239 mgr->mouseButtonPress(button, x, y);
241 glutPostRedisplay();
244 // react to mouse motions with pressed buttons
245 void motion(int x, int y)
247 mgr->mouseMove(x, y);
248 glutPostRedisplay();
251 // react to keys
252 void keyboard(unsigned char k, int x, int y)
254 switch(k)
256 case 27:
258 // clean up global variables
259 trans = NULL;
260 mgr = NULL;
262 OSG::osgExit();
263 exit(0);
265 break;
267 case 's':
269 mgr->setStatistics(!mgr->getStatistics());
271 break;
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);
292 return winid;