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

Revision 13574, 24.6 kB (checked in by robert, 13 hours ago)

From Jason Beverage, "It looks like the Callback header got accidentally removed from the CMakeLists.txt in the submission yesterday for the geometry instancing example."

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