For everyone who wants to use a bump map shader in osg you can find a working 
code in the lines below.

The source file:

> 
> #include <osg/Group>
> #include <osg/Image>
> #include <osg/MatrixTransform>
> #include <osg/Node>
> #include <osg/StateSet>
> #include <osg/TexMat>
> #include <osg/Texture2D>
> #include <osgDB/ReadFile>
> #include <osgGA/TrackballManipulator>
> #include <osgViewer/Viewer>
> 
> #include <osg/Program>
> #include <osg/Shader>
> #include <osg/Uniform>
> #include <osgDB/FileUtils>
> 
> int main(int argc, char **argv) {
> 
>       // Root
>       osg::Group* bumpRoot = new osg::Group();
> 
>       // Model with bump mapping
>       osg::Geometry* bumpModel = new osg::Geometry();
>       bumpModel = (osg::Geometry*) 
> osgDB::readNodeFile("Models/model_scifi_roadway_00straight.3ds");
> 
>       // Model stateset to activate bump mapping shader
>       osg::StateSet* bumpState = new osg::StateSet();
>       bumpState = bumpModel->getOrCreateStateSet();
> 
>       // Stateset shader program
>       osg::Program* bumpMapProgramObject = new osg::Program;
> 
>       // Set shader
>       osg::Shader* bumpVertexObject = new osg::Shader(osg::Shader::VERTEX);
>       osg::Shader* bumpFragmentObject = new 
> osg::Shader(osg::Shader::FRAGMENT);
> 
>       std::string bumpVertexFileName = 
> osgDB::findDataFile("Shading/newbump.vert");
>       bumpVertexObject->loadShaderSourceFromFile(bumpVertexFileName.c_str());
>       std::string bumpFragmentFileName = 
> osgDB::findDataFile("Shading/newbump.frag");
>       
> bumpFragmentObject->loadShaderSourceFromFile(bumpFragmentFileName.c_str());
> 
>       bumpMapProgramObject->addShader(bumpVertexObject);
>       bumpMapProgramObject->addShader(bumpFragmentObject);
> 
>       // Set textures
>       osg::Texture2D *normal = new osg::Texture2D();
>       osg::Texture2D *diffuse = new osg::Texture2D();
> 
>       const char* normal_texture = 
> "Textures/Normal/texture_scifi_roadway_00normal.png";
>       const char* diffuse_texture = "Textures/texture_scifi_roadway_00.png";
> 
>       osg::Image *normal_image = osgDB::readImageFile(normal_texture);
>       osg::Image *diffuse_image = osgDB::readImageFile(diffuse_texture);
> 
>       normal->setImage(normal_image);
>       normal->setDataVariance(osg::Object::DYNAMIC);
>       normal->setFilter(osg::Texture::MIN_FILTER, 
> osg::Texture::LINEAR_MIPMAP_LINEAR);
>       normal->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
>       normal->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
>       normal->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
>       normal->setResizeNonPowerOfTwoHint(false);
>       normal->setMaxAnisotropy(8.0f);
> 
>       diffuse->setImage(diffuse_image);
>       diffuse->setDataVariance(osg::Object::DYNAMIC);
>       diffuse->setFilter(osg::Texture::MIN_FILTER, 
> osg::Texture::LINEAR_MIPMAP_LINEAR);
>       diffuse->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
>       diffuse->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
>       diffuse->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
>       diffuse->setResizeNonPowerOfTwoHint(false);
>       diffuse->setMaxAnisotropy(8.0f);
> 
>       // Set texture matrix
>       osg::TexMat* texMat = new osg::TexMat;
>       osg::Vec3d texPosition(0.0, 0.0, 0.0);
>       osg::Vec3d texAngles(0.0, 0.0, 0.0);
>       osg::Vec3d texScale(2.0, 2.0, 2.0);
>       osg::Matrixd texRot;
>       texRot.makeRotate(
>               osg::DegreesToRadians(texAngles.x()), osg::Vec3(1, 0, 0),
>               osg::DegreesToRadians(texAngles.y()), osg::Vec3(0, 1, 0),
>               osg::DegreesToRadians(texAngles.z()), osg::Vec3(0, 0, 1));
>       
> texMat->setMatrix(osg::Matrix::scale(texScale)*texRot*osg::Matrix::translate(texPosition));
> 
>       const int TEXTURE_UNIT_BUMP = 1;
>       const int TEXTURE_UNIT_DIFFUSE = 0;
> 
>       // Add bump map to model stateset
>       // normalTexture in newbump.frag as uniform sampler2D normalTexture
>       // diffuseTexture in newbump.frag as uniform sampler2D diffuseTexture
>       bumpState->addUniform(new osg::Uniform("normalTexture", 
> TEXTURE_UNIT_BUMP));
>       bumpState->addUniform(new osg::Uniform("diffuseTexture", 
> TEXTURE_UNIT_DIFFUSE));
>       bumpState->setAttributeAndModes(bumpMapProgramObject, 
> osg::StateAttribute::ON);
>       // State set options
>       bumpState->setTextureAttributeAndModes(TEXTURE_UNIT_DIFFUSE, diffuse, 
> osg::StateAttribute::ON);
>       bumpState->setTextureAttributeAndModes(TEXTURE_UNIT_DIFFUSE, texMat, 
> osg::StateAttribute::ON);
>       bumpState->setTextureAttributeAndModes(TEXTURE_UNIT_BUMP, normal, 
> osg::StateAttribute::ON);
>       bumpState->setTextureAttributeAndModes(TEXTURE_UNIT_BUMP, texMat, 
> osg::StateAttribute::ON);
>       //bumpState->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
>       //bumpState->setTextureMode(1, GL_TEXTURE_2D, osg::StateAttribute::ON);
>       //bumpState->setGlobalDefaults();
>       //bumpState->setMode(GL_LIGHTING, osg::StateAttribute::ON);
>       //bumpState->setMode(GL_BLEND, osg::StateAttribute::ON);
>       bumpState->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
>       //bumpState->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
> 
>       // Add stateset to model
>       bumpModel->setStateSet(bumpState);
> 
>       // Set model matrix transform
>       osg::MatrixTransform *MT = new osg::MatrixTransform();
>       MT->setDataVariance(osg::Object::STATIC);
>       MT->addChild(bumpModel);
>       bumpRoot->addChild(MT);
> 
>       // Set viewer
>       osgViewer::Viewer bumpViewer;
>       bumpViewer.setSceneData(bumpRoot);
>       bumpViewer.setCameraManipulator(new osgGA::TrackballManipulator());
>       bumpViewer.realize();
> 
>       while (!bumpViewer.done()) {
>               bumpViewer.frame();
>       }
>       return 0;
> }
> 


The modified newbump.frag

> 
> uniform sampler2D diffuseTexture;
> uniform sampler2D normalTexture;
> 
> // New bumpmapping
> varying vec3 lightVec;
> varying vec3 halfVec;
> varying vec3 eyeVec;
> 
> void main() { 
> 
>       // lookup normal from normal map, move from [0,1] to  [-1, 1] range, 
> normalize
>       vec3 normal = 2.0 * texture2D (normalTexture, gl_TexCoord[0].st).rgb - 
> 1.0;
>       normal = normalize (normal);
>       
>       // compute diffuse lighting
>       float lamberFactor= max (dot (lightVec, normal), 0.0) ;
>       vec4 diffuseMaterial = 0.0;
>       vec4 diffuseLight  = 0.0;
>       
>       // compute specular lighting
>       vec4 specularMaterial ;
>       vec4 specularLight ;
>       float shininess ;
>   
>       // compute ambient
>       vec4 ambientLight = gl_LightSource[0].ambient;  
>       
>       if (lamberFactor > 0.0)
>       {
>               diffuseMaterial = texture2D (diffuseTexture, gl_TexCoord[0].st);
>               diffuseLight  = gl_LightSource[0].diffuse;
>               
>               // In doom3, specular value comes from a texture 
>               specularMaterial =  vec4(1.0)  ;
>               specularLight = gl_LightSource[0].specular;
>               shininess = 0.05;
>               //shininess = pow (max (dot (halfVec, normal), 0.0), 2.0)  ;
>                
>               gl_FragColor =  diffuseMaterial * diffuseLight * lamberFactor ;
>               gl_FragColor += specularMaterial * specularLight * shininess ;  
>                         
>       
>       }
>       
>       gl_FragColor += ambientLight;
>       
>   }           
> 


The modified newbump.vert

> 
> attribute vec3 tangent;
> varying vec3 lightVec;
> varying vec3 halfVec;
> varying vec3 eyeVec;
> 
> void main() {
> 
>       //gl_TexCoord[0] =  gl_MultiTexCoord0;
>       gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
>       gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;
>       
>       // Building the matrix Eye Space -> Tangent Space
>       vec3 n = normalize (gl_NormalMatrix * gl_Normal);
>       vec3 t = normalize (gl_NormalMatrix * tangent);
>       vec3 b = cross (n, t);
>       
>       vec3 vertexPosition = vec3(gl_ModelViewMatrix *  gl_Vertex);
>       vec3 lightDir = normalize(gl_LightSource[0].position.xyz - 
> vertexPosition);
>               
>               
>       // transform light and half angle vectors by tangent basis
>       vec3 v;
>       v.x = dot (lightDir, t);
>       v.y = dot (lightDir, b);
>       v.z = dot (lightDir, n);
>       lightVec = normalize (v);
>       
>         
>       v.x = dot (vertexPosition, t);
>       v.y = dot (vertexPosition, b);
>       v.z = dot (vertexPosition, n);
>       eyeVec = normalize (v);
>       
>       
>       vertexPosition = normalize(vertexPosition);
>       
>       /* Normalize the halfVector to pass it to the fragment shader */
> 
>       // No need to divide by two, the result is normalized anyway.
>       // vec3 halfVector = normalize((vertexPosition + lightDir) / 2.0); 
>       vec3 halfVector = normalize(vertexPosition + lightDir);
>       v.x = dot (halfVector, t);
>       v.y = dot (halfVector, b);
>       v.z = dot (halfVector, n);
> 
>       // No need to normalize, t,b,n and halfVector are normal vectors.
>       //normalize (v);
>       halfVec = v ; 
>         
>         
>       gl_Position = ftransform();
> 
> }
> 


I hope I can help osg programmer with the same problem.[/quote]

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=65610#65610





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to