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

Revision 5381, 16.9 kB (checked in by robert, 8 years ago)

Added viewer.cleanup_frame() to all examples.

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