root/OpenSceneGraph/trunk/examples/osgscalarbar/osgscalarbar.cpp @ 5381

Revision 5381, 6.2 kB (checked in by robert, 8 years ago)

Added viewer.cleanup_frame() to all examples.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Geode>
2#include <osg/ShapeDrawable>
3#include <osg/Material>
4#include <osg/Texture2D>
5#include <osg/MatrixTransform>
6#include <osg/PositionAttitudeTransform>
7#include <osg/BlendFunc>
8#include <osg/ClearNode>
9#include <osg/Projection>
10
11#include <osgUtil/Tesselator>
12#include <osgUtil/CullVisitor>
13
14
15#include <osgGA/TrackballManipulator>
16#include <osgProducer/Viewer>
17#include <osgDB/ReadFile>
18
19#include <osgSim/ScalarsToColors>
20#include <osgSim/ColorRange>
21#include <osgSim/ScalarBar>
22
23#include <sstream>
24#include <math.h>
25
26using namespace osgSim;
27using osgSim::ScalarBar;
28
29#if defined(_MSC_VER)
30// not have to have this pathway for just VS6.0 as its unable to handle the full
31// ScalarBar::ScalarPrinter::printScalar scoping.
32
33// Create a custom scalar printer
34struct MyScalarPrinter: public ScalarBar::ScalarPrinter
35{
36    std::string printScalar(float scalar)
37    {
38        std::cout<<"In MyScalarPrinter::printScalar"<<std::endl;
39        if(scalar==0.0f) return ScalarPrinter::printScalar(scalar)+" Bottom";
40        else if(scalar==0.5f) return ScalarPrinter::printScalar(scalar)+" Middle";
41        else if(scalar==1.0f) return ScalarPrinter::printScalar(scalar)+" Top";
42        else return ScalarPrinter::printScalar(scalar);
43    }
44};
45#else
46// Create a custom scalar printer
47struct MyScalarPrinter: public ScalarBar::ScalarPrinter
48{
49    std::string printScalar(float scalar)
50    {
51        std::cout<<"In MyScalarPrinter::printScalar"<<std::endl;
52        if(scalar==0.0f) return ScalarBar::ScalarPrinter::printScalar(scalar)+" Bottom";
53        else if(scalar==0.5f) return ScalarBar::ScalarPrinter::printScalar(scalar)+" Middle";
54        else if(scalar==1.0f) return ScalarBar::ScalarPrinter::printScalar(scalar)+" Top";
55        else return ScalarBar::ScalarPrinter::printScalar(scalar);
56    }
57};
58#endif
59
60osg::Node* createScalarBar()
61{
62#if 1
63    //ScalarsToColors* stc = new ScalarsToColors(0.0f,1.0f);
64    //ScalarBar* sb = new ScalarBar(2,3,stc,"STC_ScalarBar");
65
66    // Create a custom color set
67    std::vector<osg::Vec4> cs;
68    cs.push_back(osg::Vec4(1.0f,0.0f,0.0f,1.0f));   // R
69    cs.push_back(osg::Vec4(0.0f,1.0f,0.0f,1.0f));   // G
70    cs.push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));   // G
71    cs.push_back(osg::Vec4(0.0f,0.0f,1.0f,1.0f));   // B
72    cs.push_back(osg::Vec4(0.0f,1.0f,1.0f,1.0f));   // R
73
74
75    ColorRange* cr = new ColorRange(0.0f,1.0f,cs);
76    ScalarBar* sb = new ScalarBar(20, 11, cr, "ScalarBar", ScalarBar::VERTICAL, 0.1f, new MyScalarPrinter);
77    sb->setScalarPrinter(new MyScalarPrinter);
78
79    return sb;
80#else
81    ScalarBar *sb = new ScalarBar;
82    ScalarBar::TextProperties tp;
83    tp._fontFile = "fonts/times.ttf";
84
85    sb->setTextProperties(tp);
86
87    return sb;
88#endif
89
90}
91
92osg::Node * createScalarBar_HUD()
93{
94    osgSim::ScalarBar * geode = new osgSim::ScalarBar;
95    osgSim::ScalarBar::TextProperties tp;
96    tp._fontFile = "fonts/times.ttf";
97    geode->setTextProperties(tp);
98    osg::StateSet * stateset = geode->getOrCreateStateSet();
99    stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
100
101    stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
102    stateset->setRenderBinDetails(11, "RenderBin");
103
104    osg::MatrixTransform * modelview = new osg::MatrixTransform;
105    modelview->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
106    osg::Matrixd matrix(osg::Matrixd::scale(1000,1000,1000) * osg::Matrixd::translate(120,10,0)); // I've played with these values a lot and it seems to work, but I have no idea why
107    modelview->setMatrix(matrix);
108    modelview->addChild(geode);
109
110    osg::Projection * projection = new osg::Projection;
111    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); // or whatever the OSG window res is
112    projection->addChild(modelview);
113
114    return projection; //make sure you delete the return sb line
115}
116
117int main( int argc, char **argv )
118{
119    // use an ArgumentParser object to manage the program arguments.
120    osg::ArgumentParser arguments(&argc,argv);
121
122    // set up the usage document, in case we need to print out how to use this program.
123    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates both text, animation and billboard via custom transform to create the OpenSceneGraph logo..");
124    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
125    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
126    arguments.getApplicationUsage()->addCommandLineOption("ps","Render the Professional Services logo");
127
128    // construct the viewer.
129    osgProducer::Viewer viewer(arguments);
130
131    // set up the value with sensible default event handlers.
132    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
133
134    // get details on keyboard and mouse bindings used by the viewer.
135    viewer.getUsage(*arguments.getApplicationUsage());
136
137    // if user request help write it out to cout.
138    if (arguments.read("-h") || arguments.read("--help"))
139    {
140        arguments.getApplicationUsage()->write(std::cout);
141        return 1;
142    }
143
144    // any option left unread are converted into errors to write out later.
145    arguments.reportRemainingOptionsAsUnrecognized();
146
147    // report any errors if they have occured when parsing the program aguments.
148    if (arguments.errors())
149    {
150        arguments.writeErrorMessages(std::cout);
151        return 1;
152    }
153
154    osg::Group* group = new osg::Group;
155    group->addChild(createScalarBar());
156    group->addChild(createScalarBar_HUD());
157
158    // add model to viewer.
159    viewer.setSceneData( group );
160
161    // create the windows and run the threads.
162    viewer.realize();
163
164    while( !viewer.done() )
165    {
166       
167        // wait for all cull and draw threads to complete.
168        viewer.sync();
169
170        // update the scene by traversing it with the the update visitor which will
171        // call all node update callbacks and animations.
172        viewer.update();
173
174        // fire off the cull and draw traversals of the scene.
175        viewer.frame();
176    }
177
178    // wait for all cull and draw threads to complete.
179    viewer.sync();
180
181    // run a clean up frame to delete all OpenGL objects.
182    viewer.cleanup_frame();
183
184    // wait for all the clean up frame to complete.
185    viewer.sync();
186
187    return 0;
188}
Note: See TracBrowser for help on using the browser.