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

Revision 5928, 6.2 kB (checked in by robert, 7 years ago)

From Paul Martz and Robert Osfield, renamed include/osgUtil/Tesselator and associated classes/references to Tessellator etc.

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