|
大家好,我通过纹理投影方式给三维模型贴图,代码大体如下:
/**************************************/
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);
texture->setDataVariance(osg::Object:YNAMIC);
texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D:INEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
texture->setBorderColor(osg::Vec4(0.0f, 0.0f, 0.0f, 0.0f));
texture->setBorderWidth(0);
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(szImgPath);
texture->setImage(image);
osg::ref_ptr<osg::TexGen> texgen = new osg::TexGen;
texgen->setMode(osg::TexGen::OBJECT_LINEAR);
texgen->setPlane(osg::TexGen::S, osg:lane(...));
texgen->setPlane(osg::TexGen::T, osg::Plane(...));
osg::ref_ptr<osg::TexEnv> texenv = new osg::TexEnv;
texenv->setMode(osg::TexEnv::DECAL);
texenv->setColor(osg::Vec4(0.0f, 0.0f, 0.0f, 0.0f));
// modelGeode:三维模型
osg::StateSet *stateset = modelGeode->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
// nTextureIndex:纹理通道序号
stateset->setTextureMode(nTextureIndex, GL_TEXTURE_GEN_S, osg::StateAttribute::ON);
stateset->setTextureMode(nTextureIndex, GL_TEXTURE_GEN_T, osg::StateAttribute::ON);
stateset->setTextureAttributeAndModes(nTextureIndex, texture, osg::StateAttribute::ON);
stateset->setTextureAttributeAndModes(nTextureIndex, texgen, osg::StateAttribute::ON);
stateset->setTextureAttribute(nTextureIndex, texenv, osg::StateAttribute::ON);
/**************************************/
现在的问题是,纹理不仅投影在了我希望它出现的地方,也投影在了模型的背面。我了解了一下,这个问题好像叫做“back projection”,我没有找到解决的方法。在SGI OpenGL教程中有这么一段话,说明了纹理投影方式可能会出现这种情况,但并没有明确给出解决方案 :
请注意,就像观察和投影,纹理投影也不是完全符合光学原理的。除非采用特殊的方法,纹理将会影响到所有的表面----不管是前面的还是后面的(译者注:就是电影机后面也会有电影图象,当然这是不符合光学原理的)。因为没有默认的视见体裁剪(View Volume ),应用程序需要小心的避免不希望出现的投影纹理效果。用户自定义裁剪面(附加裁剪面)有助于更好的控制投影纹理该出现在什么地方。
Please note that like the viewing projections, the texture projection is not really optical. Unless special steps are taken, the texture will affect all surfaces within the projection, both in front and in back of the projection. Since there is no implicit view volume clipping (like there is with the OpenGL viewing pipeline), the application needs to be carefully modeled to avoid undesired texture projections, or user defined clipping planes can be used to control where the projected texture appears.
请教各位高手,用什么办法解决这个问题呢?
先谢谢了! |
|