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

Revision 6941, 3.7 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgshape.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Geode>
20#include <osg/ShapeDrawable>
21#include <osg/Material>
22#include <osg/Texture2D>
23
24#include <osgViewer/Viewer>
25
26#include <osgDB/ReadFile>
27
28#include <osg/Math>
29
30// for the grid data..
31#include "../osghangglide/terrain_coords.h"
32
33osg::Geode* createShapes()
34{
35    osg::Geode* geode = new osg::Geode();
36
37    // ---------------------------------------
38    // Set up a StateSet to texture the objects
39    // ---------------------------------------
40    osg::StateSet* stateset = new osg::StateSet();
41
42    osg::Image* image = osgDB::readImageFile( "Images/lz.rgb" );
43
44    if (image)
45    {
46        osg::Texture2D* texture = new osg::Texture2D;
47        texture->setImage(image);
48        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
49    }
50   
51    geode->setStateSet( stateset );
52   
53    float radius = 0.8f;
54    float height = 1.0f;
55   
56    osg::TessellationHints* hints = new osg::TessellationHints;
57    hints->setDetailRatio(0.5f);
58   
59    geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints));
60    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),2*radius),hints));
61    geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height),hints));
62    geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height),hints));
63    geode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(8.0f,0.0f,0.0f),radius,height),hints));
64
65    osg::HeightField* grid = new osg::HeightField;
66    grid->allocate(38,39);
67    grid->setXInterval(0.28f);
68    grid->setYInterval(0.28f);
69   
70    for(unsigned int r=0;r<39;++r)
71    {
72        for(unsigned int c=0;c<38;++c)
73        {
74            grid->setHeight(c,r,vertex[r+c*39][2]);
75        }
76    }
77    geode->addDrawable(new osg::ShapeDrawable(grid));
78   
79    osg::ConvexHull* mesh = new osg::ConvexHull;
80    osg::Vec3Array* vertices = new osg::Vec3Array(4);
81    (*vertices)[0].set(9.0+0.0f,-1.0f+2.0f,-1.0f+0.0f);
82    (*vertices)[1].set(9.0+1.0f,-1.0f+0.0f,-1.0f+0.0f);
83    (*vertices)[2].set(9.0+2.0f,-1.0f+2.0f,-1.0f+0.0f);
84    (*vertices)[3].set(9.0+1.0f,-1.0f+1.0f,-1.0f+2.0f);
85    osg::UByteArray* indices = new osg::UByteArray(12);
86    (*indices)[0]=0;
87    (*indices)[1]=2;
88    (*indices)[2]=1;
89    (*indices)[3]=0;
90    (*indices)[4]=1;
91    (*indices)[5]=3;
92    (*indices)[6]=1;
93    (*indices)[7]=2;
94    (*indices)[8]=3;
95    (*indices)[9]=2;
96    (*indices)[10]=0;
97    (*indices)[11]=3;
98    mesh->setVertices(vertices);
99    mesh->setIndices(indices);
100    geode->addDrawable(new osg::ShapeDrawable(mesh));
101   
102    return geode;
103}
104
105int main(int, char **)
106{
107    // construct the viewer.
108    osgViewer::Viewer viewer;
109
110    // add model to viewer.
111    viewer.setSceneData( createShapes() );
112
113    return viewer.run();
114}
Note: See TracBrowser for help on using the browser.