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

Revision 1844, 4.4 kB (checked in by robert, 11 years ago)

Added desciptions to all the demos.

  • 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()->setDescription(arguments.getApplicationName()+" is the example which demonstrates the osg::Shape classes.");
90    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
91    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
92   
93    // construct the viewer.
94    osgProducer::Viewer viewer(arguments);
95
96    // set up the value with sensible default event handlers.
97    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
98
99    // get details on keyboard and mouse bindings used by the viewer.
100    viewer.getUsage(*arguments.getApplicationUsage());
101
102    // if user request help write it out to cout.
103    if (arguments.read("-h") || arguments.read("--help"))
104    {
105        arguments.getApplicationUsage()->write(std::cout);
106        return 1;
107    }
108
109    // any option left unread are converted into errors to write out later.
110    arguments.reportRemainingOptionsAsUnrecognized();
111
112    // report any errors if they have occured when parsing the program aguments.
113    if (arguments.errors())
114    {
115        arguments.writeErrorMessages(std::cout);
116        return 1;
117    }
118   
119    osg::Node* node = createShapes();
120
121    // add model to viewer.
122    viewer.setSceneData( node );
123
124    // create the windows and run the threads.
125    viewer.realize();
126
127    while( !viewer.done() )
128    {
129        // wait for all cull and draw threads to complete.
130        viewer.sync();
131
132        // update the scene by traversing it with the the update visitor which will
133        // call all node update callbacks and animations.
134        viewer.update();
135         
136        // fire off the cull and draw traversals of the scene.
137        viewer.frame();
138       
139    }
140   
141    // wait for all cull and draw threads to complete before exit.
142    viewer.sync();
143
144    return 0;
145}
Note: See TracBrowser for help on using the browser.