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

Revision 12529, 5.5 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgcubemap.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Group>
20#include <osg/StateSet>
21#include <osg/TextureCubeMap>
22#include <osg/TexGen>
23#include <osg/TexEnvCombine>
24
25#include <osgUtil/ReflectionMapGenerator>
26#include <osgUtil/HighlightMapGenerator>
27#include <osgUtil/HalfWayMapGenerator>
28#include <osgUtil/Optimizer>
29
30#include <osgDB/ReadFile>
31#include <osgDB/Registry>
32
33#include <osgGA/TrackballManipulator>
34#include <osgGA/FlightManipulator>
35#include <osgGA/DriveManipulator>
36
37#include <osgViewer/Viewer>
38
39#include <iostream>
40#include <string>
41#include <vector>
42
43void create_specular_highlights(osg::Node *node)
44{
45    osg::StateSet *ss = node->getOrCreateStateSet();
46   
47    // create and setup the texture object
48    osg::TextureCubeMap *tcm = new osg::TextureCubeMap;
49    tcm->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
50    tcm->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
51    tcm->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP);
52    tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
53    tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);   
54
55    // generate the six highlight map images (light direction = [1, 1, -1])
56    osgUtil::HighlightMapGenerator *mapgen = new osgUtil::HighlightMapGenerator(
57        osg::Vec3(1, 1, -1),            // light direction
58        osg::Vec4(1, 0.9f, 0.8f, 1),    // light color
59        8);                             // specular exponent
60   
61    mapgen->generateMap();
62
63    // assign the six images to the texture object
64    tcm->setImage(osg::TextureCubeMap::POSITIVE_X, mapgen->getImage(osg::TextureCubeMap::POSITIVE_X));
65    tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_X));
66    tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Y));
67    tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Y));
68    tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Z));
69    tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Z));
70
71    // enable texturing, replacing any textures in the subgraphs
72    ss->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
73
74    // texture coordinate generation
75    osg::TexGen *tg = new osg::TexGen;
76    tg->setMode(osg::TexGen::REFLECTION_MAP);
77    ss->setTextureAttributeAndModes(0, tg, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
78
79    // use TexEnvCombine to add the highlights to the original lighting
80    osg::TexEnvCombine *te = new osg::TexEnvCombine;   
81    te->setCombine_RGB(osg::TexEnvCombine::ADD);
82    te->setSource0_RGB(osg::TexEnvCombine::TEXTURE);
83    te->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
84    te->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
85    te->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
86    ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
87}
88
89int main(int argc, char *argv[])
90{
91    // use an ArgumentParser object to manage the program arguments.
92    osg::ArgumentParser arguments(&argc,argv);
93
94    // construct the viewer.
95    osgViewer::Viewer viewer;
96   
97    // load the nodes from the commandline arguments.
98    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
99
100    // if not loaded assume no arguments passed in, try use default mode instead.
101    if (!rootnode) rootnode = osgDB::readNodeFile("cessna.osgt");
102
103    if (!rootnode)
104    {
105        osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
106        return 1;
107    }
108
109    // create specular highlights
110    create_specular_highlights(rootnode);
111   
112    // run optimization over the scene graph
113    osgUtil::Optimizer optimzer;
114    optimzer.optimize(rootnode);
115     
116    // add a viewport to the viewer and attach the scene graph.
117    viewer.setSceneData(rootnode);
118   
119    // create the windows and run the threads.
120    viewer.realize();
121
122    // now check to see if texture cube map is supported.
123    for(unsigned int contextID = 0;
124        contextID<osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts();
125        ++contextID)
126    {
127        osg::TextureCubeMap::Extensions* tcmExt = osg::TextureCubeMap::getExtensions(contextID,false);
128        if (tcmExt)
129        {
130            if (!tcmExt->isCubeMapSupported())
131            {
132                std::cout<<"Warning: texture_cube_map not supported by OpenGL drivers, unable to run application."<<std::endl;
133                return 1;
134            }
135        }
136    }
137
138    return viewer.run();
139}
Note: See TracBrowser for help on using the browser.