23#include <osg/Drawable>
24#include <osg/PrimitiveSet>
41 template<
typename M1,
typename M2>
44 static_assert(
sizeof( M1 ) ==
sizeof( M2 ),
"Expecting 4x4 matrices with the same value type." );
45 for (
int r = 0; r < 4; ++r )
46 for (
int c = 0; c < 4; ++c )
47 if ( m1( r, c ) != m2( r, c ) )
79 template<
typename TriangleBuffer>
82 using Index =
typename TriangleBuffer::value_type;
120 static bool sort( osg::Geometry& geometry,
121 const osg::Matrixf& modelViewMatrix,
141 template<
typename TriangleBuffer>
142 static bool impl( TriangleBuffer& triangles,
143 const osg::Vec3Array& vertices,
144 const osg::Matrixf& modelViewMatrix,
147 if ( triangles.empty() || ( triangles.size() % 3 ) != 0 || !depthReduce )
151 using Triangles = std::vector<TriangleType>;
153 const auto getDepth = [&modelViewMatrix, &vertices](
typename TriangleType::Index index ) ->
float
155 return ( vertices[ index ] * modelViewMatrix ).z();
158 Triangles triangleDepths;
159 triangleDepths.reserve( triangles.size() / 3 );
160 for (
size_t i = 0u; i < triangles.size(); i += 3 ) {
166 triangleDepths.push_back( {
169 getDepth( indices[ 0 ] ),
170 getDepth( indices[ 1 ] ),
171 getDepth( indices[ 2 ] )
176 std::stable_sort( triangleDepths.begin(),
177 triangleDepths.end(),
178 [](
const TriangleType& t1,
const TriangleType& t2 ) ->
bool
180 return t1.depth < t2.depth;
183 for (
size_t t = 0u; t < triangleDepths.size(); ++t )
184 for (
size_t i = 0u; i < 3u; ++i )
185 triangles[ 3 * t + i ] = triangleDepths[ t ].indices[ i ];
193 float min = std::numeric_limits<float>::infinity();
194 for (
const auto depth : depths )
195 min = std::min( depth, min );
201 return ( 1.0f / 3.0f ) * ( depths[ 0 ] + depths[ 1 ] + depths[ 2 ] );
206 float max = -std::numeric_limits<float>::infinity();
207 for (
const auto depth : depths )
208 max = std::max( depth, max );
247 using osg::Drawable::CullCallback::cull;
249 virtual bool cull( osg::NodeVisitor* visitor, osg::Drawable* drawable, osg::RenderInfo* renderInfo )
const override;
252 mutable bool isUpdated =
false;
253 mutable osg::Matrixf prevModelViewMatrix{};
254 mutable osg::Matrixf modelViewMatrix{};
Checks all primitive sets of a render geometry for triangles and sort the triangles back to front giv...
std::function< float(TriangleVertexDepths)> DepthReduceFunc
const std::array< float, 3u > & TriangleVertexDepths
static bool impl(TriangleBuffer &triangles, const osg::Vec3Array &vertices, const osg::Matrixf &modelViewMatrix, const DepthReduceFunc &depthReduce)
Implementation of the sorting when triangle primitive sets can be many things.
static float midOf(TriangleVertexDepths depths)
Depth reduce as avg([d1, d2, d3]).
static bool sort(osg::Geometry &geometry, const osg::Matrixf &modelViewMatrix, DepthReduceFunc depthReduce=midOf)
Sort all triangle primitive sets of the given geometry.
static float maxOf(TriangleVertexDepths depths)
Depth reduce as max([d1, d2, d3]).
static float minOf(TriangleVertexDepths depths)
Depth reduce as min([d1, d2, d3]).
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
AGXOSG_EXPORT osg::StateSet & configureStateSet(osg::StateSet &stateSet, bool isTransparent)
Configures, or reconfigures, state set for opaque or transparent objects.
bool matrix4x4ExactlyEqual(const M1 &m1, const M2 &m2)
Callback when the model view matrix of the main camera has been updated.
ModelViewMatrixFinder()=default
Default constructor for polling and manual handling of isUpdated.
virtual ~ModelViewMatrixFinder()=default
ModelViewMatrixFinder(OnModelViewUpdated onUpdated)
Construct given callback fired when the model view matrix of the main camera has been changed.
std::function< void(const osg::Matrixf &)> OnModelViewUpdated
virtual bool cull(osg::NodeVisitor *visitor, osg::Drawable *drawable, osg::RenderInfo *renderInfo) const override
std::array< Index, 3u > Indices
typename TriangleBuffer::value_type Index