Hi, all.
I tried to use osgFX::Bumpmapping on a rectangle plane. I use followed fuction
to crteate a bump map then set it as an osgViewer::Viewer's scene data. It
works well while using GL_QUADS to create the plane.
However, I found if I use GL_POLYGON to replace GL_QUADS, the bump mapping will
fail and display just a black plane. I've no idea why this happen. Why POLYGON
fails? Any suggestion will be appricate. Thanks.
Here is my code:
osgFX::BumpMapping* create_bumpmapping()
{
/// create a plane.
osg::Geometry* geom = new osg::Geometry();
/// set vertex.
osg::Vec3Array* ver = new osg::Vec3Array();
float off = 100.0f;
ver->push_back(osg::Vec3(-off, -off, off));
ver->push_back(osg::Vec3(off, -off, off));
ver->push_back(osg::Vec3(off, off, off));
ver->push_back(osg::Vec3(-off, off, off));
geom->setVertexArray(ver);
/// [here is the problem] if replace "QUADS" to "POLYGON", the bumpmapping will
fail and display just a black plane.
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS/*POLYGON*/,
0, ver->size()));
/// set normal. [note]normal size must be same as vertex size, othrewise it
will crash.
osg::Vec3Array* norms = new osg::Vec3Array(4);
(*norms)[0].set(0.0f,0.0f,1.0f);
(*norms)[1].set(0.0f,0.0f,1.0f);
(*norms)[2].set(0.0f,0.0f,1.0f);
(*norms)[3].set(0.0f,0.0f,1.0f);
geom->setNormalArray(norms);
geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
/// set texture coord. [note] must use two texture unit.
osg::Vec2Array* tcoords = new osg::Vec2Array(4);
(*tcoords)[0].set(0.0f,0.0f);
(*tcoords)[1].set(0.0f,3.0f);
(*tcoords)[2].set(3.0f,3.0f);
(*tcoords)[3].set(3.0f,0.0f);
geom->setTexCoordArray(0, tcoords);
geom->setTexCoordArray(1, tcoords);
/// add the plane to a osgFX::BumpMapping.
osg::Geode *geo = new osg::Geode();
geo->addDrawable(geom);
osgFX::BumpMapping* bm = new osgFX::BumpMapping();
bm->addChild(geo);
/// set the bumpmapping.
bm->setLightNumber(1);
bm->setDiffuseTextureUnit(0);
bm->setNormalMapTextureUnit(1);
std::string dif_fn = "F://Test//bump_mapping//Fieldstone.png";
osg::ref_ptr<osg::Texture2D> diffuseTex = new osg::Texture2D;
diffuseTex->setImage(osgDB::readImageFile(dif_fn));
diffuseTex->setFilter(osg::Texture::MIN_FILTER,
osg::Texture::LINEAR_MIPMAP_LINEAR);
diffuseTex->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
diffuseTex->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
diffuseTex->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
diffuseTex->setMaxAnisotropy(8);
bm->setOverrideDiffuseTexture(diffuseTex);
std::string nor_fn = "F://Test//bump_mapping//FieldstoneBumpDOT.png";
osg::ref_ptr<osg::Texture2D> normalTex = new osg::Texture2D;
normalTex->setImage(osgDB::readImageFile(nor_fn));
normalTex->setFilter(osg::Texture::MIN_FILTER,
osg::Texture::LINEAR_MIPMAP_LINEAR);
normalTex->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
normalTex->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
normalTex->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
normalTex->setMaxAnisotropy(8);
bm->setOverrideNormalMapTexture(normalTex);
/// prepare the bumpmapping.
bm->prepareChildren();
return bm;
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org