在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 model 11 //osgUtil::Optimizer optimizer; 12 //optimizer.optimize(mModel.get()); 13 //optimizer.reset();
14
15 //// Add the model to the scene
16 //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 left
26 pyramidVertices->push_back( osg::Vec3(10, 0, 0) ); // front right
27 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) ); // peak
30
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 red
69 colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); //index 1 green
70 colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); //index 2 blue
71 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 red
73
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 to 81 // add the transform as a child of the root node and the 82 // 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 the 87 // position of the tank model in the scene
88 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 scene 96 // mRoot->addChild(root.get());
97
98 }
运行效果:
http://blog.csdn.net/column/details/osgdemo.html
http://trac.openscenegraph.org/projects/osg//wiki/Support/Tutorials/BasicGeometry