This is not related to GLSL, but if someone needs it, I quickly wrote a 
function that transforms a gray-scale bump map into a normal map: 
http://pvle.sourceforge.net/Doc/Html/Utility3D_8cpp-source.html#l00115
Hope this may be useful to someone, even if the function is not that clean.

Sukender
PVLE - Lightweight cross-platform game engine - http://pvle.sourceforge.net/


Le Wed, 26 Nov 2008 13:29:33 +0100, Morné Pistorius <[EMAIL PROTECTED]> a écrit:

> Thanks,
> I managed to implement a standard bump mapping shader in OSG.  Below is a
> code snippet that does it if it might help someone else.  Shaders are
> attached.
>
> Cheers,
> Morne
>
> osg::Node * VOSGFloorGrid::CreateBumpMappedFloor()
> {
>   const int BASE_TEX_UNIT = 0;
>   const int BUMP_TEX_UNIT = 1;
>   const int TANGENT_ATR_UNIT = 6;
>   const int BINORMAL_ATR_UNIT = 7;
>
>   osg::Geode* geode = new osg::Geode;
>
>   // set up the Geometry.
>   osg::Geometry* geom = new osg::Geometry;
>
>   osg::Vec3 width, depth, topleft;
>   topleft = osg::Vec3( -Size/2.0, -Size/2.0, 0.0 );
>   width = osg::Vec3( Size, 0.0, 0.0 );
>   depth = osg::Vec3( 0.0, Size, 0.0 );
>
>   osg::Vec3Array* coords = new osg::Vec3Array(4);
>   (*coords)[0] = topleft;
>   (*coords)[1] = topleft+width;
>   (*coords)[2] = topleft+width+depth;
>   (*coords)[3] = topleft+depth;
>
>   geom->setVertexArray(coords);
>
>   osg::Vec3Array* norms = new osg::Vec3Array(1);
>   (*norms)[0] = osg::Vec3( 0.0, 1.0, 0.0 );
>
>   geom->setNormalArray(norms);
>   geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
>
>   osg::Vec4Array* color = new osg::Vec4Array(1);
>   (*color)[0] = osg::Vec4(1.0f,1.0f,1.0f,1.0f);
>
>   geom->setColorArray( color );
>   geom->setColorBinding( osg::Geometry::BIND_OVERALL );
>
>   osg::Vec2Array* tcoords = new osg::Vec2Array(4);
>   float n = 5.0f;  // no of times the texture is repeated
>   (*tcoords)[0].set( 0.0f, 0.0f );
>   (*tcoords)[1].set( n, 0.0f );
>   (*tcoords)[2].set( n, n );
>   (*tcoords)[3].set( 0.0f, n );
>   geom->setTexCoordArray( BASE_TEX_UNIT, tcoords );
>   geom->setTexCoordArray( BUMP_TEX_UNIT, tcoords );
>
>   geom->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0 ,
> coords->size() ) );
>   geode->addDrawable( geom );
>
>   // Compute smoothed normals
>   osgUtil::SmoothingVisitor smoother;
>   smoother.apply( *geode );
>
>   // model texture
>   osg::Texture2D * diffuse = new osg::Texture2D;
>   diffuse->setImage( osgDB::readImageFile( m_sSystemPath +
> "/Resources/Floor.tga" ) );
>   diffuse->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT );
>   diffuse->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT );
>   diffuse->setResizeNonPowerOfTwoHint( false );
>   diffuse->setMaxAnisotropy( 8.0f );
>
>   // bump map texture
>   osg::Texture2D * normal = new osg::Texture2D;
>   normal->setImage( osgDB::readImageFile( m_sSystemPath +
> "/Resources/FloorNormalMap.tga" ) );
>   normal->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT );
>   normal->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT );
>   normal->setResizeNonPowerOfTwoHint( false );
>   normal->setMaxAnisotropy( 8.0f );
>
>
>   //////////////////////////////////////////////////////////////////////////
>   // Set up bump mapping shaders
>
>   // Load and compile shader source
>   osg::Program * bump = new osg::Program;
>   osg::Shader * bumpVertexShader = new osg::Shader( osg::Shader::VERTEX );
>   osg::Shader * bumpPixelShader  = new osg::Shader( osg::Shader::FRAGMENT );
>   bumpVertexShader->loadShaderSourceFromFile( m_sSystemPath +
> "/Resources/bumpmap.vert" );
>   bumpPixelShader->loadShaderSourceFromFile( m_sSystemPath +
> "/Resources/bumpmap.frag" );
>   bump->addShader( bumpVertexShader );
>   bump->addShader( bumpPixelShader );
>
>   // Set up the shader stateset
>   osg::StateSet * stateset = geode->getOrCreateStateSet();
>   stateset->setTextureAttribute( BASE_TEX_UNIT, diffuse );
>   stateset->setTextureAttribute( BUMP_TEX_UNIT, normal );
>   stateset->setAttributeAndModes( bump, osg::StateAttribute::ON );
>
>   // Compute tangent space for geometry
>   osg::ref_ptr< osgUtil::TangentSpaceGenerator > tsg = new
> osgUtil::TangentSpaceGenerator;
>   tsg->generate( geom, BUMP_TEX_UNIT );
>   geom->setVertexAttribData( TANGENT_ATR_UNIT, osg::Geometry::ArrayData(
> tsg->getTangentArray(), osg::Geometry::BIND_PER_VERTEX, GL_FALSE ) );
>   geom->setVertexAttribData( BINORMAL_ATR_UNIT, osg::Geometry::ArrayData(
> tsg->getBinormalArray(), osg::Geometry::BIND_PER_VERTEX, GL_FALSE ) );
>   bump->addBindAttribLocation( "rm_Tangent", TANGENT_ATR_UNIT );
>   bump->addBindAttribLocation( "rm_Binormal", BINORMAL_ATR_UNIT );
>
>   // Initialise Uniforms
>   // Vertex shader
>   osg::Vec3 Pos( 0, 0, 1000 );
>   stateset->addUniform( new osg::Uniform( "fvLightPosition", Pos ) );
>   stateset->addUniform( new osg::Uniform( "fvEyePosition", Pos ) );
>   stateset->addUniform( new osg::Uniform( "bumpMap", BUMP_TEX_UNIT ) );
>   stateset->addUniform( new osg::Uniform( "bumpMap", BUMP_TEX_UNIT ) );
>
>   // Fragment shader
>   stateset->addUniform( new osg::Uniform( "fvAmbient", osg::Vec4( 0.36,
> 0.36, 0.36, 1.0 ) ) );
>   stateset->addUniform( new osg::Uniform( "fvSpecular", osg::Vec4( 0.49,
> 0.49, 0.49, 1.0 ) ) );
>   stateset->addUniform( new osg::Uniform( "fvDiffuse", osg::Vec4( 0.88,
> 0.88, 0.88, 1.0 ) ) );
>   stateset->addUniform( new osg::Uniform( "fSpecularPower", 100.0f ) );
>
>   return geode;
> }
>
>
>
>
> On Mon, Nov 24, 2008 at 5:02 PM, Alejandro Aguilar Sierra <
> [EMAIL PROTECTED]> wrote:
>
>> Hi,
>>
>> I suggest you to see the osgshaders example (how osg deals with glsl)
>> and then consult chapter 11 of the orange book (OpenGL shading
>> language). You can find the shaders used in the book at
>> http://3dshaders.com/
>>
>> Hope it helps.
>>
>> -- A.
>>
>> On Mon, Nov 24, 2008 at 10:39 AM, Morné Pistorius
>> <[EMAIL PROTECTED]> wrote:
>> > Hi all,
>> > I added bump mapping to a model using osgFX::BumpMapping, but I need
>> > something more flexible.  My model has two sided lighting and osgFX
>> doesn't
>> > support that.  Do we have GLSL shaders for bumpmapping in OSG?  I think
>> that
>> > would be easier to modify to suit my needs than the assembler coded
>> shaders
>> > used in osgFX.  If anyone has an example of applying normal mapping with
>> > shaders in OSG, I would greatly appreciate it.
>> > Thank you kindly,
>> > Morne
>> > _______________________________________________
>> > osg-users mailing list
>> > [email protected]
>> >
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>> >
>> >
>> _______________________________________________
>> osg-users mailing list
>> [email protected]
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to