root/OpenSceneGraph/trunk/examples/osggeometry/osggeometry.cpp @ 1832

Revision 1832, 26.4 kB (checked in by robert, 11 years ago)

Changed the Viewer::realize() calls across to not using the threading paramter
leaving it up to the Viewer to specify the mode (which by default is MultiThreaded?).
Added a check for the presence of osgParticle systems so that threading is
disabled in this case.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Geode>
2#include <osg/Geometry>
3#include <osg/Material>
4#include <osg/Vec3>
5#include <osg/MatrixTransform>
6#include <osg/Texture2D>
7#include <osg/PolygonStipple>
8#include <osg/TriangleFunctor>
9
10#include <osgDB/ReadFile>
11#include <osgDB/WriteFile>
12
13#include <osgGA/TrackballManipulator>
14
15#include <osgProducer/Viewer>
16
17#include <osg/Math>
18
19
20
21// This demos uses the illustrates how to creates the various different types of geometry that
22// the osg::Geometry class can represent.  This demos uses the OpenGL red books diagram of different
23// OpenGL Primitives as a template for all the equivilant OpenSceneGraph Primitives.  The OpenSceneGraph
24// wraps OpenGL very thinly so uses all the same enum and naming conventions. The coordinate data is also
25// wrapped around OpenGL's vertex arrays and draw arrays/elements calls.  Familarity with
26// OpenGL will help understand the the osg::Geometry class which encapsulate all this, or if you
27// havn't learnt OpenGL yet, learning osg::Geometry will help you understand how OpenGL
28// works!
29
30// The osg::Geometry class "is a" subclass of osg::Drawable base class, so is an object that provides
31// a draw method for drawing objects in the scene.  osg::Geometry contains all the vertex, normal
32// color and texture coordate arrays required to specify the coordinates of your objects, and the
33// primtives join these coordinates together as the points, lines or surfaces that you will see
34// rendered on your screen.
35//
36// This demo is split into two functions, the createScene() function which creates the scene graph
37// with the various primitives in it, and the main() which sets up a basic viewer window and
38// adds to the it the scene generated by createScene().
39
40
41struct NormalPrint
42{
43    void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3) const 
44    {
45        osg::Vec3 normal = (v2-v1)^(v3-v2);
46        normal.normalize();
47        std::cout << "\t("<<v1<<") ("<<v2<<") ("<<v3<<") "<<") normal ("<<normal<<")"<<std::endl;
48    }
49};
50
51// decompose Drawable primtives into triangles, print out these triangles and computed normals.
52void printTriangles(const std::string& name, osg::Drawable& drawable)
53{
54    std::cout<<name<<std::endl;
55   
56    osg::TriangleFunctor<NormalPrint> tf;
57    drawable.accept(tf);
58 
59    std::cout<<std::endl;
60}
61
62
63osg::Node* createScene()
64{
65    // create the Geode (Geometry Node) to contain all our osg::Geometry objects.
66    osg::Geode* geode = new osg::Geode();
67
68    // follows are separate blocks for creating POINTS, LINES, LINE_STRIP, LINE_LOOP, POLYGON, QUADS,
69    // QUAD_STRIP, TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN primtives.  A image of these primtives
70    // are provided in the distribution : OpenSceneGraph-Data/Images/primtives.gif.
71
72
73    // create POINTS
74    {
75        // create Geometry object to store all the vetices and points primtive.
76        osg::Geometry* pointsGeom = new osg::Geometry();
77       
78        // create a Vec3Array and add to it all my coordinates.
79        // Like all the *Array variants (see include/osg/Array) , Vec3Array is derivied from both osg::Array
80        // and std::vector<>.  osg::Array's are reference counted and hence sharable,
81        // which std::vector<> provides all the convinience, flexibility and robustness
82        // of the most popular of all STL containers.
83        osg::Vec3Array* vertices = new osg::Vec3Array;
84        vertices->push_back(osg::Vec3(-1.02168, -2.15188e-09, 0.885735));
85        vertices->push_back(osg::Vec3(-0.976368, -2.15188e-09, 0.832179));
86        vertices->push_back(osg::Vec3(-0.873376, 9.18133e-09, 0.832179));
87        vertices->push_back(osg::Vec3(-0.836299, -2.15188e-09, 0.885735));
88        vertices->push_back(osg::Vec3(-0.790982, 9.18133e-09, 0.959889));
89       
90        // pass the created vertex array to the points geometry object.
91        pointsGeom->setVertexArray(vertices);
92       
93       
94       
95        // create the color of the geometry, one single for the whole geometry.
96        // for consitency of design even one single color must added as an element
97        // in a color array.
98        osg::Vec4Array* colors = new osg::Vec4Array;
99        // add a white color, colors take the form r,g,b,a with 0.0 off, 1.0 full on.
100        colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
101       
102        // pass the color arry to points geometry, note the binding to tell the geometry
103        // that only use one color for the whole object.
104        pointsGeom->setColorArray(colors);
105        pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
106       
107       
108        // set the normal in the same way color.
109        osg::Vec3Array* normals = new osg::Vec3Array;
110        normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
111        pointsGeom->setNormalArray(normals);
112        pointsGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
113
114
115        // create and add a DrawArray Primitive (see include/osg/Primtive).  The first
116        // paramter passed to the DrawArrays constructor is the Primtive::Mode which
117        // in this case is POINTS (which has the same value GL_POINTS), the second
118        // parameter is the index position into the vertex array of the first point
119        // to draw, and the third parameter is the number of points to draw.
120        pointsGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices->size()));
121       
122       
123        // add the points geomtry to the geode.
124        geode->addDrawable(pointsGeom);
125    }
126
127    // create LINES
128    {
129        // create Geometry object to store all the vetices and lines primtive.
130        osg::Geometry* linesGeom = new osg::Geometry();
131       
132        // this time we'll prealloacte the vertex array to the size we
133        // need and then simple set them as array elements, 8 points
134        // makes 4 line segments.
135        osg::Vec3Array* vertices = new osg::Vec3Array(8);
136        (*vertices)[0].set(-1.13704, -2.15188e-09, 0.40373);
137        (*vertices)[1].set(-0.856897, -2.15188e-09, 0.531441);
138        (*vertices)[2].set(-0.889855, -2.15188e-09, 0.444927);
139        (*vertices)[3].set(-0.568518, -2.15188e-09, 0.40373);
140        (*vertices)[4].set(-1.00933, -2.15188e-09, 0.370773);
141        (*vertices)[5].set(-0.716827, -2.15188e-09, 0.292498);
142        (*vertices)[6].set(-1.07936, 9.18133e-09, 0.317217);
143        (*vertices)[7].set(-0.700348, 9.18133e-09, 0.362533);
144
145       
146        // pass the created vertex array to the points geometry object.
147        linesGeom->setVertexArray(vertices);
148       
149        // set the colors as before, plus using the aobve
150        osg::Vec4Array* colors = new osg::Vec4Array;
151        colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
152        linesGeom->setColorArray(colors);
153        linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
154       
155
156        // set the normal in the same way color.
157        osg::Vec3Array* normals = new osg::Vec3Array;
158        normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
159        linesGeom->setNormalArray(normals);
160        linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
161
162
163        // This time we simply use primitive, and hardwire the number of coords to use
164        // since we know up front,
165        linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,8));
166       
167       
168        // add the points geomtry to the geode.
169        geode->addDrawable(linesGeom);
170    }
171   
172    // create LINE_STRIP
173    {
174        // create Geometry object to store all the vetices and lines primtive.
175        osg::Geometry* linesGeom = new osg::Geometry();
176       
177        // this time we'll prealloacte the vertex array to the size
178        // and then use an iterator to fill in the values, a bit perverse
179        // but does demonstrate that we have just a standard std::vector underneath.
180        osg::Vec3Array* vertices = new osg::Vec3Array(5);
181        osg::Vec3Array::iterator vitr = vertices->begin();
182        (vitr++)->set(-0.0741545, -2.15188e-09, 0.416089);
183        (vitr++)->set(0.234823, -2.15188e-09, 0.259541);
184        (vitr++)->set(0.164788, -2.15188e-09, 0.366653);
185        (vitr++)->set(-0.0288379, -2.15188e-09, 0.333695);
186        (vitr++)->set(-0.0453167, -2.15188e-09, 0.280139);
187       
188        // pass the created vertex array to the points geometry object.
189        linesGeom->setVertexArray(vertices);
190       
191        // set the colors as before, plus using the aobve
192        osg::Vec4Array* colors = new osg::Vec4Array;
193        colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
194        linesGeom->setColorArray(colors);
195        linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
196
197
198        // set the normal in the same way color.
199        osg::Vec3Array* normals = new osg::Vec3Array;
200        normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
201        linesGeom->setNormalArray(normals);
202        linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
203
204
205        // This time we simply use primitive, and hardwire the number of coords to use
206        // since we know up front,
207        linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,5));
208       
209       
210        // add the points geomtry to the geode.
211        geode->addDrawable(linesGeom);
212    }
213
214    // create LINE_LOOP
215    {
216        // create Geometry object to store all the vetices and lines primtive.
217        osg::Geometry* linesGeom = new osg::Geometry();
218       
219        // this time we'll a C arrays to initilize the vertices.
220       
221        osg::Vec3 myCoords[] =
222        {
223            osg::Vec3(0.741546, -2.15188e-09, 0.453167),
224            osg::Vec3(0.840418, -2.15188e-09, 0.304858),
225            osg::Vec3(1.12468, -2.15188e-09, 0.300738),
226            osg::Vec3(1.03816, 9.18133e-09, 0.453167),
227            osg::Vec3(0.968129, -2.15188e-09, 0.337815),
228            osg::Vec3(0.869256, -2.15188e-09, 0.531441)
229        };
230       
231        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
232       
233        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
234       
235        // pass the created vertex array to the points geometry object.
236        linesGeom->setVertexArray(vertices);
237       
238        // set the colors as before, plus using the aobve
239        osg::Vec4Array* colors = new osg::Vec4Array;
240        colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
241        linesGeom->setColorArray(colors);
242        linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
243       
244
245        // set the normal in the same way color.
246        osg::Vec3Array* normals = new osg::Vec3Array;
247        normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
248        linesGeom->setNormalArray(normals);
249        linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
250       
251
252        // This time we simply use primitive, and hardwire the number of coords to use
253        // since we know up front,
254        linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,numCoords));
255       
256       
257        // add the points geomtry to the geode.
258        geode->addDrawable(linesGeom);
259    }
260
261
262
263
264    // now we'll stop creating separate normal and color arrays
265    // since we are using the same values all the time, we'll just
266    // share the same ColorArray and NormalArrays..
267
268    // set the colors as before, use a ref_ptr rather than just
269    // standard C pointer, as that in the case of it not being
270    // assigned it will still be cleaned up automatically.
271    osg::ref_ptr<osg::Vec4Array> shared_colors = new osg::Vec4Array;
272    shared_colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
273
274    // same trick for shared normal.
275    osg::ref_ptr<osg::Vec3Array> shared_normals = new osg::Vec3Array;
276    shared_normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
277
278
279
280    // Note on vertex ordering.
281    // While the OpenGL diagram should vertices specified in a clockwise direction
282    // in reality you need to specify coords for polygons into a anticlockwise direction
283    // for their front face to be pointing towards your, get this wrong and you could
284    // find back face culling removing the wrong faces of your models.  The OpenGL diagram
285    // is just plain wrong, but its nice diagram so we'll keep it for now!
286
287    // create POLYGON
288    {
289        // create Geometry object to store all the vetices and lines primtive.
290        osg::Geometry* polyGeom = new osg::Geometry();
291       
292        // this time we'll a C arrays to initilize the vertices.
293        // note, anticlockwsie ordering.
294        // note II, OpenGL polygons must be convex plan polygons, otherwise
295        // undefined results will occur.  If you have concave polygons or ones
296        // that cross over themselves then use the osgUtil::Tesselator to fix
297        // the polygons into a set of valid polygons.
298        osg::Vec3 myCoords[] =
299        {
300            osg::Vec3(-1.0464, 0.0f, -0.193626),
301            osg::Vec3(-1.0258, 0.0f, -0.26778),
302            osg::Vec3(-0.807461, 0.0f, -0.181267),
303            osg::Vec3(-0.766264, 0.0f, -0.0576758),
304            osg::Vec3(-0.980488, 0.0f, -0.094753)
305        };
306       
307        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
308       
309        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
310       
311        // pass the created vertex array to the points geometry object.
312        polyGeom->setVertexArray(vertices);
313       
314        // use the shared color array.
315        polyGeom->setColorArray(shared_colors.get());
316        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
317       
318
319        // use the shared normal array.
320        polyGeom->setNormalArray(shared_normals.get());
321        polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
322       
323
324        // This time we simply use primitive, and hardwire the number of coords to use
325        // since we know up front,
326        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,numCoords));
327
328        printTriangles("Polygon",*polyGeom);
329       
330        // add the points geomtry to the geode.
331        geode->addDrawable(polyGeom);
332    }
333
334
335    // create QUADS
336    {
337        // create Geometry object to store all the vetices and lines primtive.
338        osg::Geometry* polyGeom = new osg::Geometry();
339       
340        // note, anticlockwsie ordering.
341        osg::Vec3 myCoords[] =
342        {
343            osg::Vec3(0.0247182, 0.0f, -0.156548),
344            osg::Vec3(0.0247182, 0.0f, -0.00823939),
345            osg::Vec3(-0.160668, 0.0f, -0.0453167),
346            osg::Vec3(-0.222464, 0.0f, -0.13183),
347
348            osg::Vec3(0.238942, 0.0f, -0.251302),
349            osg::Vec3(0.333696, 0.0f, 0.0329576),
350            osg::Vec3(0.164788, 0.0f, -0.0453167),
351            osg::Vec3(0.13595,  0.0f, -0.255421)
352        };
353       
354        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
355       
356        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
357       
358        // pass the created vertex array to the points geometry object.
359        polyGeom->setVertexArray(vertices);
360       
361        // use the shared color array.
362        polyGeom->setColorArray(shared_colors.get());
363        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
364       
365
366        // use the shared normal array.
367        polyGeom->setNormalArray(shared_normals.get());
368        polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
369       
370
371        // This time we simply use primitive, and hardwire the number of coords to use
372        // since we know up front,
373        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,numCoords));
374       
375       
376        printTriangles("Quads",*polyGeom);
377
378        // add the points geomtry to the geode.
379        geode->addDrawable(polyGeom);
380    }
381
382    // create QUAD_STRIP
383    {
384        // create Geometry object to store all the vetices and lines primtive.
385        osg::Geometry* polyGeom = new osg::Geometry();
386       
387        // note, first coord at top, second at bottom, reverse to that buggy OpenGL image..
388        osg::Vec3 myCoords[] =
389        {
390            osg::Vec3(0.733306, -2.15188e-09, -0.0741545),
391            osg::Vec3(0.758024, -2.15188e-09, -0.205985),
392
393            osg::Vec3(0.885735, -2.15188e-09, -0.0576757),
394            osg::Vec3(0.885735, -2.15188e-09, -0.214224),
395
396            osg::Vec3(0.964009, 9.18133e-09, -0.0370773),
397            osg::Vec3(1.0464, 9.18133e-09, -0.173027),
398
399            osg::Vec3(1.11232, -2.15188e-09, 0.0123591),
400            osg::Vec3(1.12468, 9.18133e-09, -0.164788),
401        };
402       
403        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
404       
405        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
406       
407        // pass the created vertex array to the points geometry object.
408        polyGeom->setVertexArray(vertices);
409       
410        // use the shared color array.
411        polyGeom->setColorArray(shared_colors.get());
412        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
413       
414
415        // use the shared normal array.
416        polyGeom->setNormalArray(shared_normals.get());
417        polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
418       
419
420        // This time we simply use primitive, and hardwire the number of coords to use
421        // since we know up front,
422        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,numCoords));
423       
424       
425        printTriangles("Quads strip",*polyGeom);
426
427        // add the points geomtry to the geode.
428        geode->addDrawable(polyGeom);
429    }
430
431    // create TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN all in one Geometry/
432    {
433        // create Geometry object to store all the vetices and lines primtive.
434        osg::Geometry* polyGeom = new osg::Geometry();
435       
436        // note, first coord at top, second at bottom, reverse to that buggy OpenGL image..
437        osg::Vec3 myCoords[] =
438        {
439            // TRIANGLES 6 vertices, v0..v5
440            // note in aniclockwise order.
441            osg::Vec3(-1.12056, -2.15188e-09, -0.840418),
442            osg::Vec3(-0.95165, -2.15188e-09, -0.840418),
443            osg::Vec3(-1.11644, 9.18133e-09, -0.716827),
444
445            // note in aniclockwise order.
446            osg::Vec3(-0.840418, 9.18133e-09, -0.778623),
447            osg::Vec3(-0.622074, 9.18133e-09, -0.613835),
448            osg::Vec3(-1.067, 9.18133e-09, -0.609715),
449
450            // TRIANGLE STRIP 6 vertices, v6..v11
451            // note defined top point first,
452            // then anticlockwise for the next two points,
453            // then alternating to bottom there after.
454            osg::Vec3(-0.160668, -2.15188e-09, -0.531441),
455            osg::Vec3(-0.160668, -2.15188e-09, -0.749785),
456            osg::Vec3(0.0617955, 9.18133e-09, -0.531441),
457            osg::Vec3(0.168908, -2.15188e-09, -0.753905),
458            osg::Vec3(0.238942, -2.15188e-09, -0.531441),
459            osg::Vec3(0.280139, -2.15188e-09, -0.823939),
460
461            // TRIANGLE FAN 5 vertices, v12..v16
462            // note defined in anticlockwsie order.
463            osg::Vec3(0.844538, 9.18133e-09, -0.712708),
464            osg::Vec3(1.0258, 9.18133e-09, -0.799221),
465            osg::Vec3(1.03816, -2.15188e-09, -0.692109),
466            osg::Vec3(0.988727, 9.18133e-09, -0.568518),
467            osg::Vec3(0.840418, -2.15188e-09, -0.506723),
468
469        };
470       
471        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
472       
473        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
474       
475        // pass the created vertex array to the points geometry object.
476        polyGeom->setVertexArray(vertices);
477       
478        // use the shared color array.
479        polyGeom->setColorArray(shared_colors.get());
480        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
481       
482
483        // use the shared normal array.
484        polyGeom->setNormalArray(shared_normals.get());
485        polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
486       
487
488        // This time we simply use primitive, and hardwire the number of coords to use
489        // since we know up front,
490        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,6));
491        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,6,6));
492        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN,12,5));
493       
494        // polygon stipple
495        osg::StateSet* stateSet = new osg::StateSet();
496        polyGeom->setStateSet(stateSet);
497        osg::PolygonStipple* polygonStipple = new osg::PolygonStipple;
498        stateSet->setAttributeAndModes(polygonStipple,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
499       
500        printTriangles("Triangles/Strip/Fan",*polyGeom);
501
502        // add the points geomtry to the geode.
503        geode->addDrawable(polyGeom);
504    }
505   
506    return geode;   
507}
508
509
510// define a node callback to animation a transform as a cycle along the y axis, between 0 and 2.0.
511class MyTransformCallback : public osg::NodeCallback
512{
513
514    public:
515
516        MyTransformCallback(float angularVelocity)
517        {
518            _angular_velocity = angularVelocity;
519        }
520
521        virtual void operator() (osg::Node* node, osg::NodeVisitor* nv)
522        {
523            osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node);               
524            if (nv && transform && nv->getFrameStamp())
525            {
526                double time = nv->getFrameStamp()->getReferenceTime();
527                transform->setMatrix(osg::Matrix::translate(0.0f,1.0f+cosf(time*_angular_velocity),0.0f));
528            }
529           
530            // must continue subgraph traversal.
531            traverse(node,nv);           
532           
533        }
534       
535    protected:
536   
537        float               _angular_velocity;
538
539};
540
541
542osg::Node* createBackground()
543{   
544
545    // we'll create a texture mapped quad to sit behind the Geometry
546    osg::Image* image = osgDB::readImageFile("Images/primitives.gif");
547    if (!image) return NULL;
548   
549 
550    // create Geometry object to store all the vetices and lines primtive.
551    osg::Geometry* polyGeom = new osg::Geometry();
552
553    // note, anticlockwsie ordering.
554    osg::Vec3 myCoords[] =
555    {
556        osg::Vec3(-1.22908f,0.0f,1.0f),
557        osg::Vec3(-1.22908f,0.0f,-1.0f),
558        osg::Vec3(1.22908f,0.0f,-1.0f),
559        osg::Vec3(1.22908f,0.0f,1.0f)
560    };
561
562    int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
563
564    // pass the created vertex array to the points geometry object.
565    polyGeom->setVertexArray(new osg::Vec3Array(numCoords,myCoords));
566
567    osg::Vec4Array* colors = new osg::Vec4Array;
568    colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
569    polyGeom->setColorArray(colors);
570    polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
571
572
573    // set the normal in the same way color.
574    osg::Vec3Array* normals = new osg::Vec3Array;
575    normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
576    polyGeom->setNormalArray(normals);
577    polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
578
579    osg::Vec2 myTexCoords[] =
580    {
581        osg::Vec2(0,1),
582        osg::Vec2(0,0),
583        osg::Vec2(1,0),
584        osg::Vec2(1,1)
585    };
586
587    int numTexCoords = sizeof(myCoords)/sizeof(osg::Vec2);
588
589    // pass the created tex coord array to the points geometry object,
590    // and use it to set texture unit 0.
591    polyGeom->setTexCoordArray(0,new osg::Vec2Array(numTexCoords,myTexCoords));
592
593    // well use indices and DrawElements to define the primitive this time.
594    unsigned short myIndices[] =
595    {
596        0,
597        1,
598        2,
599        3
600    };
601
602    int numIndices = sizeof(myIndices)/sizeof(unsigned short);
603
604    // Theere are three variants of the DrawElements osg::Primitive, UByteDrawElements which
605    // contains unsigned char indicies, UShortDrawElements which contains unsigned short indices,
606    // and UIntDrawElements whcih contains ... unsigned int indices. 
607    // The first parameter to DrawElements is
608    polyGeom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::QUADS,numIndices,myIndices));
609
610    // new we need to add the texture to the Drawable, we do so by creating a
611    // StateSet to contain the Texture2D StateAttribute.
612    osg::StateSet* stateset = new osg::StateSet;
613
614    // set up the texture.
615    osg::Texture2D* texture = new osg::Texture2D;
616    texture->setImage(image);
617
618    stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
619
620    polyGeom->setStateSet(stateset);
621
622 
623    // create the Geode (Geometry Node) to contain all our osg::Geometry objects.
624    osg::Geode* geode = new osg::Geode();
625
626    // add the points geomtry to the geode.
627    geode->addDrawable(polyGeom);
628
629    //return geode;
630
631    // create a tranform to move the background back and forward with.
632 
633    osg::MatrixTransform* transform = new osg::MatrixTransform();
634    transform->setUpdateCallback(new MyTransformCallback(1.0f));
635    transform->addChild(geode);
636
637    return transform;
638}
639
640
641
642
643int main( int argc, char **argv )
644{
645    // use an ArgumentParser object to manage the program arguments.
646    osg::ArgumentParser arguments(&argc,argv);
647
648    // set up the usage document, in case we need to print out how to use this program.
649    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
650    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
651
652    // construct the viewer.
653    osgProducer::Viewer viewer(arguments);
654
655    // set up the value with sensible default event handlers.
656    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
657
658    // get details on keyboard and mouse bindings used by the viewer.
659    viewer.getUsage(*arguments.getApplicationUsage());
660
661    // if user request help write it out to cout.
662    if (arguments.read("-h") || arguments.read("--help"))
663    {
664        arguments.getApplicationUsage()->write(std::cout);
665        return 1;
666    }
667
668    // any option left unread are converted into errors to write out later.
669    arguments.reportRemainingOptionsAsUnrecognized();
670
671    // report any errors if they have occured when parsing the program aguments.
672    if (arguments.errors())
673    {
674        arguments.writeErrorMessages(std::cout);
675        return 1;
676    }
677   
678
679    // create the model
680    osg::Group* root = new osg::Group;
681    root->addChild( createScene() );
682    root->addChild( createBackground() );
683
684    //osgDB::writeNodeFile(*root,"geoemtry.osg");
685
686
687    // add model to viewer.
688    viewer.setSceneData( root );
689
690    // create the windows and run the threads.
691    viewer.realize();
692
693    while( !viewer.done() )
694    {
695        // wait for all cull and draw threads to complete.
696        viewer.sync();
697
698        // update the scene by traversing it with the the update visitor which will
699        // call all node update callbacks and animations.
700        viewer.update();
701         
702        // fire off the cull and draw traversals of the scene.
703        viewer.frame();
704       
705    }
706   
707    // wait for all cull and draw threads to complete before exit.
708    viewer.sync();
709
710    return 0;
711}
Note: See TracBrowser for help on using the browser.