首页 > 代码库 > 美国海军(NPS)的OSG教程——示例代码一
美国海军(NPS)的OSG教程——示例代码一
//OSG 3.2版本 osg::Group *root = new osg::Group(); osg::Geode *pyramidGeode = new osg::Geode(); deprecated_osg::Geometry *pyramidGeometry = new deprecated_osg::Geometry(); //注意,用deprecated_osg,某些功能以后可能去掉 pyramidGeode->addDrawable( pyramidGeometry ); root->addChild( pyramidGeode ); //定义点 osg::Vec3Array *pyramidVertices = new osg::Vec3Array(); pyramidVertices->push_back( osg::Vec3(0,0,0) ); //左前 pyramidVertices->push_back( osg::Vec3(10,0,0) ); //右前 pyramidVertices->push_back( osg::Vec3(10,10,0) ); //右后 pyramidVertices->push_back( osg::Vec3(0,10,0) ); //左后 pyramidVertices->push_back( osg::Vec3(5,5,10) ); //塔尖 pyramidGeometry->setVertexArray( pyramidVertices ); //定义面 osg::DrawElementsUInt *pyramidBase = new osg::DrawElementsUInt( osg::PrimitiveSet::QUADS, 0 ); pyramidBase->push_back(3); pyramidBase->push_back(2); pyramidBase->push_back(1); pyramidBase->push_back(0); pyramidGeometry->addPrimitiveSet( pyramidBase ); osg::DrawElementsUInt *pyramidFaceOne = new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 ); pyramidFaceOne->push_back(0); pyramidFaceOne->push_back(1); pyramidFaceOne->push_back(4); pyramidGeometry->addPrimitiveSet( pyramidFaceOne ); osg::DrawElementsUInt *pyramidFaceTwo = new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 ); pyramidFaceTwo->push_back(1); pyramidFaceTwo->push_back(2); pyramidFaceTwo->push_back(4); pyramidGeometry->addPrimitiveSet( pyramidFaceTwo ); osg::DrawElementsUInt *pyramidFaceThree = new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 ); pyramidFaceThree->push_back(2); pyramidFaceThree->push_back(3); pyramidFaceThree->push_back(4); pyramidGeometry->addPrimitiveSet( pyramidFaceThree ); osg::DrawElementsUInt *pyramidFaceFour = new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 ); pyramidFaceFour->push_back(3); pyramidFaceFour->push_back(0); pyramidFaceFour->push_back(4); pyramidGeometry->addPrimitiveSet( pyramidFaceFour ); //定义颜色 osg::Vec4Array *colors = new osg::Vec4Array; colors->push_back( osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //红色 colors->push_back( osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); //绿色 colors->push_back( osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); //蓝色 colors->push_back( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //白色 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); colorIndexArray->push_back(1); colorIndexArray->push_back(2); colorIndexArray->push_back(3); colorIndexArray->push_back(0); pyramidGeometry->setColorArray( colors ); pyramidGeometry->setColorIndices( colorIndexArray ); pyramidGeometry->setColorBinding( deprecated_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); //必须加 osgUtil::Optimizer optimizer ; optimizer.optimize(root) ; osgViewer::Viewer viewer; viewer.setSceneData(root); //初始化并创建窗口 //viewer.realize(); viewer.run();
美国海军(NPS)的OSG教程——示例代码一
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。