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

Revision 1815, 5.4 kB (checked in by robert, 11 years ago)

Added the usage report to the examples for when no arguments are passed
to the examples.

  • 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()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
78    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
79   
80    // construct the viewer.
81    osgProducer::Viewer viewer(arguments);
82
83    // set up the value with sensible default event handlers.
84    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
85
86    // get details on keyboard and mouse bindings used by the viewer.
87    viewer.getUsage(*arguments.getApplicationUsage());
88
89    // if user request help write it out to cout.
90    if (arguments.read("-h") || arguments.read("--help"))
91    {
92        arguments.getApplicationUsage()->write(std::cout);
93        return 1;
94    }
95
96    // any option left unread are converted into errors to write out later.
97    arguments.reportRemainingOptionsAsUnrecognized();
98
99    // report any errors if they have occured when parsing the program aguments.
100    if (arguments.errors())
101    {
102        arguments.writeErrorMessages(std::cout);
103        return 1;
104    }
105   
106    if (arguments.argc()<=1)
107    {
108        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
109        return 1;
110    }
111
112    // load the nodes from the commandline arguments.
113    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
114    if (!rootnode) {
115        return 1;
116    }
117
118    // create specular highlights
119    create_specular_highlights(rootnode);
120   
121    // run optimization over the scene graph
122    osgUtil::Optimizer optimzer;
123    optimzer.optimize(rootnode);
124     
125    // add a viewport to the viewer and attach the scene graph.
126    viewer.setSceneData(rootnode);
127   
128    // create the windows and run the threads.
129    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
130
131    while( !viewer.done() )
132    {
133        // wait for all cull and draw threads to complete.
134        viewer.sync();
135
136        // update the scene by traversing it with the the update visitor which will
137        // call all node update callbacks and animations.
138        viewer.update();
139         
140        // fire off the cull and draw traversals of the scene.
141        viewer.frame();
142       
143    }
144   
145    // wait for all cull and draw threads to complete before exit.
146    viewer.sync();
147
148    return 0;
149}
Note: See TracBrowser for help on using the browser.