root/OpenSceneGraph/trunk/examples/osgcubemap/osgcubemap.cpp @ 1857

Revision 1857, 6.1 kB (checked in by robert, 11 years ago)

Added std:: infront of cout instances.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Group>
2#include <osg/StateSet>
3#include <osg/TextureCubeMap>
4#include <osg/TexGen>
5#include <osg/TexEnvCombine>
6
7#include <osgUtil/ReflectionMapGenerator>
8#include <osgUtil/HighlightMapGenerator>
9#include <osgUtil/HalfWayMapGenerator>
10#include <osgUtil/Optimizer>
11
12#include <osgDB/ReadFile>
13#include <osgDB/Registry>
14
15#include <osgGA/TrackballManipulator>
16#include <osgGA/FlightManipulator>
17#include <osgGA/DriveManipulator>
18
19#include <osgProducer/Viewer>
20
21#include <iostream>
22#include <string>
23#include <vector>
24
25void create_specular_highlights(osg::Node *node)
26{
27    osg::StateSet *ss = node->getOrCreateStateSet();
28   
29    // create and setup the texture object
30    osg::TextureCubeMap *tcm = new osg::TextureCubeMap;
31    tcm->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
32    tcm->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
33    tcm->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP);
34    tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
35    tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);   
36
37    // generate the six highlight map images (light direction = [1, 1, -1])
38    osgUtil::HighlightMapGenerator *mapgen = new osgUtil::HighlightMapGenerator(
39        osg::Vec3(1, 1, -1),            // light direction
40        osg::Vec4(1, 0.9f, 0.8f, 1),    // light color
41        8);                             // specular exponent
42   
43    mapgen->generateMap();
44
45    // assign the six images to the texture object
46    tcm->setImage(osg::TextureCubeMap::POSITIVE_X, mapgen->getImage(osg::TextureCubeMap::POSITIVE_X));
47    tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_X));
48    tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Y));
49    tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Y));
50    tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Z));
51    tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Z));
52
53    // enable texturing, replacing any textures in the subgraphs
54    ss->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
55
56    // texture coordinate generation
57    osg::TexGen *tg = new osg::TexGen;
58    tg->setMode(osg::TexGen::REFLECTION_MAP);
59    ss->setTextureAttributeAndModes(0, tg, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
60
61    // use TexEnvCombine to add the highlights to the original lighting
62    osg::TexEnvCombine *te = new osg::TexEnvCombine;   
63    te->setCombine_RGB(osg::TexEnvCombine::ADD);
64    te->setSource0_RGB(osg::TexEnvCombine::TEXTURE);
65    te->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
66    te->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
67    te->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
68    ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
69}
70
71int main(int argc, char *argv[])
72{
73    // use an ArgumentParser object to manage the program arguments.
74    osg::ArgumentParser arguments(&argc,argv);
75
76    // set up the usage document, in case we need to print out how to use this program.
77    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use texture cube mapping.");
78    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
79    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
80   
81    // construct the viewer.
82    osgProducer::Viewer viewer(arguments);
83
84    // set up the value with sensible default event handlers.
85    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
86
87    // get details on keyboard and mouse bindings used by the viewer.
88    viewer.getUsage(*arguments.getApplicationUsage());
89
90    // if user request help write it out to cout.
91    if (arguments.read("-h") || arguments.read("--help"))
92    {
93        arguments.getApplicationUsage()->write(std::cout);
94        return 1;
95    }
96
97    // any option left unread are converted into errors to write out later.
98    arguments.reportRemainingOptionsAsUnrecognized();
99
100    // report any errors if they have occured when parsing the program aguments.
101    if (arguments.errors())
102    {
103        arguments.writeErrorMessages(std::cout);
104        return 1;
105    }
106   
107    if (arguments.argc()<=1)
108    {
109        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
110        return 1;
111    }
112
113    // load the nodes from the commandline arguments.
114    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
115    if (!rootnode) {
116        return 1;
117    }
118
119    // create specular highlights
120    create_specular_highlights(rootnode);
121   
122    // run optimization over the scene graph
123    osgUtil::Optimizer optimzer;
124    optimzer.optimize(rootnode);
125     
126    // add a viewport to the viewer and attach the scene graph.
127    viewer.setSceneData(rootnode);
128   
129    // create the windows and run the threads.
130    viewer.realize();
131
132
133    // now check to see if texture cube map is supported.
134    for(unsigned int contextID = 0;
135        contextID<viewer.getDisplaySettings()->getMaxNumberOfGraphicsContexts();
136        ++contextID)
137    {
138        osg::TextureCubeMap::Extensions* tcmExt = osg::TextureCubeMap::getExtensions(contextID,false);
139        if (tcmExt)
140        {
141            if (!tcmExt->isCubeMapSupported())
142            {
143                std::cout<<"Warning: texture_cube_map not supported by OpenGL drivers, unable to run application."<<std::endl;
144                return 1;
145            }
146        }
147    }
148
149    while( !viewer.done() )
150    {
151        // wait for all cull and draw threads to complete.
152        viewer.sync();
153
154        // update the scene by traversing it with the the update visitor which will
155        // call all node update callbacks and animations.
156        viewer.update();
157         
158        // fire off the cull and draw traversals of the scene.
159        viewer.frame();
160       
161    }
162   
163    // wait for all cull and draw threads to complete before exit.
164    viewer.sync();
165
166    return 0;
167}
Note: See TracBrowser for help on using the browser.