~ubuntu-branches/ubuntu/trusty/openscenegraph/trusty

« back to all changes in this revision

Viewing changes to OpenSceneGraph/examples/osgshadow/osgshadow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-07-29 04:34:38 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 lenny)
  • Revision ID: james.westby@ubuntu.com-20080729043438-no1h9h0dpsrlzp1y
* Non-maintainer upload.
* No longer try to detect (using /proc/cpuinfo when available) how many
  CPUs are available, fixing the FTBFS (due to -j0) on various platforms
  (Closes: #477353). The right way to do it is to support parallel=n in
  DEB_BUILD_OPTIONS (see Debian Policy §4.9.1), and adequate support has
  been implemented.
* Add patch to fix FTBFS due to the build system now refusing to handle
  whitespaces (Policy CMP0004 say the logs), thanks to Andreas Putzo who
  provided it (Closes: #482239):
   - debian/patches/fix-cmp0004-build-failure.dpatch
* Remove myself from Uploaders, as requested a while ago, done by Luk in
  his 2.2.0-2.1 NMU, which was never acknowledged.

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
 
53
53
const int ReceivesShadowTraversalMask = 0x1;
54
54
const int CastsShadowTraversalMask = 0x2;
55
 
  
 
55
 
56
56
namespace ModelOne
57
57
{
58
58
 
63
63
        LEFT_FACE = 4,
64
64
        RIGHT_FACE = 8,
65
65
        TOP_FACE = 16,
66
 
        BOTTOM_FACE = 32        
 
66
        BOTTOM_FACE = 32
67
67
    };
68
68
 
69
69
    osg::Node* createCube(unsigned int mask)
100
100
 
101
101
        if (mask & FRONT_FACE)
102
102
        {
103
 
            // front face    
 
103
            // front face
104
104
            vertices->push_back(origin);
105
105
            vertices->push_back(origin+dx);
106
106
            vertices->push_back(origin+dx+dz);
113
113
 
114
114
        if (mask & BACK_FACE)
115
115
        {
116
 
            // back face    
 
116
            // back face
117
117
            vertices->push_back(origin+dy);
118
118
            vertices->push_back(origin+dy+dz);
119
119
            vertices->push_back(origin+dy+dx+dz);
126
126
 
127
127
        if (mask & LEFT_FACE)
128
128
        {
129
 
            // left face    
 
129
            // left face
130
130
            vertices->push_back(origin+dy);
131
131
            vertices->push_back(origin);
132
132
            vertices->push_back(origin+dz);
139
139
 
140
140
        if (mask & RIGHT_FACE)
141
141
        {
142
 
            // right face    
 
142
            // right face
143
143
            vertices->push_back(origin+dx+dy);
144
144
            vertices->push_back(origin+dx+dy+dz);
145
145
            vertices->push_back(origin+dx+dz);
152
152
 
153
153
        if (mask & TOP_FACE)
154
154
        {
155
 
            // top face    
 
155
            // top face
156
156
            vertices->push_back(origin+dz);
157
157
            vertices->push_back(origin+dz+dx);
158
158
            vertices->push_back(origin+dz+dx+dy);
165
165
 
166
166
        if (mask & BOTTOM_FACE)
167
167
        {
168
 
            // bottom face    
 
168
            // bottom face
169
169
            vertices->push_back(origin);
170
170
            vertices->push_back(origin+dy);
171
171
            vertices->push_back(origin+dx+dy);
206
206
 
207
207
                        sw->setSingleChildOn(_childNum);
208
208
                        return true;
209
 
                    }                
 
209
                    }
210
210
                    break;
211
211
                }
212
212
                default:
234
234
        sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE | ModelOne::RIGHT_FACE), false);
235
235
        sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE | ModelOne::RIGHT_FACE | ModelOne::TOP_FACE), false);
236
236
        sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE | ModelOne::RIGHT_FACE | ModelOne::TOP_FACE | ModelOne::BOTTOM_FACE), false);
237
 
        
238
 
        return sw;    
 
237
 
 
238
        return sw;
239
239
    }
240
240
}
241
241
 
243
243
{
244
244
    osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
245
245
    {
246
 
        // set up the animation path 
 
246
        // set up the animation path
247
247
        osg::AnimationPath* animationPath = new osg::AnimationPath;
248
248
        animationPath->setLoopMode(osg::AnimationPath::LOOP);
249
249
 
265
265
            time += time_delta;
266
266
 
267
267
        }
268
 
        return animationPath;    
 
268
        return animationPath;
269
269
    }
270
270
 
271
271
    osg::Node* createBase(const osg::Vec3& center,float radius)
373
373
        // the shadowed model
374
374
        osg::Node* shadowed = createBase(center-osg::Vec3(0.0f,0.0f,radius*0.25),radius);
375
375
        shadowed->setNodeMask(ReceivesShadowTraversalMask);
376
 
        
 
376
 
377
377
        osg::Group* group = new osg::Group;
378
378
 
379
379
        group->addChild(shadowed);
380
380
        group->addChild(shadower);
381
 
        
 
381
 
382
382
        return group;
383
383
    }
384
384
}
457
457
 
458
458
        return scene;
459
459
    }
460
 
    
 
460
 
461
461
}
462
462
 
463
463
 
475
475
    {
476
476
        return ModelThree::createModel(arguments);
477
477
    }
478
 
    
 
478
 
479
479
}
480
480
 
481
481
int main(int argc, char** argv)
502
502
    arguments.getApplicationUsage()->addCommandLineOption("--minNearSplit", "ParallelSplitShadowMap shadow map near offset.");//ADEGLI
503
503
    arguments.getApplicationUsage()->addCommandLineOption("--maxFarDist", "ParallelSplitShadowMap max far distance to shadow.");//ADEGLI
504
504
    arguments.getApplicationUsage()->addCommandLineOption("--moveVCamFactor", "ParallelSplitShadowMap move the virtual frustum behind the real camera, (also back ground object can cast shadow).");//ADEGLI
505
 
    arguments.getApplicationUsage()->addCommandLineOption("--NVidea", "ParallelSplitShadowMap set default PolygonOffset for NVidea.");//ADEGLI
 
505
    arguments.getApplicationUsage()->addCommandLineOption("--NVidia", "ParallelSplitShadowMap set default PolygonOffset for NVidia.");//ADEGLI
506
506
    arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Factor", "ParallelSplitShadowMap set PolygonOffset factor.");//ADEGLI
507
507
    arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Unit", "ParallelSplitShadowMap set PolygonOffset unit.");//ADEGLI
508
508
    arguments.getApplicationUsage()->addCommandLineOption("--CullFaceFront", "ParallelSplitShadowMap add a cull face: front.");//ADEGLI
509
509
 
510
 
    
 
510
 
511
511
    arguments.getApplicationUsage()->addCommandLineOption("-1", "Use test model one.");
512
512
    arguments.getApplicationUsage()->addCommandLineOption("-2", "Use test model two.");
513
513
    arguments.getApplicationUsage()->addCommandLineOption("-3", "Use test model three.");
514
514
    arguments.getApplicationUsage()->addCommandLineOption("--two-sided", "Use two-sided stencil extension for shadow volumes.");
515
515
    arguments.getApplicationUsage()->addCommandLineOption("--two-pass", "Use two-pass stencil for shadow volumes.");
516
516
 
517
 
 
518
517
    // construct the viewer.
519
518
    osgViewer::Viewer viewer(arguments);
520
519
 
525
524
        return 1;
526
525
    }
527
526
 
528
 
    // default to single threaded during dev work.
529
 
    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
530
 
    
531
 
    while (arguments.read("--SingleThreaded")) viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
532
 
    while (arguments.read("--CullDrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
533
 
    while (arguments.read("--DrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
534
 
    while (arguments.read("--CullThreadPerCameraDrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
535
 
 
536
 
 
537
527
    bool postionalLight = true;
538
528
    while (arguments.read("--positionalLight")) postionalLight = true;
539
529
    while (arguments.read("--directionalLight")) postionalLight = false;
541
531
    bool updateLightPosition = true;
542
532
    while (arguments.read("--noUpdate")) updateLightPosition = false;
543
533
 
544
 
 
545
 
    int screenNum = -1;
546
 
    while (arguments.read("--screen", screenNum)) viewer.setUpViewOnSingleScreen(screenNum);
547
 
 
548
534
    // set up the camera manipulators.
549
535
    {
550
536
        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
559
545
        while (arguments.read("-p",pathfile))
560
546
        {
561
547
            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
562
 
            if (apm || !apm->valid()) 
 
548
            if (apm || !apm->valid())
563
549
            {
564
550
                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
565
551
                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
580
566
    // add the record camera path handler
581
567
    viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
582
568
 
 
569
    // add the threading handler
 
570
    viewer.addEventHandler( new osgViewer::ThreadingHandler() );
583
571
 
584
572
    osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new osgShadow::ShadowedScene;
585
 
    
 
573
 
586
574
    shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
587
575
    shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
588
 
    
589
 
    
 
576
 
590
577
    if (arguments.read("--sv"))
591
578
    {
592
579
        // hint to tell viewer to request stencil buffer when setting up windows
611
598
        osg::ref_ptr<osgShadow::ParallelSplitShadowMap> pssm = new osgShadow::ParallelSplitShadowMap(NULL,mapcount);
612
599
 
613
600
        int mapres = 1024;
614
 
        while (arguments.read("--mapres", mapres)) 
 
601
        while (arguments.read("--mapres", mapres))
615
602
            pssm->setTextureResolution(mapres);
616
 
        
 
603
 
617
604
        while (arguments.read("--debug-color")) { pssm->setDebugColorOn(); }
618
 
         
 
605
 
619
606
 
620
607
        int minNearSplit=0;
621
608
        while (arguments.read("--minNearSplit", minNearSplit))
637
624
                pssm->setMoveVCamBehindRCamFactor(moveVCamFactor);
638
625
                std::cout << "ParallelSplitShadowMap : setMoveVCamBehindRCamFactor(" << moveVCamFactor<<")" << std::endl;
639
626
            }
640
 
            
 
627
 
641
628
 
642
629
        double polyoffsetfactor = -0.02;
643
630
        double polyoffsetunit = 1.0;
645
632
        while (arguments.read("--PolyOffset-Unit", polyoffsetunit));
646
633
        pssm->setPolygonOffset(osg::Vec2(polyoffsetfactor,polyoffsetunit)); //ATI Radeon
647
634
 
648
 
        if (arguments.read("--NVidea")){
 
635
        if (arguments.read("--NVidia")){
649
636
            //pssm->setPolygonOffset(osg::Vec2(-0.02,1.0)); //ATI Radeon
650
 
            pssm->setPolygonOffset(osg::Vec2(10.0f,20.0f)); //NVidea 
 
637
            pssm->setPolygonOffset(osg::Vec2(10.0f,20.0f)); //NVidia
651
638
        }
652
639
 
653
640
        if ( arguments.read("--CullFaceFront") ) {
667
654
        shadowedScene->setShadowTechnique(sm.get());
668
655
 
669
656
        int mapres = 1024;
670
 
        while (arguments.read("--mapres", mapres)) 
 
657
        while (arguments.read("--mapres", mapres))
671
658
            sm->setTextureSize(osg::Vec2s(mapres,mapres));
672
659
   }
673
 
    
 
660
 
674
661
    osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(arguments);
675
662
    if (model.valid())
676
663
    {
681
668
        model = createTestModel(arguments);
682
669
    }
683
670
 
684
 
    // get the bounds of the model.    
 
671
    // get the bounds of the model.
685
672
    osg::ComputeBoundsVisitor cbbv;
686
673
    model->accept(cbbv);
687
674
    osg::BoundingBox bb = cbbv.getBoundingBox();
688
675
 
689
676
    osg::Vec4 lightpos;
690
 
    
 
677
 
691
678
    if (postionalLight)
692
679
    {
693
680
        lightpos.set(bb.center().x(), bb.center().y(), bb.zMax() + bb.radius()*2.0f  ,1.0f);
702
689
    {
703
690
 
704
691
        osg::Geode* geode = new osg::Geode;
705
 
        
 
692
 
706
693
        osg::Vec3 widthVec(bb.radius(), 0.0f, 0.0f);
707
694
        osg::Vec3 depthVec(0.0f, bb.radius(), 0.0f);
708
695
        osg::Vec3 centerBase( (bb.xMin()+bb.xMax())*0.5f, (bb.yMin()+bb.yMax())*0.5f, bb.zMin()-bb.radius()*0.1f );
709
 
        
710
 
        geode->addDrawable( osg::createTexturedQuadGeometry( centerBase-widthVec*1.5f-depthVec*1.5f, 
 
696
 
 
697
        geode->addDrawable( osg::createTexturedQuadGeometry( centerBase-widthVec*1.5f-depthVec*1.5f,
711
698
                                                             widthVec*3.0f, depthVec*3.0f) );
712
 
                                                             
 
699
 
713
700
        geode->setNodeMask(shadowedScene->getReceivesShadowTraversalMask());
714
 
        
 
701
 
715
702
        geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readImageFile("Images/lz.rgb")));
716
703
 
717
704
        shadowedScene->addChild(geode);
719
706
 
720
707
    osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
721
708
    ls->getLight()->setPosition(lightpos);
722
 
    
 
709
 
723
710
    bool spotlight = false;
724
711
    if (arguments.read("--spotLight"))
725
712
    {
748
735
 
749
736
    shadowedScene->addChild(model.get());
750
737
    shadowedScene->addChild(ls.get());
751
 
    
 
738
 
752
739
    viewer.setSceneData(shadowedScene.get());
753
740
 
754
741
    // create the windows and run the threads.
759
746
    {
760
747
        osgViewer::Viewer::Windows windows;
761
748
        viewer.getWindows(windows);
762
 
        
 
749
 
763
750
        if (windows.empty()) return 1;
764
 
        
 
751
 
765
752
        osgShadow::ShadowMap* sm = dynamic_cast<osgShadow::ShadowMap*>(shadowedScene->getShadowTechnique());
766
753
        osg::ref_ptr<osg::Camera> hudCamera = sm->makeDebugHUD();
767
 
        
 
754
 
768
755
        // set up cameras to rendering on the first window available.
769
756
        hudCamera->setGraphicsContext(windows[0]);
770
757
        hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height);
789
776
                lightpos.set(sinf(t),cosf(t),1.0f,0.0f);
790
777
            }
791
778
            ls->getLight()->setPosition(lightpos);
792
 
            
 
779
 
793
780
            osg::Vec3f lightDir(-lightpos.x(),-lightpos.y(),-lightpos.z());
794
781
            if(spotlight)
795
 
                lightDir =  osg::Vec3(bb.center().x()+sinf(t)*bb.radius()/2.0, bb.center().y() + cosf(t)*bb.radius()/2.0, bb.center().z()) 
 
782
                lightDir =  osg::Vec3(bb.center().x()+sinf(t)*bb.radius()/2.0, bb.center().y() + cosf(t)*bb.radius()/2.0, bb.center().z())
796
783
                - osg::Vec3(lightpos.x(), lightpos.y(), lightpos.z()) ;
797
784
            lightDir.normalize();
798
785
            ls->getLight()->setDirection(lightDir);
800
787
 
801
788
        viewer.frame();
802
789
    }
803
 
    
 
790
 
804
791
    return 0;
805
792
}