root/OpenSceneGraph/trunk/examples/osgscribe/osgscribe.cpp @ 13288

Revision 12529, 4.7 kB (checked in by robert, 4 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, osgscribe.
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/Geode>
20#include <osg/Group>
21#include <osg/Notify>
22#include <osg/Material>
23#include <osg/PolygonOffset>
24#include <osg/PolygonMode>
25#include <osg/LineStipple>
26
27#include <osgDB/Registry>
28#include <osgDB/ReadFile>
29
30#include <osgViewer/Viewer>
31
32#include <osgUtil/Optimizer>
33
34int main( int argc, char **argv )
35{
36    // use an ArgumentParser object to manage the program arguments.
37    osg::ArgumentParser arguments(&argc,argv);
38
39    // construct the viewer.
40    osgViewer::Viewer viewer;
41
42    // load the nodes from the commandline arguments.
43    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
44
45    // if not loaded assume no arguments passed in, try use default mode instead.
46    if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osgt");
47   
48    if (!loadedModel)
49    {
50        osg::notify(osg::NOTICE)<<"Please specifiy a model filename on the command line."<<std::endl;
51        return 1;
52    }
53 
54    // to do scribe mode we create a top most group to contain the
55    // original model, and then a second group contains the same model
56    // but overrides various state attributes, so that the second instance
57    // is rendered as wireframe.
58   
59    osg::Group* rootnode = new osg::Group;
60
61    osg::Group* decorator = new osg::Group;
62   
63    rootnode->addChild(loadedModel);
64   
65   
66    rootnode->addChild(decorator);
67   
68    decorator->addChild(loadedModel); 
69
70    // set up the state so that the underlying color is not seen through
71    // and that the drawing mode is changed to wireframe, and a polygon offset
72    // is added to ensure that we see the wireframe itself, and turn off
73    // so texturing too.
74    osg::StateSet* stateset = new osg::StateSet;
75    osg::PolygonOffset* polyoffset = new osg::PolygonOffset;
76    polyoffset->setFactor(-1.0f);
77    polyoffset->setUnits(-1.0f);
78    osg::PolygonMode* polymode = new osg::PolygonMode;
79    polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
80    stateset->setAttributeAndModes(polyoffset,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
81    stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
82
83#if 1
84    osg::Material* material = new osg::Material;
85    stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
86    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
87#else
88    // version which sets the color of the wireframe.
89    osg::Material* material = new osg::Material;
90    material->setColorMode(osg::Material::OFF); // switch glColor usage off
91    // turn all lighting off
92    material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f));
93    material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f));
94    material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f));
95    // except emission... in which we set the color we desire
96    material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,1.0f,0.0f,1.0f));
97    stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
98    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
99#endif
100
101    stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
102   
103//     osg::LineStipple* linestipple = new osg::LineStipple;
104//     linestipple->setFactor(1);
105//     linestipple->setPattern(0xf0f0);
106//     stateset->setAttributeAndModes(linestipple,osg::StateAttribute::OVERRIDE_ON);
107   
108    decorator->setStateSet(stateset);
109 
110   
111    // run optimization over the scene graph
112    osgUtil::Optimizer optimzer;
113    optimzer.optimize(rootnode);
114     
115    // add a viewport to the viewer and attach the scene graph.
116    viewer.setSceneData( rootnode );
117   
118    return viewer.run();
119}
Note: See TracBrowser for help on using the browser.