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

Revision 2907, 5.5 kB (checked in by robert, 10 years ago)

Added pathway in code for override the colour of the wireframe scribe

  • 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 <osgProducer/Viewer>
13
14#include <osgUtil/Optimizer>
15
16int main( int argc, char **argv )
17{
18
19    // use an ArgumentParser object to manage the program arguments.
20    osg::ArgumentParser arguments(&argc,argv);
21
22    // set up the usage document, in case we need to print out how to use this program.
23    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to use multi-pass and state inheritance to create a scribed rendered effect.");
24    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
25    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
26   
27    // construct the viewer.
28    osgProducer::Viewer viewer(arguments);
29
30    // set up the value with sensible default event handlers.
31    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
32
33    // get details on keyboard and mouse bindings used by the viewer.
34    viewer.getUsage(*arguments.getApplicationUsage());
35
36    // if user request help write it out to cout.
37    if (arguments.read("-h") || arguments.read("--help"))
38    {
39        arguments.getApplicationUsage()->write(std::cout);
40        return 1;
41    }
42
43    // any option left unread are converted into errors to write out later.
44    arguments.reportRemainingOptionsAsUnrecognized();
45
46    // report any errors if they have occured when parsing the program aguments.
47    if (arguments.errors())
48    {
49        arguments.writeErrorMessages(std::cout);
50        return 1;
51    }
52   
53    if (arguments.argc()<=1)
54    {
55        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
56        return 1;
57    }
58
59    // load the nodes from the commandline arguments.
60    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
61    if (!loadedModel)
62    {
63        return 1;
64    }
65 
66    // to do scribe mode we create a top most group to contain the
67    // original model, and then a second group contains the same model
68    // but overrides various state attributes, so that the second instance
69    // is rendered as wireframe.
70   
71    osg::Group* rootnode = new osg::Group;
72
73    osg::Group* decorator = new osg::Group;
74   
75    rootnode->addChild(loadedModel);
76   
77   
78    rootnode->addChild(decorator);
79   
80    decorator->addChild(loadedModel); 
81
82    // set up the state so that the underlying color is not seen through
83    // and that the drawing mode is changed to wireframe, and a polygon offset
84    // is added to ensure that we see the wireframe itself, and turn off
85    // so texturing too.
86    osg::StateSet* stateset = new osg::StateSet;
87    osg::PolygonOffset* polyoffset = new osg::PolygonOffset;
88    polyoffset->setFactor(-1.0f);
89    polyoffset->setUnits(-1.0f);
90    osg::PolygonMode* polymode = new osg::PolygonMode;
91    polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
92    stateset->setAttributeAndModes(polyoffset,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
93    stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
94
95#if 1
96    osg::Material* material = new osg::Material;
97    stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
98    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
99#else
100    // version which sets the color of the wireframe.
101    osg::Material* material = new osg::Material;
102    material->setColorMode(osg::Material::OFF); // switch glColor usage off
103    // turn all lighting off
104    material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f));
105    material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f));
106    material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,0.0f,0.0f,1.0f));
107    // except emission... in which we set the color we desire
108    material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0,1.0f,0.0f,1.0f));
109    stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
110    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
111#endif
112
113    stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
114   
115//     osg::LineStipple* linestipple = new osg::LineStipple;
116//     linestipple->setFactor(1);
117//     linestipple->setPattern(0xf0f0);
118//     stateset->setAttributeAndModes(linestipple,osg::StateAttribute::OVERRIDE_ON);
119   
120    decorator->setStateSet(stateset);
121 
122   
123    // run optimization over the scene graph
124    osgUtil::Optimizer optimzer;
125    optimzer.optimize(rootnode);
126     
127    // add a viewport to the viewer and attach the scene graph.
128    viewer.setSceneData( rootnode );
129   
130    // create the windows and run the threads.
131    viewer.realize();
132
133    while( !viewer.done() )
134    {
135        // wait for all cull and draw threads to complete.
136        viewer.sync();
137
138        // update the scene by traversing it with the the update visitor which will
139        // call all node update callbacks and animations.
140        viewer.update();
141         
142        // fire off the cull and draw traversals of the scene.
143        viewer.frame();
144       
145    }
146   
147    // wait for all cull and draw threads to complete before exit.
148    viewer.sync();
149
150    return 0;
151}
Note: See TracBrowser for help on using the browser.