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

Revision 3701, 4.9 kB (checked in by robert, 10 years ago)

from Mike Weiblen, added wrap mode to planet texture to avoid black seam, and
add option of specifying the texture to use in osgshape.

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