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

Revision 6908, 4.5 kB (checked in by robert, 8 years ago)

Added default file load to avoid the need for command line option.

  • 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 <osgViewer/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    // construct the viewer.
77    osgViewer::Viewer viewer;
78   
79    // load the nodes from the commandline arguments.
80    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
81
82    // if not loaded assume no arguments passed in, try use default mode instead.
83    if (!rootnode) rootnode = osgDB::readNodeFile("cessna.osg");
84
85    if (!rootnode)
86    {
87        osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
88        return 1;
89    }
90
91    // create specular highlights
92    create_specular_highlights(rootnode);
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    // now check to see if texture cube map is supported.
105    for(unsigned int contextID = 0;
106        contextID<osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts();
107        ++contextID)
108    {
109        osg::TextureCubeMap::Extensions* tcmExt = osg::TextureCubeMap::getExtensions(contextID,false);
110        if (tcmExt)
111        {
112            if (!tcmExt->isCubeMapSupported())
113            {
114                std::cout<<"Warning: texture_cube_map not supported by OpenGL drivers, unable to run application."<<std::endl;
115                return 1;
116            }
117        }
118    }
119
120    return viewer.run();
121}
Note: See TracBrowser for help on using the browser.