OSG绘制几何图形

时间:2024-04-15 17:35:21

在OSGMFC程序基础上修改OSG_MFC类的方法,如下:

 void cOSG::InitSceneGraph(void)
{
// Init the main Root Node/Group
mRoot = new osg::Group; //// Load the Model from the model name
//mModel = osgDB::readNodeFile(m_ModelName);
//if (!mModel) return; // Optimize the model
//osgUtil::Optimizer optimizer;
//optimizer.optimize(mModel.get());
//optimizer.reset(); //// Add the model to the scene
//mRoot->addChild(mModel.get()); osg::Geode* pyramidGeode = new osg::Geode();
osg::Geometry* pyramidGeometry = new osg::Geometry();
pyramidGeode->addDrawable(pyramidGeometry);
mRoot->addChild(pyramidGeode); osg::Vec3Array* pyramidVertices = new osg::Vec3Array;
pyramidVertices->push_back( osg::Vec3( , , ) ); // front left
pyramidVertices->push_back( osg::Vec3(, , ) ); // front right
pyramidVertices->push_back( osg::Vec3(,, ) ); // back right
pyramidVertices->push_back( osg::Vec3( ,, ) ); // back left
pyramidVertices->push_back( osg::Vec3( , ,) ); // peak pyramidGeometry->setVertexArray( pyramidVertices ); osg::DrawElementsUInt* pyramidBase = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, );
pyramidBase->push_back();
pyramidBase->push_back();
pyramidBase->push_back();
pyramidBase->push_back();
pyramidGeometry->addPrimitiveSet(pyramidBase); osg::DrawElementsUInt* pyramidFaceOne = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, );
pyramidFaceOne->push_back();
pyramidFaceOne->push_back();
pyramidFaceOne->push_back();
pyramidGeometry->addPrimitiveSet(pyramidFaceOne); osg::DrawElementsUInt* pyramidFaceTwo =
new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, );
pyramidFaceTwo->push_back();
pyramidFaceTwo->push_back();
pyramidFaceTwo->push_back();
pyramidGeometry->addPrimitiveSet(pyramidFaceTwo); osg::DrawElementsUInt* pyramidFaceThree =
new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, );
pyramidFaceThree->push_back();
pyramidFaceThree->push_back();
pyramidFaceThree->push_back();
pyramidGeometry->addPrimitiveSet(pyramidFaceThree); osg::DrawElementsUInt* pyramidFaceFour =
new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, );
pyramidFaceFour->push_back();
pyramidFaceFour->push_back();
pyramidFaceFour->push_back();
pyramidGeometry->addPrimitiveSet(pyramidFaceFour); osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //index 0 red
colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); //index 1 green
colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); //index 2 blue
colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //index 3 white
colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //index 4 red pyramidGeometry->setColorArray(colors);
pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); // Declare and initialize a transform node.
osg::PositionAttitudeTransform* pyramidTwoXForm = new osg::PositionAttitudeTransform; // Use the 'addChild' method of the osg::Group class to
// add the transform as a child of the root node and the
// pyramid node as a child of the transform.
mRoot->addChild(pyramidTwoXForm);
pyramidTwoXForm->addChild(pyramidGeode); // Declare and initialize a Vec3 instance to change the
// position of the tank model in the scene
osg::Vec3 pyramidTwoPosition(,,);
pyramidTwoXForm->setPosition( pyramidTwoPosition ); osgUtil::Optimizer optimizer;
optimizer.optimize(mRoot.get());
optimizer.reset(); // Add the model to the scene
// mRoot->addChild(root.get()); }

运行效果:

OSG绘制几何图形

http://blog.****.net/column/details/osgdemo.html

http://trac.openscenegraph.org/projects/osg//wiki/Support/Tutorials/BasicGeometry