root/OpenSceneGraph/trunk/examples/osgforest/osgforest.cpp @ 8998

Revision 8998, 33.8 kB (checked in by robert, 6 years ago)

Replaced usage of depreacted IntersectVisitor? with IntersectionVisitor?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgforest.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/AlphaFunc>
20#include <osg/Billboard>
21#include <osg/BlendFunc>
22#include <osg/Depth>
23#include <osg/Geode>
24#include <osg/Geometry>
25#include <osg/Material>
26#include <osg/Math>
27#include <osg/MatrixTransform>
28#include <osg/PolygonOffset>
29#include <osg/Projection>
30#include <osg/ShapeDrawable>
31#include <osg/StateSet>
32#include <osg/Switch>
33#include <osg/Texture2D>
34#include <osg/TexEnv>
35
36#include <osgDB/ReadFile>
37#include <osgDB/FileUtils>
38
39#include <osgUtil/LineSegmentIntersector>
40#include <osgUtil/IntersectionVisitor>
41#include <osgUtil/SmoothingVisitor>
42
43#include <osgText/Text>
44
45#include <osgViewer/Viewer>
46#include <osgGA/StateSetManipulator>
47
48#include <iostream>
49
50// for the grid data..
51#include "../osghangglide/terrain_coords.h"
52
53// class to create the forest and manage the movement between various techniques.
54class ForestTechniqueManager : public osg::Referenced
55{
56public:
57
58    ForestTechniqueManager() {}
59
60    class Tree : public osg::Referenced
61    {
62    public:
63
64        Tree():
65            _color(255,255,255,255),
66            _width(1.0f),
67            _height(1.0f),
68            _type(0) {}
69
70        Tree(const osg::Vec3& position, const osg::Vec4ub& color, float width, float height, unsigned int type):
71            _position(position),
72            _color(color),
73            _width(width),
74            _height(height),
75            _type(type) {}
76
77        osg::Vec3       _position;
78        osg::Vec4ub     _color;
79        float           _width;
80        float           _height;
81        unsigned int    _type;
82    };
83   
84    typedef std::vector< osg::ref_ptr<Tree> > TreeList;
85   
86    class Cell : public osg::Referenced
87    {
88    public:
89        typedef std::vector< osg::ref_ptr<Cell> > CellList;
90
91        Cell():_parent(0) {}
92        Cell(osg::BoundingBox& bb):_parent(0), _bb(bb) {}
93       
94        void addCell(Cell* cell) { cell->_parent=this; _cells.push_back(cell); }
95
96        void addTree(Tree* tree) { _trees.push_back(tree); }
97       
98        void addTrees(const TreeList& trees) { _trees.insert(_trees.end(),trees.begin(),trees.end()); }
99       
100        void computeBound();
101       
102        bool contains(const osg::Vec3& position) const { return _bb.contains(position); }
103       
104        bool divide(unsigned int maxNumTreesPerCell=10);
105       
106        bool divide(bool xAxis, bool yAxis, bool zAxis);
107       
108        void bin();
109
110
111        Cell*               _parent;
112        osg::BoundingBox    _bb;
113        CellList            _cells;
114        TreeList            _trees;
115       
116    };
117
118    float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; }
119    int random(int min,int max) { return min + (int)((float)(max-min)*(float)rand()/(float)RAND_MAX); }
120
121    osg::Geode* createTerrain(const osg::Vec3& origin, const osg::Vec3& size);
122
123    void createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees);
124
125    osg::Geometry* createSprite( float w, float h, osg::Vec4ub color );
126
127    osg::Geometry* createOrthogonalQuads( const osg::Vec3& pos, float w, float h, osg::Vec4ub color );
128    osg::Geometry* createOrthogonalQuadsNoColor( const osg::Vec3& pos, float w, float h );
129
130    osg::Node* createBillboardGraph(Cell* cell,osg::StateSet* stateset);
131
132    osg::Node* createXGraph(Cell* cell,osg::StateSet* stateset);
133
134    osg::Node* createTransformGraph(Cell* cell,osg::StateSet* stateset);
135
136    osg::Node* createShaderGraph(Cell* cell,osg::StateSet* stateset);
137   
138    osg::Node* createHUDWithText(const std::string& text);
139
140    osg::Node* createScene(unsigned int numTreesToCreates);
141   
142    void advanceToNextTechnique(int delta=1)
143    {
144        if (_techniqueSwitch.valid())
145        {
146            _currentTechnique += delta;
147            if (_currentTechnique<0)
148                _currentTechnique = _techniqueSwitch->getNumChildren()-1;
149            if (_currentTechnique>=(int)_techniqueSwitch->getNumChildren())
150                _currentTechnique = 0;
151            _techniqueSwitch->setSingleChildOn(_currentTechnique);
152        }
153    }
154   
155    osg::ref_ptr<osg::Switch>   _techniqueSwitch;
156    int                         _currentTechnique;
157   
158
159};
160
161// event handler to capture keyboard events and use them to advance the technique used for rendering
162class TechniqueEventHandler : public osgGA::GUIEventHandler
163{
164public:
165
166    TechniqueEventHandler(ForestTechniqueManager* ttm=0) { _ForestTechniqueManager = ttm; }
167   
168    META_Object(osgforestApp,TechniqueEventHandler);
169
170    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object*, osg::NodeVisitor*);
171   
172    virtual void getUsage(osg::ApplicationUsage& usage) const;
173
174protected:
175
176    ~TechniqueEventHandler() {}
177   
178    TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {}
179   
180    osg::ref_ptr<ForestTechniqueManager> _ForestTechniqueManager;
181
182       
183};
184
185bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object*, osg::NodeVisitor*)
186{
187    switch(ea.getEventType())
188    {
189        case(osgGA::GUIEventAdapter::KEYDOWN):
190        {
191            if (ea.getKey()=='n' ||
192                ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
193                ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)
194            {
195                _ForestTechniqueManager->advanceToNextTechnique(1);
196                return true;
197            }
198            else if (ea.getKey()=='p' ||
199                     ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
200                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
201            {
202                _ForestTechniqueManager->advanceToNextTechnique(-1);
203                return true;
204            }
205            return false;
206        }
207
208        default:
209            return false;
210    }
211}
212
213void TechniqueEventHandler::getUsage(osg::ApplicationUsage& usage) const
214{
215    usage.addKeyboardMouseBinding("n or Left Arrow","Advance to next technique");
216    usage.addKeyboardMouseBinding("p or Right Array","Move to previous technique");
217}
218
219
220void ForestTechniqueManager::Cell::computeBound()
221{
222    _bb.init();
223    for(CellList::iterator citr=_cells.begin();
224        citr!=_cells.end();
225        ++citr)
226    {
227        (*citr)->computeBound();
228        _bb.expandBy((*citr)->_bb);
229    }
230
231    for(TreeList::iterator titr=_trees.begin();
232        titr!=_trees.end();
233        ++titr)
234    {
235        _bb.expandBy((*titr)->_position);
236    }
237}
238
239bool ForestTechniqueManager::Cell::divide(unsigned int maxNumTreesPerCell)
240{
241
242    if (_trees.size()<=maxNumTreesPerCell) return false;
243
244    computeBound();
245
246    float radius = _bb.radius();
247    float divide_distance = radius*0.7f;
248    if (divide((_bb.xMax()-_bb.xMin())>divide_distance,(_bb.yMax()-_bb.yMin())>divide_distance,(_bb.zMax()-_bb.zMin())>divide_distance))
249    {
250        // recusively divide the new cells till maxNumTreesPerCell is met.
251        for(CellList::iterator citr=_cells.begin();
252            citr!=_cells.end();
253            ++citr)
254        {
255            (*citr)->divide(maxNumTreesPerCell);
256        }
257        return true;
258   }
259   else
260   {
261        return false;
262   }
263}
264
265bool ForestTechniqueManager::Cell::divide(bool xAxis, bool yAxis, bool zAxis)
266{
267    if (!(xAxis || yAxis || zAxis)) return false;
268
269    if (_cells.empty())
270        _cells.push_back(new Cell(_bb));
271
272    if (xAxis)
273    {
274        unsigned int numCellsToDivide=_cells.size();
275        for(unsigned int i=0;i<numCellsToDivide;++i)
276        {
277            Cell* orig_cell = _cells[i].get();
278            Cell* new_cell = new Cell(orig_cell->_bb);
279
280            float xCenter = (orig_cell->_bb.xMin()+orig_cell->_bb.xMax())*0.5f;
281            orig_cell->_bb.xMax() = xCenter;
282            new_cell->_bb.xMin() = xCenter;
283
284            _cells.push_back(new_cell);
285        }
286    }
287
288    if (yAxis)
289    {
290        unsigned int numCellsToDivide=_cells.size();
291        for(unsigned int i=0;i<numCellsToDivide;++i)
292        {
293            Cell* orig_cell = _cells[i].get();
294            Cell* new_cell = new Cell(orig_cell->_bb);
295
296            float yCenter = (orig_cell->_bb.yMin()+orig_cell->_bb.yMax())*0.5f;
297            orig_cell->_bb.yMax() = yCenter;
298            new_cell->_bb.yMin() = yCenter;
299
300            _cells.push_back(new_cell);
301        }
302    }
303
304    if (zAxis)
305    {
306        unsigned int numCellsToDivide=_cells.size();
307        for(unsigned int i=0;i<numCellsToDivide;++i)
308        {
309            Cell* orig_cell = _cells[i].get();
310            Cell* new_cell = new Cell(orig_cell->_bb);
311
312            float zCenter = (orig_cell->_bb.zMin()+orig_cell->_bb.zMax())*0.5f;
313            orig_cell->_bb.zMax() = zCenter;
314            new_cell->_bb.zMin() = zCenter;
315
316            _cells.push_back(new_cell);
317        }
318    }
319
320    bin();
321
322    return true;
323
324}
325
326void ForestTechniqueManager::Cell::bin()
327{   
328    // put trees in apprpriate cells.
329    TreeList treesNotAssigned;
330    for(TreeList::iterator titr=_trees.begin();
331        titr!=_trees.end();
332        ++titr)
333    {
334        Tree* tree = titr->get();
335        bool assigned = false;
336        for(CellList::iterator citr=_cells.begin();
337            citr!=_cells.end() && !assigned;
338            ++citr)
339        {
340            if ((*citr)->contains(tree->_position))
341            {
342                (*citr)->addTree(tree);
343                assigned = true;
344            }
345        }
346        if (!assigned) treesNotAssigned.push_back(tree);
347    }
348
349    // put the unassigned trees back into the original local tree list.
350    _trees.swap(treesNotAssigned);
351
352
353    // prune empty cells.
354    CellList cellsNotEmpty;
355    for(CellList::iterator citr=_cells.begin();
356        citr!=_cells.end();
357        ++citr)
358    {
359        if (!((*citr)->_trees.empty()))
360        {
361            cellsNotEmpty.push_back(*citr);
362        }
363    }
364    _cells.swap(cellsNotEmpty);
365
366
367}
368
369osg::Geode* ForestTechniqueManager::createTerrain(const osg::Vec3& origin, const osg::Vec3& size)
370{
371    osg::Geode* geode = new osg::Geode();
372
373    // ---------------------------------------
374    // Set up a StateSet to texture the objects
375    // ---------------------------------------
376    osg::StateSet* stateset = new osg::StateSet();
377
378    osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
379    if (image)
380    {
381        osg::Texture2D* texture = new osg::Texture2D;
382        texture->setImage(image);
383        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
384    }
385   
386    geode->setStateSet( stateset );
387   
388    unsigned int numColumns = 38;
389    unsigned int numRows = 39;
390    unsigned int r;
391    unsigned int c;
392
393    // compute z range of z values of grid data so we can scale it.
394    float min_z = FLT_MAX;
395    float max_z = -FLT_MAX;
396    for(r=0;r<numRows;++r)
397    {
398        for(c=0;c<numColumns;++c)
399        {
400            min_z = osg::minimum(min_z,vertex[r+c*numRows][2]);
401            max_z = osg::maximum(max_z,vertex[r+c*numRows][2]);
402        }
403    }
404   
405    float scale_z = size.z()/(max_z-min_z);
406
407
408    bool createGrid = false;
409    if (createGrid)
410    {
411
412        osg::HeightField* grid = new osg::HeightField;
413        grid->allocate(numColumns,numRows);
414        grid->setOrigin(origin);
415        grid->setXInterval(size.x()/(float)(numColumns-1));
416        grid->setYInterval(size.y()/(float)(numRows-1));
417
418        for(r=0;r<numRows;++r)
419        {
420            for(c=0;c<numColumns;++c)
421            {
422                grid->setHeight(c,r,(vertex[r+c*numRows][2]-min_z)*scale_z);
423            }
424        }
425       
426        geode->addDrawable(new osg::ShapeDrawable(grid));
427    }
428    else
429    {
430        osg::Geometry* geometry = new osg::Geometry;
431       
432        osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows));
433        osg::Vec2Array& t = *(new osg::Vec2Array(numColumns*numRows));
434        osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1));
435       
436        color[0].set(255,255,255,255);
437
438        float rowCoordDelta = size.y()/(float)(numRows-1);
439        float columnCoordDelta = size.x()/(float)(numColumns-1);
440       
441        float rowTexDelta = 1.0f/(float)(numRows-1);
442        float columnTexDelta = 1.0f/(float)(numColumns-1);
443
444        osg::Vec3 pos = origin;
445        osg::Vec2 tex(0.0f,0.0f);
446        int vi=0;
447        for(r=0;r<numRows;++r)
448        {
449            pos.x() = origin.x();
450            tex.x() = 0.0f;
451            for(c=0;c<numColumns;++c)
452            {
453                v[vi].set(pos.x(),pos.y(),pos.z()+(vertex[r+c*numRows][2]-min_z)*scale_z);
454                t[vi].set(tex.x(),tex.y());
455                pos.x()+=columnCoordDelta;
456                tex.x()+=columnTexDelta;
457                ++vi;
458            }
459            pos.y() += rowCoordDelta;
460            tex.y() += rowTexDelta;
461        }
462       
463        geometry->setVertexArray(&v);
464        geometry->setColorArray(&color);
465        geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
466        geometry->setTexCoordArray(0,&t);
467       
468        for(r=0;r<numRows-1;++r)
469        {
470            osg::DrawElementsUShort& drawElements = *(new osg::DrawElementsUShort(GL_QUAD_STRIP,2*numColumns));
471            geometry->addPrimitiveSet(&drawElements);
472            int ei=0;
473            for(c=0;c<numColumns;++c)
474            {
475                drawElements[ei++] = (r+1)*numColumns+c;
476                drawElements[ei++] = (r)*numColumns+c;
477            }
478        }
479       
480        geode->addDrawable(geometry);
481       
482        osgUtil::SmoothingVisitor sv;
483        sv.smooth(*geometry);
484    }
485   
486    return geode;
487}
488
489void ForestTechniqueManager::createTreeList(osg::Node* terrain,const osg::Vec3& origin, const osg::Vec3& size,unsigned int numTreesToCreate,TreeList& trees)
490{
491
492    float max_TreeHeight = sqrtf(size.length2()/(float)numTreesToCreate);
493    float max_TreeWidth = max_TreeHeight*0.5f;
494   
495    float min_TreeHeight = max_TreeHeight*0.3f;
496    float min_TreeWidth = min_TreeHeight*0.5f;
497
498    trees.reserve(trees.size()+numTreesToCreate);
499
500
501    for(unsigned int i=0;i<numTreesToCreate;++i)
502    {
503        Tree* tree = new Tree;
504        tree->_position.set(random(origin.x(),origin.x()+size.x()),random(origin.y(),origin.y()+size.y()),origin.z());
505        tree->_color.set(random(128,255),random(128,255),random(128,255),255);
506        tree->_width = random(min_TreeWidth,max_TreeWidth);
507        tree->_height = random(min_TreeHeight,max_TreeHeight);
508        tree->_type = 0;
509       
510        if (terrain)
511        {
512            osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector =
513                new osgUtil::LineSegmentIntersector(tree->_position,tree->_position+osg::Vec3(0.0f,0.0f,size.z()));
514
515            osgUtil::IntersectionVisitor iv(intersector.get());
516           
517            terrain->accept(iv);
518
519            if (intersector->containsIntersections())
520            {
521                osgUtil::LineSegmentIntersector::Intersections& intersections = intersector->getIntersections();
522                for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
523                    itr != intersections.end();
524                    ++itr)
525                {
526                    const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
527                    tree->_position = intersection.getWorldIntersectPoint();
528                }
529            }
530        }
531       
532        trees.push_back(tree);
533    }
534}
535
536osg::Geometry* ForestTechniqueManager::createSprite( float w, float h, osg::Vec4ub color )
537{
538    // set up the coords
539    osg::Vec3Array& v = *(new osg::Vec3Array(4));
540    osg::Vec2Array& t = *(new osg::Vec2Array(4));
541    osg::Vec4ubArray& c = *(new osg::Vec4ubArray(1));
542
543    v[0].set(-w*0.5f,0.0f,0.0f);
544    v[1].set( w*0.5f,0.0f,0.0f);
545    v[2].set( w*0.5f,0.0f,h);
546    v[3].set(-w*0.5f,0.0f,h);
547
548    c[0] = color;
549
550    t[0].set(0.0f,0.0f);
551    t[1].set(1.0f,0.0f);
552    t[2].set(1.0f,1.0f);
553    t[3].set(0.0f,1.0f);
554
555    osg::Geometry *geom = new osg::Geometry;
556
557    geom->setVertexArray( &v );
558
559    geom->setTexCoordArray( 0, &t );
560
561    geom->setColorArray( &c );
562    geom->setColorBinding( osg::Geometry::BIND_OVERALL );
563
564    geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4) );
565
566    return geom;
567}
568
569osg::Geometry* ForestTechniqueManager::createOrthogonalQuads( const osg::Vec3& pos, float w, float h, osg::Vec4ub color )
570{
571    // set up the coords
572    osg::Vec3Array& v = *(new osg::Vec3Array(8));
573    osg::Vec2Array& t = *(new osg::Vec2Array(8));
574    osg::Vec4ubArray& c = *(new osg::Vec4ubArray(1));
575   
576    float rotation = random(0.0f,osg::PI/2.0f);
577    float sw = sinf(rotation)*w*0.5f;
578    float cw = cosf(rotation)*w*0.5f;
579
580    v[0].set(pos.x()-sw,pos.y()-cw,pos.z()+0.0f);
581    v[1].set(pos.x()+sw,pos.y()+cw,pos.z()+0.0f);
582    v[2].set(pos.x()+sw,pos.y()+cw,pos.z()+h);
583    v[3].set(pos.x()-sw,pos.y()-cw,pos.z()+h);
584
585    v[4].set(pos.x()-cw,pos.y()+sw,pos.z()+0.0f);
586    v[5].set(pos.x()+cw,pos.y()-sw,pos.z()+0.0f);
587    v[6].set(pos.x()+cw,pos.y()-sw,pos.z()+h);
588    v[7].set(pos.x()-cw,pos.y()+sw,pos.z()+h);
589
590    c[0] = color;
591
592    t[0].set(0.0f,0.0f);
593    t[1].set(1.0f,0.0f);
594    t[2].set(1.0f,1.0f);
595    t[3].set(0.0f,1.0f);
596
597    t[4].set(0.0f,0.0f);
598    t[5].set(1.0f,0.0f);
599    t[6].set(1.0f,1.0f);
600    t[7].set(0.0f,1.0f);
601
602    osg::Geometry *geom = new osg::Geometry;
603
604    geom->setVertexArray( &v );
605
606    geom->setTexCoordArray( 0, &t );
607
608    geom->setColorArray( &c );
609    geom->setColorBinding( osg::Geometry::BIND_OVERALL );
610
611    geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,8) );
612
613    return geom;
614}
615
616osg::Node* ForestTechniqueManager::createBillboardGraph(Cell* cell,osg::StateSet* stateset)
617{
618    bool needGroup = !(cell->_cells.empty());
619    bool needBillboard = !(cell->_trees.empty());
620   
621    osg::Billboard* billboard = 0;
622    osg::Group* group = 0;
623   
624    if (needBillboard)
625    {
626        billboard = new osg::Billboard;
627        billboard->setStateSet(stateset);
628        for(TreeList::iterator itr=cell->_trees.begin();
629            itr!=cell->_trees.end();
630            ++itr)
631        {
632            Tree& tree = **itr;
633            billboard->addDrawable(createSprite(tree._width,tree._height,tree._color),tree._position);   
634        }
635    }
636   
637    if (needGroup)
638    {
639        group = new osg::Group;
640        for(Cell::CellList::iterator itr=cell->_cells.begin();
641            itr!=cell->_cells.end();
642            ++itr)
643        {
644            group->addChild(createBillboardGraph(itr->get(),stateset));
645        }
646       
647        if (billboard) group->addChild(billboard);
648       
649    }
650    if (group) return group;
651    else return billboard;
652}
653
654osg::Node* ForestTechniqueManager::createXGraph(Cell* cell,osg::StateSet* stateset)
655{
656    bool needGroup = !(cell->_cells.empty());
657    bool needTrees = !(cell->_trees.empty());
658   
659    osg::Geode* geode = 0;
660    osg::Group* group = 0;
661   
662    if (needTrees)
663    {
664        geode = new osg::Geode;
665        geode->setStateSet(stateset);
666       
667        for(TreeList::iterator itr=cell->_trees.begin();
668            itr!=cell->_trees.end();
669            ++itr)
670        {
671            Tree& tree = **itr;
672            geode->addDrawable(createOrthogonalQuads(tree._position,tree._width,tree._height,tree._color));
673        }
674    }
675   
676    if (needGroup)
677    {
678        group = new osg::Group;
679        for(Cell::CellList::iterator itr=cell->_cells.begin();
680            itr!=cell->_cells.end();
681            ++itr)
682        {
683            group->addChild(createXGraph(itr->get(),stateset));
684        }
685       
686        if (geode) group->addChild(geode);
687       
688    }
689    if (group) return group;
690    else return geode;
691}
692
693osg::Node* ForestTechniqueManager::createTransformGraph(Cell* cell,osg::StateSet* stateset)
694{
695    bool needGroup = !(cell->_cells.empty());
696    bool needTrees = !(cell->_trees.empty());
697   
698    osg::Group* transform_group = 0;
699    osg::Group* group = 0;
700   
701    if (needTrees)
702    {
703        transform_group = new osg::Group;
704       
705        osg::Geometry* geometry = createOrthogonalQuads(osg::Vec3(0.0f,0.0f,0.0f),1.0f,1.0f,osg::Vec4ub(255,255,255,255));
706       
707        for(TreeList::iterator itr=cell->_trees.begin();
708            itr!=cell->_trees.end();
709            ++itr)
710        {
711            Tree& tree = **itr;
712            osg::MatrixTransform* transform = new osg::MatrixTransform;
713            transform->setMatrix(osg::Matrix::scale(tree._width,tree._width,tree._height)*osg::Matrix::translate(tree._position));
714   
715            osg::Geode* geode = new osg::Geode;
716            geode->setStateSet(stateset);
717            geode->addDrawable(geometry);
718            transform->addChild(geode);
719            transform_group->addChild(transform);
720        }
721    }
722   
723    if (needGroup)
724    {
725        group = new osg::Group;
726        for(Cell::CellList::iterator itr=cell->_cells.begin();
727            itr!=cell->_cells.end();
728            ++itr)
729        {
730            group->addChild(createTransformGraph(itr->get(),stateset));
731        }
732       
733        if (transform_group) group->addChild(transform_group);
734       
735    }
736    if (group) return group;
737    else return transform_group;
738}
739
740osg::Geometry* ForestTechniqueManager::createOrthogonalQuadsNoColor( const osg::Vec3& pos, float w, float h)
741{
742    // set up the coords
743    osg::Vec3Array& v = *(new osg::Vec3Array(8));
744    osg::Vec2Array& t = *(new osg::Vec2Array(8));
745   
746    float rotation = random(0.0f,osg::PI/2.0f);
747    float sw = sinf(rotation)*w*0.5f;
748    float cw = cosf(rotation)*w*0.5f;
749
750    v[0].set(pos.x()-sw,pos.y()-cw,pos.z()+0.0f);
751    v[1].set(pos.x()+sw,pos.y()+cw,pos.z()+0.0f);
752    v[2].set(pos.x()+sw,pos.y()+cw,pos.z()+h);
753    v[3].set(pos.x()-sw,pos.y()-cw,pos.z()+h);
754
755    v[4].set(pos.x()-cw,pos.y()+sw,pos.z()+0.0f);
756    v[5].set(pos.x()+cw,pos.y()-sw,pos.z()+0.0f);
757    v[6].set(pos.x()+cw,pos.y()-sw,pos.z()+h);
758    v[7].set(pos.x()-cw,pos.y()+sw,pos.z()+h);
759
760    t[0].set(0.0f,0.0f);
761    t[1].set(1.0f,0.0f);
762    t[2].set(1.0f,1.0f);
763    t[3].set(0.0f,1.0f);
764
765    t[4].set(0.0f,0.0f);
766    t[5].set(1.0f,0.0f);
767    t[6].set(1.0f,1.0f);
768    t[7].set(0.0f,1.0f);
769
770    osg::Geometry *geom = new osg::Geometry;
771
772    geom->setVertexArray( &v );
773
774    geom->setTexCoordArray( 0, &t );
775
776    geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,8) );
777
778    return geom;
779}
780
781class ShaderGeometry : public osg::Drawable
782{
783    public:
784        ShaderGeometry() { setUseDisplayList(false); }
785
786        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
787        ShaderGeometry(const ShaderGeometry& ShaderGeometry,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
788            osg::Drawable(ShaderGeometry,copyop) {}
789
790        META_Object(osg,ShaderGeometry)
791
792        typedef std::vector<osg::Vec4> PositionSizeList;
793       
794        virtual void drawImplementation(osg::RenderInfo& renderInfo) const
795        {
796            for(PositionSizeList::const_iterator itr = _trees.begin();
797                itr != _trees.end();
798                ++itr)
799            {
800                glColor4fv(itr->ptr());
801                _geometry->draw(renderInfo);
802            }
803        }
804
805        virtual osg::BoundingBox computeBound() const
806        {
807            osg::BoundingBox geom_box = _geometry->getBound();
808            osg::BoundingBox bb;
809            for(PositionSizeList::const_iterator itr = _trees.begin();
810                itr != _trees.end();
811                ++itr)
812            {
813                bb.expandBy(geom_box.corner(0)*(*itr)[3] +
814                            osg::Vec3( (*itr)[0], (*itr)[1], (*itr)[2] ));
815                bb.expandBy(geom_box.corner(7)*(*itr)[3] +
816                            osg::Vec3( (*itr)[0], (*itr)[1], (*itr)[2] ));
817            }
818            return bb;
819        }
820       
821        void setGeometry(osg::Geometry* geometry)
822        {
823            _geometry = geometry;
824        }
825       
826        void addTree(ForestTechniqueManager::Tree& tree)
827        {
828            _trees.push_back(osg::Vec4(tree._position.x(), tree._position.y(), tree._position.z(), tree._height));
829        }
830       
831        osg::ref_ptr<osg::Geometry> _geometry;
832
833        PositionSizeList _trees;
834
835    protected:
836   
837        virtual ~ShaderGeometry() {}
838       
839};
840
841osg::Geometry* shared_geometry = 0;
842
843osg::Node* ForestTechniqueManager::createShaderGraph(Cell* cell,osg::StateSet* stateset)
844{
845    if (shared_geometry==0)
846    {
847        shared_geometry = createOrthogonalQuadsNoColor(osg::Vec3(0.0f,0.0f,0.0f),1.0f,1.0f);
848        //shared_geometry->setUseDisplayList(false);
849    }
850
851
852    bool needGroup = !(cell->_cells.empty());
853    bool needTrees = !(cell->_trees.empty());
854   
855    osg::Geode* geode = 0;
856    osg::Group* group = 0;
857   
858    if (needTrees)
859    {
860        geode = new osg::Geode;
861       
862        ShaderGeometry* shader_geometry = new ShaderGeometry;
863        shader_geometry->setGeometry(shared_geometry);
864       
865       
866        for(TreeList::iterator itr=cell->_trees.begin();
867            itr!=cell->_trees.end();
868            ++itr)
869        {
870            Tree& tree = **itr;
871            shader_geometry->addTree(tree);
872
873        }
874
875        geode->setStateSet(stateset);
876        geode->addDrawable(shader_geometry);
877    }
878   
879    if (needGroup)
880    {
881        group = new osg::Group;
882        for(Cell::CellList::iterator itr=cell->_cells.begin();
883            itr!=cell->_cells.end();
884            ++itr)
885        {
886            group->addChild(createShaderGraph(itr->get(),stateset));
887        }
888       
889        if (geode) group->addChild(geode);
890       
891    }
892    if (group) return group;
893    else return geode;
894}
895
896osg::Node* ForestTechniqueManager::createHUDWithText(const std::string& str)
897{
898    osg::Geode* geode = new osg::Geode();
899   
900    std::string timesFont("fonts/arial.ttf");
901
902    // turn lighting off for the text and disable depth test to ensure its always ontop.
903    osg::StateSet* stateset = geode->getOrCreateStateSet();
904    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
905
906    // or disable depth test, and make sure that the hud is drawn after everything
907    // else so that it always appears ontop.
908    stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
909    stateset->setRenderBinDetails(11,"RenderBin");
910
911    osg::Vec3 position(150.0f,800.0f,0.0f);
912    osg::Vec3 delta(0.0f,-120.0f,0.0f);
913
914    {
915        osgText::Text* text = new  osgText::Text;
916        geode->addDrawable( text );
917
918        text->setFont(timesFont);
919        text->setPosition(position);
920        text->setText(str);
921       
922        position += delta;
923    }   
924
925   
926    // create the hud.
927    osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
928    modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
929    modelview_abs->setMatrix(osg::Matrix::identity());
930    modelview_abs->addChild(geode);
931
932    osg::Projection* projection = new osg::Projection;
933    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
934    projection->addChild(modelview_abs);
935
936    return projection;
937}
938
939osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates)
940{
941    osg::Vec3 origin(0.0f,0.0f,0.0f);
942    osg::Vec3 size(1000.0f,1000.0f,200.0f);
943
944    std::cout<<"Creating terrain...";
945    osg::ref_ptr<osg::Node> terrain = createTerrain(origin,size);
946    std::cout<<"done."<<std::endl;
947   
948    std::cout<<"Creating tree locations...";std::cout.flush();
949    TreeList trees;
950    createTreeList(terrain.get(),origin,size,numTreesToCreates,trees);
951    std::cout<<"done."<<std::endl;
952   
953    std::cout<<"Creating cell subdivision...";
954    osg::ref_ptr<Cell> cell = new Cell;
955    cell->addTrees(trees);
956    cell->divide();
957     std::cout<<"done."<<std::endl;
958   
959   
960    osg::Texture2D *tex = new osg::Texture2D;
961    tex->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP );
962    tex->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP );
963    tex->setImage(osgDB::readImageFile("Images/tree0.rgba"));
964
965    osg::StateSet *dstate = new osg::StateSet;
966    {   
967        dstate->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON );
968        dstate->setTextureAttribute(0, new osg::TexEnv );
969
970        dstate->setAttributeAndModes( new osg::BlendFunc, osg::StateAttribute::ON );
971
972        osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
973        alphaFunc->setFunction(osg::AlphaFunc::GEQUAL,0.05f);
974        dstate->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON );
975
976        dstate->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
977
978        dstate->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
979    }
980   
981
982    _techniqueSwitch = new osg::Switch;
983
984    {
985        std::cout<<"Creating osg::Billboard based forest...";
986        osg::Group* group = new osg::Group;
987        group->addChild(createBillboardGraph(cell.get(),dstate));
988        group->addChild(createHUDWithText("Using osg::Billboard's to create a forest\n\nPress left cursor key to select OpenGL shader based forest\nPress right cursor key to select double quad based forest"));
989        _techniqueSwitch->addChild(group);
990        std::cout<<"done."<<std::endl;
991    }
992   
993    {
994        std::cout<<"Creating double quad based forest...";
995        osg::Group* group = new osg::Group;
996        group->addChild(createXGraph(cell.get(),dstate));
997        group->addChild(createHUDWithText("Using double quads to create a forest\n\nPress left cursor key to select osg::Billboard based forest\nPress right cursor key to select osg::MatrixTransform based forest\n"));
998        _techniqueSwitch->addChild(group);
999        std::cout<<"done."<<std::endl;
1000    }
1001
1002    {
1003        std::cout<<"Creating osg::MatrixTransform based forest...";
1004        osg::Group* group = new osg::Group;
1005        group->addChild(createTransformGraph(cell.get(),dstate));
1006        group->addChild(createHUDWithText("Using osg::MatrixTransform's to create a forest\n\nPress left cursor key to select double quad based forest\nPress right cursor key to select OpenGL shader based forest"));
1007        _techniqueSwitch->addChild(group);
1008        std::cout<<"done."<<std::endl;
1009    }
1010
1011    {
1012        osg::Group* group = new osg::Group;
1013       
1014
1015        osg::StateSet* stateset = new osg::StateSet;
1016
1017        stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON );
1018        stateset->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
1019
1020        {
1021            osg::Program* program = new osg::Program;
1022            stateset->setAttribute(program);
1023
1024#if 1
1025            // use inline shaders
1026           
1027            ///////////////////////////////////////////////////////////////////
1028            // vertex shader using just Vec4 coefficients
1029            char vertexShaderSource[] =
1030                "varying vec2 texcoord;\n"
1031                "\n"
1032                "void main(void)\n"
1033                "{\n"
1034                "    vec3 position = gl_Vertex.xyz * gl_Color.w + gl_Color.xyz;\n"
1035                "    gl_Position     = gl_ModelViewProjectionMatrix * vec4(position,1.0);\n"
1036                "    gl_FrontColor = vec4(1.0,1.0,1.0,1.0);\n"
1037                "    texcoord = gl_MultiTexCoord0.st;\n"
1038                "}\n";
1039
1040            //////////////////////////////////////////////////////////////////
1041            // fragment shader
1042            //
1043            char fragmentShaderSource[] =
1044                "uniform sampler2D baseTexture; \n"
1045                "varying vec2 texcoord; \n"
1046                "\n"
1047                "void main(void) \n"
1048                "{ \n"
1049                "    gl_FragColor = texture2D( baseTexture, texcoord); \n"
1050                "}\n";
1051
1052            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource);
1053            program->addShader(vertex_shader);
1054
1055            osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
1056            program->addShader(fragment_shader);
1057           
1058#else
1059
1060            // get shaders from source
1061            program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile("shaders/forest.vert")));
1062            program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile("shaders/forest.frag")));
1063
1064#endif
1065
1066            osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0);
1067            stateset->addUniform(baseTextureSampler);
1068        }
1069
1070        std::cout<<"Creating OpenGL shader based forest...";
1071        group->addChild(createShaderGraph(cell.get(),stateset));
1072        group->addChild(createHUDWithText("Using OpenGL Shader to create a forest\n\nPress left cursor key to select osg::MatrixTransform based forest\nPress right cursor key to select osg::Billboard based forest"));
1073        _techniqueSwitch->addChild(group);
1074        std::cout<<"done."<<std::endl;
1075    }
1076
1077    _currentTechnique = 0;
1078    _techniqueSwitch->setSingleChildOn(_currentTechnique);
1079   
1080
1081    osg::Group* scene = new osg::Group;
1082   
1083    scene->addChild(terrain.get());
1084    scene->addChild(_techniqueSwitch.get());
1085
1086    return scene;
1087}
1088
1089int main( int argc, char **argv )
1090{
1091
1092    // use an ArgumentParser object to manage the program arguments.
1093    osg::ArgumentParser arguments(&argc,argv);
1094   
1095    // construct the viewer.
1096    osgViewer::Viewer viewer(arguments);
1097
1098    float numTreesToCreates = 10000;
1099    arguments.read("--trees",numTreesToCreates);
1100   
1101    osg::ref_ptr<ForestTechniqueManager> ttm = new ForestTechniqueManager;
1102   
1103    viewer.addEventHandler(new TechniqueEventHandler(ttm.get()));
1104    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
1105
1106    // add model to viewer.
1107    viewer.setSceneData( ttm->createScene((unsigned int)numTreesToCreates) );
1108
1109
1110    return viewer.run();
1111}
Note: See TracBrowser for help on using the browser.