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

Revision 5928, 24.7 kB (checked in by robert, 8 years ago)

From Paul Martz and Robert Osfield, renamed include/osgUtil/Tesselator and associated classes/references to Tessellator etc.

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