首页 > 代码库 > OSG绘制几何图形
OSG绘制几何图形
在OSGMFC程序基础上修改OSG_MFC类的方法,如下:
1 void cOSG::InitSceneGraph(void) 2 { 3 // Init the main Root Node/Group 4 mRoot = new osg::Group; 5 6 //// Load the Model from the model name 7 //mModel = osgDB::readNodeFile(m_ModelName); 8 //if (!mModel) return; 9 10 // Optimize the model11 //osgUtil::Optimizer optimizer;12 //optimizer.optimize(mModel.get());13 //optimizer.reset();14 15 //// Add the model to the scene16 //mRoot->addChild(mModel.get());17 18 19 osg::Geode* pyramidGeode = new osg::Geode();20 osg::Geometry* pyramidGeometry = new osg::Geometry();21 pyramidGeode->addDrawable(pyramidGeometry); 22 mRoot->addChild(pyramidGeode);23 24 osg::Vec3Array* pyramidVertices = new osg::Vec3Array;25 pyramidVertices->push_back( osg::Vec3( 0, 0, 0) ); // front left26 pyramidVertices->push_back( osg::Vec3(10, 0, 0) ); // front right27 pyramidVertices->push_back( osg::Vec3(10,10, 0) ); // back right 28 pyramidVertices->push_back( osg::Vec3( 0,10, 0) ); // back left 29 pyramidVertices->push_back( osg::Vec3( 5, 5,10) ); // peak30 31 pyramidGeometry->setVertexArray( pyramidVertices ); 32 33 osg::DrawElementsUInt* pyramidBase = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);34 pyramidBase->push_back(3);35 pyramidBase->push_back(2);36 pyramidBase->push_back(1);37 pyramidBase->push_back(0);38 pyramidGeometry->addPrimitiveSet(pyramidBase); 39 40 osg::DrawElementsUInt* pyramidFaceOne = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);41 pyramidFaceOne->push_back(0);42 pyramidFaceOne->push_back(1);43 pyramidFaceOne->push_back(4);44 pyramidGeometry->addPrimitiveSet(pyramidFaceOne);45 46 osg::DrawElementsUInt* pyramidFaceTwo = 47 new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);48 pyramidFaceTwo->push_back(1);49 pyramidFaceTwo->push_back(2);50 pyramidFaceTwo->push_back(4);51 pyramidGeometry->addPrimitiveSet(pyramidFaceTwo);52 53 osg::DrawElementsUInt* pyramidFaceThree = 54 new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);55 pyramidFaceThree->push_back(2);56 pyramidFaceThree->push_back(3);57 pyramidFaceThree->push_back(4);58 pyramidGeometry->addPrimitiveSet(pyramidFaceThree);59 60 osg::DrawElementsUInt* pyramidFaceFour = 61 new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);62 pyramidFaceFour->push_back(3);63 pyramidFaceFour->push_back(0);64 pyramidFaceFour->push_back(4);65 pyramidGeometry->addPrimitiveSet(pyramidFaceFour);66 67 osg::Vec4Array* colors = new osg::Vec4Array;68 colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //index 0 red69 colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); //index 1 green70 colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); //index 2 blue71 colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //index 3 white 72 colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //index 4 red73 74 pyramidGeometry->setColorArray(colors);75 pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);76 77 // Declare and initialize a transform node.78 osg::PositionAttitudeTransform* pyramidTwoXForm = new osg::PositionAttitudeTransform;79 80 // Use the ‘addChild‘ method of the osg::Group class to81 // add the transform as a child of the root node and the82 // pyramid node as a child of the transform.83 mRoot->addChild(pyramidTwoXForm);84 pyramidTwoXForm->addChild(pyramidGeode);85 86 // Declare and initialize a Vec3 instance to change the87 // position of the tank model in the scene88 osg::Vec3 pyramidTwoPosition(15,0,0);89 pyramidTwoXForm->setPosition( pyramidTwoPosition ); 90 91 osgUtil::Optimizer optimizer;92 optimizer.optimize(mRoot.get());93 optimizer.reset();94 95 // Add the model to the scene96 // mRoot->addChild(root.get());97 98 }
运行效果:
http://blog.csdn.net/column/details/osgdemo.html
http://trac.openscenegraph.org/projects/osg//wiki/Support/Tutorials/BasicGeometry
OSG绘制几何图形
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。