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

Revision 6865, 23.8 kB (checked in by robert, 7 years ago)

Added #include<osg/Geometry>

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