Currently I have problems by handling uniforms on GLSL shaders. If I have only one object in the scene, the uniform works as I expect. However, for multiple objects, the uniform is not set per object, in other words, the last uniform is used to represent all scene objects.
How can I handle with this problem? Follows my C++ code:
void addSimpleObject(osg::ref_ptr<osg::Group> root, osg::Vec3 position, float radius, float myUniform) {
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(position, radius)));
root->addChild(geode);
root->getChild(0)->asGeode()->addDrawable(geode->getDrawable(0));
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();
stateset->addUniform(new osg::Uniform("myUniform", myUniform));
root->setStateSet(stateset);
}
osg::ref_ptr<osg::Group> root = new osg::Group();
addSimpleObject(root, osg::Vec3(-3.0, 2.5, -10), 2, 0.5);
addSimpleObject(root, osg::Vec3( 3.0, 2.5, -10), 2, 1.5);
addSimpleObject(root, osg::Vec3( 0.0, -2.5, -10), 2, 1.0);
The vertex shader:
#version 130
out vec3 pos;
out vec3 normal;
void main() {
pos = (gl_ModelViewMatrix * gl_Vertex).xyz;
normal = gl_NormalMatrix * gl_Normal;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
And the fragment shader:
#version 130
in vec3 pos;
in vec3 normal;
uniform float myUniform;
void main() {
vec3 normNormal;
normNormal = normalize(normal);
if (myUniform > 0)
normNormal = min(normNormal * myUniform, 1.0);
...
}