16#ifndef AGXOSG_RenderStateManager_H
17#define AGXOSG_RenderStateManager_H
22#include <agxCFG/ConfigScript.h>
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; }
95 std::string m_name, m_program_name;
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);
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]) ); }
143 Texture(osg::Texture *t, osg::TexMat *tm, osg::TexGen *tg ) : texture(t), texMat(tm), texGen(tg) {}
166 SINGLETON_CLASSNAME_METHOD();
168 osg::Shader *
findShader(
const std::string& name, osg::Shader::Type type) ;
174 void addTexture(
const std::string& name, osg::Texture *texture);
180 const std::string& shader,
181 osg::Shader::Type type,
bool isAFile );
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();
249 void shutdown()
override;
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;
#define OSG_VEC4F_TO_AGX(X)
A node that can be associated with a collision geometry so that the transformation of this node is up...
void setColor(Color color, const agx::Vec4f &col)
DIFFUSE, AMBIENT, SPECULAR, EMISSIVE, ROUGHNESS, ALPHA.
void addUniform(osg::Uniform *uniform)
agx::Vec4f getColor(Color color)
RenderState(const std::string &name)
osg::Uniform * findUniform(const std::string &name)
void addTexture(int unit, osg::Texture *texture, osg::TexMat *tm=nullptr, osg::TexGen *texgen=nullptr)
agx::HashTable< int, Texture > TextureHash
agx::HashTable< agx::UInt, osg::Vec4f > ColorHash
void apply(osg::Node *node, RenderStateManager::Values value=RenderStateManager::ON)
agx::HashTable< std::string, osg::ref_ptr< osg::Uniform > > UniformHashTable
void setName(const std::string &name)
void setShaderState(ShaderState *shaderState)
void apply(agxOSG::GeometryNode *node, RenderStateManager::Values value=RenderStateManager::ON)
agx::ref_ptr< ShaderState > m_shaderState
UniformHashTable m_uniforms
void apply(osg::StateSet *stateSet, RenderStateManager::Values value=RenderStateManager::ON)
void apply(agxOSG::SimulationObject &obj, RenderStateManager::Values value=RenderStateManager::ON)
TextureHash m_textureHash
std::string getName() const
void apply(osg::Group *node, RenderStateManager::Values value=RenderStateManager::ON)
void apply(osg::Node *node, RenderStateManager::Values value=RenderStateManager::ON)
std::string getProgramID() const
std::string getName() const
void addUniform(osg::Uniform *uniform)
void apply(osg::StateSet *stateset, RenderStateManager::Values value=RenderStateManager::ON)
Add a ShaderState to a specified stateset with given StateAttributes.
ShaderState(const std::string &name, const std::string &program_name)
osg::Uniform * findUniform(const std::string &name)
RenderState * findRenderState(const std::string &name)
ShaderState * findShaderState(const std::string &name)
osg::Texture * findTexture(const std::string &name)
void parse(agxCFG::ConfigScript *cfg)
agx::ref_ptr< RenderState > RenderStateRef
void addRenderState(RenderState *renderState)
osg::Shader * createShader(const std::string &name, const std::string &shader, osg::Shader::Type type, bool isAFile)
void addTexture(const std::string &name, osg::Texture *texture)
void reset()
Will call clear and then init to reinitialize back to default state.
osg::Shader * findShader(const std::string &name, osg::Shader::Type type)
void addShaderState(ShaderState *shaderState)
static RenderStateManager * instance(void)
Return the singleton object.
osg::Program * findProgram(const std::string &name)
static void parseUniform(T *obj, agxCFG::ConfigScript *cfg)
osg::Program * createProgram(const std::string &name)
void clear()
Remove all previous configurations, Shader/RenderStates.
Inheritance with partial specialization due to bug with ref_ptr containers.
Base class providing referencing counted objects.
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
A class holding 4 dimensional vectors and providing basic arithmetic.
Smart pointer for handling referenced counted objects.
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
osg::ref_ptr< osg::TexGen > texGen
Texture(osg::Texture *t, osg::TexMat *tm, osg::TexGen *tg)
osg::ref_ptr< osg::TexMat > texMat
osg::ref_ptr< osg::Texture > texture