VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
osgToBullet.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2025 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
8 
10 
11 #pragma once
12 
17 #include <vrfutil/vrfPrint.h>
18 #include <btBulletCollisionCommon.h>
19 #include <btBulletDynamicsCommon.h>
20 #include <memory>
22 #include <vlutil/vlSmartPointers.h>
23 #include <vlutil/vlFilename.h>
24 #include <geometry/surface.h>
26 
28 #include <osg/Matrix>
29 #include <osg/NodeVisitor>
30 #include <osg/TextureBuffer>
31 
33 #include <osgEarth/DrawInstanced>
34 
36 #include <vrvOsg/DtOsgRenderer.h>
37 #include <vrvOsg/DtSceneRoot.h>
40 
42 #include <osgEarth/MetadataNode>
43 
45 #include <vlutil/vlSmartPointers.h>
46 
48 #include <memory>
49 
50 #include <boost/regex.hpp>
51 
52 class btTriangleMesh;
53 class Coordinate_System;
55 
56 namespace osg
57 {
58  class Node;
59  class LOD;
60 }
61 
62 typedef std::vector<osg::Drawable*> DrawablesList;
63 
68  : public osg::NodeVisitor
70 {
71 public:
73  {
74  TreatPagedLODAsLOD = 0x00000001,
75  CheckGeodesForSurfaces = 0x00000002,
76  UseQuantization = 0x00000004,
77  BuildBvhNow = 0x00000008,
80  };
81 
83  osg::NodeVisitor::VisitorType visitorType = NodeVisitor::INTERSECTION_VISITOR,
84  osg::NodeVisitor::TraversalMode traverseMode = TRAVERSE_ALL_CHILDREN);
85 
86  virtual ~ConvertToBulletVisitor() override;
87 
88  void setLoadOnlyActiveSwitchState(bool value);
89  void setDefaultSoiltype(const std::string& defaultSoiltype);
90  bool loadOnlyActiveSwitchState() const;
91  bool hasFlags(ConvertToBulletFlags flags) const;
92  void setFlagsFor(ConvertToBulletFlags flags, bool val);
93  void setIgnoreSurfacesMatching(const std::vector<std::string>& ignoreSurfacesMatching);
95  void setDefaultSurface(const DtSurface& surface);
96  const osg::Matrix& matrix() const;
97  void setMatrix(const osg::Matrix& matrix);
100  void processModelsEncodedInUserdata(osg::Node& node);
101  virtual DtSurface getCurrentSurfaceFromSMCFID(osg::Node& node, bool& hasValidSurface);
102  virtual void getCurrentSurface(osg::Group& node);
103  DtSurface convertFilenameToSurface(const std::string& fname, int& position, int& matchClass, bool& ignored);
104  virtual DtSurface convertImageToSurfaceType(osg::StateSet* stateSet, bool& hasValidSurface, bool& ignored);
105 
107  struct BtTriangle
108  {
109  btVector3 point1;
110  btVector3 point2;
111  btVector3 point3;
112  };
113  typedef std::multimap<long long, BtTriangle> Fid2BttriangleMap;
114 
116  {
117  public:
119 
120  void addDynamicFeature(const std::shared_ptr<makVrv::DtDynamicTerrainFeature>& feature);
121  virtual DynamicFeatures dynamicFeatures() const override;
123 
124  protected:
125  static std::atomic<unsigned int> theLastId;
126 
127  typedef std::vector< std::weak_ptr<makVrv::DtDynamicTerrainFeature> > DynamicFeatureVector;
130  };
131 
133  {
135  DtSwitchableCollisionObject(long long fid);
136 
137  public:
138  static std::shared_ptr<DtSwitchableCollisionObject> Create();
139  static std::shared_ptr<DtSwitchableCollisionObject> Create(long long fid);
140  long long getFid();
141  virtual bool enabled() const override;
142  void hide(bool value);
145 
146  protected:
147  std::atomic<unsigned int> myDisabledCounter;
148  long long myFid;
149  bool myHidden;
150  };
151 
152  class NodeId
153  {
154  public:
155  typedef std::list<NodeId> List;
156  explicit NodeId(size_t hash);
157  NodeId* addChild(size_t hash);
158  const List& children() const;
159  void addCollisionObject(const std::shared_ptr<btCollisionObject>& obj);
161 
162  private:
165  size_t myHash;
166  };
167 
168  bool ignoreDrawable(osg::Drawable* drawable) const;
169  size_t getScaleHash(const osg::Vec3d& scale) const;
170  void createMeshForDrawables(const DtSurface& surface, const DrawablesList& drawables, osg::Node& node);
171  void createMeshFromDrawables(osg::Geode& geode, DtSurface surface, bool hasValidSurface);
172  virtual bool createGeometryFromCache(osg::Node& node);
173 
175  virtual void apply(osg::Geode& geode) override;
176  virtual void apply(osg::Drawable& drawable) override;
177  virtual void apply(osg::Geometry& geometry) override;
178  virtual void apply(osg::LOD& node) override;
179  virtual void apply(osg::PagedLOD& node) override;
180  virtual void apply(osg::Group& node) override;
181  virtual void apply(osg::Sequence& node) override;
182  virtual void apply(osg::Transform& node) override;
183  virtual void apply(osg::MatrixTransform& node) override;
184  virtual void apply(osg::Node& node) override;
185 
187  {
188  public:
189  VrfSwitch();
190  virtual void setFeature(const std::shared_ptr<makVrv::DtDynamicTerrainFeature>& feature) override;
191  void addObjectToStateState(unsigned int index, const std::shared_ptr<btCollisionObject>& obj);
192  void addObjectsToStateState(unsigned int index, DtCollisionObjectList& objects);
193  virtual void setState(unsigned int index) override;
194  virtual unsigned int getState() const override;
195  virtual unsigned int getPreviousState() const override;
196  virtual unsigned int numStates() const;
197 
198  private:
199  typedef std::vector<std::shared_ptr<DtSwitchableCollisionObject>> CollisionObjectVector;
200  typedef std::vector<CollisionObjectVector> CollisionObjectSets;
201 
204  unsigned int myPreviousState;
205  };
206 
210  const osg::ref_ptr<makVrv::DtDynamicTerrainSwitchNode>& sw, size_t hash) override;
212 
213  typedef std::vector<osg::Matrix> MatrixStack;
214 
215 protected:
216 
217  void createCollisionObjectFromMesh(std::unique_ptr<DtTriangleMeshWithSoil>& mesh, osg::Node& node, long long fid);
218  void collectInstanceTriangles(osgEarth::DrawInstanced::InstanceGeometry* instancedGeometry, Fid2BttriangleMap& geometryMap);
219 
220  typedef std::list<DtBvhTriangleMeshShape::Ptr> ShapeList;
221  typedef std::map<size_t, ShapeList> ScaledInstanceShapeMap;
222  typedef std::unordered_map<osg::Node*, ScaledInstanceShapeMap> NodeToShapeMap;
223 
225 
229  osg::Matrix myMatrix;
230 
232  inline void pushMatrix(osg::Matrix& matrix) { myMatrixStack.push_back(matrix); }
233  inline void popMatrix() { myMatrixStack.pop_back(); }
234 
235  std::vector<boost::regex> myIgnoreRegexSurfacesMatching;
239  std::string myDefaultSoiltype;
240 
241  class ParentStackGuard : boost::noncopyable
242  {
243  public:
244  explicit ParentStackGuard(osg::Node& node, ConvertToBulletVisitor& visitor);
246 
247  private:
250  };
251 
252  friend class ParentStackGuard;
253 
256 
257  typedef boost::unordered_map<size_t, NodeId*> HashToNodeIdMap;
259 
260  typedef boost::unordered_map<osg::Node*, NodeId*> NodeToNodeIdMap;
262 
264  osgEarth::MetadataNode* myMetadataNode;
266  osg::observer_ptr< osg::TextureBuffer > myTbo;
267 
268 public:
274 };
275 
276 
282 {
283 public:
285  static std::unique_ptr<DtVrfSimTerrainProcessor> Create();
286 
287  static std::unique_ptr<DtVrfSimTerrainProcessor> Create(DtVantageTerrainEngine&);
288 
290  virtual ~DtVrfSimTerrainProcessor();
291 
292  virtual unsigned int numberOfTriangles() const = 0;
293 
294  virtual void setDefaultSurface(const DtSurface&) = 0;
295  virtual void setMatrix(const osg::Matrix&) = 0;
296  virtual void setTraversalMode(osg::NodeVisitor::TraversalMode) = 0;
297  virtual void setVisitorType(osg::NodeVisitor::VisitorType) = 0;
298  virtual void setSurfaceCharacteristicsMap(makVrf::DtSurfaceCharacteristicMapConstPtr) = 0;
299  virtual void setSurfacesToIgnore(const std::vector<std::string>&) = 0;
300 
301  virtual void setTreatPagedLODAsLOD(bool) = 0;
302  virtual void setCheckGeodesForSurfaces(bool) = 0;
303  virtual void setUseQuantization(bool) = 0;
304  virtual void setBuildBvhNow(bool) = 0;
305  virtual void setFilterTransparentPolygons(bool) = 0;
306 
307  virtual makVrv::DtDynamicTerrainWorld::FeatureHandle createTerrainObjects(
308  osg::Node*, const std::string& defaultSoiltype, bool LoadOnlyActiveSwitchState = false) = 0;
309 
310  virtual void getCollisionObjects(DtCollisionObjectList& objs) = 0;
311 
312 protected:
314 
316 };
List myChildNodes
Definition: osgToBullet.h:164
void setConversionFlags(ConvertToBulletVisitor::ConvertToBulletFlags f)
int myNumBulletObjects
Definition: osgToBullet.h:272
std::vector< std::shared_ptr< makVrv::DtDynamicTerrainFeature > > DynamicFeatures
Definition: bulletUtil.h:53
virtual unsigned int getState() const override
Get the current state of the switch.
makVrf::DtSurfaceCharacteristicMapConstPtr mySurfaceCharacteristicMap
Definition: osgToBullet.h:228
long long myFid
Definition: osgToBullet.h:148
std::shared_ptr< const DtSurfaceCharacteristicMap > DtSurfaceCharacteristicMapConstPtr
Definition: surfaceCharacteristicsMap.h:36
ParentStackGuard(osg::Node &node, ConvertToBulletVisitor &visitor)
void createMeshFromDrawables(osg::Geode &geode, DtSurface surface, bool hasValidSurface)
bool myLoadOnlyActiveSwitchState
Definition: osgToBullet.h:238
std::list< NodeId > List
Definition: osgToBullet.h:155
bool ignoreDrawable(osg::Drawable *drawable) const
MatrixStack myMatrixStack
Definition: osgToBullet.h:231
Definition: osgToBullet.h:186
virtual DtSurface convertImageToSurfaceType(osg::StateSet *stateSet, bool &hasValidSurface, bool &ignored)
DtVantageTerrainEngine * myTerrainEngine
Definition: osgToBullet.h:315
HashToNodeIdMap myHashToNodeIdMap
Definition: osgToBullet.h:258
DtSurface myCurrentSurface
Definition: osgToBullet.h:227
std::vector< CollisionObjectVector > CollisionObjectSets
Definition: osgToBullet.h:200
ConvertToBulletFlags myFlags
Definition: osgToBullet.h:236
boost::unordered_map< size_t, NodeId * > HashToNodeIdMap
Definition: osgToBullet.h:257
btVector3 point3
Definition: osgToBullet.h:111
Definition: osgToBullet.h:74
virtual unsigned int getPreviousState() const override
Get the previous state of the switch. Return default (0) if the switch state has never been changed...
unsigned myNumGeodeParents
Definition: osgToBullet.h:237
DynamicFeatureVector myFeatures
Definition: osgToBullet.h:128
std::vector< osg::Drawable * > DrawablesList
Definition: osgToBullet.h:62
size_t getScaleHash(const osg::Vec3d &scale) const
NodeId myRootNodeId
Definition: osgToBullet.h:254
unsigned int DtTerrainInstanceId
Unique ID numbers that reference a specific instance of a set of geometry in the terrain. Each terrain instance references a specific terrain geometry by id, and multiple objects may share the same geometry.
Definition: terrainProcessPolygonsBox.h:41
ConvertToBulletVisitor::ConvertToBulletFlags conversionFlags() const
osg::Matrix myMatrix
Definition: osgToBullet.h:229
void createCollisionObjectFromMesh(std::unique_ptr< DtTriangleMeshWithSoil > &mesh, osg::Node &node, long long fid)
virtual void apply(osg::Geode &geode) override
Apply.
std::vector< std::weak_ptr< makVrv::DtDynamicTerrainFeature > > DynamicFeatureVector
Definition: osgToBullet.h:127
ConvertToBulletVisitor(DtVantageTerrainEngine *terrainEngine, osg::NodeVisitor::VisitorType visitorType=NodeVisitor::INTERSECTION_VISITOR, osg::NodeVisitor::TraversalMode traverseMode=TRAVERSE_ALL_CHILDREN)
NodeToShapeMap myNodeToShapeMap
Definition: osgToBullet.h:226
Definition: osgToBullet.h:79
void setIgnoreSurfacesMatching(const std::vector< std::string > &ignoreSurfacesMatching)
DtSurface convertFilenameToSurface(const std::string &fname, int &position, int &matchClass, bool &ignored)
Definition: osgToBullet.h:152
Definition: surface.h:181
virtual bool createGeometryFromCache(osg::Node &node)
void collectInstanceTriangles(osgEarth::DrawInstanced::InstanceGeometry *instancedGeometry, Fid2BttriangleMap &geometryMap)
State switch. This is an interface for manipulating a switch. A switch in this context is defined as ...
Definition: DtDynamicTerrainStateManipulatorFactory.h:107
osgEarth::MetadataNode * myMetadataNode
Store the metadata node currently visiting.
Definition: osgToBullet.h:264
virtual std::shared_ptr< DtDynamicTerrainFeature > feature() const
virtual ~ConvertToBulletVisitor() override
NodeToNodeIdMap myNodeToNodeIdMap
Definition: osgToBullet.h:261
ConvertToBulletVisitor & myVisitor
Definition: osgToBullet.h:248
btVector3 point1
Definition: osgToBullet.h:109
Contains makVrv::DtDynamicTerrainStateManipulatorFactory class.
btVector3 point2
Definition: osgToBullet.h:110
bool myHidden
Definition: osgToBullet.h:149
std::map< size_t, ShapeList > ScaledInstanceShapeMap
Definition: osgToBullet.h:221
Definition: coordSystem.h:54
void addCollisionObject(const std::shared_ptr< btCollisionObject > &obj)
virtual void setFeature(const std::shared_ptr< makVrv::DtDynamicTerrainFeature > &feature) override
Definition: osgToBullet.h:241
void addObjectToStateState(unsigned int index, const std::shared_ptr< btCollisionObject > &obj)
virtual DtSurface getCurrentSurfaceFromSMCFID(osg::Node &node, bool &hasValidSurface)
void setDefaultSoiltype(const std::string &defaultSoiltype)
DtCollisionObjectList myObjects
Definition: osgToBullet.h:163
NodeId * addChild(size_t hash)
std::multimap< long long, BtTriangle > Fid2BttriangleMap
Definition: osgToBullet.h:113
Definition: osgToBullet.h:77
Factory for creating objects which implement some kind of manipulation of the state of dynamically ch...
Definition: DtDynamicTerrainStateManipulatorFactory.h:64
CollisionObjectSets myObjects
Definition: osgToBullet.h:202
DtTerrainInstanceId myObjectId
Definition: osgToBullet.h:129
std::unordered_map< osg::Node *, ScaledInstanceShapeMap > NodeToShapeMap
Definition: osgToBullet.h:222
DtTerrainInstanceId objectId() const
int myNumTriangles
Definition: osgToBullet.h:270
DtVantageTerrainEngine * myTerrainEngine
Definition: osgToBullet.h:224
void popMatrix()
Definition: osgToBullet.h:233
const List & children() const
int myNumScaledGeometries
Definition: osgToBullet.h:273
void getCollisionObjects(DtCollisionObjectList &objs)
void addObjectsToStateState(unsigned int index, DtCollisionObjectList &objects)
std::list< DtBvhTriangleMeshShape::Ptr > ShapeList
Definition: osgToBullet.h:220
Definition: bvhTriangleMeshShape.h:112
int myNumDrawables
Definition: osgToBullet.h:269
void setFlagsFor(ConvertToBulletFlags flags, bool val)
virtual void getCurrentSurface(osg::Group &node)
Bullet triangle.
Definition: osgToBullet.h:107
NodeId * myCurrentParentNodeId
Definition: osgToBullet.h:255
virtual makVrv::DtDynamicTerrainSwitch * createSwitch(const osg::ref_ptr< osgSim::MultiSwitch > &mswitch) override
Create a DtDynamicTerrainSwitch which manipulates the supplied multiswitch.
virtual DynamicFeatures dynamicFeatures() const override
Return all the dynamic features which contain this object.
void setSurfaceCharMap(makVrf::DtSurfaceCharacteristicMapConstPtr surfCharMap)
ConvertToBulletFlags
Definition: osgToBullet.h:72
Contains makVrv::DtSceneRoot class.
virtual unsigned int numStates() const
void setLoadOnlyActiveSwitchState(bool value)
static std::atomic< unsigned int > theLastId
Definition: osgToBullet.h:125
std::list< std::shared_ptr< btCollisionObject > > DtCollisionObjectList
Definition: bulletUtil.h:47
#define DT_DLL_vantageTerrainImplementation
This file is used to determine how to build.
Definition: vantageTerrainImplementationDefines.h:28
Contains makVrv::DtDynamicTerrainWorld class.
std::vector< std::shared_ptr< DtSwitchableCollisionObject > > CollisionObjectVector
Definition: osgToBullet.h:199
Note: The implementation of this processor class can be found in osgToBullet.cxx (class called DtVrfS...
Definition: osgToBullet.h:281
void addDynamicFeature(const std::shared_ptr< makVrv::DtDynamicTerrainFeature > &feature)
Feature Insertion/Removal/Paging Features represent parts of the terrain which are individually ident...
Definition: DtDynamicTerrainWorld.h:97
std::atomic< unsigned int > myDisabledCounter
Definition: osgToBullet.h:147
void createMeshForDrawables(const DtSurface &surface, const DrawablesList &drawables, osg::Node &node)
int myNumBulletShapes
Definition: osgToBullet.h:271
This class wraps the VR-Vantage Display Engine, and uses as a terrain loader and intersector. This is the terrain engine that powers the DtVantageTerrainImplementation. The DtVantageTerrainEngine runs the DtDe it its own thread, and processes commands thought the command queue.
Definition: vantageTerrainEngine.h:112
const osg::Matrix & matrix() const
virtual bool enabled() const override
Intersections will ignore this object if this returns false.
size_t myHash
Definition: osgToBullet.h:165
std::vector< boost::regex > myIgnoreRegexSurfacesMatching
Definition: osgToBullet.h:235
int myCurrentState
Definition: osgToBullet.h:203
Contains makVrv::DtOsgRenderer class.
ConvertToBulletVisitor.
Definition: osgToBullet.h:67
virtual void setState(unsigned int index) override
Set the current state of the switch (0 indexed).
void setDefaultSurface(const DtSurface &surface)
void setMatrix(const osg::Matrix &matrix)
unsigned int myPreviousState
Definition: osgToBullet.h:204
std::string myDefaultSoiltype
Definition: osgToBullet.h:239
void getAllCollisionObjects(DtCollisionObjectList &objs) const
void pushMatrix(osg::Matrix &matrix)
Definition: osgToBullet.h:232
Definition: osgToBullet.h:76
static std::shared_ptr< DtSwitchableCollisionObject > Create()
Definition: osgToBullet.h:115
std::vector< osg::Matrix > MatrixStack
Definition: osgToBullet.h:213
osg::observer_ptr< osg::TextureBuffer > myTbo
TBO to look up instance ids.
Definition: osgToBullet.h:266
bool loadOnlyActiveSwitchState() const
boost::unordered_map< osg::Node *, NodeId * > NodeToNodeIdMap
Definition: osgToBullet.h:260
void processModelsEncodedInUserdata(osg::Node &node)
NodeId * mySavedParentId
Definition: osgToBullet.h:249
bool hasFlags(ConvertToBulletFlags flags) const

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)