|
楼主 |
发表于 2008-3-9 14:13:45
|
显示全部楼层
谢谢array以及ls几位的回复.
貌似都做了,这是主程序代码:
int main()
{
osg::Group* root=new osg::Group();
osg::Geode* pyramidGeode =new osg::Geode();
osg::Geometry* pyramidGeometry= new osg::Geometry();
pyramidGeode->addDrawable(pyramidGeometry);
root->addChild(pyramidGeode);
osg::Vec3Array* pyramidVertices = new osg::Vec3Array;
pyramidVertices->push_back( osg::Vec3( 0, 0, 0 ) ); //Left front -----0
pyramidVertices->push_back( osg::Vec3(10, 0, 0 ) ); //right front -----1
pyramidVertices->push_back( osg::Vec3(10, 10, 0 ) ); //right back -----2
pyramidVertices->push_back( osg::Vec3( 0, 10, 0 ) ); //left back-----3
pyramidVertices->push_back( osg::Vec3( 5, 5, 10 ) ); //Top-----4
pyramidGeometry->setVertexArray( pyramidVertices );
//为了保证背面剔除(backface cullling)的正确,我们需要按照逆时针的顺序添加顶点数据
//法线垂直于底面指向几何体外;所以顶点顺序:3, 2, 1, 0
osg:rawElementsUInt* pyramidBase = new
osg::DrawElementsUInt( osg:rimitiveSet:UADS, 0 );
pyramidBase->push_back(3);
pyramidBase->push_back(2);
pyramidBase->push_back(1);
pyramidBase->push_back(0);
//法线垂直于侧面向外,顺序;
osg::DrawElementsUInt* pyramidFaceOne = new
osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
pyramidFaceOne->push_back(0); //----0
pyramidFaceOne->push_back(1);
pyramidFaceOne->push_back(4);
osg::DrawElementsUInt* pyramidFaceTwo = new
osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
pyramidFaceTwo->push_back(1); //----1
pyramidFaceTwo->push_back(2);
pyramidFaceTwo->push_back(4);
osg::DrawElementsUInt* pyramidFaceThree = new
osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
pyramidFaceThree->push_back(2); //----2
pyramidFaceThree->push_back(3);
pyramidFaceThree->push_back(4);
osg::DrawElementsUInt* pyramidFaceFour = new
osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
pyramidFaceFour->push_back(3); //----3
pyramidFaceFour->push_back(0);
pyramidFaceFour->push_back(4);
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back( osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //索引0--red
colors->push_back( osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); //索引1--green
colors->push_back( osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); //索引2--blue
colors->push_back( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //索引3--white
osg::TemplateIndexArray
<unsigned int, osg::Array::UIntArrayType, 4, 4> *colorIndexArray;
colorIndexArray =
new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType, 4, 4>;
colorIndexArray->push_back(0); // vertex 0 assigned color array element 0
colorIndexArray->push_back(1); // vertex 1 assigned color array element 1
colorIndexArray->push_back(2); // vertex 2 assigned color array element 2
colorIndexArray->push_back(3); // vertex 3 assigned color array element 3
colorIndexArray->push_back(0); // vertex 4 assigned color array element 0
pyramidGeometry->setColorArray( colors );
pyramidGeometry->setColorIndices( colorIndexArray );
pyramidGeometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
osg::Vec2Array* texcoords = new osg::Vec2Array(5);
(*texcoords)[0].set(0.00f,0.0f);
(*texcoords)[1].set(0.25f,0.0f);
(*texcoords)[2].set(0.50f,0.0f);
(*texcoords)[3].set(0.75f,0.0f);
(*texcoords)[4].set(0.50f,1.0f);
pyramidGeometry->setTexCoordArray(0,texcoords);
//初始化位置变换节点
osg::PositionAttitudeTransform* pyramidTwoXForm =
new osg::PositionAttitudeTransform();
//使用osg::Group的addChild方法,将位置变换节点添加到根节点的子节点上,并将金字塔节点作为变换节点的子节点
root->addChild(pyramidTwoXForm);
pyramidTwoXForm->addChild(pyramidGeode);
// 初始化一个Vec3实例,用于改变模型在场景中的位置
osg::Vec3 pyramidTwoPosition(15,0,0);
pyramidTwoXForm->setPosition( pyramidTwoPosition );
osgViewer::Viewer viewer;
viewer.setSceneData( root );
viewer.run();
} |
|