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

Revision 5959, 2.8 kB (checked in by robert, 8 years ago)

Ported follow examples to osgViewer:

osgshape
osgsimplifier
osgsimulation
osgslice
osgspacewarp
osgspheresegment
osgspotlight
osgstereoimage

  • 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 <osgViewer/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
26    if (image)
27    {
28        osg::Texture2D* texture = new osg::Texture2D;
29        texture->setImage(image);
30        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
31    }
32   
33    geode->setStateSet( stateset );
34   
35    float radius = 0.8f;
36    float height = 1.0f;
37   
38    osg::TessellationHints* hints = new osg::TessellationHints;
39    hints->setDetailRatio(0.5f);
40   
41    geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints));
42    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),2*radius),hints));
43    geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height),hints));
44    geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height),hints));
45    geode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(8.0f,0.0f,0.0f),radius,height),hints));
46
47    osg::HeightField* grid = new osg::HeightField;
48    grid->allocate(38,39);
49    grid->setXInterval(0.28f);
50    grid->setYInterval(0.28f);
51   
52    for(unsigned int r=0;r<39;++r)
53    {
54        for(unsigned int c=0;c<38;++c)
55        {
56            grid->setHeight(c,r,vertex[r+c*39][2]);
57        }
58    }
59    geode->addDrawable(new osg::ShapeDrawable(grid));
60   
61    osg::ConvexHull* mesh = new osg::ConvexHull;
62    osg::Vec3Array* vertices = new osg::Vec3Array(4);
63    (*vertices)[0].set(9.0+0.0f,-1.0f+2.0f,-1.0f+0.0f);
64    (*vertices)[1].set(9.0+1.0f,-1.0f+0.0f,-1.0f+0.0f);
65    (*vertices)[2].set(9.0+2.0f,-1.0f+2.0f,-1.0f+0.0f);
66    (*vertices)[3].set(9.0+1.0f,-1.0f+1.0f,-1.0f+2.0f);
67    osg::UByteArray* indices = new osg::UByteArray(12);
68    (*indices)[0]=0;
69    (*indices)[1]=2;
70    (*indices)[2]=1;
71    (*indices)[3]=0;
72    (*indices)[4]=1;
73    (*indices)[5]=3;
74    (*indices)[6]=1;
75    (*indices)[7]=2;
76    (*indices)[8]=3;
77    (*indices)[9]=2;
78    (*indices)[10]=0;
79    (*indices)[11]=3;
80    mesh->setVertices(vertices);
81    mesh->setIndices(indices);
82    geode->addDrawable(new osg::ShapeDrawable(mesh));
83   
84    return geode;
85}
86
87int main(int, char **)
88{
89    // construct the viewer.
90    osgViewer::Viewer viewer;
91
92    // add model to viewer.
93    viewer.setSceneData( createShapes() );
94
95    return viewer.run();
96}
Note: See TracBrowser for help on using the browser.