| 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 | int main() |
|---|
| 11 | { |
|---|
| 12 | osgViewer::Viewer viewer; |
|---|
| 13 | osg::Group* root = new osg::Group(); |
|---|
| 14 | osg::Geode* pyramidGeode = new osg::Geode(); |
|---|
| 15 | osg::Geometry* pyramidGeometry = new osg::Geometry(); |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | pyramidGeode->addDrawable(pyramidGeometry); |
|---|
| 21 | root->addChild(pyramidGeode); |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | osg::Vec3Array* pyramidVertices = new osg::Vec3Array; |
|---|
| 35 | pyramidVertices->push_back( osg::Vec3( 0, 0, 0) ); |
|---|
| 36 | pyramidVertices->push_back( osg::Vec3(10, 0, 0) ); |
|---|
| 37 | pyramidVertices->push_back( osg::Vec3(10,10, 0) ); |
|---|
| 38 | pyramidVertices->push_back( osg::Vec3( 0,10, 0) ); |
|---|
| 39 | pyramidVertices->push_back( osg::Vec3( 5, 5,10) ); |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | pyramidGeometry->setVertexArray( pyramidVertices ); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | osg::DrawElementsUInt* pyramidBase = |
|---|
| 57 | new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0); |
|---|
| 58 | pyramidBase->push_back(3); |
|---|
| 59 | pyramidBase->push_back(2); |
|---|
| 60 | pyramidBase->push_back(1); |
|---|
| 61 | pyramidBase->push_back(0); |
|---|
| 62 | pyramidGeometry->addPrimitiveSet(pyramidBase); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | osg::DrawElementsUInt* pyramidFaceOne = |
|---|
| 68 | new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); |
|---|
| 69 | pyramidFaceOne->push_back(0); |
|---|
| 70 | pyramidFaceOne->push_back(1); |
|---|
| 71 | pyramidFaceOne->push_back(4); |
|---|
| 72 | pyramidGeometry->addPrimitiveSet(pyramidFaceOne); |
|---|
| 73 | |
|---|
| 74 | osg::DrawElementsUInt* pyramidFaceTwo = |
|---|
| 75 | new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); |
|---|
| 76 | pyramidFaceTwo->push_back(1); |
|---|
| 77 | pyramidFaceTwo->push_back(2); |
|---|
| 78 | pyramidFaceTwo->push_back(4); |
|---|
| 79 | pyramidGeometry->addPrimitiveSet(pyramidFaceTwo); |
|---|
| 80 | |
|---|
| 81 | osg::DrawElementsUInt* pyramidFaceThree = |
|---|
| 82 | new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); |
|---|
| 83 | pyramidFaceThree->push_back(2); |
|---|
| 84 | pyramidFaceThree->push_back(3); |
|---|
| 85 | pyramidFaceThree->push_back(4); |
|---|
| 86 | pyramidGeometry->addPrimitiveSet(pyramidFaceThree); |
|---|
| 87 | |
|---|
| 88 | osg::DrawElementsUInt* pyramidFaceFour = |
|---|
| 89 | new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); |
|---|
| 90 | pyramidFaceFour->push_back(3); |
|---|
| 91 | pyramidFaceFour->push_back(0); |
|---|
| 92 | pyramidFaceFour->push_back(4); |
|---|
| 93 | pyramidGeometry->addPrimitiveSet(pyramidFaceFour); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 98 | colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); |
|---|
| 99 | colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); |
|---|
| 100 | colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); |
|---|
| 101 | colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); |
|---|
| 102 | colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | pyramidGeometry->setColorArray(colors); |
|---|
| 109 | pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | osg::PositionAttitudeTransform* pyramidTwoXForm = |
|---|
| 118 | new osg::PositionAttitudeTransform(); |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | root->addChild(pyramidTwoXForm); |
|---|
| 125 | pyramidTwoXForm->addChild(pyramidGeode); |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | osg::Vec3 pyramidTwoPosition(15,0,0); |
|---|
| 131 | pyramidTwoXForm->setPosition( pyramidTwoPosition ); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | viewer.setSceneData( root ); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | viewer.setCameraManipulator(new osgGA::TrackballManipulator()); |
|---|
| 142 | viewer.realize(); |
|---|
| 143 | |
|---|
| 144 | while( !viewer.done() ) |
|---|
| 145 | { |
|---|
| 146 | viewer.frame(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | return 0; |
|---|
| 150 | } |
|---|