16 #ifndef AGXOSG_RenderStateManager_H 17 #define AGXOSG_RenderStateManager_H 22 #include <agxCFG/ConfigScript.h> 28 #include <osg/Program> 30 #include <osg/Uniform> 31 #include <osg/StateAttribute> 32 #include <osg/Texture2D> 46 class SimulationObject;
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
76 ShaderState(
const std::string& name,
const std::string& program_name) : m_name(name), m_program_name(program_name) {}
78 std::string
getName()
const {
return m_name; }
82 void addUniform(osg::Uniform* uniform);
83 osg::Uniform* findUniform(
const std::string& name);
95 std::string m_name, m_program_name;
98 UniformHashTable m_uniforms;
115 void setName(
const std::string &name ) { m_name = name; }
116 std::string
getName()
const {
return m_name; }
118 void addTexture(
int unit, osg::Texture* texture, osg::TexMat* tm=
nullptr, osg::TexGen* texgen=
nullptr )
120 m_textureHash[unit]=
Texture(texture, tm, texgen);
129 void addUniform(osg::Uniform* uniform);
130 osg::Uniform *findUniform(
const std::string& name);
143 Texture(osg::Texture *t, osg::TexMat *tm, osg::TexGen *tg ) : texture(t), texMat(tm), texGen(tg) {}
166 SINGLETON_CLASSNAME_METHOD();
169 osg::Program *findProgram(
const std::string& name) ;
171 RenderState *findRenderState(
const std::string& name);
174 void addTexture(
const std::string& name, osg::Texture *texture);
175 osg::Texture *findTexture(
const std::string& name);
177 osg::Program *createProgram(
const std::string& name);
179 osg::Shader *createShader(
const std::string& name,
180 const std::string& shader,
186 ShaderState *findShaderState(
const std::string& name);
188 void parse( agxCFG::ConfigScript* cfg);
203 if (!cfg->get(
"name",
id))
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));
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));
215 else if (cfg->exist(
"value", agxCFG::ConfigValue::VALUE_FLOAT_ARRAY)) {
216 cfg->get(
"value", v_val);
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));
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));
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));
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]
237 obj->addUniform(
new osg::Uniform(
id.c_str(), m));
243 LOGGER_ERROR() <<
"Uniform.value is either missing or is of an invalid type in " << cfg->currentScope() <<
"(" <<
id <<
")" <<
LOGGER_END();
260 ShaderMap m_vertexShaders;
261 ShaderMap m_fragmentShaders;
265 ProgramMap m_programs;
268 ShaderStateMap m_shader_states;
271 RenderStateMap m_render_states;
274 TextureHashTable m_textures;
std::string getName() const
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.
RenderState(const std::string &name)
A class holding 4 dimensional vectors and providing basic arithmetic.
#define OSG_VEC4F_TO_AGX(X)
std::string getName() const
std::string getProgramID() const
Vec2T< Real > Vec2
The object holding 2 dimensional vectors and providing basic arithmetic.
static void parseUniform(T *obj, agxCFG::ConfigScript *cfg)
ShaderState(const std::string &name, const std::string &program_name)
agx::Vec4f getColor(Color color)
agx::ref_ptr< ShaderState > m_shaderState
Texture(osg::Texture *t, osg::TexMat *tm, osg::TexGen *tg)
Base class providing referencing counted objects.
agx::HashTable< std::string, osg::ref_ptr< osg::Uniform > > UniformHashTable
osg::ref_ptr< osg::TexGen > texGen
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...
Smart pointer for handling referenced counted objects.
osg::ref_ptr< osg::Texture > texture
agx::ref_ptr< RenderState > RenderStateRef
agx::HashTable< agx::UInt, osg::Vec4f > ColorHash
TextureHash m_textureHash
agx::HashTable< int, Texture > TextureHash
UniformHashTable m_uniforms
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)
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
osg::ref_ptr< osg::TexMat > texMat