root/OpenSceneGraph/trunk/include/osgTerrain/GeometryTechnique @ 10801

Revision 10801, 3.8 kB (checked in by robert, 4 years ago)

From Jason Beverage, "I posted a question on osg users about resources not being properly released when using osgTerrain databases and multiple viewers are used a few weeks ago and I've found that at least part of the problem comes down to the fact that the nodes that are traversed by the GeometryTechnique? are never actually added to the scene graph, and thus don't have releaseGLObjects called on them. I'm submitting a few changes that takes care of this by allowing the TerrainTechnique? to provide a releaseGLObjects implementation. I've applied these changes in osgEarth and this example program no longer crashes on the second run, although I get corrupt geometry (see attached shot) which could be down to a driver issue. If I increment the context ID for the second viewer, I no longer get the corrupt geometry.

The attached changes are against OpenSceneGraph 2.8.2.

//Sample program. Run against an osgEarth or VPB database based on osgTerrain.
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>

int main(int argc, char** argv)
{

osg::ArgumentParser? arguments(&argc,argv);

osgViewer::Viewer* viewer = new osgViewer::Viewer();
viewer->setUpViewInWindow(100,

100,500,500);

osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
viewer->setSceneData( loadedModel.get() );
viewer->run();
delete viewer;

viewer = new osgViewer::Viewer();
viewer->setUpViewInWindow(100,100,500,500);
loadedModel = osgDB::readNodeFiles(arguments);
viewer->setSceneData( loadedModel.get() );
viewer->run();
delete viewer;

}"

Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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#ifndef OSGTERRAIN_GEOMETRYTECHNIQUE
15#define OSGTERRAIN_GEOMETRYTECHNIQUE 1
16
17#include <osg/MatrixTransform>
18#include <osg/Geode>
19#include <osg/Geometry>
20
21#include <osgTerrain/TerrainTechnique>
22#include <osgTerrain/Locator>
23
24namespace osgTerrain {
25
26class OSGTERRAIN_EXPORT GeometryTechnique : public TerrainTechnique
27{
28    public:
29
30        GeometryTechnique();
31
32        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
33        GeometryTechnique(const GeometryTechnique&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
34       
35        META_Object(osgTerrain, GeometryTechnique);
36       
37        virtual void init();
38       
39        virtual Locator* computeMasterLocator();
40       
41        virtual osg::Vec3d computeCenterModel(Locator* masterLocator);
42       
43        virtual void generateGeometry(Locator* masterLocator, const osg::Vec3d& centerModel);
44       
45        virtual void applyColorLayers();
46       
47        virtual void applyTransparency();
48       
49        virtual void smoothGeometry();
50
51        virtual void update(osgUtil::UpdateVisitor* nv);
52
53        virtual void cull(osgUtil::CullVisitor* nv);
54
55        /** Traverse the terain subgraph.*/
56        virtual void traverse(osg::NodeVisitor& nv);
57
58        virtual void cleanSceneGraph();
59
60        void setFilterBias(float filterBias);
61        float getFilterBias() const { return _filterBias; }
62       
63        void setFilterWidth(float filterWidth);
64        float getFilterWidth() const { return _filterWidth; }
65
66        void setFilterMatrix(const osg::Matrix3& matrix);
67        osg::Matrix3& getFilterMatrix() { return _filterMatrix; }
68        const osg::Matrix3& getFilterMatrix() const { return _filterMatrix; }
69       
70        enum FilterType
71        {
72            GAUSSIAN,
73            SMOOTH,
74            SHARPEN
75        };
76       
77        void setFilterMatrixAs(FilterType filterType);
78
79        /** If State is non-zero, this function releases any associated OpenGL objects for
80        * the specified graphics context. Otherwise, releases OpenGL objects
81        * for all graphics contexts. */
82        virtual void releaseGLObjects(osg::State* = 0) const;
83
84
85    private:
86
87        virtual ~GeometryTechnique();
88
89        struct BufferData
90        {
91            osg::ref_ptr<osg::MatrixTransform>  _transform;
92            osg::ref_ptr<osg::Geode>            _geode;
93            osg::ref_ptr<osg::Geometry>         _geometry;
94        };
95       
96        unsigned int                        _currentReadOnlyBuffer;
97        unsigned int                        _currentWriteBuffer;
98       
99        BufferData                          _bufferData[2];
100       
101        void swapBuffers();
102
103        inline BufferData& getReadOnlyBuffer() { return _bufferData[_currentReadOnlyBuffer]; }
104        inline BufferData& getWriteBuffer() { return _bufferData[_currentWriteBuffer]; }
105       
106               
107        float                               _filterBias;
108        osg::ref_ptr<osg::Uniform>          _filterBiasUniform;
109        float                               _filterWidth;
110        osg::ref_ptr<osg::Uniform>          _filterWidthUniform;
111        osg::Matrix3                        _filterMatrix;
112        osg::ref_ptr<osg::Uniform>          _filterMatrixUniform;
113};
114
115}
116
117#endif
Note: See TracBrowser for help on using the browser.