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

Revision 1984, 4.5 kB (checked in by robert, 11 years ago)

Additions since the CVS back up was made.

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