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

Revision 1697, 7.0 kB (checked in by robert, 11 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/Node>
2#include <osg/Geometry>
3#include <osg/Notify>
4#include <osg/MatrixTransform>
5#include <osg/Texture2D>
6#include <osg/Billboard>
7#include <osg/LineWidth>
8
9#include <osgGA/TrackballManipulator>
10#include <osgGA/FlightManipulator>
11#include <osgGA/DriveManipulator>
12
13#include <osgDB/Registry>
14#include <osgDB/ReadFile>
15
16#include <osgProducer/Viewer>
17
18
19//
20// A simple demo demonstrating different texturing modes,
21// including using of texture extensions.
22//
23
24
25typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
26
27/** create quad at specified position. */
28osg::Drawable* createSquare(const osg::Vec3& corner,const osg::Vec3& width,const osg::Vec3& height, osg::Image* image=NULL)
29{
30    // set up the Geometry.
31    osg::Geometry* geom = new osg::Geometry;
32
33    osg::Vec3Array* coords = new osg::Vec3Array(4);
34    (*coords)[0] = corner;
35    (*coords)[1] = corner+width;
36    (*coords)[2] = corner+width+height;
37    (*coords)[3] = corner+height;
38
39
40    geom->setVertexArray(coords);
41
42    osg::Vec3Array* norms = new osg::Vec3Array(1);
43    (*norms)[0] = width^height;
44    (*norms)[0].normalize();
45   
46    geom->setNormalArray(norms);
47    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
48
49    osg::Vec2Array* tcoords = new osg::Vec2Array(4);
50    (*tcoords)[0].set(0.0f,0.0f);
51    (*tcoords)[1].set(1.0f,0.0f);
52    (*tcoords)[2].set(1.0f,1.0f);
53    (*tcoords)[3].set(0.0f,1.0f);
54    geom->setTexCoordArray(0,tcoords);
55   
56    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
57   
58    if (image)
59    {
60        osg::StateSet* stateset = new osg::StateSet;
61        osg::Texture2D* texture = new osg::Texture2D;
62        texture->setImage(image);
63        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
64        geom->setStateSet(stateset);
65    }
66   
67    return geom;
68}
69
70osg::Drawable* createAxis(const osg::Vec3& corner,const osg::Vec3& xdir,const osg::Vec3& ydir,const osg::Vec3& zdir)
71{
72    // set up the Geometry.
73    osg::Geometry* geom = new osg::Geometry;
74
75    osg::Vec3Array* coords = new osg::Vec3Array(6);
76    (*coords)[0] = corner;
77    (*coords)[1] = corner+xdir;
78    (*coords)[2] = corner;
79    (*coords)[3] = corner+ydir;
80    (*coords)[4] = corner;
81    (*coords)[5] = corner+zdir;
82
83    geom->setVertexArray(coords);
84
85    osg::Vec4 x_color(0.0f,1.0f,1.0f,1.0f);
86    osg::Vec4 y_color(0.0f,1.0f,1.0f,1.0f);
87    osg::Vec4 z_color(1.0f,0.0f,0.0f,1.0f);
88
89    osg::Vec4Array* color = new osg::Vec4Array(6);
90    (*color)[0] = x_color;
91    (*color)[1] = x_color;
92    (*color)[2] = y_color;
93    (*color)[3] = y_color;
94    (*color)[4] = z_color;
95    (*color)[5] = z_color;
96   
97    geom->setColorArray(color);
98    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
99   
100    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
101   
102    osg::StateSet* stateset = new osg::StateSet;
103    osg::LineWidth* linewidth = new osg::LineWidth();
104    linewidth->setWidth(4.0f);
105    stateset->setAttributeAndModes(linewidth,osg::StateAttribute::ON);
106    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
107    geom->setStateSet(stateset);
108   
109    return geom;
110}
111
112osg::Node* createModel()
113{
114
115    // create the root node which will hold the model.
116    osg::Group* root = new osg::Group();
117
118    // add the drawable into a single goede to be shared...
119    osg::Billboard* center = new osg::Billboard();
120    center->setMode(osg::Billboard::POINT_ROT_EYE);
121    center->addDrawable(
122        createSquare(osg::Vec3(-0.5f,0.0f,-0.5f),osg::Vec3(1.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Images/reflect.rgb")),
123        osg::Vec3(0.0f,0.0f,0.0f));
124       
125    osg::Billboard* x_arrow = new osg::Billboard();
126    x_arrow->setMode(osg::Billboard::AXIAL_ROT);
127    x_arrow->setAxis(osg::Vec3(1.0f,0.0f,0.0f));
128    x_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f));
129    x_arrow->addDrawable(
130       createSquare(osg::Vec3(-0.5f,0.0f,-0.5f),osg::Vec3(1.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Cubemap_axis/posx.png")),
131       osg::Vec3(5.0f,0.0f,0.0f));
132
133    osg::Billboard* y_arrow = new osg::Billboard();
134    y_arrow->setMode(osg::Billboard::AXIAL_ROT);
135    y_arrow->setAxis(osg::Vec3(0.0f,1.0f,0.0f));
136    y_arrow->setNormal(osg::Vec3(1.0f,0.0f,0.0f));
137    y_arrow->addDrawable(
138        createSquare(osg::Vec3(0.0f,-0.5f,-0.5f),osg::Vec3(0.0f,1.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Cubemap_axis/posy.png")),
139        osg::Vec3(0.0f,5.0f,0.0f));
140
141    osg::Billboard* z_arrow = new osg::Billboard();
142    z_arrow->setMode(osg::Billboard::AXIAL_ROT);
143    z_arrow->setAxis(osg::Vec3(0.0f,0.0f,1.0f));
144    z_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f));
145    z_arrow->addDrawable(
146        createSquare(osg::Vec3(-0.5f,0.0f,-0.5f),osg::Vec3(1.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Cubemap_axis/posz.png")),
147        osg::Vec3(0.0f,0.0f,5.0f));
148
149
150
151    osg::Geode* axis = new osg::Geode();
152    axis->addDrawable(createAxis(osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(5.0f,0.0f,0.0f),osg::Vec3(0.0f,5.0f,0.0f),osg::Vec3(0.0f,0.0f,5.0f)));
153
154
155    root->addChild(center);
156    root->addChild(x_arrow);
157    root->addChild(y_arrow);
158    root->addChild(z_arrow);
159    root->addChild(axis);
160
161    return root;
162}
163
164int main( int argc, char **argv )
165{
166
167    // use an ArgumentParser object to manage the program arguments.
168    osg::ArgumentParser arguments(&argc,argv);
169
170    // set up the usage document, in case we need to print out how to use this program.
171    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ...");
172    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
173   
174    // initialize the viewer.
175    osgProducer::Viewer viewer(arguments);
176
177    // set up the value with sensible default event handlers.
178    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
179
180    // get details on keyboard and mouse bindings used by the viewer.
181    viewer.getUsage(*arguments.getApplicationUsage());
182
183    // if user request help write it out to cout.
184    if (arguments.read("-h") || arguments.read("--help"))
185    {
186        arguments.getApplicationUsage()->write(std::cout);
187        return 1;
188    }
189
190    // any option left unread are converted into errors to write out later.
191    arguments.reportRemainingOptionsAsUnrecognized();
192
193    // report any errors if they have occured when parsing the program aguments.
194    if (arguments.errors())
195    {
196        arguments.writeErrorMessages(std::cout);
197        return 1;
198    }
199
200    // create a model from the images.
201    osg::Node* rootNode = createModel();
202
203    // set the scene to render
204    viewer.setSceneData(rootNode);
205
206    // create the windows and run the threads.
207    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
208
209    while( !viewer.done() )
210    {
211        // wait for all cull and draw threads to complete.
212        viewer.sync();
213
214        // update the scene by traversing it with the the update visitor which will
215        // call all node update callbacks and animations.
216        viewer.update();
217         
218        // fire off the cull and draw traversals of the scene.
219        viewer.frame();
220       
221    }
222   
223    return 0;
224}
Note: See TracBrowser for help on using the browser.