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

Revision 3645, 6.1 kB (checked in by robert, 10 years ago)

Attempted fix for VS6.0 compile problems

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