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

Revision 5927, 15.1 kB (checked in by robert, 7 years ago)

Ported following examples to osgViewer:

osggeodemo
osggeometry
osghud
osgimpostor
osgkeyboard
osglauncher
osglight
osglightpoint
osglogicop
osglogo
osgmovie
osgmultiplecameras
osgmultitexture
osgoccluder
osgparametric
osgparticle

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