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

Revision 4805, 15.7 kB (checked in by robert, 9 years ago)

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