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

Revision 13574, 6.2 kB (checked in by robert, 94 minutes ago)

From Jannik Heller, "I noticed the rotation in the OrbitManipulator? depends on the framerate. To reproduce this issue, start the osganimate example, rotate the model with the left mouse button, then let go of the mouse button while still moving. You will notice that with V-Sync enabled, the model rotates slower.

The OrbitManipulator? calculates a scale to counteract the framerate dependency, but it turns out this scale wasn't used for the rotation yet."

  • 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, osg::Array::BIND_OVERALL);
64
65    osg::Vec2Array* tcoords = new osg::Vec2Array(4);
66    (*tcoords)[0].set(0.0f,0.0f);
67    (*tcoords)[1].set(1.0f,0.0f);
68    (*tcoords)[2].set(1.0f,1.0f);
69    (*tcoords)[3].set(0.0f,1.0f);
70    geom->setTexCoordArray(0,tcoords);
71
72    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
73
74    if (image)
75    {
76        osg::StateSet* stateset = new osg::StateSet;
77        osg::Texture2D* texture = new osg::Texture2D;
78        texture->setImage(image);
79        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
80        geom->setStateSet(stateset);
81    }
82
83    return geom;
84}
85
86osg::Drawable* createAxis(const osg::Vec3& corner,const osg::Vec3& xdir,const osg::Vec3& ydir,const osg::Vec3& zdir)
87{
88    // set up the Geometry.
89    osg::Geometry* geom = new osg::Geometry;
90
91    osg::Vec3Array* coords = new osg::Vec3Array(6);
92    (*coords)[0] = corner;
93    (*coords)[1] = corner+xdir;
94    (*coords)[2] = corner;
95    (*coords)[3] = corner+ydir;
96    (*coords)[4] = corner;
97    (*coords)[5] = corner+zdir;
98
99    geom->setVertexArray(coords);
100
101    osg::Vec4 x_color(0.0f,1.0f,1.0f,1.0f);
102    osg::Vec4 y_color(0.0f,1.0f,1.0f,1.0f);
103    osg::Vec4 z_color(1.0f,0.0f,0.0f,1.0f);
104
105    osg::Vec4Array* color = new osg::Vec4Array(6);
106    (*color)[0] = x_color;
107    (*color)[1] = x_color;
108    (*color)[2] = y_color;
109    (*color)[3] = y_color;
110    (*color)[4] = z_color;
111    (*color)[5] = z_color;
112
113    geom->setColorArray(color, osg::Array::BIND_PER_VERTEX);
114
115    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
116
117    osg::StateSet* stateset = new osg::StateSet;
118    osg::LineWidth* linewidth = new osg::LineWidth();
119    linewidth->setWidth(4.0f);
120    stateset->setAttributeAndModes(linewidth,osg::StateAttribute::ON);
121    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
122    geom->setStateSet(stateset);
123
124    return geom;
125}
126
127osg::Node* createModel()
128{
129
130    // create the root node which will hold the model.
131    osg::Group* root = new osg::Group();
132
133    // add the drawable into a single goede to be shared...
134    osg::Billboard* center = new osg::Billboard();
135    center->setMode(osg::Billboard::POINT_ROT_EYE);
136    center->addDrawable(
137        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")),
138        osg::Vec3(0.0f,0.0f,0.0f));
139
140    osg::Billboard* x_arrow = new osg::Billboard();
141    x_arrow->setMode(osg::Billboard::AXIAL_ROT);
142    x_arrow->setAxis(osg::Vec3(1.0f,0.0f,0.0f));
143    x_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f));
144    x_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/posx.png")),
146       osg::Vec3(5.0f,0.0f,0.0f));
147
148    osg::Billboard* y_arrow = new osg::Billboard();
149    y_arrow->setMode(osg::Billboard::AXIAL_ROT);
150    y_arrow->setAxis(osg::Vec3(0.0f,1.0f,0.0f));
151    y_arrow->setNormal(osg::Vec3(1.0f,0.0f,0.0f));
152    y_arrow->addDrawable(
153        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")),
154        osg::Vec3(0.0f,5.0f,0.0f));
155
156    osg::Billboard* z_arrow = new osg::Billboard();
157    z_arrow->setMode(osg::Billboard::AXIAL_ROT);
158    z_arrow->setAxis(osg::Vec3(0.0f,0.0f,1.0f));
159    z_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f));
160    z_arrow->addDrawable(
161        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")),
162        osg::Vec3(0.0f,0.0f,5.0f));
163
164
165
166    osg::Geode* axis = new osg::Geode();
167    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)));
168
169
170    root->addChild(center);
171    root->addChild(x_arrow);
172    root->addChild(y_arrow);
173    root->addChild(z_arrow);
174    root->addChild(axis);
175
176    return root;
177}
178
179int main(int, char**)
180{
181    // construct the viewer
182    osgViewer::Viewer viewer;
183
184    // set the scene to render
185    viewer.setSceneData(createModel());
186
187    // run the viewers frame loop
188    return viewer.run();
189}
Note: See TracBrowser for help on using the browser.