| 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 |
|
|---|
| 18 |
|
|---|
| 19 |
pyramidGeode->addDrawable(pyramidGeometry); |
|---|
| 20 |
root->addChild(pyramidGeode); |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
osg::Vec3Array* pyramidVertices = new osg::Vec3Array; |
|---|
| 24 |
pyramidVertices->push_back( osg::Vec3( 0, 0, 0) ); |
|---|
| 25 |
pyramidVertices->push_back( osg::Vec3(10, 0, 0) ); |
|---|
| 26 |
pyramidVertices->push_back( osg::Vec3(10,10, 0) ); |
|---|
| 27 |
pyramidVertices->push_back( osg::Vec3( 0,10, 0) ); |
|---|
| 28 |
pyramidVertices->push_back( osg::Vec3( 5, 5,10) ); |
|---|
| 29 |
pyramidGeometry->setVertexArray( pyramidVertices ); |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 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 |
|
|---|
| 40 |
|
|---|
| 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 |
|
|---|
| 66 |
osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 67 |
colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); |
|---|
| 68 |
colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); |
|---|
| 69 |
colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); |
|---|
| 70 |
colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 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); |
|---|
| 77 |
colorIndexArray->push_back(1); |
|---|
| 78 |
colorIndexArray->push_back(2); |
|---|
| 79 |
colorIndexArray->push_back(3); |
|---|
| 80 |
colorIndexArray->push_back(0); |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
pyramidGeometry->setColorArray(colors); |
|---|
| 86 |
pyramidGeometry->setColorIndices(colorIndexArray); |
|---|
| 87 |
pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
osg::PositionAttitudeTransform* pyramidTwoXForm = new osg::PositionAttitudeTransform; |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
root->addChild(pyramidTwoXForm); |
|---|
| 96 |
pyramidTwoXForm->addChild(pyramidGeode); |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
osg::Vec3 pyramidTwoPosition(15,0,0); |
|---|
| 101 |
pyramidTwoXForm->setPosition( pyramidTwoPosition ); |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 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 |
} |
|---|