Hello Jerome,
  I've recently been working with bump mapping.  Looks like you still need to 
do a little more work on setting up the texture coordinates.  With every 
model/geometry you load you must make sure to be able to have access to their 
Geometry node.  The 'setUpDemo' function of osgFX::BumpMapping only seems to 
read predefined texture coordinates which doesn't always seem to work 
(resulting in the problem you and I were getting).  So, manually go in and 
retrieve the osg::Geometry from your object and set up the texture coordinates. 
 Here's the following steps I've used to create the effect:

osg::Group *root = new Group();

/////////////////////
// load a model

const char* modelFile =  some model;

osgFX::BumpMapping *bump = new osgFX::BumpMapping();
bump->addChild(osgDB::readNodeFile( modelFile );

/////////////////////////////////////
// handle texture coordinates
GeometryVisitor gv;
osg::Node *node = bump->getChild(0);
node->accept( gv );

for( int i = 0; i < gv.getGeodes().size(); i++)
{
    osg::Geode *geode = gv.getGeodes()[i];
    if(geode)
    {
            if( geode->getNumDrawables() > 0)
            {
                   osg::Geometry *geom = geode->getDrawable(0)->asGeometry();
                    if(geom)
                    {
                            Vec2Array *texcoords = 
dynamic_cast<Vec2Array*>(geom->getTexCoordArray(0));
                            geom->setTexCoordArray(0, texcoords);
                            geom->setTexCoordArray(1, texcoords);
                    }
            }
    }
}
/////////////////////////
// assign textures

osg::Texture2D *normal = new osg::Texture2D();
osg::Texture2D *diffuse = new osg::Texture2D();

const char* normal_texture = the normal texture;
const char* diffuse_texture = the diffuse texture;

osg::Image *normal_image  = osgDB::readImageFile( normal_texture );
osg::Image *diffuse_image = osgDB::readImageFile( diffuse_texture );

normal->setImage( normal_image );
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->setMaxAnisotropy(8);

diffuse->setImage( diffuse_image );
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->setMaxAnisotropy(8);

/////////////////////////////
// create the state set
osg::StateSet *state = new osg::StateSet();
state->setTextureAttributeAndModes( 0, diffuse, osg::StateAttribute::ON );
state->setTextureAttributeAndModes( 1, normal, osg::StateAttribute::ON );
state->setGlobalDefaults();
state->setMode( GL_LIGHTING, osg::StateAttribute::ON );
state->setMode( GL_BLEND, osg::StateAttribute::ON );
state->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );

bump->setStateSet( state );

//////////////////////////////////////////
// create the bump mapping effect

bump->setEnabled( true );
bump->setLightNumber( 0 );
bump->setOverrideNormalMapTexture( normal );
bump->setOverrideDiffuseTexture( diffuse );
bump->prepareChildren();

///////////////////////////
// add to the scene

osg::MatrixTranform *MT = new osg::MatrixTransform();
MT->addChild( bump );

root->addChild( MT );

I've included the GeometryVisitor class as well.  So I'll you need to do is 
just include it.  Try starting with simple flat planes to get an idea of how 
the texture coordinates affect the bump mapping.  Then move up to models.  My 
current problem is that I can't control the light used to create the bump 
mapping effect; tell me if you have any solutions or advice.  Hope this helps 
or at least gives you ideas!

Regards,
Dan
From: [EMAIL PROTECTED]
To: [email protected]
Date: Fri, 28 Mar 2008 15:52:37 +0100
Subject: [osg-users] How to correctly use osgFX::BumpMapping ?

Hello, I am trying to use osgFX::BumpMapping.For some reason, my diffuse map 
looks dark.I can only see the normal map.If I simply apply the diffuse map on 
mynode without bumpmapping, it looks fine. Here is my code:                
osg::Group*root = new osg::Group;                osg::Image*normalMapImage = 
osgDB::readImageFile('bricknormal.tga');                
osg::Image*diffuseMapImage = osgDB::readImageFile('brick.tga');                 
osg::Texture2D*normalMap = new osg::Texture2D(normalMapImage);                
osg::Texture2D*diffuseMap = new osg::Texture2D(diffuseMapImage);                
                //create the BumpMapping group                
osgFX::BumpMapping*pBump = new osgFX::BumpMapping;                
pBump->setLightNumber(0);                //add our node as a child              
  pBump->addChild(loadedModel.get());                pBump->setEnabled(true);   
             pBump->setOverrideNormalMapTexture(normalMap);                
pBump->setOverrideDiffuseTexture(diffuseMap);                
pBump->prepareChildren();                root->addChild(pBump);                 
               osg::Light*myLight2 = new osg::Light;               
myLight2->setLightNum(0);                
myLight2->setAmbient(osg::Vec4(1.0f,0.0f,0.0f,0.0f));               
myLight2->setDiffuse(osg::Vec4(1.0f,0.0f,0.0f,1.0f));                
myLight2->setPosition(osg::Vec4(1,0, 1, 0));                
osg::LightSource*lightS2 = new osg::LightSource;                     
lightS2->setLight(myLight2);                 
lightS2->setLocalStateSetModes(osg::StateAttribute::ON);                
root->addChild(lightS2);                viewerWindow->setSceneData(root); If no 
light ispresent this is the same problem, can anybody help please?Thanks 
inadvance,Regards,   
_________________________________________________________________
Pack up or back up–use SkyDrive to transfer files or keep extra copies. Learn 
how.
hthttp://www.windowslive.com/skydrive/overview.html?ocid=TXT_TAGLM_WL_Refresh_skydrive_packup_042008
#include <iostream>
#include "GeometryVisitor.h"

using namespace std;
using namespace osg;

   // run from mainNode
  GeometryVisitor::GeometryVisitor():osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
  {};

  vector<Geode*> GeometryVisitor::getGeodes()
  {
    return _geodes;
  }
  
  void GeometryVisitor::apply(osg::Geode &geode)
  {
    _geodes.push_back(&geode);
  }
#ifndef _GV_H
#define _GV_H

#include <vector>
#include <osg/Geode>
#include <osg/Node>
#include <osg/Matrix>
#include <osg/Transform>
#include <osg/BoundingBox>

class GeometryVisitor : public osg::NodeVisitor
{
  private:
    std::vector <osg::Geode*> _geodes;
        
  public:

    GeometryVisitor();
    std::vector <osg::Geode*> getGeodes();
    virtual void apply(osg::Geode&);
}; 
#endif
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to