AGX Dynamics  2.37.2.0
TerrainVoxelDrawable.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agxOSG/export.h>
20 
21 #include <agxTerrain/Terrain.h>
22 
23 #include <agx/Task.h>
24 
25 #include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
26 #include <osg/Drawable>
27 #include <osg/Version>
28 #include <agx/PopDisableWarnings.h> // End of disabled warnings.
29 
30 namespace osgSim
31 {
32  class ColorRange;
33 }
34 
35 
36 namespace agxOSG
37 {
42  class AGXOSG_EXPORT TerrainVoxelDrawable : public osg::Drawable
43  {
44  public:
45  TerrainVoxelDrawable(agxTerrain::Terrain* terrain);
46 
47  void setRenderVoxelSolidMass( bool enable );
48  bool getRenderVoxelSolidMass() const;
49 
50  void setRenderVoxelFluidMass( bool enable );
51  bool getRenderVoxelFluidMass() const;
52 
53  void setRenderVoxelBoundingBox( bool enable );
54  bool getRenderVoxelBoundingBox() const;
55 
56  void setRenderVoxelCompaction( bool enable );
57  bool getRenderVoxelCompaction() const;
58 
59  void setRenderVelocityField( bool enable );
60  bool getRenderVelocityField() const;
61 
62  void setRenderTerrainMaterials(bool enable);
63  bool getRenderTerrainMaterials() const;
64 
65  void setRenderDefaultTerrainMaterial(bool enable);
66  bool getRenderDefaultTerrainMaterial() const;
67 
68  void setTerrainMaterialsColor( const agxRender::ColorVector& colors );
69  agxRender::ColorVector getTerrainMaterialColors() const;
70 
71  void setVelocityFieldLineColor(const agx::Vec4& lineColor);
72 
73  void setMaxVoxelValueColor(const agx::Vec4f& maxVoxelColor);
74 
78  bool hasEnabledFeatures() const;
79 
83  bool isInitialized() const;
84 
85  virtual osg::Object* cloneType() const { return new TerrainVoxelDrawable(m_terrain); }
86  virtual osg::Object* clone(const osg::CopyOp&) const { return new TerrainVoxelDrawable(*this); }
87  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const TerrainVoxelDrawable *>(obj) != nullptr; }
88  virtual const char* libraryName() const { return "agxOSG"; }
89  virtual const char* className() const { return "TerrainVoxelDrawable"; }
90 
92 #if OSG_VERSION_GREATER_OR_EQUAL(3,4,0)
93  virtual osg::BoundingSphere computeBound() const;
94  virtual osg::BoundingBox computeBoundingBox() const;
95 #else
96  virtual osg::BoundingBox computeBound() const;
97 #endif
98 
100  virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
101 
102  void updateDrawable();
103 
104  public:
105  void onAddNotification( agxSDK::Simulation* simulation );
106  void onRemoveNotification( agxSDK::Simulation* simulation );
107 
108  protected:
109  virtual ~TerrainVoxelDrawable();
110 
111 
112  void renderVelocityTableLines(
113  const agxTerrain::BasicGrid* solid,
114  const agx::AffineMatrix4x4& terrainTransform,
115  const agx::HashTable< agx::Vec3i, agx::Vec3f >& velocityTable,
116  agx::Real elementSize,
117  bool enableVelocityFieldRendering,
118  agxData::Buffer* linePositions,
119  agxData::Buffer* lineOffsets);
120 
121  void renderVoxels(
122  const agxTerrain::BasicGrid* solid,
123  const agxTerrain::BasicGrid* fluid,
124  const agxTerrain::BasicGrid* compaction,
125  const agxTerrain::VoxelToTerrainMaterialTable* inhomogeneousVoxels,
126  const agx::AffineMatrix4x4& terrainTransform,
127  agx::Real elementSize,
128  int numSolidVoxels,
129  bool enableRenderVoxelSolidMass,
130  bool enableRenderVoxelFluidMass,
131  bool enableRenderVoxelCompaction,
132  bool enableRenderTerrainMaterials,
133  bool enableRenderDefaultTerrainMaterial,
134  agxData::Buffer* verticesBuffer,
135  agxData::Buffer* colorBuffer,
136  agxData::Buffer* voxelSizeBuffer,
137  agxData::Buffer* enableRenderBuffer,
138  std::function<agx::Vec4f(float)> getColor);
139 
140  void renderInternalNodes(
141  const agxTerrain::BasicGrid* solid,
142  const agx::AffineMatrix4x4& terrainTransform,
143  int numSolidVoxels,
144  agxData::Buffer* nodesVertices,
145  agxData::Buffer* nodesColorBuffer,
146  agxData::Buffer* nodesFirstBuffer,
147  agxData::Buffer* nodesCountBuffer,
148  bool enableRenderVoxelBoundingBox);
149 
150 
152  // Variables
154  protected:
155  agxTerrain::TerrainRef m_terrain;
156  osg::Group* m_group;
157  agx::TaskGroupRef m_renderTask;
158  osg::ref_ptr<osgSim::ColorRange> m_colorRange;
159  bool m_enable;
160  float m_rScaling;
161  bool m_enableRenderVoxelSolidMass;
162  bool m_enableRenderVoxelFluidMass;
163  bool m_enableRenderVoxelCompaction;
164  bool m_enableRenderVoxelBoundingBox;
165  bool m_enableVelocityFieldRendering;
166  bool m_enableRenderTerrainMaterials;
167  bool m_enableRenderDefaultTerrainMaterial;
168 
169  agxRender::ColorVector m_terrainMaterialsColors;
170 
171  agxData::BufferRef m_verticesBuffer;
172  agxData::BufferRef m_colorBuffer;
173  agxData::BufferRef m_voxelSizeBuffer;
174  agxData::BufferRef m_voxelEnableRenderBuffer;
175  agx::Vec4f m_maxVoxelColor;
176 
177  // Bounding box data
178  agxData::BufferRef m_nodesVertices;
179  agxData::BufferRef m_nodesColorBuffer;
180  agxData::BufferRef m_nodesCountBuffer;
181  agxData::BufferRef m_nodesFirstBuffer;
182 
183  // Velocity field data
184  agxData::BufferRef m_linePositions;
185  agxData::BufferRef m_lineOffsets;
186  agx::TaskRef m_lineKernel;
187  };
189 }
#define AGXOSG_EXPORT
Pure virtual interface class for the data storage grid classes we have.
Definition: Grid.h:71
A terrain model based a 3D grid model with overlapping height field that can be deformed by interacti...
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
Templated vector class.
Definition: Vector.h:52
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
Abstract representation of a data buffer.
Definition: Buffer.h:55
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
AGXBRICK_EXPORT bool isInitialized()
double Real
Definition: Real.h:42
Inheritance with partial specialization due to bug with ref_ptr containers.
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97