root/OpenSceneGraph/trunk/examples/osgshadow/osgshadow.cpp @ 6237

Revision 6237, 23.3 kB (checked in by robert, 8 years ago)

Added -1, -2 and -3 model creation options to osgshadow example.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/ArgumentParser>
2#include <osg/ComputeBoundsVisitor>
3#include <osg/Texture2D>
4#include <osg/ShapeDrawable>
5#include <osg/MatrixTransform>
6
7#include <osgGA/TrackballManipulator>
8#include <osgGA/FlightManipulator>
9#include <osgGA/DriveManipulator>
10#include <osgGA/KeySwitchMatrixManipulator>
11#include <osgGA/AnimationPathManipulator>
12#include <osgGA/TerrainManipulator>
13#include <osgGA/AnimationPathManipulator>
14#include <osgGA/StateSetManipulator>
15
16#include <osgViewer/Viewer>
17#include <osgViewer/StatsHandler>
18
19#include <osgShadow/ShadowedScene>
20#include <osgShadow/ShadowVolume>
21#include <osgShadow/ShadowTexture>
22#include <osgShadow/ShadowMap>
23#include <osgShadow/ParallelSplitShadowMap>
24
25#include <osgDB/ReadFile>
26#include <osgDB/WriteFile>
27
28#include <iostream>
29
30// for the grid data..
31#include "../osghangglide/terrain_coords.h"
32
33const int RecievesShadowTraversalMask = 0x1;
34const int CastsShadowTraversalMask = 0x2;
35 
36namespace ModelOne
37{
38
39    enum Faces
40    {
41        FRONT_FACE = 1,
42        BACK_FACE = 2,
43        LEFT_FACE = 4,
44        RIGHT_FACE = 8,
45        TOP_FACE = 16,
46        BOTTOM_FACE = 32       
47    };
48
49    osg::Node* createCube(unsigned int mask)
50    {
51        osg::Geode* geode = new osg::Geode;
52
53        osg::Geometry* geometry = new osg::Geometry;
54        geode->addDrawable(geometry);
55
56        osg::Vec3Array* vertices = new osg::Vec3Array;
57        geometry->setVertexArray(vertices);
58
59        osg::Vec3Array* normals = new osg::Vec3Array;
60        geometry->setNormalArray(normals);
61        geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
62
63        osg::Vec4Array* colours = new osg::Vec4Array;
64        geometry->setColorArray(colours);
65        geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
66        colours->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
67
68
69        osg::Vec3 origin(0.0f,0.0f,0.0f);
70        osg::Vec3 dx(2.0f,0.0f,0.0f);
71        osg::Vec3 dy(0.0f,1.0f,0.0f);
72        osg::Vec3 dz(0.0f,0.0f,1.0f);
73
74        osg::Vec3 px(1.0f,0.0,0.0f);
75        osg::Vec3 nx(-1.0f,0.0,0.0f);
76        osg::Vec3 py(0.0f,1.0f,0.0f);
77        osg::Vec3 ny(0.0f,-1.0f,0.0f);
78        osg::Vec3 pz(0.0f,0.0f,1.0f);
79        osg::Vec3 nz(0.0f,0.0f,-1.0f);
80
81        if (mask & FRONT_FACE)
82        {
83            // front face   
84            vertices->push_back(origin);
85            vertices->push_back(origin+dx);
86            vertices->push_back(origin+dx+dz);
87            vertices->push_back(origin+dz);
88            normals->push_back(ny);
89            normals->push_back(ny);
90            normals->push_back(ny);
91            normals->push_back(ny);
92        }
93
94        if (mask & BACK_FACE)
95        {
96            // back face   
97            vertices->push_back(origin+dy);
98            vertices->push_back(origin+dy+dz);
99            vertices->push_back(origin+dy+dx+dz);
100            vertices->push_back(origin+dy+dx);
101            normals->push_back(py);
102            normals->push_back(py);
103            normals->push_back(py);
104            normals->push_back(py);
105        }
106
107        if (mask & LEFT_FACE)
108        {
109            // left face   
110            vertices->push_back(origin+dy);
111            vertices->push_back(origin);
112            vertices->push_back(origin+dz);
113            vertices->push_back(origin+dy+dz);
114            normals->push_back(nx);
115            normals->push_back(nx);
116            normals->push_back(nx);
117            normals->push_back(nx);
118        }
119
120        if (mask & RIGHT_FACE)
121        {
122            // right face   
123            vertices->push_back(origin+dx+dy);
124            vertices->push_back(origin+dx+dy+dz);
125            vertices->push_back(origin+dx+dz);
126            vertices->push_back(origin+dx);
127            normals->push_back(px);
128            normals->push_back(px);
129            normals->push_back(px);
130            normals->push_back(px);
131        }
132
133        if (mask & TOP_FACE)
134        {
135            // top face   
136            vertices->push_back(origin+dz);
137            vertices->push_back(origin+dz+dx);
138            vertices->push_back(origin+dz+dx+dy);
139            vertices->push_back(origin+dz+dy);
140            normals->push_back(pz);
141            normals->push_back(pz);
142            normals->push_back(pz);
143            normals->push_back(pz);
144        }
145
146        if (mask & BOTTOM_FACE)
147        {
148            // bottom face   
149            vertices->push_back(origin);
150            vertices->push_back(origin+dy);
151            vertices->push_back(origin+dx+dy);
152            vertices->push_back(origin+dx);
153            normals->push_back(nz);
154            normals->push_back(nz);
155            normals->push_back(nz);
156            normals->push_back(nz);
157        }
158
159        geometry->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, vertices->size()));
160
161        return geode;
162    }
163
164    class SwitchHandler : public osgGA::GUIEventHandler
165    {
166    public:
167
168        SwitchHandler():
169            _childNum(0) {}
170
171        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& /*aa*/, osg::Object* object, osg::NodeVisitor* /*nv*/)
172        {
173            osg::Switch* sw = dynamic_cast<osg::Switch*>(object);
174            if (!sw) return false;
175
176            if (ea.getHandled()) return false;
177
178            switch(ea.getEventType())
179            {
180                case(osgGA::GUIEventAdapter::KEYDOWN):
181                {
182                    if (ea.getKey()=='n')
183                    {
184                        ++_childNum;
185                        if (_childNum >= sw->getNumChildren()) _childNum = 0;
186
187                        sw->setSingleChildOn(_childNum);
188                        return true;
189                    }               
190                    break;
191                }
192                default:
193                    break;
194            }
195            return false;
196        }
197
198    protected:
199
200        virtual ~SwitchHandler() {}
201        unsigned int    _childNum;
202
203    };
204
205
206    osg::Node* createModel(osg::ArgumentParser& /*arguments*/)
207    {
208        osg::Switch* sw = new osg::Switch;
209        sw->setEventCallback(new ModelOne::SwitchHandler);
210
211        sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE), true);
212        sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE), false);
213        sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE), false);
214        sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE | ModelOne::RIGHT_FACE), false);
215        sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE | ModelOne::RIGHT_FACE | ModelOne::TOP_FACE), false);
216        sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE | ModelOne::RIGHT_FACE | ModelOne::TOP_FACE | ModelOne::BOTTOM_FACE), false);
217       
218        return sw;   
219    }
220}
221
222namespace ModelTwo
223{
224    osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
225    {
226        // set up the animation path
227        osg::AnimationPath* animationPath = new osg::AnimationPath;
228        animationPath->setLoopMode(osg::AnimationPath::LOOP);
229
230        int numSamples = 40;
231        float yaw = 0.0f;
232        float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
233        float roll = osg::inDegrees(30.0f);
234
235        double time=0.0f;
236        double time_delta = looptime/(double)numSamples;
237        for(int i=0;i<numSamples;++i)
238        {
239            osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
240            osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0)));
241
242            animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
243
244            yaw += yaw_delta;
245            time += time_delta;
246
247        }
248        return animationPath;   
249    }
250
251    osg::Node* createBase(const osg::Vec3& center,float radius)
252    {
253
254        osg::Geode* geode = new osg::Geode;
255
256        // set up the texture of the base.
257        osg::StateSet* stateset = new osg::StateSet();
258        osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
259        if (image)
260        {
261            osg::Texture2D* texture = new osg::Texture2D;
262            texture->setImage(image);
263            stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
264        }
265
266        geode->setStateSet( stateset );
267
268
269        osg::HeightField* grid = new osg::HeightField;
270        grid->allocate(38,39);
271        grid->setOrigin(center+osg::Vec3(-radius,-radius,0.0f));
272        grid->setXInterval(radius*2.0f/(float)(38-1));
273        grid->setYInterval(radius*2.0f/(float)(39-1));
274
275        float minHeight = FLT_MAX;
276        float maxHeight = -FLT_MAX;
277
278
279        unsigned int r;
280        for(r=0;r<39;++r)
281        {
282            for(unsigned int c=0;c<38;++c)
283            {
284                float h = vertex[r+c*39][2];
285                if (h>maxHeight) maxHeight=h;
286                if (h<minHeight) minHeight=h;
287            }
288        }
289
290        float hieghtScale = radius*0.5f/(maxHeight-minHeight);
291        float hieghtOffset = -(minHeight+maxHeight)*0.5f;
292
293        for(r=0;r<39;++r)
294        {
295            for(unsigned int c=0;c<38;++c)
296            {
297                float h = vertex[r+c*39][2];
298                grid->setHeight(c,r,(h+hieghtOffset)*hieghtScale);
299            }
300        }
301
302        geode->addDrawable(new osg::ShapeDrawable(grid));
303
304        osg::Group* group = new osg::Group;
305        group->addChild(geode);
306
307        return group;
308
309    }
310
311    osg::Node* createMovingModel(const osg::Vec3& center, float radius)
312    {
313        float animationLength = 10.0f;
314
315        osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);
316
317        osg::Group* model = new osg::Group;
318
319        osg::Node* cessna = osgDB::readNodeFile("cessna.osg");
320        if (cessna)
321        {
322            const osg::BoundingSphere& bs = cessna->getBound();
323
324            float size = radius/bs.radius()*0.3f;
325            osg::MatrixTransform* positioned = new osg::MatrixTransform;
326            positioned->setDataVariance(osg::Object::STATIC);
327            positioned->setMatrix(osg::Matrix::translate(-bs.center())*
328                                  osg::Matrix::scale(size,size,size)*
329                                  osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,2.0f));
330
331            positioned->addChild(cessna);
332
333            osg::MatrixTransform* xform = new osg::MatrixTransform;
334            xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
335            xform->addChild(positioned);
336
337            model->addChild(xform);
338        }
339
340        return model;
341    }
342
343    osg::Node* createModel(osg::ArgumentParser& /*arguments*/)
344    {
345        osg::Vec3 center(0.0f,0.0f,0.0f);
346        float radius = 100.0f;
347        osg::Vec3 lightPosition(center+osg::Vec3(0.0f,0.0f,radius));
348
349        // the shadower model
350        osg::Node* shadower = createMovingModel(center,radius*0.5f);
351        shadower->setNodeMask(CastsShadowTraversalMask);
352
353        // the shadowed model
354        osg::Node* shadowed = createBase(center-osg::Vec3(0.0f,0.0f,radius*0.25),radius);
355        shadowed->setNodeMask(RecievesShadowTraversalMask);
356       
357        osg::Group* group = new osg::Group;
358
359        group->addChild(shadowed);
360        group->addChild(shadower);
361       
362        return group;
363    }
364}
365
366namespace ModelThree
367{
368    osg::Group* createModel(osg::ArgumentParser& arguments)
369    {
370        osg::Group* scene = new osg::Group;
371
372        osg::ref_ptr<osg::Geode> geode_1 = new osg::Geode;
373        scene->addChild(geode_1.get());
374
375        osg::ref_ptr<osg::Geode> geode_2 = new osg::Geode;
376        osg::ref_ptr<osg::MatrixTransform> transform_2 = new osg::MatrixTransform;
377        transform_2->addChild(geode_2.get());
378        transform_2->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(0, 0, 0), osg::Z_AXIS, osg::inDegrees(45.0f)));
379        scene->addChild(transform_2.get());
380
381        osg::ref_ptr<osg::Geode> geode_3 = new osg::Geode;
382        osg::ref_ptr<osg::MatrixTransform> transform_3 = new osg::MatrixTransform;
383        transform_3->addChild(geode_3.get());
384        transform_3->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(0, 0, 0), osg::Z_AXIS, osg::inDegrees(-22.5f)));
385        scene->addChild(transform_3.get());
386
387        const float radius = 0.8f;
388        const float height = 1.0f;
389        osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
390        hints->setDetailRatio(2.0f);
391        osg::ref_ptr<osg::ShapeDrawable> shape;
392
393        shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, -2.0f), 10, 10.0f, 0.1f), hints.get());
394        shape->setColor(osg::Vec4(0.5f, 0.5f, 0.7f, 1.0f));
395        geode_1->addDrawable(shape.get());
396
397        shape = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), radius * 2), hints.get());
398        shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));
399        geode_1->addDrawable(shape.get());
400
401        shape = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-3.0f, 0.0f, 0.0f), radius), hints.get());
402        shape->setColor(osg::Vec4(0.6f, 0.8f, 0.8f, 1.0f));
403        geode_2->addDrawable(shape.get());
404
405        shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(3.0f, 0.0f, 0.0f), 2 * radius), hints.get());
406        shape->setColor(osg::Vec4(0.4f, 0.9f, 0.3f, 1.0f));
407        geode_2->addDrawable(shape.get());
408
409        shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f, -3.0f, 0.0f), radius, height), hints.get());
410        shape->setColor(osg::Vec4(0.2f, 0.5f, 0.7f, 1.0f));
411        geode_2->addDrawable(shape.get());
412
413        shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 3.0f, 0.0f), radius, height), hints.get());
414        shape->setColor(osg::Vec4(1.0f, 0.3f, 0.3f, 1.0f));
415        geode_2->addDrawable(shape.get());
416
417        shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, 3.0f), 2.0f, 2.0f, 0.1f), hints.get());
418        shape->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f));
419        geode_3->addDrawable(shape.get());
420
421        // material
422        osg::ref_ptr<osg::Material> matirial = new osg::Material;
423        matirial->setColorMode(osg::Material::DIFFUSE);
424        matirial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
425        matirial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
426        matirial->setShininess(osg::Material::FRONT_AND_BACK, 64.0f);
427        scene->getOrCreateStateSet()->setAttributeAndModes(matirial.get(), osg::StateAttribute::ON);
428
429        bool withBaseTexture = true;
430        while(arguments.read("--with-base-texture")) { withBaseTexture = true; }
431        while(arguments.read("--no-base-texture")) { withBaseTexture = false; }
432
433        if (withBaseTexture)
434        {
435            scene->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D(osgDB::readImageFile("Images/lz.rgb")), osg::StateAttribute::ON);
436        }
437
438        return scene;
439    }
440   
441}
442
443
444osg::Node* createTestModel(osg::ArgumentParser& arguments)
445{
446    if (arguments.read("-1"))
447    {
448        return ModelOne::createModel(arguments);
449    }
450    else if (arguments.read("-2"))
451    {
452        return ModelTwo::createModel(arguments);
453    }
454    else /*if (arguments.read("-3"))*/
455    {
456        return ModelThree::createModel(arguments);
457    }
458   
459}
460
461int main(int argc, char** argv)
462{
463    // use an ArgumentParser object to manage the program arguments.
464    osg::ArgumentParser arguments(&argc, argv);
465
466    // set up the usage document, in case we need to print out how to use this program.
467    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName() + " is the example which demonstrates using of GL_ARB_shadow extension implemented in osg::Texture class");
468    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName());
469    arguments.getApplicationUsage()->addCommandLineOption("-h or --help", "Display this information");
470    arguments.getApplicationUsage()->addCommandLineOption("--positionalLight", "Use a positional light.");
471    arguments.getApplicationUsage()->addCommandLineOption("--directionalLight", "Use a direction light.");
472    arguments.getApplicationUsage()->addCommandLineOption("--addOccluderToScene", "Add the occluders geometry.");
473    arguments.getApplicationUsage()->addCommandLineOption("--noUpdate", "Disable the updating the of light source.");
474    arguments.getApplicationUsage()->addCommandLineOption("--base", "Add a base geometry to test shadows.");
475    arguments.getApplicationUsage()->addCommandLineOption("--noShadow", "Disable the shadows.");
476    arguments.getApplicationUsage()->addCommandLineOption("--two-sided", "Use two-sided stencil extension for shadow volumes.");
477    arguments.getApplicationUsage()->addCommandLineOption("--two-pass", "Use two-pass stencil for shadow volumes.");
478
479    // hint to tell viewer to request stencil buffer when setting up windows
480    osg::DisplaySettings::instance()->setMinimumNumStencilBits(8);
481
482    // construct the viewer.
483    osgViewer::Viewer viewer;
484
485    // if user request help write it out to cout.
486    if (arguments.read("-h") || arguments.read("--help"))
487    {
488        arguments.getApplicationUsage()->write(std::cout);
489        return 1;
490    }
491
492    // default to single threaded during dev work.
493    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
494   
495    while (arguments.read("--SingleThreaded")) viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
496    while (arguments.read("--CullDrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
497    while (arguments.read("--DrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
498    while (arguments.read("--CullThreadPerCameraDrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
499
500
501    bool postionalLight = true;
502    while (arguments.read("--positionalLight")) postionalLight = true;
503    while (arguments.read("--directionalLight")) postionalLight = false;
504
505    bool updateLightPosition = true;
506    while (arguments.read("--noUpdate")) updateLightPosition = false;
507
508
509    int screenNum = -1;
510    while (arguments.read("--screen", screenNum)) viewer.setUpViewOnSingleScreen(screenNum);
511
512    // set up the camera manipulators.
513    {
514        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
515
516        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
517        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
518        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
519        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
520
521        std::string pathfile;
522        char keyForAnimationPath = '5';
523        while (arguments.read("-p",pathfile))
524        {
525            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
526            if (apm || !apm->valid())
527            {
528                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
529                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
530                keyswitchManipulator->selectMatrixManipulator(num);
531                ++keyForAnimationPath;
532            }
533        }
534
535        viewer.setCameraManipulator( keyswitchManipulator.get() );
536    }
537
538    // add the state manipulator
539    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
540
541    // add stats
542    viewer.addEventHandler( new osgViewer::StatsHandler() );
543
544    osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(arguments);
545    if (model.valid())
546    {
547        model->setNodeMask(CastsShadowTraversalMask | RecievesShadowTraversalMask);
548    }
549    else
550    {
551        model = createTestModel(arguments);
552    }
553
554    // get the bounds of the model.   
555    osg::ComputeBoundsVisitor cbbv;
556    model->accept(cbbv);
557    osg::BoundingBox bb = cbbv.getBoundingBox();
558
559    osg::Vec4 lightpos;
560   
561    if (postionalLight)
562    {
563        lightpos.set(bb.center().x(), bb.center().y(), bb.zMax() + bb.radius()  ,1.0f);
564    }
565    else
566    {
567        lightpos.set(0.5f,0.25f,0.8f,0.0f);
568    }
569
570
571    osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new osgShadow::ShadowedScene;
572   
573    shadowedScene->setRecievesShadowTraversalMask(RecievesShadowTraversalMask);
574    shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
575   
576   
577    if (arguments.read("--sv"))
578    {
579        osg::ref_ptr<osgShadow::ShadowVolume> sv = new osgShadow::ShadowVolume;
580        sv->setDynamicShadowVolumes(updateLightPosition);
581        while (arguments.read("--two-sided")) sv->setDrawMode(osgShadow::ShadowVolumeGeometry::STENCIL_TWO_SIDED);
582        while (arguments.read("--two-pass")) sv->setDrawMode(osgShadow::ShadowVolumeGeometry::STENCIL_TWO_PASS);
583
584        shadowedScene->setShadowTechnique(sv.get());
585    }
586    else if (arguments.read("--st"))
587    {
588        osg::ref_ptr<osgShadow::ShadowTexture> st = new osgShadow::ShadowTexture;
589        shadowedScene->setShadowTechnique(st.get());
590    }
591    else if (arguments.read("--pssm"))
592    {
593        osg::ref_ptr<osgShadow::ParallelSplitShadowMap> pssm = new osgShadow::ParallelSplitShadowMap;
594        shadowedScene->setShadowTechnique(pssm.get());
595    }
596    else /* if (arguments.read("--sm")) */
597    {
598        osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap;
599        shadowedScene->setShadowTechnique(sm.get());
600    }
601
602    if ( arguments.read("--base"))
603    {
604
605        osg::Geode* geode = new osg::Geode;
606       
607        osg::Vec3 widthVec(bb.radius(), 0.0f, 0.0f);
608        osg::Vec3 depthVec(0.0f, bb.radius(), 0.0f);
609        osg::Vec3 centerBase( (bb.xMin()+bb.xMax())*0.5f, (bb.yMin()+bb.yMax())*0.5f, bb.zMin()-bb.radius()*0.1f );
610       
611        geode->addDrawable( osg::createTexturedQuadGeometry( centerBase-widthVec*1.5f-depthVec*1.5f,
612                                                             widthVec*3.0f, depthVec*3.0f) );
613                                                             
614        geode->setNodeMask(shadowedScene->getRecievesShadowTraversalMask());
615       
616        geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readImageFile("Images/lz.rgb")));
617
618        shadowedScene->addChild(geode);
619    }
620
621    osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
622    ls->getLight()->setPosition(lightpos);
623    if ( arguments.read("--coloured-light"))
624    {
625        ls->getLight()->setAmbient(osg::Vec4(1.0,0.0,0.0,1.0));
626        ls->getLight()->setDiffuse(osg::Vec4(0.0,1.0,0.0,1.0));
627    }
628    else
629    {
630        ls->getLight()->setAmbient(osg::Vec4(0.2,0.2,0.2,1.0));
631        ls->getLight()->setDiffuse(osg::Vec4(0.8,0.8,0.8,1.0));
632    }
633   
634    shadowedScene->addChild(model.get());
635    shadowedScene->addChild(ls.get());
636   
637    viewer.setSceneData(shadowedScene.get());
638   
639    // create the windows and run the threads.
640    viewer.realize();
641
642    // osgDB::writeNodeFile(*group,"test.osg");
643
644    while (!viewer.done())
645    {
646        if (updateLightPosition)
647        {
648            float t = viewer.getFrameStamp()->getSimulationTime();
649            if (postionalLight)
650            {
651                lightpos.set(bb.center().x()+sinf(t)*bb.radius(), bb.center().y() + cosf(t)*bb.radius(), bb.zMax() + bb.radius()  ,1.0f);
652            }
653            else
654            {
655                lightpos.set(sinf(t),cosf(t),1.0f,0.0f);
656            }
657            ls->getLight()->setPosition(lightpos);
658        }
659
660        viewer.frame();
661    }
662   
663    return 0;
664}
Note: See TracBrowser for help on using the browser.