osgtexture1D这一课主要讲解了osg中一维纹理的使用,一维纹理在计算机图形学中用的远没有二维纹理那么广泛,我们看看示例中的实现。
一维纹理可以看做是二维纹理的一种特殊情况,它的图片高度只有一个像素,在示例中一维纹理的图片是采用程序生成的
//给模型创建一个一维纹理的渲染状态 osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel) { //使用程序生成图片 osg::Image* image = new osg::Image; int noPixels = 1024; image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT); image->setInternalTextureFormat(GL_RGBA); typedef std::vector<osg::Vec4> ColorBands; ColorBands colorbands; colorbands.push_back(osg::Vec4(0.0f,0.0,0.0,1.0f)); colorbands.push_back(osg::Vec4(1.0f,0.0,0.0,1.0f)); colorbands.push_back(osg::Vec4(1.0f,1.0,0.0,1.0f)); colorbands.push_back(osg::Vec4(0.0f,1.0,0.0,1.0f)); colorbands.push_back(osg::Vec4(0.0f,1.0,1.0,1.0f)); colorbands.push_back(osg::Vec4(0.0f,0.0,1.0,1.0f)); colorbands.push_back(osg::Vec4(1.0f,0.0,1.0,1.0f)); colorbands.push_back(osg::Vec4(1.0f,1.0,1.0,1.0f)); float nobands = colorbands.size(); float delta = nobands/(float)noPixels; float pos = 0.0f; osg::Vec4* dataPtr = (osg::Vec4*)image->data(); //从0-127像素全是黑色(0.0f,0.0,0.0,1.0f) //从128-255像素全是红色(1.0f,0.0,0.0,1.0f) //...依次直到第1023像素全是白色(1.0f,1.0,1.0,1.0f) for(int i=0;i<noPixels;++i,pos+=delta) { osg::Vec4 color = colorbands[(int)pos]; *dataPtr++ = color; } //设置一维纹理的参数和WRAP方式 osg::Texture1D* texture = new osg::Texture1D; texture->setWrap(osg::Texture1D::WRAP_S,osg::Texture1D::MIRROR); texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR); texture->setImage(image); //覆盖(Override)子节点的渲染状态 osg::Material* material = new osg::Material; osg::StateSet* stateset = new osg::StateSet; stateset->setTextureAttribute(0,texture,osg::StateAttribute::OVERRIDE); stateset->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); stateset->setAttribute(material,osg::StateAttribute::OVERRIDE); return stateset; }
void animateTexGen(osg::TexGenNode* texgenNode,double time) { const double timeInterval = 2.0f; //更新间隔时间 static double previousTime = time; static bool state = false; while (time>previousTime+timeInterval) { previousTime+=timeInterval; state = !state; } if (state) { //物体空间模式 texgenNode->getTexGen()->setMode(osg::TexGen::OBJECT_LINEAR); } else { //相机空间模式(根据物体到相机的远近会修改纹理坐标) texgenNode->getTexGen()->setMode(osg::TexGen::EYE_LINEAR); } }
之后我们设置纹理坐标节点osg::TexGenNode,并设置它计算的平面参数,并把它添加到场景中:
osg::TexGenNode* texgenNode = new osg::TexGenNode; texgenNode->setReferenceFrame( osg::TexGenNode::ABSOLUTE_RF ); texgenNode->getTexGen()->setMode( osg::TexGen::OBJECT_LINEAR ); const osg::BoundingSphere& bs = loadedModel->getBound(); float zBase = bs.center().z()-bs.radius(); float zScale = 2.0f/bs.radius(); texgenNode->getTexGen()->setPlane(osg::TexGen::S,osg::Plane(0.0f,0.0f,zScale,-zBase)); texgenNode->setUpdateCallback(new AnimateTexGenCallback()); root -> addChild( texgenNode );看到这里一般会有两个疑问:1、为什么一个普通的StateAttribute还需要有一个节点呢?我们接触的一般osg::PolygonMode等等也不会需要一个osg::PolygonModeNode这样的节点类配合它起作用啊? 2、为什么不是将模型loadedModel添加到texgenNode下面作为它的子节点呢?
对于第一个疑问:因为在TexGen中如果设置模式是osg::TexGen::EYE_LINEAR,需要知道TexGen这时候的模型视图矩阵用来计算物体在眼坐标下到指定平面的距离(参考OpenGL之自动纹理生成),第二个疑问可以查看osg论坛中Robert对此的解释:osgtexture1d example / osg::TexGenNode order,不需要添加为子节点,因为OSG中只会记录一个TexGenNode的位置,并把它作为OpenGL的一个状态来使用。