1、创建棋盘纹理:
棋盘纹理,就是在一个二维空间创建一个网格,在这个网格的四角为一个区域,进行颜色填充。采用QUADS方式绘制改棋盘。
osg::Node* OSGAnimateImpl::CreateBase(osg::Vec3 center,float radius)
{
// 1. 创建棋盘纹理
// 网格数
int numTileX = 10;
int numTileY = 10;
// 网格宽高
float fWidth = 2*radius;
float fHeight = 2*radius;
// 坐标
osg::Vec3 v000(center - osg::Vec3(radius,radius,0.0f));
osg::Vec3 dx(osg::Vec3(fWidth/((float)numTileX),0.0f,0.0f));
osg::Vec3 dy(osg::Vec3(0.0f,fHeight/((float)numTileY),0.0f));
// fill in vertices for grid, note numTilesX+1 * numTilesY+1
// 生成网格点,横向:x+1 纵向:y+1 共(x+1)*(y+1)个点
osg::Vec3Array* coords = new osg::Vec3Array();
for(int j = 0; j <= numTileY; ++j)
for(int i = 0; i <= numTileX; ++i)
{
coords->push_back(v000 + dx*(float)i +dy*(float)j);
}
// 黑白两种颜色
osg::Vec4Array* clors = new osg::Vec4Array();
clors->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
clors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
int numClorSize = clors->size();
// 创建坐标
int numindicePreRow = numTileX + 1;
osg::UByteArray* indicescoord = new osg::UByteArray();
osg::UByteArray* indicesclor = new osg::UByteArray();
for(int ix = 0; ix < numTileX; ++ix)
for(int iy = 0; iy < numTileY; ++iy)
{
indicescoord->push_back(ix + (iy+1)*numindicePreRow);
indicescoord->push_back(ix + iy*numindicePreRow);
indicescoord->push_back((ix+1) + iy*numindicePreRow);
indicescoord->push_back((ix+1) + (iy+1)*numindicePreRow);
//clr
indicesclor->push_back((ix+iy)%numClorSize);
}
// normal
osg::Vec3Array* normals =new osg::Vec3Array();
normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
// set up the simple geometry
osg::Geometry* geom = new osg::Geometry();
geom->setVertexArray(coords);
geom->setVertexIndices(indicescoord);
geom->setColorArray(clors);
geom->setColorIndices(indicesclor);
geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
geom->setNormalArray(normals);
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,indicescoord->size()));
osg::Geode* geode = new osg::Geode();
geode->addDrawable(geom);
return geode;
} 1.1 效果图:
2、创建动画路径:
3、创建运动物体:
4、解析命令行参数:
5、其他:
本文详细介绍了一种使用OSG库创建棋盘纹理的方法,并提供了完整的代码实现。通过设定网格数量、宽度和高度等参数,可以灵活调整棋盘的大小与外观。此外,还介绍了如何为棋盘赋予黑白相间的颜色效果。
 animationPath&spm=1001.2101.3001.5002&articleId=18329321&d=1&t=3&u=e11dfeee1c4b498eadfb651b770098a3)
8999

被折叠的 条评论
为什么被折叠?



