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

Revision 5954, 4.0 kB (checked in by robert, 8 years ago)

Ported following examples to osgViewer:

osgparticleeffects
osgphotoalbum
osgpick
osgpoints
osgpointsprite
osgprecipitation
osgprerender
osgprerendercubemap
osgreflect
osgscalarbar
osgscribe
osgsequence
osgplanets

  • 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#include <osgGA/TrackballManipulator>
14#include <osgViewer/Viewer>
15#include <osgDB/ReadFile>
16
17#include <osgSim/ScalarsToColors>
18#include <osgSim/ColorRange>
19#include <osgSim/ScalarBar>
20
21#include <sstream>
22#include <iostream>
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 , char **)
117{
118    // construct the viewer.
119    osgViewer::Viewer viewer;
120
121    osg::Group* group = new osg::Group;
122    group->addChild(createScalarBar());
123    group->addChild(createScalarBar_HUD());
124
125    // add model to viewer.
126    viewer.setSceneData( group );
127
128    return viewer.run();
129}
Note: See TracBrowser for help on using the browser.