root/OpenSceneGraph/trunk/examples/osgshape/osgshape.cpp @ 1815

Revision 1815, 4.3 kB (checked in by robert, 12 years ago)

Added the usage report to the examples for when no arguments are passed
to the examples.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Geode>
2#include <osg/ShapeDrawable>
3#include <osg/Material>
4#include <osg/Texture2D>
5
6#include <osgProducer/Viewer>
7
8#include <osgDB/ReadFile>
9
10#include <osg/Math>
11
12// for the grid data..
13#include "../osghangglide/terrain_coords.h"
14
15osg::Geode* createShapes()
16{
17    osg::Geode* geode = new osg::Geode();
18
19    // ---------------------------------------
20    // Set up a StateSet to texture the objects
21    // ---------------------------------------
22    osg::StateSet* stateset = new osg::StateSet();
23
24    osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
25    if (image)
26    {
27        osg::Texture2D* texture = new osg::Texture2D;
28        texture->setImage(image);
29        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
30    }
31   
32    geode->setStateSet( stateset );
33   
34    float radius = 0.8f;
35    float height = 1.0f;
36   
37    geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius)));
38    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),2*radius)));
39    geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height)));
40    geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height)));
41
42    osg::Grid* grid = new osg::Grid;
43    grid->allocateGrid(38,39);
44    grid->setXInterval(0.28f);
45    grid->setYInterval(0.28f);
46   
47    for(unsigned int r=0;r<39;++r)
48    {
49        for(unsigned int c=0;c<38;++c)
50        {
51            grid->setHeight(c,r,vertex[r+c*39][2]);
52        }
53    }
54    geode->addDrawable(new osg::ShapeDrawable(grid));
55   
56    osg::ConvexHull* mesh = new osg::ConvexHull;
57    osg::Vec3Array* vertices = new osg::Vec3Array(4);
58    (*vertices)[0].set(7.0+0.0f,-1.0f+2.0f,-1.0f+0.0f);
59    (*vertices)[1].set(7.0+1.0f,-1.0f+0.0f,-1.0f+0.0f);
60    (*vertices)[2].set(7.0+2.0f,-1.0f+2.0f,-1.0f+0.0f);
61    (*vertices)[3].set(7.0+1.0f,-1.0f+1.0f,-1.0f+2.0f);
62    osg::UByteArray* indices = new osg::UByteArray(12);
63    (*indices)[0]=0;
64    (*indices)[1]=2;
65    (*indices)[2]=1;
66    (*indices)[3]=0;
67    (*indices)[4]=1;
68    (*indices)[5]=3;
69    (*indices)[6]=1;
70    (*indices)[7]=2;
71    (*indices)[8]=3;
72    (*indices)[9]=2;
73    (*indices)[10]=0;
74    (*indices)[11]=3;
75    mesh->setVertices(vertices);
76    mesh->setIndices(indices);
77    geode->addDrawable(new osg::ShapeDrawable(mesh));
78   
79    return geode;
80}
81
82int main( int argc, char **argv )
83{
84
85    // use an ArgumentParser object to manage the program arguments.
86    osg::ArgumentParser arguments(&argc,argv);
87
88    // set up the usage document, in case we need to print out how to use this program.
89    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
90    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
91   
92    // construct the viewer.
93    osgProducer::Viewer viewer(arguments);
94
95    // set up the value with sensible default event handlers.
96    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
97
98    // get details on keyboard and mouse bindings used by the viewer.
99    viewer.getUsage(*arguments.getApplicationUsage());
100
101    // if user request help write it out to cout.
102    if (arguments.read("-h") || arguments.read("--help"))
103    {
104        arguments.getApplicationUsage()->write(std::cout);
105        return 1;
106    }
107
108    // any option left unread are converted into errors to write out later.
109    arguments.reportRemainingOptionsAsUnrecognized();
110
111    // report any errors if they have occured when parsing the program aguments.
112    if (arguments.errors())
113    {
114        arguments.writeErrorMessages(std::cout);
115        return 1;
116    }
117   
118    osg::Node* node = createShapes();
119
120    // add model to viewer.
121    viewer.setSceneData( node );
122
123    // create the windows and run the threads.
124    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
125
126    while( !viewer.done() )
127    {
128        // wait for all cull and draw threads to complete.
129        viewer.sync();
130
131        // update the scene by traversing it with the the update visitor which will
132        // call all node update callbacks and animations.
133        viewer.update();
134         
135        // fire off the cull and draw traversals of the scene.
136        viewer.frame();
137       
138    }
139   
140    // wait for all cull and draw threads to complete before exit.
141    viewer.sync();
142
143    return 0;
144}
Note: See TracBrowser for help on using the browser.