Support/Tutorials/BasicGeometry: BasicGeometry.cpp

Line 
1 #include <osg/Node>
2 #include <osg/Group>
3 #include <osg/Geode>
4 #include <osg/Geometry>
5 #include <osg/Texture2D>
6 #include <osgDB/ReadFile>
7 #include <osgViewer/Viewer>
8 #include <osg/PositionAttitudeTransform>
9
10 int main()
11 {
12    osg::Group* root = new osg::Group();
13    osg::Geode* pyramidGeode = new osg::Geode();
14    osg::Geometry* pyramidGeometry = new osg::Geometry();
15
16    // Associate the pyramid geometry with the pyramid geode
17    // Add the pyramid geode to the root node of the scene graph.
18    pyramidGeode->addDrawable(pyramidGeometry);
19    root->addChild(pyramidGeode);
20
21    // Declare an array of vertices.
22    osg::Vec3Array* pyramidVertices = new osg::Vec3Array;
23    pyramidVertices->push_back( osg::Vec3( 0, 0, 0) ); // front left
24    pyramidVertices->push_back( osg::Vec3(10, 0, 0) ); // front right
25    pyramidVertices->push_back( osg::Vec3(10,10, 0) ); // back right
26    pyramidVertices->push_back( osg::Vec3( 0,10, 0) ); // back left
27    pyramidVertices->push_back( osg::Vec3( 5, 5,10) ); // peak
28
29    // Associate this set of vertices with the geometry associated with the
30    // geode we added to the scene.
31    pyramidGeometry->setVertexArray( pyramidVertices );
32    crossGeometry->setVertexArray (crossVertices);
33    
34    // Create a primitive set and add it to the pyramid geometry.
35    osg::DrawElementsUInt* pyramidBase = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
36    pyramidBase->push_back(3);
37    pyramidBase->push_back(2);
38    pyramidBase->push_back(1);
39    pyramidBase->push_back(0);
40    pyramidGeometry->addPrimitiveSet(pyramidBase);
41
42    // Repeat the same for each of the four sides. Again, vertices are
43    // specified in counter-clockwise order.
44    osg::DrawElementsUInt* pyramidFaceOne = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
45    pyramidFaceOne->push_back(0);
46    pyramidFaceOne->push_back(1);
47    pyramidFaceOne->push_back(4);
48    pyramidGeometry->addPrimitiveSet(pyramidFaceOne);
49
50    osg::DrawElementsUInt* pyramidFaceTwo = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
51    pyramidFaceTwo->push_back(1);
52    pyramidFaceTwo->push_back(2);
53    pyramidFaceTwo->push_back(4);
54    pyramidGeometry->addPrimitiveSet(pyramidFaceTwo);
55
56    osg::DrawElementsUInt* pyramidFaceThree = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
57    pyramidFaceThree->push_back(2);
58    pyramidFaceThree->push_back(3);
59    pyramidFaceThree->push_back(4);
60    pyramidGeometry->addPrimitiveSet(pyramidFaceThree);
61
62    osg::DrawElementsUInt* pyramidFaceFour = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
63    pyramidFaceFour->push_back(3);
64    pyramidFaceFour->push_back(0);
65    pyramidFaceFour->push_back(4);
66    pyramidGeometry->addPrimitiveSet(pyramidFaceFour);
67
68    //Declare and load an array of Vec4 elements to store colors.
69    osg::Vec4Array* colors = new osg::Vec4Array;
70    colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //index 0 red
71    colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); //index 1 green
72    colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); //index 2 blue
73    colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //index 3 white
74
75    // Declare the variable that will match vertex array elements to color
76    // array elements.
77    osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,4> *colorIndexArray;
78    colorIndexArray = new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,4>;
79    colorIndexArray->push_back(0); // vertex 0 assigned color array element 0
80    colorIndexArray->push_back(1); // vertex 1 assigned color array element 1
81    colorIndexArray->push_back(2); // vertex 2 assigned color array element 2
82    colorIndexArray->push_back(3); // vertex 3 assigned color array element 3
83    colorIndexArray->push_back(0); // vertex 4 assigned color array element 0
84
85    //The next step is to associate the array of colors with the geometry,
86    //assign the color indices created above to the geometry and set the
87    //binding mode to BIND_PER_VERTEX.
88    pyramidGeometry->setColorArray(colors);
89    pyramidGeometry->setColorIndices(colorIndexArray);
90    pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
91
92    // Declare and initialize a transform node.
93    osg::PositionAttitudeTransform* pyramidTwoXForm = new osg::PositionAttitudeTransform();
94
95    // Use the 'addChild' method of the osg::Group class to
96    // add the transform as a child of the root node and the
97    // pyramid node as a child of the transform.
98    root->addChild(pyramidTwoXForm);
99    pyramidTwoXForm->addChild(pyramidGeode);
100
101    // Declare and initialize a Vec3 instance to change the
102    // position of the model in the scene
103    osg::Vec3 pyramidTwoPosition(15,0,0);
104    pyramidTwoXForm->setPosition( pyramidTwoPosition );
105
106    // The final step is to set up and enter a simulation loop.
107    osgViewer::Viewer viewer;
108
109    viewer.setSceneData( root );
110    return viewer.run();
111 }