From 835e315494818250a3fd3fc07814c41be49b11d1 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 16 Apr 2007 19:34:25 +0000 Subject: [PATCH] Implement basic TerrainGeometry code --- include/osgTerrain/GeometryTechnique | 57 ++++++++--------- src/osgTerrain/GeometryTechnique.cpp | 95 +++++++++++++++++++++------- 2 files changed, 101 insertions(+), 51 deletions(-) diff --git a/include/osgTerrain/GeometryTechnique b/include/osgTerrain/GeometryTechnique index c66670e56..92fa3ebc5 100644 --- a/include/osgTerrain/GeometryTechnique +++ b/include/osgTerrain/GeometryTechnique @@ -21,35 +21,6 @@ namespace osgTerrain { -class OSGTERRAIN_EXPORT GeometryTechnique : public TerrainTechnique -{ - public: - - GeometryTechnique(); - - /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ - GeometryTechnique(const GeometryTechnique&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); - - virtual void init(); - - virtual void update(osgUtil::UpdateVisitor* nv); - - virtual void cull(osgUtil::CullVisitor* nv); - - virtual void cleanSceneGraph(); - - virtual void dirty(); - - protected: - - virtual ~GeometryTechnique(); - - osg::ref_ptr _geode; - - osg::ref_ptr _geometry; - //osg::ref_ptr _geometry; -}; - class OSGTERRAIN_EXPORT TerrainGeometry : public osg::Drawable { public: @@ -116,6 +87,34 @@ class OSGTERRAIN_EXPORT TerrainGeometry : public osg::Drawable }; +class OSGTERRAIN_EXPORT GeometryTechnique : public TerrainTechnique +{ + public: + + GeometryTechnique(); + + /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ + GeometryTechnique(const GeometryTechnique&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); + + virtual void init(); + + virtual void update(osgUtil::UpdateVisitor* nv); + + virtual void cull(osgUtil::CullVisitor* nv); + + virtual void cleanSceneGraph(); + + virtual void dirty(); + + protected: + + virtual ~GeometryTechnique(); + + osg::ref_ptr _geode; + + osg::ref_ptr _terrainGeometry; +}; + } #endif diff --git a/src/osgTerrain/GeometryTechnique.cpp b/src/osgTerrain/GeometryTechnique.cpp index e2a4b08a5..3317023cc 100644 --- a/src/osgTerrain/GeometryTechnique.cpp +++ b/src/osgTerrain/GeometryTechnique.cpp @@ -108,9 +108,8 @@ void GeometryTechnique::init() osg::notify(osg::NOTICE)<<"topRightNDC = "<addDrawable(_geometry.get()); - _geode->addDrawable(new osgTerrain::TerrainGeometry); + _terrainGeometry = new osgTerrain::TerrainGeometry; + _geode->addDrawable(_terrainGeometry.get()); unsigned int numRows = 100; unsigned int numColumns = 100; @@ -126,13 +125,11 @@ void GeometryTechnique::init() // allocate and assign vertices osg::Vec3Array* _vertices = new osg::Vec3Array(numVertices); - _geometry->setVertexArray(_vertices); + _terrainGeometry->setVertices(_vertices); // allocate and assign normals osg::Vec3Array* _normals = new osg::Vec3Array(numVertices); - _geometry->setNormalArray(_normals); - _geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); - + _terrainGeometry->setNormals(_normals); int texcoord_index = 0; int color_index = -1; @@ -149,7 +146,7 @@ void GeometryTechnique::init() ++texcoord_index; _texcoords = new osg::Vec2Array(numVertices); - _geometry->setTexCoordArray(color_index, _texcoords); + _terrainGeometry->setTexCoords(color_index, _texcoords); } osg::FloatArray* _elevations = 0; @@ -162,7 +159,7 @@ void GeometryTechnique::init() if (!colorLayer) { _elevations = new osg::FloatArray(numVertices); - _geometry->setTexCoordArray(tf_index, _elevations); + _terrainGeometry->setTexCoords(tf_index, _elevations); minHeight = tf->getMinimum(); scaleHeight = 1.0f/(tf->getMaximum()-tf->getMinimum()); @@ -172,10 +169,10 @@ void GeometryTechnique::init() // allocate and assign color osg::Vec4Array* _colors = new osg::Vec4Array(1); - _geometry->setColorArray(_colors); - _geometry->setColorBinding(osg::Geometry::BIND_OVERALL); (*_colors)[0].set(1.0f,1.0f,1.0f,1.0f); + _terrainGeometry->setColors(_colors); + // populate vertex and tex coord arrays unsigned int j; for(j=0; jconvertLocalToModel(ndc_one, model_one); - model_one -= model; + model_one = model_one - model; model_one.normalize(); (*_normals)[iv] = model_one; } @@ -240,17 +237,9 @@ void GeometryTechnique::init() (*elements)[i*2+1] = iv; } - //_primitiveSets.push_back(elements); - - _geometry->addPrimitiveSet(elements); + _terrainGeometry->addPrimitiveSet(elements); } - - - osgUtil::SmoothingVisitor smoother; - _geode->accept(smoother); - - if (colorLayer) { osgTerrain::ImageLayer* imageLayer = dynamic_cast(colorLayer); @@ -332,7 +321,7 @@ void GeometryTechnique::init() } } - // _geometry->setUseDisplayList(false); + // _terrainGeometry->setUseDisplayList(false); _dirty = false; } @@ -399,6 +388,7 @@ osg::BoundingBox TerrainGeometry::computeBound() const void TerrainGeometry::drawImplementation(osg::RenderInfo& renderInfo) const { +#if 0 osg::notify(osg::NOTICE)<<"TerrainGeometry::drawImplementation"<getDataVariance()==DYNAMIC) @@ -409,4 +399,65 @@ void TerrainGeometry::drawImplementation(osg::RenderInfo& renderInfo) const { osg::notify(osg::NOTICE)<<" Vertices STATIC"<getDataSize(), _vertices.first->getDataType(), 0, _vertices.first->getDataPointer()); + else + state.disableVertexPointer(); + + if (_normals.first.valid()) + { + state.setNormalPointer(_normals.first->getDataType(),0,_normals.first->getDataPointer()); + } + else + { + state.disableNormalPointer(); + glNormal3f(0.0f,0.0f,1.0f); + } + + if (_colors.first.valid() && _colors.first->getNumElements()==_vertices.first->getNumElements()) + { + state.setColorPointer(_colors.first->getDataSize(),_colors.first->getDataType(),0,_colors.first->getDataPointer()); + } + else + { + state.disableColorPointer(); + + if (_colors.first.valid() && _colors.first->getNumElements()>=1) + { + glColor4fv(static_cast(_colors.first->getDataPointer())); + } + else + { + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + } + } + + unsigned int unit; + for(unit=0;unit<_texcoords.size();++unit) + { + const osg::Array* array = _texcoords[unit].first.get(); + if (array) + state.setTexCoordPointer(unit,array->getDataSize(),array->getDataType(),0,array->getDataPointer()); + else + state.disableTexCoordPointer(unit); + } + state.disableTexCoordPointersAboveAndIncluding(unit); + + bool usingVertexBufferObjects = false; + + for(PrimitiveSetList::const_iterator itr = _primitiveSets.begin(); + itr != _primitiveSets.end(); + ++itr) + { + (*itr)->draw(state, usingVertexBufferObjects); + + } }