|
说明:
1.在一个系统里面 用这段代码贴纹理 可以成功.
2.在另一个系统里面复制了这段代码过去,结果贴纹理只出来纹理图片的一个基本色,比如一片蓝或一片绿.
某些情况下 还只给节点贴了一半的纹理.
感觉很费解,请大家给分析一下代码有什么问题.
- //root 为需要普纹理的节点
- //img 是纹理图片
- //width 是纹理在x轴方向上重复的次数
- //height 是纹理在y轴方向上重复的次数
- void applyGeode(Geode* root,Image* img,float width,float height)
- {
- if(root==NULL||img==NULL) return;
- //获取x,y范围 把这个范围映射到width,height范围上.
- BoundingBox box=root->getBoundingBox ();
- float dx=box.xMax ()-box.xMin ();
- float dy=box.yMax ()-box.yMin();
- for(unsigned int i=0;i<root->getNumDrawables ();i++)
- {
- Drawable* draw=root->getDrawable (i);
- Geometry* geom=draw->asGeometry ();
- if(geom)
- {
- //给每个顶点设置对应的纹理坐标
- Vec3Array* va=dynamic_cast<Vec3Array*>(geom->getVertexArray ());
- Vec2Array* txtCoord=new Vec2Array(va->size());
- Vec3Array::iterator it=va->begin ();
- while(it!=va->end()){
- float px=(*it).x();
- float py=(*it).y();
- float tx=(px-box.xMin())*width/dx;
- float ty=(py-box.yMin())*height/dy;
- txtCoord->push_back (Vec2(tx,ty));
- it++;
- }
- geom->setTexCoordArray (0,txtCoord);
- //set normal data
- Vec3Array* nc=new Vec3Array;
- nc->push_back(Vec3(0.f,-1.f,0.f));
- geom->setNormalArray(nc);
- geom->setNormalBinding(Geometry::BIND_OVERALL );
- }
- }
- Texture2D* txt=new Texture2D();
- txt->setWrap (Texture2D::WRAP_S ,Texture2D::REPEAT );
- txt->setWrap (Texture2D::WRAP_T ,Texture2D::REPEAT );
- txt->setFilter(Texture::MIN_FILTER,Texture::LINEAR);
- txt->setFilter(Texture::MAG_FILTER,Texture::NEAREST);
- txt->setImage (img);
- StateSet *state=root->getOrCreateStateSet ();
- state->setTextureAttribute (0,txt,StateAttribute::ON );
- state->setTextureMode (0,GL_TEXTURE_2D,StateAttribute::ON|StateAttribute::OVERRIDE );
- root->setStateSet (state);
- }
复制代码 |
|