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

Revision 6941, 6.3 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, osgbillboard.
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/Node>
20#include <osg/Geometry>
21#include <osg/Notify>
22#include <osg/MatrixTransform>
23#include <osg/Texture2D>
24#include <osg/Billboard>
25#include <osg/LineWidth>
26
27#include <osgGA/TrackballManipulator>
28#include <osgGA/FlightManipulator>
29#include <osgGA/DriveManipulator>
30
31#include <osgDB/Registry>
32#include <osgDB/ReadFile>
33
34#include <osgViewer/Viewer>
35
36//
37// A simple demo demonstrating different texturing modes,
38// including using of texture extensions.
39//
40
41
42typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
43
44/** create quad at specified position. */
45osg::Drawable* createSquare(const osg::Vec3& corner,const osg::Vec3& width,const osg::Vec3& height, osg::Image* image=NULL)
46{
47    // set up the Geometry.
48    osg::Geometry* geom = new osg::Geometry;
49
50    osg::Vec3Array* coords = new osg::Vec3Array(4);
51    (*coords)[0] = corner;
52    (*coords)[1] = corner+width;
53    (*coords)[2] = corner+width+height;
54    (*coords)[3] = corner+height;
55
56
57    geom->setVertexArray(coords);
58
59    osg::Vec3Array* norms = new osg::Vec3Array(1);
60    (*norms)[0] = width^height;
61    (*norms)[0].normalize();
62   
63    geom->setNormalArray(norms);
64    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
65
66    osg::Vec2Array* tcoords = new osg::Vec2Array(4);
67    (*tcoords)[0].set(0.0f,0.0f);
68    (*tcoords)[1].set(1.0f,0.0f);
69    (*tcoords)[2].set(1.0f,1.0f);
70    (*tcoords)[3].set(0.0f,1.0f);
71    geom->setTexCoordArray(0,tcoords);
72   
73    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
74   
75    if (image)
76    {
77        osg::StateSet* stateset = new osg::StateSet;
78        osg::Texture2D* texture = new osg::Texture2D;
79        texture->setImage(image);
80        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
81        geom->setStateSet(stateset);
82    }
83   
84    return geom;
85}
86
87osg::Drawable* createAxis(const osg::Vec3& corner,const osg::Vec3& xdir,const osg::Vec3& ydir,const osg::Vec3& zdir)
88{
89    // set up the Geometry.
90    osg::Geometry* geom = new osg::Geometry;
91
92    osg::Vec3Array* coords = new osg::Vec3Array(6);
93    (*coords)[0] = corner;
94    (*coords)[1] = corner+xdir;
95    (*coords)[2] = corner;
96    (*coords)[3] = corner+ydir;
97    (*coords)[4] = corner;
98    (*coords)[5] = corner+zdir;
99
100    geom->setVertexArray(coords);
101
102    osg::Vec4 x_color(0.0f,1.0f,1.0f,1.0f);
103    osg::Vec4 y_color(0.0f,1.0f,1.0f,1.0f);
104    osg::Vec4 z_color(1.0f,0.0f,0.0f,1.0f);
105
106    osg::Vec4Array* color = new osg::Vec4Array(6);
107    (*color)[0] = x_color;
108    (*color)[1] = x_color;
109    (*color)[2] = y_color;
110    (*color)[3] = y_color;
111    (*color)[4] = z_color;
112    (*color)[5] = z_color;
113   
114    geom->setColorArray(color);
115    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
116   
117    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
118   
119    osg::StateSet* stateset = new osg::StateSet;
120    osg::LineWidth* linewidth = new osg::LineWidth();
121    linewidth->setWidth(4.0f);
122    stateset->setAttributeAndModes(linewidth,osg::StateAttribute::ON);
123    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
124    geom->setStateSet(stateset);
125   
126    return geom;
127}
128
129osg::Node* createModel()
130{
131
132    // create the root node which will hold the model.
133    osg::Group* root = new osg::Group();
134
135    // add the drawable into a single goede to be shared...
136    osg::Billboard* center = new osg::Billboard();
137    center->setMode(osg::Billboard::POINT_ROT_EYE);
138    center->addDrawable(
139        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")),
140        osg::Vec3(0.0f,0.0f,0.0f));
141       
142    osg::Billboard* x_arrow = new osg::Billboard();
143    x_arrow->setMode(osg::Billboard::AXIAL_ROT);
144    x_arrow->setAxis(osg::Vec3(1.0f,0.0f,0.0f));
145    x_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f));
146    x_arrow->addDrawable(
147       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")),
148       osg::Vec3(5.0f,0.0f,0.0f));
149
150    osg::Billboard* y_arrow = new osg::Billboard();
151    y_arrow->setMode(osg::Billboard::AXIAL_ROT);
152    y_arrow->setAxis(osg::Vec3(0.0f,1.0f,0.0f));
153    y_arrow->setNormal(osg::Vec3(1.0f,0.0f,0.0f));
154    y_arrow->addDrawable(
155        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")),
156        osg::Vec3(0.0f,5.0f,0.0f));
157
158    osg::Billboard* z_arrow = new osg::Billboard();
159    z_arrow->setMode(osg::Billboard::AXIAL_ROT);
160    z_arrow->setAxis(osg::Vec3(0.0f,0.0f,1.0f));
161    z_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f));
162    z_arrow->addDrawable(
163        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")),
164        osg::Vec3(0.0f,0.0f,5.0f));
165
166
167
168    osg::Geode* axis = new osg::Geode();
169    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)));
170
171
172    root->addChild(center);
173    root->addChild(x_arrow);
174    root->addChild(y_arrow);
175    root->addChild(z_arrow);
176    root->addChild(axis);
177
178    return root;
179}
180
181int main(int, char**)
182{
183    // construct the viewer
184    osgViewer::Viewer viewer;
185   
186    // set the scene to render
187    viewer.setSceneData(createModel());
188
189    // run the viewers frame loop
190    return viewer.run();
191}
Note: See TracBrowser for help on using the browser.