1 /* OpenSceneGraph example, osgmultitexture.
2 *
3 *  Permission is hereby granted, free of charge, to any person obtaining a copy
4 *  of this software and associated documentation files (the "Software"), to deal
5 *  in the Software without restriction, including without limitation the rights
6 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 *  copies of the Software, and to permit persons to whom the Software is
8 *  furnished to do so, subject to the following conditions:
9 *
10 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16 *  THE SOFTWARE.
17 */
18 
19 #include <osgViewer/Viewer>
20 
21 #include <osg/Notify>
22 
23 #include <osg/Texture2D>
24 #include <osg/TexEnv>
25 #include <osg/TexGen>
26 
27 #include <osgDB/Registry>
28 #include <osgDB/ReadFile>
29 
30 #include <osgGA/TrackballManipulator>
31 #include <osgGA/FlightManipulator>
32 #include <osgGA/DriveManipulator>
33 
34 #include <osgUtil/Optimizer>
35 
36 #include <iostream>
37 
main(int argc,char ** argv)38 int main( int argc, char **argv )
39 {
40     // use an ArgumentParser object to manage the program arguments.
41     osg::ArgumentParser arguments(&argc,argv);
42 
43     // construct the viewer.
44     osgViewer::Viewer viewer;
45 
46     // load the nodes from the commandline arguments.
47     osg::ref_ptr<osg::Node> rootnode = osgDB::readRefNodeFiles(arguments);
48 
49     // if not loaded assume no arguments passed in, try use default mode instead.
50     if (!rootnode) rootnode = osgDB::readRefNodeFile("cessnafire.osgt");
51 
52     if (!rootnode)
53     {
54         osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
55         return 1;
56     }
57 
58     osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile("Images/reflect.rgb");
59     if (image)
60     {
61         osg::Texture2D* texture = new osg::Texture2D;
62         texture->setImage(image);
63 
64         osg::TexGen* texgen = new osg::TexGen;
65         texgen->setMode(osg::TexGen::SPHERE_MAP);
66 
67         osg::TexEnv* texenv = new osg::TexEnv;
68         texenv->setMode(osg::TexEnv::BLEND);
69         texenv->setColor(osg::Vec4(0.3f,0.3f,0.3f,0.3f));
70 
71         osg::StateSet* stateset = new osg::StateSet;
72         stateset->setTextureAttributeAndModes(1,texture,osg::StateAttribute::ON);
73         stateset->setTextureAttributeAndModes(1,texgen,osg::StateAttribute::ON);
74         stateset->setTextureAttribute(1,texenv);
75 
76         rootnode->setStateSet(stateset);
77     }
78     else
79     {
80         osg::notify(osg::NOTICE)<<"unable to load reflect map, model will not be mutlitextured"<<std::endl;
81     }
82 
83     // run optimization over the scene graph
84     osgUtil::Optimizer optimzer;
85     optimzer.optimize(rootnode);
86 
87     // add a viewport to the viewer and attach the scene graph.
88     viewer.setSceneData( rootnode );
89 
90     // create the windows and run the threads.
91     viewer.realize();
92 
93     for(unsigned int contextID = 0;
94         contextID<osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts();
95         ++contextID)
96     {
97         osg::GLExtensions* textExt = osg::GLExtensions::Get(contextID,false);
98         if (textExt)
99         {
100             if (!textExt->isMultiTexturingSupported)
101             {
102                 std::cout<<"Warning: multi-texturing not supported by OpenGL drivers, unable to run application."<<std::endl;
103                 return 1;
104             }
105         }
106     }
107 
108     return viewer.run();
109 }
110