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

Revision 5911, 5.3 kB (checked in by robert, 8 years ago)

Ported osgbillboard over to osgViewer

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