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

Revision 6916, 2.6 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, added fallbacks for when now command line args are provided.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgViewer/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#include <iostream>
19
20int main( int argc, char **argv )
21{
22    // use an ArgumentParser object to manage the program arguments.
23    osg::ArgumentParser arguments(&argc,argv);
24   
25    // construct the viewer.
26    osgViewer::Viewer viewer;
27
28    // load the nodes from the commandline arguments.
29    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
30   
31    // if not loaded assume no arguments passed in, try use default mode instead.
32    if (!rootnode) rootnode = osgDB::readNodeFile("cessnafire.osg");
33   
34    if (!rootnode)
35    {
36        osg::notify(osg::NOTICE)<<"Please specify and model filename on the command line."<<std::endl;
37        return 1;
38    }
39   
40    osg::Image* image = osgDB::readImageFile("Images/reflect.rgb");
41    if (image)
42    {
43        osg::Texture2D* texture = new osg::Texture2D;
44        texture->setImage(image);
45
46        osg::TexGen* texgen = new osg::TexGen;
47        texgen->setMode(osg::TexGen::SPHERE_MAP);
48
49        osg::TexEnv* texenv = new osg::TexEnv;
50        texenv->setMode(osg::TexEnv::BLEND);
51        texenv->setColor(osg::Vec4(0.3f,0.3f,0.3f,0.3f));
52
53        osg::StateSet* stateset = new osg::StateSet;
54        stateset->setTextureAttributeAndModes(1,texture,osg::StateAttribute::ON);
55        stateset->setTextureAttributeAndModes(1,texgen,osg::StateAttribute::ON);
56        stateset->setTextureAttribute(1,texenv);
57       
58        rootnode->setStateSet(stateset);
59    }
60    else
61    {
62        osg::notify(osg::NOTICE)<<"unable to load reflect map, model will not be mutlitextured"<<std::endl;
63    }
64
65    // run optimization over the scene graph
66    osgUtil::Optimizer optimzer;
67    optimzer.optimize(rootnode);
68     
69    // add a viewport to the viewer and attach the scene graph.
70    viewer.setSceneData( rootnode );
71   
72    // create the windows and run the threads.
73    viewer.realize();
74
75    for(unsigned int contextID = 0;
76        contextID<osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts();
77        ++contextID)
78    {
79        osg::Texture::Extensions* textExt = osg::Texture::getExtensions(contextID,false);
80        if (textExt)
81        {
82            if (!textExt->isMultiTexturingSupported())
83            {
84                std::cout<<"Warning: multi-texturing not supported by OpenGL drivers, unable to run application."<<std::endl;
85                return 1;
86            }
87        }
88    }
89
90    return viewer.run();
91}
Note: See TracBrowser for help on using the browser.