AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
TransparencyUtils.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#pragma once
18
19#include <agxOSG/export.h>
20
21#include <osg/Array>
22#include <osg/Matrixf>
23#include <osg/Drawable>
24#include <osg/PrimitiveSet>
25
26#include <array>
27#include <numeric>
28#include <algorithm>
29#include <functional>
30
31namespace osg
32{
33 class Geometry;
34}
35
36namespace agxOSG
37{
41 template<typename M1, typename M2>
42 bool matrix4x4ExactlyEqual( const M1& m1, const M2& m2 )
43 {
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 ) )
48 return false;
49 return true;
50 }
51
74 {
75 public:
76 using TriangleVertexDepths = const std::array<float, 3u>&;
77 using DepthReduceFunc = std::function<float( TriangleVertexDepths )>;
78
79 template<typename TriangleBuffer>
81 {
82 using Index = typename TriangleBuffer::value_type;
83 using Indices = std::array<Index, 3u>;
84
86 float depth;
87 };
88
94 static float minOf( TriangleVertexDepths depths );
95
101 static float midOf( TriangleVertexDepths depths );
102
108 static float maxOf( TriangleVertexDepths depths );
109
120 static bool sort( osg::Geometry& geometry,
121 const osg::Matrixf& modelViewMatrix,
122 DepthReduceFunc depthReduce = midOf );
123
141 template<typename TriangleBuffer>
142 static bool impl( TriangleBuffer& triangles,
143 const osg::Vec3Array& vertices,
144 const osg::Matrixf& modelViewMatrix,
145 const DepthReduceFunc& depthReduce )
146 {
147 if ( triangles.empty() || ( triangles.size() % 3 ) != 0 || !depthReduce )
148 return false;
149
150 using TriangleType = TriangleDepth<TriangleBuffer>;
151 using Triangles = std::vector<TriangleType>;
152
153 const auto getDepth = [&modelViewMatrix, &vertices]( typename TriangleType::Index index ) -> float
154 {
155 return ( vertices[ index ] * modelViewMatrix ).z();
156 };
157
158 Triangles triangleDepths;
159 triangleDepths.reserve( triangles.size() / 3 );
160 for ( size_t i = 0u; i < triangles.size(); i += 3 ) {
161 std::array indices{
162 triangles[ i + 0 ],
163 triangles[ i + 1 ],
164 triangles[ i + 2 ],
165 };
166 triangleDepths.push_back( {
167 indices,
168 depthReduce( {
169 getDepth( indices[ 0 ] ),
170 getDepth( indices[ 1 ] ),
171 getDepth( indices[ 2 ] )
172 } )
173 } );
174 }
175
176 std::stable_sort( triangleDepths.begin(),
177 triangleDepths.end(),
178 []( const TriangleType& t1, const TriangleType& t2 ) -> bool
179 {
180 return t1.depth < t2.depth;
181 } );
182
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 ];
186
187 return true;
188 }
189 };
190
192 {
193 float min = std::numeric_limits<float>::infinity();
194 for ( const auto depth : depths )
195 min = std::min( depth, min );
196 return min;
197 }
198
200 {
201 return ( 1.0f / 3.0f ) * ( depths[ 0 ] + depths[ 1 ] + depths[ 2 ] );
202 }
203
205 {
206 float max = -std::numeric_limits<float>::infinity();
207 for ( const auto depth : depths )
208 max = std::max( depth, max );
209 return max;
210 }
211
213
229 struct AGXOSG_EXPORT ModelViewMatrixFinder : osg::Drawable::CullCallback
230 {
231 using OnModelViewUpdated = std::function<void( const osg::Matrixf& )>;
232
239
244
245 virtual ~ModelViewMatrixFinder() = default;
246
247 using osg::Drawable::CullCallback::cull;
248
249 virtual bool cull( osg::NodeVisitor* visitor, osg::Drawable* drawable, osg::RenderInfo* renderInfo ) const override;
250
252 mutable bool isUpdated = false;
253 mutable osg::Matrixf prevModelViewMatrix{};
254 mutable osg::Matrixf modelViewMatrix{};
255 };
256
263 AGXOSG_EXPORT osg::StateSet& configureStateSet( osg::StateSet& stateSet, bool isTransparent );
264}
#define AGXOSG_EXPORT
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
typename TriangleBuffer::value_type Index