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

Revision 10731, 3.9 kB (checked in by robert, 5 years ago)

Added enabling of lighting, and disabling of mipmapping to help out testing of GLES2 target

  • 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#include <osgUtil/ShaderGen>
24
25#include <osgViewer/Viewer>
26
27#include <osgDB/ReadFile>
28#include <osgDB/WriteFile>
29
30#include <osg/Math>
31
32// for the grid data..
33#include "../osghangglide/terrain_coords.h"
34
35osg::Geode* createShapes()
36{
37    osg::Geode* geode = new osg::Geode();
38
39   
40    // ---------------------------------------
41    // Set up a StateSet to texture the objects
42    // ---------------------------------------
43    osg::StateSet* stateset = new osg::StateSet();
44
45    osg::Image* image = osgDB::readImageFile( "Images/lz.rgb" );
46    if (image)
47    {
48        osg::Texture2D* texture = new osg::Texture2D;
49        texture->setImage(image);
50        texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
51        stateset->setTextureAttributeAndModes(0,texture, osg::StateAttribute::ON);
52    }
53   
54    stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON);
55   
56    geode->setStateSet( stateset );
57
58   
59    float radius = 0.8f;
60    float height = 1.0f;
61   
62    osg::TessellationHints* hints = new osg::TessellationHints;
63    hints->setDetailRatio(0.5f);
64   
65    geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints));
66    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),2*radius),hints));
67    geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height),hints));
68    geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height),hints));
69    geode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(8.0f,0.0f,0.0f),radius,height),hints));
70
71    osg::HeightField* grid = new osg::HeightField;
72    grid->allocate(38,39);
73    grid->setXInterval(0.28f);
74    grid->setYInterval(0.28f);
75   
76    for(unsigned int r=0;r<39;++r)
77    {
78        for(unsigned int c=0;c<38;++c)
79        {
80            grid->setHeight(c,r,vertex[r+c*39][2]);
81        }
82    }
83    geode->addDrawable(new osg::ShapeDrawable(grid));
84   
85    osg::ConvexHull* mesh = new osg::ConvexHull;
86    osg::Vec3Array* vertices = new osg::Vec3Array(4);
87    (*vertices)[0].set(9.0+0.0f,-1.0f+2.0f,-1.0f+0.0f);
88    (*vertices)[1].set(9.0+1.0f,-1.0f+0.0f,-1.0f+0.0f);
89    (*vertices)[2].set(9.0+2.0f,-1.0f+2.0f,-1.0f+0.0f);
90    (*vertices)[3].set(9.0+1.0f,-1.0f+1.0f,-1.0f+2.0f);
91    osg::UByteArray* indices = new osg::UByteArray(12);
92    (*indices)[0]=0;
93    (*indices)[1]=2;
94    (*indices)[2]=1;
95    (*indices)[3]=0;
96    (*indices)[4]=1;
97    (*indices)[5]=3;
98    (*indices)[6]=1;
99    (*indices)[7]=2;
100    (*indices)[8]=3;
101    (*indices)[9]=2;
102    (*indices)[10]=0;
103    (*indices)[11]=3;
104    mesh->setVertices(vertices);
105    mesh->setIndices(indices);
106    geode->addDrawable(new osg::ShapeDrawable(mesh));
107
108    return geode;
109}
110
111int main(int, char **)
112{
113    // construct the viewer.
114    osgViewer::Viewer viewer;
115
116    // add model to viewer.
117    viewer.setSceneData( createShapes() );
118
119    return viewer.run();
120}
Note: See TracBrowser for help on using the browser.