root/OpenSceneGraph/trunk/examples/osgmultitexture/osgmultitexture.cpp @ 4868

Revision 4868, 4.3 kB (checked in by robert, 8 years ago)

Added comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
19int main( int argc, char **argv )
20{
21    // use an ArgumentParser object to manage the program arguments.
22    osg::ArgumentParser arguments(&argc,argv);
23
24    // set up the usage document, in case we need to print out how to use this program.
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    // construct the viewer.
30    osgProducer::Viewer viewer(arguments);
31
32    // set up the value with sensible default event handlers.
33    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
34
35    // get details on keyboard and mouse bindings used by the viewer.
36    viewer.getUsage(*arguments.getApplicationUsage());
37
38    // if user request help write it out to cout.
39    if (arguments.read("-h") || arguments.read("--help"))
40    {
41        arguments.getApplicationUsage()->write(std::cout);
42        return 1;
43    }
44
45    // any option left unread are converted into errors to write out later.
46    arguments.reportRemainingOptionsAsUnrecognized();
47
48    // report any errors if they have occured when parsing the program aguments.
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    // load the nodes from the commandline arguments.
62    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
63    if (!rootnode)
64    {
65//        write_usage(osg::notify(osg::NOTICE),argv[0]);
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    // run optimization over the scene graph
95    osgUtil::Optimizer optimzer;
96    optimzer.optimize(rootnode);
97     
98    // add a viewport to the viewer and attach the scene graph.
99    viewer.setSceneData( rootnode );
100   
101    // create the windows and run the threads.
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: multi-texturing not supported by OpenGL drivers, unable to run application."<<std::endl;
114                return 1;
115            }
116        }
117    }
118
119    while( !viewer.done() )
120    {
121        // wait for all cull and draw threads to complete.
122        viewer.sync();
123
124        // update the scene by traversing it with the the update visitor which will
125        // call all node update callbacks and animations.
126        viewer.update();
127         
128        // fire off the cull and draw traversals of the scene.
129        viewer.frame();
130       
131    }
132   
133    // wait for all cull and draw threads to complete before exit.
134    viewer.sync();
135
136    return 0;
137}
Note: See TracBrowser for help on using the browser.