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

Revision 12529, 25.9 kB (checked in by robert, 4 years ago)

Replaced .osg with .osgt file usage

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