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

Revision 5923, 4.4 kB (checked in by robert, 8 years ago)

Ported osgcopy, osgcubemap, osgdelaunay, osgdepthpartion, osgdistortion, osgfadetext, osgforest and osgfxbrowser to osgViewer

  • 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    if (!rootnode) {
82        osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
83        return 1;
84    }
85
86    // create specular highlights
87    create_specular_highlights(rootnode);
88   
89    // run optimization over the scene graph
90    osgUtil::Optimizer optimzer;
91    optimzer.optimize(rootnode);
92     
93    // add a viewport to the viewer and attach the scene graph.
94    viewer.setSceneData(rootnode);
95   
96    // create the windows and run the threads.
97    viewer.realize();
98
99    // now check to see if texture cube map is supported.
100    for(unsigned int contextID = 0;
101        contextID<osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts();
102        ++contextID)
103    {
104        osg::TextureCubeMap::Extensions* tcmExt = osg::TextureCubeMap::getExtensions(contextID,false);
105        if (tcmExt)
106        {
107            if (!tcmExt->isCubeMapSupported())
108            {
109                std::cout<<"Warning: texture_cube_map not supported by OpenGL drivers, unable to run application."<<std::endl;
110                return 1;
111            }
112        }
113    }
114
115    return viewer.run();
116}
Note: See TracBrowser for help on using the browser.