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

Revision 1697, 4.2 kB (checked in by robert, 12 years ago)

Ported osgGLUT based src/Demos across to being osgProducer based, and placed
them in the new examples/ directory.

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