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

Revision 5297, 16.4 kB (checked in by robert, 8 years ago)

Removed the default loading of bluemarble.ive

  • 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->setBackdropOffset(0.05f);
171    text->setBackdropColor(osg::Vec4(0.0f, 0.0f, 0.5f, 1.0f));
172#endif
173
174
175#if 1
176    text->setColorGradientMode(osgText::Text::OVERALL);
177    osg::Vec4 lightblue(0.30f,0.6f,0.90f,1.0f);
178    osg::Vec4 blue(0.10f,0.30f,0.40f,1.0f);
179    text->setColorGradientCorners(lightblue, blue, blue, lightblue);
180#else
181    text->setColorGradientMode(osgText::Text::OVERALL);
182    osg::Vec4 light = osg::Vec4(0.0f, 1.0f, 1.0f, 1.0f);
183    osg::Vec4 dark = osg::Vec4(0.0f, 0.0f, 0.5f, 1.0f);
184    text->setColorGradientCorners(light, dark, dark, light);
185//    text->setColorGradientCorners(dark, light, light, dark);
186#endif
187
188    geode->addDrawable( text );
189
190
191    if (s_ProfessionalServices)
192    {
193        //osgText::Text* subscript = new  osgText::Text(new osgText::TextureFont(font,45));
194
195        osgText::Text* subscript = new osgText::Text;
196        subscript->setFont(font);
197        subscript->setText("Professional Services");
198        subscript->setAlignment(osgText::Text::RIGHT_CENTER);
199        subscript->setAxisAlignment(osgText::Text::XZ_PLANE);
200        subscript->setPosition(bb.center()-osg::Vec3((bb.xMax()-bb.xMin())*4.3f,-(bb.yMax()-bb.yMin())*0.5f,(bb.zMax()-bb.zMin())*0.6f));
201        subscript->setColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); // black
202
203        geode->addDrawable( subscript );
204    }
205       
206    return geode;
207}
208
209osg:: Node* createGlobe(const osg::BoundingBox& bb,float ratio, const std::string& filename)
210{
211    osg::MatrixTransform* xform = new osg::MatrixTransform;
212    xform->setUpdateCallback(new osg::AnimationPathCallback(bb.center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(10.0f)));
213
214    osg::Node* bluemarble = filename.empty() ? 0 : osgDB::readNodeFile(filename.c_str());
215    if (bluemarble)
216    {
217        const osg::BoundingSphere& bs = bluemarble->getBound();
218        float s = 1.2*bb.radius()/bs.radius();
219        osg::MatrixTransform* positioner = new osg::MatrixTransform;
220        positioner->setMatrix(osg::Matrix::translate(-bs.center())*osg::Matrix::scale(s,s,s)*osg::Matrix::translate(bb.center()));
221        positioner->addChild(bluemarble);
222   
223        xform->addChild(positioner);
224    }
225    else
226    {
227
228        osg::Geode* geode = new osg::Geode();
229
230        osg::StateSet* stateset = geode->getOrCreateStateSet();
231
232        osg::Image* image = osgDB::readImageFile("Images/land_shallow_topo_2048.jpg");
233        if (image)
234        {
235            osg::Texture2D* texture = new osg::Texture2D;
236            texture->setImage(image);
237            texture->setMaxAnisotropy(8);
238            stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
239        }
240
241        osg::Material* material = new osg::Material;
242        stateset->setAttribute(material);   
243
244        // the globe
245        geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(bb.center(),bb.radius()*ratio)));
246
247        xform->addChild(geode);
248    }
249           
250    return xform;
251}
252
253osg:: Node* createBox(const osg::BoundingBox& bb,float chordRatio)
254{
255    osg::Geode* geode = new osg::Geode();
256
257    osg::Vec4 white(1.0f,1.0f,1.0f,1.0f);
258
259    // front faces.
260    geode->addDrawable(createWing(bb.corner(4),bb.corner(6),bb.corner(7),chordRatio,white));
261    geode->addDrawable(createWing(bb.corner(7),bb.corner(5),bb.corner(4),chordRatio,white));
262
263    geode->addDrawable(createWing(bb.corner(4),bb.corner(5),bb.corner(1),chordRatio,white));
264    geode->addDrawable(createWing(bb.corner(1),bb.corner(0),bb.corner(4),chordRatio,white));
265   
266    geode->addDrawable(createWing(bb.corner(1),bb.corner(5),bb.corner(7),chordRatio,white));
267    geode->addDrawable(createWing(bb.corner(7),bb.corner(3),bb.corner(1),chordRatio,white));
268
269    // back faces
270    geode->addDrawable(createWing(bb.corner(2),bb.corner(0),bb.corner(1),chordRatio,white));
271    geode->addDrawable(createWing(bb.corner(1),bb.corner(3),bb.corner(2),chordRatio,white));
272
273    geode->addDrawable(createWing(bb.corner(2),bb.corner(3),bb.corner(7),chordRatio,white));
274    geode->addDrawable(createWing(bb.corner(7),bb.corner(6),bb.corner(2),chordRatio,white));
275
276    geode->addDrawable(createWing(bb.corner(2),bb.corner(6),bb.corner(4),chordRatio,white));
277    geode->addDrawable(createWing(bb.corner(4),bb.corner(0),bb.corner(2),chordRatio,white));
278
279    return geode;
280}
281
282osg:: Node* createBoxNo5(const osg::BoundingBox& bb,float chordRatio)
283{
284    osg::Geode* geode = new osg::Geode();
285
286    osg::Vec4 white(1.0f,1.0f,1.0f,1.0f);
287
288    // front faces.
289    geode->addDrawable(createWing(bb.corner(4),bb.corner(6),bb.corner(7),chordRatio,white));
290
291    geode->addDrawable(createWing(bb.corner(1),bb.corner(0),bb.corner(4),chordRatio,white));
292   
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* createBoxNo5No2(const osg::BoundingBox& bb,float chordRatio)
309{
310    osg::Geode* geode = new osg::Geode();
311
312//    osg::Vec4 red(1.0f,0.0f,0.0f,1.0f);
313//    osg::Vec4 green(0.0f,1.0f,0.0f,1.0f);
314//    osg::Vec4 blue(0.0f,0.0f,1.0f,1.0f);
315
316    osg::Vec4 red(1.0f,0.12f,0.06f,1.0f);
317    osg::Vec4 green(0.21f,0.48f,0.03f,1.0f);
318    osg::Vec4 blue(0.20f,0.45f,0.60f,1.0f);
319
320    // front faces.
321    geode->addDrawable(createWing(bb.corner(4),bb.corner(6),bb.corner(7),chordRatio,red));
322
323    geode->addDrawable(createWing(bb.corner(1),bb.corner(0),bb.corner(4),chordRatio,green));
324   
325    geode->addDrawable(createWing(bb.corner(7),bb.corner(3),bb.corner(1),chordRatio,blue));
326
327    return geode;
328}
329
330osg:: Node* createBackdrop(const osg::Vec3& corner,const osg::Vec3& top,const osg::Vec3& right)
331{
332
333
334
335    osg::Geometry* geom = new osg::Geometry;
336
337    osg::Vec3 normal = (corner-top)^(right-corner);
338    normal.normalize();
339
340    osg::Vec3Array* vertices = new osg::Vec3Array;
341    vertices->push_back(top);
342    vertices->push_back(corner);
343
344    vertices->push_back(right);
345    vertices->push_back(right+(top-corner));
346
347    geom->setVertexArray(vertices);
348
349    osg::Vec3Array* normals = new osg::Vec3Array;
350    normals->push_back(normal);
351    geom->setNormalArray(normals);
352    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
353
354    osg::Vec4Array* colors = new osg::Vec4Array;
355    colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
356    geom->setColorArray(colors);
357    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
358
359    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,vertices->getNumElements()));
360
361    osg::Geode* geode = new osg::Geode();
362    geode->addDrawable(geom);
363   
364    return geode;   
365}
366
367osg::Node* createLogo(const std::string& filename)
368{
369    osg::BoundingBox bb(osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(100.0f,100.0f,100.0f));
370    float chordRatio = 0.5f;
371    float sphereRatio = 0.6f;
372
373    // create a group to hold the whole model.
374    osg::Group* logo_group = new osg::Group;
375
376    osg::Quat r1,r2;
377    r1.makeRotate(-osg::inDegrees(45.0f),0.0f,0.0f,1.0f);
378    r2.makeRotate(osg::inDegrees(45.0f),1.0f,0.0f,0.0f);
379
380
381    MyBillboardTransform* xform = new MyBillboardTransform;
382    xform->setPivotPoint(bb.center());
383    xform->setPosition(bb.center());
384    xform->setAttitude(r1*r2);
385
386
387//     // create a transform to orientate the box and globe.
388//     osg::MatrixTransform* xform = new osg::MatrixTransform;
389//     xform->setDataVariance(osg::Object::STATIC);
390//     xform->setMatrix(osg::Matrix::translate(-bb.center())*
391//                      osg::Matrix::rotate(-osg::inDegrees(45.0f),0.0f,0.0f,1.0f)*
392//                      osg::Matrix::rotate(osg::inDegrees(45.0f),1.0f,0.0f,0.0f)*
393//                      osg::Matrix::translate(bb.center()));
394
395    // add the box and globe to it.
396    //xform->addChild(createBox(bb,chordRatio));
397    //xform->addChild(createBoxNo5(bb,chordRatio));
398    xform->addChild(createBoxNo5No2(bb,chordRatio));
399    // add the transform to the group.
400    logo_group->addChild(xform);
401
402    logo_group->addChild(createGlobe(bb,sphereRatio,filename));
403
404    // add the text to the group.
405    //group->addChild(createTextBelow(bb));
406    logo_group->addChild(createTextLeft(bb));
407   
408   
409    // create the backdrop to render the shadow to.
410    osg::Vec3 corner(-900.0f,150.0f,-100.0f);
411    osg::Vec3 top(0.0f,0.0f,300.0f); top += corner;
412    osg::Vec3 right(1100.0f,0.0f,0.0f); right += corner;
413   
414   
415//     osg::Group* backdrop = new osg::Group;
416//     backdrop->addChild(createBackdrop(corner,top,right));
417
418    osg::ClearNode* backdrop = new osg::ClearNode;
419    backdrop->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
420
421    //osg::Vec3 lightPosition(-500.0f,-2500.0f,500.0f);
422    //osg::Node* scene = createShadowedScene(logo_group,backdrop,lightPosition,0.0f,0);
423
424    osg::Group* scene = new osg::Group;
425
426    osg::StateSet* stateset = scene->getOrCreateStateSet();
427    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
428
429
430    scene->addChild(logo_group);
431    scene->addChild(backdrop);
432
433    return scene;
434}
435
436int main( int argc, char **argv )
437{
438
439    // use an ArgumentParser object to manage the program arguments.
440    osg::ArgumentParser arguments(&argc,argv);
441
442    // set up the usage document, in case we need to print out how to use this program.
443    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates both text, animation and billboard via custom transform to create the OpenSceneGraph logo..");
444   
445arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+"[options] [filename] ...");
446    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
447    arguments.getApplicationUsage()->addCommandLineOption("ps","Render the Professional Services logo");
448   
449    // construct the viewer.
450    osgProducer::Viewer viewer(arguments);
451
452    // set up the value with sensible default event handlers.
453    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
454
455    // get details on keyboard and mouse bindings used by the viewer.
456    viewer.getUsage(*arguments.getApplicationUsage());
457
458    // if user request help write it out to cout.
459    if (arguments.read("-h") || arguments.read("--help"))
460    {
461        arguments.getApplicationUsage()->write(std::cout);
462        return 1;
463    }
464   
465    while (arguments.read("ps")) s_ProfessionalServices = true;
466
467        // any option left unread are converted into errors to write out later.
468    arguments.reportRemainingOptionsAsUnrecognized();
469
470    // report any errors if they have occured when parsing the program aguments.
471    if (arguments.errors())
472    {
473        arguments.writeErrorMessages(std::cout);
474        return 1;
475    }
476   
477    osg::Node* node = 0;
478   
479    if (arguments.argc()>1) createLogo(arguments[1]);
480    else node = createLogo("");
481
482    // add model to viewer.
483    viewer.setSceneData( node );
484
485    // create the windows and run the threads.
486    viewer.realize();
487
488    while( !viewer.done() )
489    {
490        // wait for all cull and draw threads to complete.
491        viewer.sync();
492
493        // update the scene by traversing it with the the update visitor which will
494        // call all node update callbacks and animations.
495        viewer.update();
496         
497        // fire off the cull and draw traversals of the scene.
498        viewer.frame();
499       
500    }
501   
502    // wait for all cull and draw threads to complete before exit.
503    viewer.sync();
504
505    return 0;
506}
Note: See TracBrowser for help on using the browser.