| 1 | #include <osg/Geode> |
|---|
| 2 | #include <osg/ShapeDrawable> |
|---|
| 3 | #include <osg/Material> |
|---|
| 4 | #include <osg/Texture2D> |
|---|
| 5 | |
|---|
| 6 | #include <osgProducer/Viewer> |
|---|
| 7 | |
|---|
| 8 | #include <osgDB/ReadFile> |
|---|
| 9 | |
|---|
| 10 | #include <osg/Math> |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include "../osghangglide/terrain_coords.h" |
|---|
| 14 | |
|---|
| 15 | osg::Geode* createShapes( char* img_filename ) |
|---|
| 16 | { |
|---|
| 17 | osg::Geode* geode = new osg::Geode(); |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 23 | |
|---|
| 24 | if( ! img_filename ) img_filename = "Images/lz.rgb"; |
|---|
| 25 | osg::Image* image = osgDB::readImageFile( img_filename ); |
|---|
| 26 | |
|---|
| 27 | if (image) |
|---|
| 28 | { |
|---|
| 29 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 30 | texture->setImage(image); |
|---|
| 31 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | geode->setStateSet( stateset ); |
|---|
| 35 | |
|---|
| 36 | float radius = 0.8f; |
|---|
| 37 | float height = 1.0f; |
|---|
| 38 | |
|---|
| 39 | osg::TessellationHints* hints = new osg::TessellationHints; |
|---|
| 40 | hints->setDetailRatio(0.5f); |
|---|
| 41 | |
|---|
| 42 | geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints)); |
|---|
| 43 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),2*radius),hints)); |
|---|
| 44 | geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height),hints)); |
|---|
| 45 | geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height),hints)); |
|---|
| 46 | geode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(8.0f,0.0f,0.0f),radius,height),hints)); |
|---|
| 47 | |
|---|
| 48 | osg::HeightField* grid = new osg::HeightField; |
|---|
| 49 | grid->allocate(38,39); |
|---|
| 50 | grid->setXInterval(0.28f); |
|---|
| 51 | grid->setYInterval(0.28f); |
|---|
| 52 | |
|---|
| 53 | for(unsigned int r=0;r<39;++r) |
|---|
| 54 | { |
|---|
| 55 | for(unsigned int c=0;c<38;++c) |
|---|
| 56 | { |
|---|
| 57 | grid->setHeight(c,r,vertex[r+c*39][2]); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | geode->addDrawable(new osg::ShapeDrawable(grid)); |
|---|
| 61 | |
|---|
| 62 | osg::ConvexHull* mesh = new osg::ConvexHull; |
|---|
| 63 | osg::Vec3Array* vertices = new osg::Vec3Array(4); |
|---|
| 64 | (*vertices)[0].set(9.0+0.0f,-1.0f+2.0f,-1.0f+0.0f); |
|---|
| 65 | (*vertices)[1].set(9.0+1.0f,-1.0f+0.0f,-1.0f+0.0f); |
|---|
| 66 | (*vertices)[2].set(9.0+2.0f,-1.0f+2.0f,-1.0f+0.0f); |
|---|
| 67 | (*vertices)[3].set(9.0+1.0f,-1.0f+1.0f,-1.0f+2.0f); |
|---|
| 68 | osg::UByteArray* indices = new osg::UByteArray(12); |
|---|
| 69 | (*indices)[0]=0; |
|---|
| 70 | (*indices)[1]=2; |
|---|
| 71 | (*indices)[2]=1; |
|---|
| 72 | (*indices)[3]=0; |
|---|
| 73 | (*indices)[4]=1; |
|---|
| 74 | (*indices)[5]=3; |
|---|
| 75 | (*indices)[6]=1; |
|---|
| 76 | (*indices)[7]=2; |
|---|
| 77 | (*indices)[8]=3; |
|---|
| 78 | (*indices)[9]=2; |
|---|
| 79 | (*indices)[10]=0; |
|---|
| 80 | (*indices)[11]=3; |
|---|
| 81 | mesh->setVertices(vertices); |
|---|
| 82 | mesh->setIndices(indices); |
|---|
| 83 | geode->addDrawable(new osg::ShapeDrawable(mesh)); |
|---|
| 84 | |
|---|
| 85 | return geode; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | int main( int argc, char **argv ) |
|---|
| 89 | { |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates the osg::Shape classes."); |
|---|
| 96 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [image_filename]"); |
|---|
| 97 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | osgProducer::Viewer viewer(arguments); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 110 | { |
|---|
| 111 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 112 | return 1; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | if (arguments.errors()) |
|---|
| 120 | { |
|---|
| 121 | arguments.writeErrorMessages(std::cout); |
|---|
| 122 | return 1; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | char* img_filename = 0; |
|---|
| 126 | for( int pos = 1; pos < arguments.argc(); ++pos ) |
|---|
| 127 | { |
|---|
| 128 | if( arguments.isString(pos) ) |
|---|
| 129 | { |
|---|
| 130 | img_filename = arguments[pos]; |
|---|
| 131 | break; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | osg::Node* node = createShapes( img_filename ); |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | viewer.setSceneData( node ); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | viewer.realize(); |
|---|
| 142 | |
|---|
| 143 | while( !viewer.done() ) |
|---|
| 144 | { |
|---|
| 145 | |
|---|
| 146 | viewer.sync(); |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | viewer.update(); |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | viewer.frame(); |
|---|
| 154 | |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | viewer.sync(); |
|---|
| 159 | |
|---|
| 160 | return 0; |
|---|
| 161 | } |
|---|