查看: 2103|回复: 4

【疑问】凹凸贴图无法用于GL_POLYGON?

[复制链接]

该用户从未签到

发表于 2011-3-28 18:28:33 | 显示全部楼层 |阅读模式
大家好,我使用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;
}

该用户从未签到

 楼主| 发表于 2011-3-28 18:29:18 | 显示全部楼层
代码大家凑乎看吧,有些变成了表情,相信你们能懂的。

该用户从未签到

发表于 2011-3-29 08:37:52 | 显示全部楼层
发帖时请使用专门的粘帖代码工具,即工具栏上的“<>”图标

GL_POLYGON这种方式在OpenGL中是完全不推荐的,因为它事实上很难被显卡有效支持。更多的时候我们应当选择立即将几何体三角化,或者直接使用GL_TRIANGLES的方式来组装图元

该用户从未签到

 楼主| 发表于 2011-3-29 10:12:24 | 显示全部楼层
回复 3# array


   谢谢array,看来我得把polygon都改了。

该用户从未签到

 楼主| 发表于 2011-3-29 11:10:32 | 显示全部楼层
回复 3# array


   根据你的建议,我在对创建的POLYGON进行tesselate后就可以了,估计是因为tesselate后POLYGON变成了TRIANGLE_FAN的缘故,谢谢array。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表