|
大家好,我使用osgFX::Bumpmapping对一个矩形平明生成凹凸效果。我使用下面的create_bumpmapping函数创建了凹凸贴图节点,然后添加到场景中。有趣的是,函数中如果使用GL_QUADS创建平面,则凹凸效果正确;如果改为GL_POLYGON,其他部分不做任何修改,则凹凸贴图变成一片黑色,应该是创建失败了。
请大家指点,难道说osgFX::Bumpmapping无法用于GL_POLYGON?
另:create_bumpmapping函数完整代码如下:
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 "OLYGON", the bumpmapping will fail and display just a black plane.
geom->addPrimitiveSet(new osg:rawArrays(osg:rimitiveSet::/*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:INEAR_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;
} |
|