root/OpenSceneGraph/trunk/examples/osglogo/osglogo.cpp @ 7848

Revision 7848, 16.5 kB (checked in by robert, 6 years ago)

From Mike Weiblen, "an aesthetic enhancement for displaying important version information."

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osglogo.
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 <osg/Geometry>
24#include <osg/MatrixTransform>
25#include <osg/PositionAttitudeTransform>
26#include <osg/BlendFunc>
27#include <osg/ClearNode>
28#include <osg/Version>
29
30#include <osgUtil/Tessellator>
31#include <osgUtil/CullVisitor>
32
33#include <osgText/Text>
34
35#include <osgGA/TrackballManipulator>
36
37#include <osgViewer/Viewer>
38
39#include <osgDB/ReadFile>
40
41#include <iostream>
42
43class MyBillboardTransform : public osg::PositionAttitudeTransform
44{
45    public:
46   
47        MyBillboardTransform():
48            _axis(0.0f,0.0f,1.0f),
49            _normal(0.0f,-1.0f,0.0f)
50        {
51        }
52       
53        bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const
54        {
55            osg::Quat billboardRotation;
56            osgUtil::CullVisitor* cullvisitor = dynamic_cast<osgUtil::CullVisitor*>(nv);
57            if (cullvisitor)
58            {
59                osg::Vec3 eyevector = cullvisitor->getEyeLocal()-_position;
60                eyevector.normalize();
61
62                osg::Vec3 side = _axis^_normal;
63                side.normalize();
64
65                float angle = atan2(eyevector*_normal,eyevector*side);
66                billboardRotation.makeRotate(osg::PI_2-angle,_axis);
67
68            }
69
70
71            matrix.preMult(osg::Matrix::translate(-_pivotPoint)*
72                           osg::Matrix::rotate(_attitude)*
73                           osg::Matrix::rotate(billboardRotation)*
74                           osg::Matrix::translate(_position));
75            return true;
76        }
77
78
79       
80        void setAxis(const osg::Vec3& axis) { _axis = axis; }
81
82        void setNormal(const osg::Vec3& normal) { _normal = normal; }
83       
84    protected:
85   
86        virtual ~MyBillboardTransform() {}
87       
88        osg::Vec3 _axis;
89        osg::Vec3 _normal;
90};
91
92
93osg::Geometry* createWing(const osg::Vec3& left, const osg::Vec3& nose, const osg::Vec3& right,float chordRatio,const osg::Vec4& color)
94{
95    osg::Geometry* geom = new osg::Geometry;
96
97    osg::Vec3 normal = (nose-right)^(left-nose);
98    normal.normalize();
99
100    osg::Vec3 left_to_right = right-left;
101    osg::Vec3 mid = (right+left)*0.5f;
102    osg::Vec3 mid_to_nose = (nose-mid)*chordRatio*0.5f;
103   
104    osg::Vec3Array* vertices = new osg::Vec3Array;
105    vertices->push_back(left);
106    //vertices->push_back(mid+mid_to_nose);
107   
108    unsigned int noSteps = 40;
109    for(unsigned int i=1;i<noSteps;++i)
110    {
111        float ratio = (float)i/(float)noSteps;
112        vertices->push_back(left + left_to_right*ratio + mid_to_nose* (cosf((ratio-0.5f)*osg::PI*2.0f)+1.0f));
113    }
114   
115    vertices->push_back(right);
116    vertices->push_back(nose);
117
118    geom->setVertexArray(vertices);
119
120
121    osg::Vec3Array* normals = new osg::Vec3Array;
122    normals->push_back(normal);
123    geom->setNormalArray(normals);
124    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
125   
126   
127    osg::Vec4Array* colors = new osg::Vec4Array;
128    colors->push_back(color);
129    geom->setColorArray(colors);
130    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
131   
132
133    geom->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON,0,vertices->getNumElements()));
134   
135    osgUtil::Tessellator tessellator;
136    tessellator.retessellatePolygons(*geom);
137
138    return geom;
139   
140}
141
142osg:: Node* createTextBelow(const osg::BoundingBox& bb, const std::string& label, const std::string&)
143{
144    osg::Geode* geode = new osg::Geode();
145
146    std::string font("fonts/arial.ttf");
147
148    osgText::Text* text = new  osgText::Text;
149
150    text->setFont(font);
151    text->setFontResolution(64,64);
152    text->setAlignment(osgText::Text::CENTER_CENTER);
153    text->setAxisAlignment(osgText::Text::XZ_PLANE);
154    text->setPosition(bb.center()-osg::Vec3(0.0f,0.0f,(bb.zMax()-bb.zMin())));
155    text->setColor(osg::Vec4(0.37f,0.48f,0.67f,1.0f));
156    text->setText(label);
157
158    geode->addDrawable( text );
159
160    return geode;
161}
162
163osg:: Node* createTextLeft(const osg::BoundingBox& bb, const std::string& label, const std::string& subscript)
164{
165    osg::Geode* geode = new osg::Geode();
166
167
168    osg::StateSet* stateset = geode->getOrCreateStateSet();
169    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
170
171
172    //std::string font("fonts/times.ttf");
173    std::string font("fonts/arial.ttf");
174
175    osgText::Text* text = new  osgText::Text;
176 
177    text->setFont(font);
178    text->setFontResolution(110,120);
179    text->setAlignment(osgText::Text::RIGHT_CENTER);
180    text->setAxisAlignment(osgText::Text::XZ_PLANE);
181    text->setCharacterSize((bb.zMax()-bb.zMin())*1.0f);
182    text->setPosition(bb.center()-osg::Vec3((bb.xMax()-bb.xMin()),-(bb.yMax()-bb.yMin())*0.5f,(bb.zMax()-bb.zMin())*0.1f));
183    //text->setColor(osg::Vec4(0.37f,0.48f,0.67f,1.0f)); // Neil's original OSG colour
184    text->setColor(osg::Vec4(0.20f,0.45f,0.60f,1.0f)); // OGL logo colour
185    text->setText(label);
186
187#if 1
188    text->setBackdropType(osgText::Text::OUTLINE);
189//   text->setBackdropType(osgText::Text::DROP_SHADOW_BOTTOM_RIGHT);
190
191    text->setBackdropImplementation(osgText::Text::POLYGON_OFFSET);
192//    text->setBackdropImplementation(osgText::Text::NO_DEPTH_BUFFER);
193//    text->setBackdropImplementation(osgText::Text::DEPTH_RANGE);
194//    text->setBackdropImplementation(osgText::Text::STENCIL_BUFFER);
195   
196    text->setBackdropOffset(0.05f);
197    text->setBackdropColor(osg::Vec4(0.0f, 0.0f, 0.5f, 1.0f));
198#endif
199
200
201#if 1
202    text->setColorGradientMode(osgText::Text::OVERALL);
203    osg::Vec4 lightblue(0.30f,0.6f,0.90f,1.0f);
204    osg::Vec4 blue(0.10f,0.30f,0.40f,1.0f);
205    text->setColorGradientCorners(lightblue, blue, blue, lightblue);
206#else
207    text->setColorGradientMode(osgText::Text::OVERALL);
208    osg::Vec4 light = osg::Vec4(0.0f, 1.0f, 1.0f, 1.0f);
209    osg::Vec4 dark = osg::Vec4(0.0f, 0.0f, 0.5f, 1.0f);
210    text->setColorGradientCorners(light, dark, dark, light);
211//    text->setColorGradientCorners(dark, light, light, dark);
212#endif
213
214    geode->addDrawable( text );
215
216
217    if (!subscript.empty())
218    {
219        //osgText::Text* subscript = new  osgText::Text(new osgText::TextureFont(font,45));
220
221        osgText::Text* subscriptText = new osgText::Text;
222        subscriptText->setFont(font);
223        subscriptText->setText(subscript);
224        subscriptText->setAlignment(osgText::Text::RIGHT_CENTER);
225        subscriptText->setAxisAlignment(osgText::Text::XZ_PLANE);
226        subscriptText->setPosition(bb.center()-osg::Vec3((bb.xMax()-bb.xMin())*4.3f,-(bb.yMax()-bb.yMin())*0.5f,(bb.zMax()-bb.zMin())*0.6f));
227        subscriptText->setColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); // black
228
229        geode->addDrawable( subscriptText );
230    }
231       
232    return geode;
233}
234
235osg:: Node* createGlobe(const osg::BoundingBox& bb,float ratio, const std::string& filename)
236{
237    osg::MatrixTransform* xform = new osg::MatrixTransform;
238    xform->setUpdateCallback(new osg::AnimationPathCallback(bb.center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(10.0f)));
239
240    osg::Node* bluemarble = filename.empty() ? 0 : osgDB::readNodeFile(filename.c_str());
241    if (bluemarble)
242    {
243        const osg::BoundingSphere& bs = bluemarble->getBound();
244        float s = 1.2*bb.radius()/bs.radius();
245        osg::MatrixTransform* positioner = new osg::MatrixTransform;
246        positioner->setMatrix(osg::Matrix::translate(-bs.center())*osg::Matrix::scale(s,s,s)*osg::Matrix::translate(bb.center()));
247        positioner->addChild(bluemarble);
248   
249        xform->addChild(positioner);
250    }
251    else
252    {
253
254        osg::Geode* geode = new osg::Geode();
255
256        osg::StateSet* stateset = geode->getOrCreateStateSet();
257
258        osg::Image* image = osgDB::readImageFile("Images/land_shallow_topo_2048.jpg");
259        if (image)
260        {
261            osg::Texture2D* texture = new osg::Texture2D;
262            texture->setImage(image);
263            texture->setMaxAnisotropy(8);
264            stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
265        }
266
267        osg::Material* material = new osg::Material;
268        stateset->setAttribute(material);   
269
270        // the globe
271        geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(bb.center(),bb.radius()*ratio)));
272
273        xform->addChild(geode);
274    }
275           
276    return xform;
277}
278
279osg:: Node* createBox(const osg::BoundingBox& bb,float chordRatio)
280{
281    osg::Geode* geode = new osg::Geode();
282
283    osg::Vec4 white(1.0f,1.0f,1.0f,1.0f);
284
285    // front faces.
286    geode->addDrawable(createWing(bb.corner(4),bb.corner(6),bb.corner(7),chordRatio,white));
287    geode->addDrawable(createWing(bb.corner(7),bb.corner(5),bb.corner(4),chordRatio,white));
288
289    geode->addDrawable(createWing(bb.corner(4),bb.corner(5),bb.corner(1),chordRatio,white));
290    geode->addDrawable(createWing(bb.corner(1),bb.corner(0),bb.corner(4),chordRatio,white));
291   
292    geode->addDrawable(createWing(bb.corner(1),bb.corner(5),bb.corner(7),chordRatio,white));
293    geode->addDrawable(createWing(bb.corner(7),bb.corner(3),bb.corner(1),chordRatio,white));
294
295    // back faces
296    geode->addDrawable(createWing(bb.corner(2),bb.corner(0),bb.corner(1),chordRatio,white));
297    geode->addDrawable(createWing(bb.corner(1),bb.corner(3),bb.corner(2),chordRatio,white));
298
299    geode->addDrawable(createWing(bb.corner(2),bb.corner(3),bb.corner(7),chordRatio,white));
300    geode->addDrawable(createWing(bb.corner(7),bb.corner(6),bb.corner(2),chordRatio,white));
301
302    geode->addDrawable(createWing(bb.corner(2),bb.corner(6),bb.corner(4),chordRatio,white));
303    geode->addDrawable(createWing(bb.corner(4),bb.corner(0),bb.corner(2),chordRatio,white));
304
305    return geode;
306}
307
308osg:: Node* createBoxNo5(const osg::BoundingBox& bb,float chordRatio)
309{
310    osg::Geode* geode = new osg::Geode();
311
312    osg::Vec4 white(1.0f,1.0f,1.0f,1.0f);
313
314    // front faces.
315    geode->addDrawable(createWing(bb.corner(4),bb.corner(6),bb.corner(7),chordRatio,white));
316
317    geode->addDrawable(createWing(bb.corner(1),bb.corner(0),bb.corner(4),chordRatio,white));
318   
319    geode->addDrawable(createWing(bb.corner(7),bb.corner(3),bb.corner(1),chordRatio,white));
320
321    // back faces
322    geode->addDrawable(createWing(bb.corner(2),bb.corner(0),bb.corner(1),chordRatio,white));
323    geode->addDrawable(createWing(bb.corner(1),bb.corner(3),bb.corner(2),chordRatio,white));
324
325    geode->addDrawable(createWing(bb.corner(2),bb.corner(3),bb.corner(7),chordRatio,white));
326    geode->addDrawable(createWing(bb.corner(7),bb.corner(6),bb.corner(2),chordRatio,white));
327
328    geode->addDrawable(createWing(bb.corner(2),bb.corner(6),bb.corner(4),chordRatio,white));
329    geode->addDrawable(createWing(bb.corner(4),bb.corner(0),bb.corner(2),chordRatio,white));
330
331    return geode;
332}
333
334osg:: Node* createBoxNo5No2(const osg::BoundingBox& bb,float chordRatio)
335{
336    osg::Geode* geode = new osg::Geode();
337
338//    osg::Vec4 red(1.0f,0.0f,0.0f,1.0f);
339//    osg::Vec4 green(0.0f,1.0f,0.0f,1.0f);
340//    osg::Vec4 blue(0.0f,0.0f,1.0f,1.0f);
341
342    osg::Vec4 red(1.0f,0.12f,0.06f,1.0f);
343    osg::Vec4 green(0.21f,0.48f,0.03f,1.0f);
344    osg::Vec4 blue(0.20f,0.45f,0.60f,1.0f);
345
346    // front faces.
347    geode->addDrawable(createWing(bb.corner(4),bb.corner(6),bb.corner(7),chordRatio,red));
348
349    geode->addDrawable(createWing(bb.corner(1),bb.corner(0),bb.corner(4),chordRatio,green));
350   
351    geode->addDrawable(createWing(bb.corner(7),bb.corner(3),bb.corner(1),chordRatio,blue));
352
353    return geode;
354}
355
356osg:: Node* createBackdrop(const osg::Vec3& corner,const osg::Vec3& top,const osg::Vec3& right)
357{
358
359
360
361    osg::Geometry* geom = new osg::Geometry;
362
363    osg::Vec3 normal = (corner-top)^(right-corner);
364    normal.normalize();
365
366    osg::Vec3Array* vertices = new osg::Vec3Array;
367    vertices->push_back(top);
368    vertices->push_back(corner);
369
370    vertices->push_back(right);
371    vertices->push_back(right+(top-corner));
372
373    geom->setVertexArray(vertices);
374
375    osg::Vec3Array* normals = new osg::Vec3Array;
376    normals->push_back(normal);
377    geom->setNormalArray(normals);
378    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
379
380    osg::Vec4Array* colors = new osg::Vec4Array;
381    colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
382    geom->setColorArray(colors);
383    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
384
385    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,vertices->getNumElements()));
386
387    osg::Geode* geode = new osg::Geode();
388    geode->addDrawable(geom);
389   
390    return geode;   
391}
392
393osg::Node* createLogo(const std::string& filename, const std::string& label, const std::string& subscript)
394{
395    osg::BoundingBox bb(osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(100.0f,100.0f,100.0f));
396    float chordRatio = 0.5f;
397    float sphereRatio = 0.6f;
398
399    // create a group to hold the whole model.
400    osg::Group* logo_group = new osg::Group;
401
402    osg::Quat r1,r2;
403    r1.makeRotate(-osg::inDegrees(45.0f),0.0f,0.0f,1.0f);
404    r2.makeRotate(osg::inDegrees(45.0f),1.0f,0.0f,0.0f);
405
406
407    MyBillboardTransform* xform = new MyBillboardTransform;
408    xform->setPivotPoint(bb.center());
409    xform->setPosition(bb.center());
410    xform->setAttitude(r1*r2);
411
412
413//     // create a transform to orientate the box and globe.
414//     osg::MatrixTransform* xform = new osg::MatrixTransform;
415//     xform->setDataVariance(osg::Object::STATIC);
416//     xform->setMatrix(osg::Matrix::translate(-bb.center())*
417//                      osg::Matrix::rotate(-osg::inDegrees(45.0f),0.0f,0.0f,1.0f)*
418//                      osg::Matrix::rotate(osg::inDegrees(45.0f),1.0f,0.0f,0.0f)*
419//                      osg::Matrix::translate(bb.center()));
420
421    // add the box and globe to it.
422    //xform->addChild(createBox(bb,chordRatio));
423    //xform->addChild(createBoxNo5(bb,chordRatio));
424    xform->addChild(createBoxNo5No2(bb,chordRatio));
425    // add the transform to the group.
426    logo_group->addChild(xform);
427
428    logo_group->addChild(createGlobe(bb,sphereRatio,filename));
429
430    // add the text to the group.
431    //group->addChild(createTextBelow(bb));
432    logo_group->addChild(createTextLeft(bb, label, subscript));
433   
434   
435    // create the backdrop to render the shadow to.
436    osg::Vec3 corner(-900.0f,150.0f,-100.0f);
437    osg::Vec3 top(0.0f,0.0f,300.0f); top += corner;
438    osg::Vec3 right(1100.0f,0.0f,0.0f); right += corner;
439   
440   
441//     osg::Group* backdrop = new osg::Group;
442//     backdrop->addChild(createBackdrop(corner,top,right));
443
444    osg::ClearNode* backdrop = new osg::ClearNode;
445    backdrop->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,0.0f));
446
447    //osg::Vec3 lightPosition(-500.0f,-2500.0f,500.0f);
448    //osg::Node* scene = createShadowedScene(logo_group,backdrop,lightPosition,0.0f,0);
449
450    osg::Group* scene = new osg::Group;
451
452    osg::StateSet* stateset = scene->getOrCreateStateSet();
453    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
454
455
456    scene->addChild(logo_group);
457    scene->addChild(backdrop);
458
459    return scene;
460}
461
462int main( int argc, char **argv )
463{
464    // use an ArgumentParser object to manage the program arguments.
465    osg::ArgumentParser arguments(&argc,argv);
466   
467    osg::DisplaySettings::instance()->setMinimumNumAlphaBits(8);
468   
469    // construct the viewer.
470    osgViewer::Viewer viewer;
471
472    // if user request help write it out to cout.
473    if (arguments.read("-h") || arguments.read("--help"))
474    {
475        arguments.getApplicationUsage()->write(std::cout);
476        return 1;
477    }
478   
479    std::string label = "OpenSceneGraph";
480    std::string subscript = "";
481
482    bool showVersion = false;
483    while (arguments.read("--version")) { showVersion = true; }
484    if( showVersion )
485    {
486        label += " ";
487        label += osgGetVersion();
488    }
489
490    while (arguments.read("--label", label)) {}
491    while (arguments.read("--subscript", subscript)) {}
492   
493    osg::ref_ptr<osg::Node> node;
494   
495    if (arguments.argc()>1) node = createLogo(arguments[1], label, subscript);
496    else node = createLogo("", label, subscript);
497
498    // add model to viewer.
499    viewer.setSceneData( node.get() );
500
501    return viewer.run();
502}
Note: See TracBrowser for help on using the browser.