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

Revision 3023, 5.4 kB (checked in by robert, 11 years ago)

Added setPosition and setWidth methods into ScalarBar? and changed the
implementation to use these values to position and set the size of the
scalar bar. Also made the characterSize value a float rather than
the previous int.

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