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

Revision 2767, 4.6 kB (checked in by robert, 10 years ago)

From David Jung, added support for osg::Capsule shape

  • 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    osg::TessellationHints* hints = new osg::TessellationHints;
38    hints->setDetailRatio(0.5f);
39   
40    geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints));
41    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),2*radius),hints));
42    geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height),hints));
43    geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height),hints));
44    geode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(8.0f,0.0f,0.0f),radius,height),hints));
45
46    osg::HeightField* grid = new osg::HeightField;
47    grid->allocateGrid(38,39);
48    grid->setXInterval(0.28f);
49    grid->setYInterval(0.28f);
50   
51    for(unsigned int r=0;r<39;++r)
52    {
53        for(unsigned int c=0;c<38;++c)
54        {
55            grid->setHeight(c,r,vertex[r+c*39][2]);
56        }
57    }
58    geode->addDrawable(new osg::ShapeDrawable(grid));
59   
60    osg::ConvexHull* mesh = new osg::ConvexHull;
61    osg::Vec3Array* vertices = new osg::Vec3Array(4);
62    (*vertices)[0].set(9.0+0.0f,-1.0f+2.0f,-1.0f+0.0f);
63    (*vertices)[1].set(9.0+1.0f,-1.0f+0.0f,-1.0f+0.0f);
64    (*vertices)[2].set(9.0+2.0f,-1.0f+2.0f,-1.0f+0.0f);
65    (*vertices)[3].set(9.0+1.0f,-1.0f+1.0f,-1.0f+2.0f);
66    osg::UByteArray* indices = new osg::UByteArray(12);
67    (*indices)[0]=0;
68    (*indices)[1]=2;
69    (*indices)[2]=1;
70    (*indices)[3]=0;
71    (*indices)[4]=1;
72    (*indices)[5]=3;
73    (*indices)[6]=1;
74    (*indices)[7]=2;
75    (*indices)[8]=3;
76    (*indices)[9]=2;
77    (*indices)[10]=0;
78    (*indices)[11]=3;
79    mesh->setVertices(vertices);
80    mesh->setIndices(indices);
81    geode->addDrawable(new osg::ShapeDrawable(mesh));
82   
83    return geode;
84}
85
86int main( int argc, char **argv )
87{
88
89    // use an ArgumentParser object to manage the program arguments.
90    osg::ArgumentParser arguments(&argc,argv);
91
92    // set up the usage document, in case we need to print out how to use this program.
93    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates the osg::Shape classes.");
94    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
95    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
96   
97    // construct the viewer.
98    osgProducer::Viewer viewer(arguments);
99
100    // set up the value with sensible default event handlers.
101    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
102
103    // get details on keyboard and mouse bindings used by the viewer.
104    viewer.getUsage(*arguments.getApplicationUsage());
105
106    // if user request help write it out to cout.
107    if (arguments.read("-h") || arguments.read("--help"))
108    {
109        arguments.getApplicationUsage()->write(std::cout);
110        return 1;
111    }
112
113    // any option left unread are converted into errors to write out later.
114    arguments.reportRemainingOptionsAsUnrecognized();
115
116    // report any errors if they have occured when parsing the program aguments.
117    if (arguments.errors())
118    {
119        arguments.writeErrorMessages(std::cout);
120        return 1;
121    }
122   
123    osg::Node* node = createShapes();
124
125    // add model to viewer.
126    viewer.setSceneData( node );
127
128    // create the windows and run the threads.
129    viewer.realize();
130
131    while( !viewer.done() )
132    {
133        // wait for all cull and draw threads to complete.
134        viewer.sync();
135
136        // update the scene by traversing it with the the update visitor which will
137        // call all node update callbacks and animations.
138        viewer.update();
139         
140        // fire off the cull and draw traversals of the scene.
141        viewer.frame();
142       
143    }
144   
145    // wait for all cull and draw threads to complete before exit.
146    viewer.sync();
147
148    return 0;
149}
Note: See TracBrowser for help on using the browser.