From 7a879e2abf3c3601741971c964ba05ebaef57770 Mon Sep 17 00:00:00 2001 From: Mathias Froehlich Date: Fri, 24 Aug 2012 21:20:40 +0200 Subject: [PATCH] bvh: Introduce BVHMaterial independent of SGMaterial. The bounding volumes in core should not depend on scenery. Therefore reference material properties relevant for the BVH tree in BVHMaterial. --- simgear/bvh/BVHLineSegmentVisitor.hxx | 4 +- simgear/bvh/BVHMaterial.cxx | 35 +++++++++ simgear/bvh/BVHMaterial.hxx | 75 +++++++++++++++++++ simgear/bvh/BVHNearestPointVisitor.hxx | 4 +- simgear/bvh/BVHStaticData.hxx | 14 ++-- simgear/bvh/BVHStaticGeometryBuilder.hxx | 10 +-- simgear/bvh/CMakeLists.txt | 2 + simgear/scene/material/mat.cxx | 16 ++-- simgear/scene/material/mat.hxx | 44 +---------- .../model/BoundingVolumeBuildVisitor.hxx | 16 ++-- 10 files changed, 141 insertions(+), 79 deletions(-) create mode 100644 simgear/bvh/BVHMaterial.cxx create mode 100644 simgear/bvh/BVHMaterial.hxx diff --git a/simgear/bvh/BVHLineSegmentVisitor.hxx b/simgear/bvh/BVHLineSegmentVisitor.hxx index cca02588..40783ef3 100644 --- a/simgear/bvh/BVHLineSegmentVisitor.hxx +++ b/simgear/bvh/BVHLineSegmentVisitor.hxx @@ -54,7 +54,7 @@ public: { return _linearVelocity; } const SGVec3d& getAngularVelocity() const { return _angularVelocity; } - const SGMaterial* getMaterial() const + const BVHMaterial* getMaterial() const { return _material; } BVHNode::Id getId() const { return _id; } @@ -87,7 +87,7 @@ private: SGVec3d _normal; SGVec3d _linearVelocity; SGVec3d _angularVelocity; - const SGMaterial* _material; + const BVHMaterial* _material; BVHNode::Id _id; bool _haveHit; diff --git a/simgear/bvh/BVHMaterial.cxx b/simgear/bvh/BVHMaterial.cxx new file mode 100644 index 00000000..c04af86d --- /dev/null +++ b/simgear/bvh/BVHMaterial.cxx @@ -0,0 +1,35 @@ +// Copyright (C) 2008 - 2012 Mathias Froehlich - Mathias.Froehlich@web.de +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#include "BVHMaterial.hxx" + +namespace simgear { + +BVHMaterial::BVHMaterial() : + _solid(true), + _friction_factor(1), + _rolling_friction(0.02), + _bumpiness(0), + _load_resistance(1e30) +{ +} + +BVHMaterial::~BVHMaterial() +{ +} + +} diff --git a/simgear/bvh/BVHMaterial.hxx b/simgear/bvh/BVHMaterial.hxx new file mode 100644 index 00000000..90c4d381 --- /dev/null +++ b/simgear/bvh/BVHMaterial.hxx @@ -0,0 +1,75 @@ +// Copyright (C) 2008 - 2012 Mathias Froehlich - Mathias.Froehlich@web.de +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#ifndef BVHMaterial_hxx +#define BVHMaterial_hxx + +#include + +namespace simgear { + +class BVHMaterial : public SGReferenced { +public: + BVHMaterial(); + virtual ~BVHMaterial(); + + /** + * Return if the surface material is solid, if it is not solid, a fluid + * can be assumed, that is usually water. + */ + bool get_solid () const { return _solid; } + + /** + * Get the friction factor for that material + */ + double get_friction_factor () const { return _friction_factor; } + + /** + * Get the rolling friction for that material + */ + double get_rolling_friction () const { return _rolling_friction; } + + /** + * Get the bumpines for that material + */ + double get_bumpiness () const { return _bumpiness; } + + /** + * Get the load resistance + */ + double get_load_resistance () const { return _load_resistance; } + +protected: + // True if the material is solid, false if it is a fluid + bool _solid; + + // the friction factor of that surface material + double _friction_factor; + + // the rolling friction of that surface material + double _rolling_friction; + + // the bumpiness of that surface material + double _bumpiness; + + // the load resistance of that surface material + double _load_resistance; +}; + +} + +#endif diff --git a/simgear/bvh/BVHNearestPointVisitor.hxx b/simgear/bvh/BVHNearestPointVisitor.hxx index 1c07c66a..5b5e1975 100644 --- a/simgear/bvh/BVHNearestPointVisitor.hxx +++ b/simgear/bvh/BVHNearestPointVisitor.hxx @@ -140,7 +140,7 @@ public: { return _linearVelocity; } const SGVec3d& getAngularVelocity() const { return _angularVelocity; } - const SGMaterial* getMaterial() const + const BVHMaterial* getMaterial() const { return _material; } BVHNode::Id getId() const { return _id; } @@ -155,7 +155,7 @@ private: SGVec3d _point; SGVec3d _linearVelocity; SGVec3d _angularVelocity; - const SGMaterial* _material; + const BVHMaterial* _material; BVHNode::Id _id; bool _havePoint; diff --git a/simgear/bvh/BVHStaticData.hxx b/simgear/bvh/BVHStaticData.hxx index 400632ad..d90bcb80 100644 --- a/simgear/bvh/BVHStaticData.hxx +++ b/simgear/bvh/BVHStaticData.hxx @@ -22,11 +22,7 @@ #include #include #include - -/// FIXME, the SGMaterial class is too much tied to the scenegraph aspects of -/// the materials. Use some class more decribing the -/// nature of the surface we live on ... -class SGMaterial; +#include "BVHMaterial.hxx" namespace simgear { @@ -40,20 +36,20 @@ public: { return _vertices[i]; } - unsigned addMaterial(const SGMaterial* material) + unsigned addMaterial(const BVHMaterial* material) { _materials.push_back(material); return _materials.size() - 1; } - const SGMaterial* getMaterial(unsigned i) const + const BVHMaterial* getMaterial(unsigned i) const { if (_materials.size() <= i) return 0; return _materials[i]; } void trim() { std::vector(_vertices).swap(_vertices); - std::vector(_materials).swap(_materials); + std::vector >(_materials).swap(_materials); } private: std::vector _vertices; - std::vector _materials; + std::vector > _materials; }; } diff --git a/simgear/bvh/BVHStaticGeometryBuilder.hxx b/simgear/bvh/BVHStaticGeometryBuilder.hxx index 98968162..b390d931 100644 --- a/simgear/bvh/BVHStaticGeometryBuilder.hxx +++ b/simgear/bvh/BVHStaticGeometryBuilder.hxx @@ -78,16 +78,16 @@ public: typedef std::set > TriangleSet; TriangleSet _triangleSet; - void setCurrentMaterial(const SGMaterial* material) + void setCurrentMaterial(const BVHMaterial* material) { _currentMaterial = material; _currentMaterialIndex = addMaterial(material); } - const SGMaterial* getCurrentMaterial() const + const BVHMaterial* getCurrentMaterial() const { return _currentMaterial; } - unsigned addMaterial(const SGMaterial* material) + unsigned addMaterial(const BVHMaterial* material) { MaterialMap::const_iterator i = _materialMap.find(material); if (i != _materialMap.end()) @@ -97,9 +97,9 @@ public: return index; } - typedef std::map MaterialMap; + typedef std::map MaterialMap; MaterialMap _materialMap; - const SGMaterial* _currentMaterial; + const BVHMaterial* _currentMaterial; unsigned _currentMaterialIndex; void addTriangle(const SGVec3f& v1, const SGVec3f& v2, const SGVec3f& v3) diff --git a/simgear/bvh/CMakeLists.txt b/simgear/bvh/CMakeLists.txt index f1a8340e..ee6f9eb5 100644 --- a/simgear/bvh/CMakeLists.txt +++ b/simgear/bvh/CMakeLists.txt @@ -16,6 +16,7 @@ set(HEADERS BVHStaticNode.hxx BVHStaticTriangle.hxx BVHSubTreeCollector.hxx + BVHMaterial.hxx BVHTransform.hxx BVHVisitor.hxx ) @@ -32,6 +33,7 @@ set(SOURCES BVHStaticNode.cxx BVHStaticTriangle.cxx BVHSubTreeCollector.cxx + BVHMaterial.cxx BVHTransform.cxx ) diff --git a/simgear/scene/material/mat.cxx b/simgear/scene/material/mat.cxx index 5f252918..51b2d2db 100644 --- a/simgear/scene/material/mat.cxx +++ b/simgear/scene/material/mat.cxx @@ -347,11 +347,11 @@ SGMaterial::read_properties(const SGReaderWriterOptions* options, } // surface values for use with ground reactions - solid = props->getBoolValue("solid", true); - friction_factor = props->getDoubleValue("friction-factor", 1.0); - rolling_friction = props->getDoubleValue("rolling-friction", 0.02); - bumpiness = props->getDoubleValue("bumpiness", 0.0); - load_resistance = props->getDoubleValue("load-resistance", 1e30); + _solid = props->getBoolValue("solid", _solid); + _friction_factor = props->getDoubleValue("friction-factor", _friction_factor); + _rolling_friction = props->getDoubleValue("rolling-friction", _rolling_friction); + _bumpiness = props->getDoubleValue("bumpiness", _bumpiness); + _load_resistance = props->getDoubleValue("load-resistance", _load_resistance); // Taken from default values as used in ac3d ambient[0] = props->getDoubleValue("ambient/r", 0.2); @@ -425,12 +425,6 @@ SGMaterial::init () light_coverage = 0.0; building_coverage = 0.0; - solid = true; - friction_factor = 1; - rolling_friction = 0.02; - bumpiness = 0; - load_resistance = 1e30; - shininess = 1.0; for (int i = 0; i < 4; i++) { ambient[i] = (i < 3) ? 0.2 : 1.0; diff --git a/simgear/scene/material/mat.hxx b/simgear/scene/material/mat.hxx index 3a4afe87..1f175790 100644 --- a/simgear/scene/material/mat.hxx +++ b/simgear/scene/material/mat.hxx @@ -44,6 +44,7 @@ class StateSet; #include #include +#include namespace simgear { @@ -67,7 +68,7 @@ class SGTexturedTriangleBin; * defined in the $FG_ROOT/materials.xml file, and can be changed * at runtime. */ -class SGMaterial : public SGReferenced { +class SGMaterial : public simgear::BVHMaterial { public: @@ -254,32 +255,6 @@ public: */ inline std::string get_tree_texture () const { return tree_texture; } - /** - * Return if the surface material is solid, if it is not solid, a fluid - * can be assumed, that is usually water. - */ - bool get_solid () const { return solid; } - - /** - * Get the friction factor for that material - */ - double get_friction_factor () const { return friction_factor; } - - /** - * Get the rolling friction for that material - */ - double get_rolling_friction () const { return rolling_friction; } - - /** - * Get the bumpines for that material - */ - double get_bumpiness () const { return bumpiness; } - - /** - * Get the load resistance - */ - double get_load_resistance () const { return load_resistance; } - /** * Get the list of names for this material */ @@ -434,21 +409,6 @@ private: // Number of varieties of tree texture int tree_varieties; - // True if the material is solid, false if it is a fluid - bool solid; - - // the friction factor of that surface material - double friction_factor; - - // the rolling friction of that surface material - double rolling_friction; - - // the bumpiness of that surface material - double bumpiness; - - // the load resistance of that surface material - double load_resistance; - // material properties SGVec4f ambient, diffuse, specular, emission; double shininess; diff --git a/simgear/scene/model/BoundingVolumeBuildVisitor.hxx b/simgear/scene/model/BoundingVolumeBuildVisitor.hxx index f544606b..5be621bf 100644 --- a/simgear/scene/model/BoundingVolumeBuildVisitor.hxx +++ b/simgear/scene/model/BoundingVolumeBuildVisitor.hxx @@ -334,11 +334,11 @@ public: std::swap(_geometryBuilder, primitiveFunctor._geometryBuilder); } - void setCurrentMaterial(const SGMaterial* material) + void setCurrentMaterial(const BVHMaterial* material) { _geometryBuilder->setCurrentMaterial(material); } - const SGMaterial* getCurrentMaterial() const + const BVHMaterial* getCurrentMaterial() const { return _geometryBuilder->getCurrentMaterial(); } @@ -384,10 +384,10 @@ public: { } - const SGMaterial* pushMaterial(osg::Geode* geode) + const BVHMaterial* pushMaterial(osg::Geode* geode) { - const SGMaterial* oldMaterial = _primitiveFunctor.getCurrentMaterial(); - const SGMaterial* material = SGMaterialLib::findMaterial(geode); + const BVHMaterial* oldMaterial = _primitiveFunctor.getCurrentMaterial(); + const BVHMaterial* material = SGMaterialLib::findMaterial(geode); if (material) _primitiveFunctor.setCurrentMaterial(material); return oldMaterial; @@ -403,7 +403,7 @@ public: if (hasBoundingVolumeTree(geode)) return; - const SGMaterial* oldMaterial = pushMaterial(&geode); + const BVHMaterial* oldMaterial = pushMaterial(&geode); bool flushHere = getNodePath().size() <= 1 || _dumpIntoLeafs; if (flushHere) { @@ -411,7 +411,7 @@ public: PFunctor previousPrimitives; _primitiveFunctor.swap(previousPrimitives); - const SGMaterial* mat = previousPrimitives.getCurrentMaterial(); + const BVHMaterial* mat = previousPrimitives.getCurrentMaterial(); _primitiveFunctor.setCurrentMaterial(mat); // walk the children @@ -458,7 +458,7 @@ public: PFunctor previousPrimitives; _primitiveFunctor.swap(previousPrimitives); - const SGMaterial* mat = previousPrimitives.getCurrentMaterial(); + const BVHMaterial* mat = previousPrimitives.getCurrentMaterial(); _primitiveFunctor.setCurrentMaterial(mat); // walk the children