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

Revision 6916, 3.8 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, added fallbacks for when now command line args are provided.

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