|
本帖最后由 rookie 于 2013-1-2 21:58 编辑
想通过投影纹理技术,给指定的.osg模型设置纹理,但总是不成功(整个变成黑色,试着在场景中添加了一个球体模型来测试,球体模型可成功的映射上纹理。(效果图如下)纠结了很久不知道是什么原因!!!
效果图
关键代码如下:
// 在场景总结点上添加牛模型节点
osg::MatrixTransform *pMt = new osg::MatrixTransform;
osg::Node *pCowNode = osgDB::readNodeFile("cow.osg");
pMt->addChild(pCowNode);
pMt->setMatrix(osg::Matrix::translate(osg::Vec3(-22.89,4.17,32.20)));
pSceneRoot->addChild(pMt);
//在场景总结点上添加球模型节点
osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable( new osg::Sphere(osg::Vec3(-30.89,4.17,32.20),2.5) );
shape->setColor( osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f) );
osg::Geode *pSphere = new osg::Geode;
pSphere->addDrawable(shape);
pSceneRoot->addChild(pSphere);
//投影纹理技术
osg::ref_ptr<osg::Image> image=osgDB::readImageFile("Image001.jpg");
if (image.get())
{
osg::ref_ptr<osg::Texture2D> texture=new osg::Texture2D();
texture->setImage(image.get());
//设置滤波方式
texture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_BORDER);
texture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_BORDER);
texture->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_BORDER);
texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
//设置纹理环境
osg::ref_ptr<osg::TexEnv> texenv=new osg::TexEnv;
texenv->setMode(osg::TexEnv::Mode:ECAL);
texenv->setColor(osg::Vec4(0.6,0.6,0.6,0.5));
osg::ref_ptr<osg::TexGen> texgen=new osg::TexGen();
texgen->setMode(osg::TexGen::EYE_LINEAR);
texgen->setPlanesFromMatrix(m_MatirxVM*m_MatirxPM
*osg::Matrixd::translate(1.0,1.0,1.0)
*osg::Matrixd::scale(0.5,0.5,0.5));
osg::ref_ptr<osg::StateSet> state1=new osg::StateSet;
state1->setTextureAttributeAndModes(1,texture.get(),osg::StateAttribute::ON);
state1->setTextureAttributeAndModes(1,texgen.get(),osg::StateAttribute::ON);
state1->setTextureAttribute(1,texenv.get());
pPaneNode->setStateSet(state1.get());
pSphereGeode->setStateSet(state1.get());
//启动单元一自动生成纹理坐标,并使用纹理
osg::ref_ptr<osg::StateSet> state = new osg::StateSet;
state->setTextureAttributeAndModes(1,texture.get(),osg::StateAttribute::ON|osg::StateAttribute:ROTECTED);
state->setTextureAttributeAndModes(1,texgen.get(),osg::StateAttribute::ON|osg::StateAttribute::PROTECTED);
state->setTextureAttribute(1,texenv.get());
pCowNode->setStateSet(state.get());//为牛模型设置纹理
pSphere->setStateSet(state.get());//为球模型设置纹理
}
|
|