1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version.  The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #include <osgTerrain/Terrain>
15 #include <osgTerrain/DisplacementMappingTechnique>
16 
17 
18 using namespace osgTerrain;
19 
20 /////////////////////////////////////////////////////////////////////////////////////////////////////////
21 //
22 //  DisplacementMappingTechnique
23 //
DisplacementMappingTechnique()24 DisplacementMappingTechnique::DisplacementMappingTechnique()
25 {
26     // OSG_NOTICE<<"DisplacementMappingTechnique::DisplacementMappingTechnique()"<<std::endl;
27 }
28 
DisplacementMappingTechnique(const DisplacementMappingTechnique & st,const osg::CopyOp & copyop)29 DisplacementMappingTechnique::DisplacementMappingTechnique(const DisplacementMappingTechnique& st,const osg::CopyOp& copyop):
30     osgTerrain::TerrainTechnique(st, copyop)
31 {
32 }
33 
~DisplacementMappingTechnique()34 DisplacementMappingTechnique::~DisplacementMappingTechnique()
35 {
36 }
37 
init(int,bool)38 void DisplacementMappingTechnique::init(int /*dirtyMask*/, bool /*assumeMultiThreaded*/)
39 {
40     if (!_terrainTile) return;
41     if (!_terrainTile->getTerrain()) return;
42 
43     GeometryPool* geometryPool = _terrainTile->getTerrain()->getGeometryPool();
44     _transform = geometryPool->getTileSubgraph(_terrainTile);
45 
46     // set tile as no longer dirty.
47     _terrainTile->setDirtyMask(0);
48 }
49 
update(osgUtil::UpdateVisitor * uv)50 void DisplacementMappingTechnique::update(osgUtil::UpdateVisitor* uv)
51 {
52     if (_terrainTile) _terrainTile->osg::Group::traverse(*uv);
53 
54     if (_transform.valid()) _transform->accept(*uv);
55 }
56 
57 
cull(osgUtil::CullVisitor * cv)58 void DisplacementMappingTechnique::cull(osgUtil::CullVisitor* cv)
59 {
60     if (_transform.valid()) _transform->accept(*cv);
61 }
62 
63 
traverse(osg::NodeVisitor & nv)64 void DisplacementMappingTechnique::traverse(osg::NodeVisitor& nv)
65 {
66     if (!_terrainTile) return;
67 
68     // if app traversal update the frame count.
69     if (nv.getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR)
70     {
71         // if (_terrainTile->getDirty()) _terrainTile->init(_terrainTile->getDirtyMask(), false);
72 
73         osgUtil::UpdateVisitor* uv = nv.asUpdateVisitor();
74         if (uv)
75         {
76             update(uv);
77             return;
78         }
79     }
80     else if (nv.getVisitorType()==osg::NodeVisitor::CULL_VISITOR)
81     {
82         osgUtil::CullVisitor* cv = nv.asCullVisitor();
83         if (cv)
84         {
85             cull(cv);
86             return;
87         }
88     }
89 
90     {
91         if (_transform.valid())
92         {
93             _transform->accept(nv);
94         }
95     }
96 }
97 
98 
cleanSceneGraph()99 void DisplacementMappingTechnique::cleanSceneGraph()
100 {
101 }
102 
releaseGLObjects(osg::State * state) const103 void DisplacementMappingTechnique::releaseGLObjects(osg::State* state) const
104 {
105     if (_transform.valid())
106     {
107 //      OSG_NOTICE<<"DisplacementMappingTechnique::releaseGLObjects()"<<std::endl;
108         _transform->releaseGLObjects(state);
109     }
110 }
111