AGX Dynamics  2.37.2.0
RenderStateManager.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 #ifndef AGXOSG_RenderStateManager_H
17 #define AGXOSG_RenderStateManager_H
18 
19 
20 #include <agxOSG/export.h>
21 #include <agx/Singleton.h>
22 #include <agxCFG/ConfigScript.h>
23 #include <agx/HashTable.h>
24 #include <agx/Vec4.h>
25 #include <agx/Logger.h>
26 
27 #include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
28 #include <osg/Program>
29 #include <osg/Shader>
30 #include <osg/Uniform>
31 #include <osg/StateAttribute>
32 #include <osg/Texture2D>
33 #include <osg/TexMat>
34 #include <osg/TexGen>
35 #include <agx/PopDisableWarnings.h> // End of disabled warnings.
36 
37 namespace osg
38 {
39  class Group;
40 }
41 
42 namespace agxOSG {
43  class Node;
44  class Group;
45  class GeometryNode;
46  class SimulationObject;
47 
48 
49 
51  {
52  public:
53 
54  enum Values
55  {
57  OFF = osg::StateAttribute::OFF,
59  ON = osg::StateAttribute::ON,
61  OVERRIDE = osg::StateAttribute::OVERRIDE,
63  PROTECTED = osg::StateAttribute::PROTECTED,
65  INHERIT = osg::StateAttribute::INHERIT
66  };
67 
71  void reset();
72 
74  {
75  public:
76  ShaderState(const std::string& name, const std::string& program_name) : m_name(name), m_program_name(program_name) {}
77 
78  std::string getName() const { return m_name; }
79 
80  std::string getProgramID() const { return m_program_name; }
81 
82  void addUniform(osg::Uniform* uniform);
83  osg::Uniform* findUniform(const std::string& name);
84 
90  void apply(osg::StateSet* stateset, RenderStateManager::Values value=RenderStateManager::ON);
91  void apply(osg::Node* node, RenderStateManager::Values value=RenderStateManager::ON);
92 
93  private:
94  virtual ~ShaderState();
95  std::string m_name, m_program_name;
96 
98  UniformHashTable m_uniforms;
99  };
100 
102  {
103  public:
104 
105  enum Color {
111  EMISSIVE
112  };
113 
114  RenderState( const std::string& name ) : m_name( name ) {}
115  void setName( const std::string &name ) { m_name = name; }
116  std::string getName() const { return m_name; }
117 
118  void addTexture(int unit, osg::Texture* texture, osg::TexMat* tm=nullptr, osg::TexGen* texgen=nullptr )
119  {
120  m_textureHash[unit]= Texture(texture, tm, texgen);
121  }
122 
123  void apply( osg::Group* node, RenderStateManager::Values value=RenderStateManager::ON );
124  void apply( osg::Node* node, RenderStateManager::Values value=RenderStateManager::ON );
125  void apply( agxOSG::GeometryNode* node, RenderStateManager::Values value=RenderStateManager::ON );
126  void apply( osg::StateSet* stateSet, RenderStateManager::Values value=RenderStateManager::ON );
127  void apply( agxOSG::SimulationObject& obj, RenderStateManager::Values value=RenderStateManager::ON );
128 
129  void addUniform(osg::Uniform* uniform);
130  osg::Uniform *findUniform(const std::string& name);
131 
132 
134  void setColor( Color color, const agx::Vec4f& col ) { m_colorHash[color] = osg::Vec4f(float(col[0]), float(col[1]), float(col[2]), float(col[3]) ); }
135  agx::Vec4f getColor( Color color ) { return OSG_VEC4F_TO_AGX(m_colorHash[color]); }
136 
137  void setShaderState( ShaderState *shaderState ) { m_shaderState = shaderState; }
138  protected:
139  virtual ~RenderState() {}
140 
141  std::string m_name;
142  struct Texture {
143  Texture(osg::Texture *t, osg::TexMat *tm, osg::TexGen *tg ) : texture(t), texMat(tm), texGen(tg) {}
144  Texture( ) {}
145  osg::ref_ptr<osg::Texture> texture;
146  osg::ref_ptr<osg::TexMat> texMat;
147  osg::ref_ptr<osg::TexGen> texGen;
148  };
150  TextureHash m_textureHash;
153  ColorHash m_colorHash;
154 
156  UniformHashTable m_uniforms;
157 
158  };
159 
161 
162 
164  static RenderStateManager *instance( void );
165 
166  SINGLETON_CLASSNAME_METHOD();
167 
168  osg::Shader *findShader(const std::string& name, osg::Shader::Type type) ;
169  osg::Program *findProgram(const std::string& name) ;
170 
171  RenderState *findRenderState(const std::string& name);
172  void addRenderState( RenderState *renderState );
173 
174  void addTexture(const std::string& name, osg::Texture *texture);
175  osg::Texture *findTexture(const std::string& name);
176 
177  osg::Program *createProgram(const std::string& name);
178 
179  osg::Shader *createShader(const std::string& name,
180  const std::string& shader,
181  osg::Shader::Type type, bool isAFile );
182 
183  //bool loadShaderSource(const std::string& shader_id);
184 
185  void addShaderState(ShaderState* shaderState);
186  ShaderState *findShaderState(const std::string& name);
187 
188  void parse( agxCFG::ConfigScript* cfg);
189 
193  void clear();
194 
195  template<typename T>
196  static void parseUniform(T* obj, agxCFG::ConfigScript *cfg)
197  {
198  std::string id;
199  int i_val;
200  float f_val;
201  agx::RealVector v_val;
202 
203  if (!cfg->get("name", id))
204  LOGGER_ERROR() << "Required key " << cfg->currentScope() << "name is missing" << LOGGER_END();
205 
206  if (cfg->exist("value", agxCFG::ConfigValue::VALUE_FLOAT) ||
207  cfg->exist("value", agxCFG::ConfigValue::VALUE_EXPRESSION)) {
208  f_val = cfg->returns("value", 0.0f);
209  obj->addUniform(new osg::Uniform(id.c_str(), f_val));
210  }
211  else if (cfg->exist("value", agxCFG::ConfigValue::VALUE_INT)) {
212  i_val = cfg->returns("value", 0);
213  obj->addUniform(new osg::Uniform(id.c_str(), i_val));
214  }
215  else if (cfg->exist("value", agxCFG::ConfigValue::VALUE_FLOAT_ARRAY)) {
216  cfg->get("value", v_val);
217  if (!v_val.size())
218  LOGGER_ERROR() << "Invalid vector size for data in Uniform: ("+id+")" << LOGGER_END();
219  if (v_val.size()==2) {
220  osg::Vec2 v2((float)v_val[0], (float)v_val[1]);
221  obj->addUniform(new osg::Uniform(id.c_str(), v2));
222  }
223  else if (v_val.size()==3) {
224  osg::Vec3 v3((float)v_val[0], (float)v_val[1], (float)v_val[2]);
225  obj->addUniform(new osg::Uniform(id.c_str(), v3));
226  }
227  else if (v_val.size()==4) {
228  osg::Vec4f v4((float)v_val[0], (float)v_val[1], (float)v_val[2], (float)v_val[3]);
229  obj->addUniform(new osg::Uniform(id.c_str(), v4));
230  }
231  else if (v_val.size()==16) {
232  osg::Matrixf m((float)v_val[0], (float)v_val[1], (float)v_val[2], (float)v_val[3],
233  (float)v_val[4], (float)v_val[5], (float)v_val[6], (float)v_val[7],
234  (float)v_val[8], (float)v_val[9], (float)v_val[10], (float)v_val[11],
235  (float)v_val[12], (float)v_val[13], (float)v_val[14], (float)v_val[15]
236  );
237  obj->addUniform(new osg::Uniform(id.c_str(), m));
238  }
239  else
240  LOGGER_ERROR() << "Invalid vector size for data in Uniform: (" << id << ")" << LOGGER_END();
241  }
242  else
243  LOGGER_ERROR() << "Uniform.value is either missing or is of an invalid type in " << cfg->currentScope() << "(" << id << ")" << LOGGER_END();
244  }
245 
246 
247  private:
248 
249  void shutdown() override;
250 
251  void init();
252 
254  virtual ~RenderStateManager();
255 
257  RenderStateManager( void );
258 
260  ShaderMap m_vertexShaders;
261  ShaderMap m_fragmentShaders;
262 
263 
265  ProgramMap m_programs;
266 
268  ShaderStateMap m_shader_states;
269 
271  RenderStateMap m_render_states;
272 
274  TextureHashTable m_textures;
275 
276  private:
277  static RenderStateManager *s_instance;
278  bool m_initialized;
279 
280  };
281 
282 
283 } // namespace agxOSG
284 
285 
286 #endif
void setColor(Color color, const agx::Vec4f &col)
DIFFUSE, AMBIENT, SPECULAR, EMISSIVE, SHININESS, ALPHA.
AGXTERRAIN_EXPORT void clear()
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
void AGXPHYSICS_EXPORT shutdown()
Shutdown of the AGX Dynamics API will be done when the number of shutdown matches the number of calls...
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
#define AGXOSG_EXPORT
Vec4T< Real32 > Vec4f
Definition: Vec4.h:26
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
#define OSG_VEC4F_TO_AGX(X)
Vec2T< Real > Vec2
The object holding 2 dimensional vectors and providing basic arithmetic.
Definition: Vec2.h:35
static void parseUniform(T *obj, agxCFG::ConfigScript *cfg)
ShaderState(const std::string &name, const std::string &program_name)
agx::ref_ptr< ShaderState > m_shaderState
Texture(osg::Texture *t, osg::TexMat *tm, osg::TexGen *tg)
Base class providing referencing counted objects.
Definition: Referenced.h:119
agx::HashTable< std::string, osg::ref_ptr< osg::Uniform > > UniformHashTable
void setShaderState(ShaderState *shaderState)
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
void setName(const std::string &name)
A node that can be associated with a collision geometry so that the transformation of this node is up...
Definition: GeometryNode.h:41
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
agx::ref_ptr< RenderState > RenderStateRef
agx::HashTable< agx::UInt, osg::Vec4f > ColorHash
size_t size() const
Definition: Container.h:134
#define LOGGER_ERROR()
Definition: Logger.h:22
agx::HashTable< int, Texture > TextureHash
Inheritance with partial specialization due to bug with ref_ptr containers.
void addTexture(int unit, osg::Texture *texture, osg::TexMat *tm=nullptr, osg::TexGen *texgen=nullptr)
#define LOGGER_END()
Definition: Logger.h:26
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
Definition: Singleton.h:30