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