| 1 | #include <osgProducer/Viewer> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Notify> |
|---|
| 4 | |
|---|
| 5 | #include <osg/Texture2D> |
|---|
| 6 | #include <osg/TexEnv> |
|---|
| 7 | #include <osg/TexGen> |
|---|
| 8 | |
|---|
| 9 | #include <osgDB/Registry> |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | |
|---|
| 12 | #include <osgGA/TrackballManipulator> |
|---|
| 13 | #include <osgGA/FlightManipulator> |
|---|
| 14 | #include <osgGA/DriveManipulator> |
|---|
| 15 | |
|---|
| 16 | #include <osgUtil/Optimizer> |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | int main( int argc, char **argv ) |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates a simple use of multi-texturing."); |
|---|
| 26 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 27 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | osgProducer::Viewer viewer(arguments); |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 40 | { |
|---|
| 41 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 42 | return 1; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | if (arguments.errors()) |
|---|
| 50 | { |
|---|
| 51 | arguments.writeErrorMessages(std::cout); |
|---|
| 52 | return 1; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | if (arguments.argc()<=1) |
|---|
| 56 | { |
|---|
| 57 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 58 | return 1; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | osg::Node* rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 63 | if (!rootnode) |
|---|
| 64 | { |
|---|
| 65 | |
|---|
| 66 | return 1; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | osg::Image* image = osgDB::readImageFile("Images/reflect.rgb"); |
|---|
| 70 | if (image) |
|---|
| 71 | { |
|---|
| 72 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 73 | texture->setImage(image); |
|---|
| 74 | |
|---|
| 75 | osg::TexGen* texgen = new osg::TexGen; |
|---|
| 76 | texgen->setMode(osg::TexGen::SPHERE_MAP); |
|---|
| 77 | |
|---|
| 78 | osg::TexEnv* texenv = new osg::TexEnv; |
|---|
| 79 | texenv->setMode(osg::TexEnv::BLEND); |
|---|
| 80 | texenv->setColor(osg::Vec4(0.3f,0.3f,0.3f,0.3f)); |
|---|
| 81 | |
|---|
| 82 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 83 | stateset->setTextureAttributeAndModes(1,texture,osg::StateAttribute::ON); |
|---|
| 84 | stateset->setTextureAttributeAndModes(1,texgen,osg::StateAttribute::ON); |
|---|
| 85 | stateset->setTextureAttribute(1,texenv); |
|---|
| 86 | |
|---|
| 87 | rootnode->setStateSet(stateset); |
|---|
| 88 | } |
|---|
| 89 | else |
|---|
| 90 | { |
|---|
| 91 | osg::notify(osg::NOTICE)<<"unable to load reflect map, model will not be mutlitextured"<<std::endl; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | osgUtil::Optimizer optimzer; |
|---|
| 96 | optimzer.optimize(rootnode); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | viewer.setSceneData( rootnode ); |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | viewer.realize(); |
|---|
| 103 | |
|---|
| 104 | for(unsigned int contextID = 0; |
|---|
| 105 | contextID<viewer.getDisplaySettings()->getMaxNumberOfGraphicsContexts(); |
|---|
| 106 | ++contextID) |
|---|
| 107 | { |
|---|
| 108 | osg::Texture::Extensions* textExt = osg::Texture::getExtensions(contextID,false); |
|---|
| 109 | if (textExt) |
|---|
| 110 | { |
|---|
| 111 | if (!textExt->isMultiTexturingSupported()) |
|---|
| 112 | { |
|---|
| 113 | std::cout<<"Warning: texture_cube_map not supported by OpenGL drivers, unable to run application."<<std::endl; |
|---|
| 114 | return 1; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | while( !viewer.done() ) |
|---|
| 120 | { |
|---|
| 121 | |
|---|
| 122 | viewer.sync(); |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | viewer.update(); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | viewer.frame(); |
|---|
| 130 | |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | viewer.sync(); |
|---|
| 135 | |
|---|
| 136 | return 0; |
|---|
| 137 | } |
|---|