Andrea, The vertex color is available in the vertex shader as gl_Color. Pass this to your fragment shader in a varying and apply it there.
Glenn Waldron / @glennwaldron On Wed, Jan 30, 2013 at 5:23 AM, Andrea Martini <martini.and...@gmail.com>wrote: > Hi, > > i'm wondering if it is possible to keep original materials and colors of a > 3D model, even if we want to apply some shaders effect. In detail, i have a > 3D model that appears as attached figure (original 3d model with color - > objectwithcolor.png) without any shaders applied. > If i apply some shaders effect, i loose colors from my model (figure > with shaders applied - objectWithShader.png) > Here i report the simple code : > > int main( int argc, char** argv ) > { > > osg::ref_ptr<osg::Node> root = osgDB::readNodeFiles( arguments ); > if ( !root ) root = osgDB::readNodeFile("myModel.osg"); > > osg::ref_ptr<osg::Program> program =new osg::Program; > > osgViewer::Viewer viewer; > > > // Shaders effect > osg::Shader* vertexShader = osgDB::readShaderFile(osg::Shader::VERTEX, > "dynamic.vert"); > > if (!vertexShader) > { > std::cout << " No vertex Shader Loaded." << std::endl; > return 0; > } > > osg::Shader* fragShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, > "dynamic.frag"); > > if (!fragShader) > { > std::cout << " No fragment Shader Loaded." << std::endl; > return 0; > } > > program->addShader(vertexShader); > program->addShader(fragShader); > > osg::StateSet* stateset = root->getOrCreateStateSet(); > > stateset->setAttributeAndModes( program.get()); > > stateset->addUniform( new osg::Uniform("lightDiffuse", osg::Vec4(0.8f, > 0.8f, 0.8f, 1.0f)) ); > > stateset->addUniform( new osg::Uniform("lightSpecular", osg::Vec4(1.0f, > 1.0f, 0.4f, 1.0f)) ); > > stateset->addUniform( new osg::Uniform("shininess", 64.0f) ); > > osg::ref_ptr<osg::Uniform> lightPos = new osg::Uniform( "lightPosition", > osg::Vec3() ); > > lightPos->setUpdateCallback( new LightPosCallback ); > stateset->addUniform( lightPos.get() ); > > > viewer.setSceneData( root.get() ); > viewer.setUpViewInWindow( 100, 100, 800, 600 ); > > viewer.run(); > > > return 0; > } > > Now the vertex and fragment code : > > //dynamic.vert > > uniform vec3 lightPosition; > varying vec3 normal, eyeVec, lightDir; > > void main() > { > vec4 vertexInEye = gl_ModelViewMatrix * gl_Vertex; > eyeVec = -vertexInEye.xyz; > lightDir = vec3(lightPosition - vertexInEye.xyz); > normal = gl_NormalMatrix * gl_Normal; > gl_Position = ftransform(); > } > > > //dynamic.frag > > uniform vec4 lightDiffuse; > uniform vec4 lightSpecular; > uniform float shininess; > varying vec3 normal, eyeVec, lightDir; > void main (void) > { > vec4 finalColor = gl_FrontLightModelProduct.sceneColor; > vec3 N = normalize(normal); > vec3 L = normalize(lightDir); > float lambert = dot(N,L); > if (lambert > 0.0) > { > finalColor += lightDiffuse * lambert; > vec3 E = normalize(eyeVec); > vec3 R = reflect(-L, N); > float specular = pow(max(dot(R, E), 0.0), shininess); > finalColor += lightSpecular * specular; > } > gl_FragColor = finalColor; > } > > > > Thank you! > > Cheers, > > ------------------ > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=52273#52273 > > > > > _______________________________________________ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > >
_______________________________________________ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org