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

Revision 14059, 5.6 kB (checked in by robert, 2 days ago)

Release OpenSceneGraph-3.3.3

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgscalarbar.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Geode>
20#include <osg/ShapeDrawable>
21#include <osg/Material>
22#include <osg/Texture2D>
23#include <osg/MatrixTransform>
24#include <osg/PositionAttitudeTransform>
25#include <osg/BlendFunc>
26#include <osg/ClearNode>
27#include <osg/Projection>
28
29#include <osgUtil/CullVisitor>
30
31#include <osgGA/TrackballManipulator>
32#include <osgViewer/Viewer>
33#include <osgDB/ReadFile>
34
35#include <osgSim/ScalarsToColors>
36#include <osgSim/ColorRange>
37#include <osgSim/ScalarBar>
38
39#include <sstream>
40#include <iostream>
41#include <math.h>
42
43using namespace osgSim;
44using osgSim::ScalarBar;
45
46#if defined(_MSC_VER)
47// not have to have this pathway for just VS6.0 as its unable to handle the full
48// ScalarBar::ScalarPrinter::printScalar scoping.
49
50// Create a custom scalar printer
51struct MyScalarPrinter: public ScalarBar::ScalarPrinter
52{
53    std::string printScalar(float scalar)
54    {
55        std::cout<<"In MyScalarPrinter::printScalar"<<std::endl;
56        if(scalar==0.0f) return ScalarPrinter::printScalar(scalar)+" Bottom";
57        else if(scalar==0.5f) return ScalarPrinter::printScalar(scalar)+" Middle";
58        else if(scalar==1.0f) return ScalarPrinter::printScalar(scalar)+" Top";
59        else return ScalarPrinter::printScalar(scalar);
60    }
61};
62#else
63// Create a custom scalar printer
64struct MyScalarPrinter: public ScalarBar::ScalarPrinter
65{
66    std::string printScalar(float scalar)
67    {
68        std::cout<<"In MyScalarPrinter::printScalar"<<std::endl;
69        if(scalar==0.0f) return ScalarBar::ScalarPrinter::printScalar(scalar)+" Bottom";
70        else if(scalar==0.5f) return ScalarBar::ScalarPrinter::printScalar(scalar)+" Middle";
71        else if(scalar==1.0f) return ScalarBar::ScalarPrinter::printScalar(scalar)+" Top";
72        else return ScalarBar::ScalarPrinter::printScalar(scalar);
73    }
74};
75#endif
76
77osg::Node* createScalarBar(bool vertical)
78{
79#if 1
80    //ScalarsToColors* stc = new ScalarsToColors(0.0f,1.0f);
81    //ScalarBar* sb = new ScalarBar(2,3,stc,"STC_ScalarBar");
82
83    // Create a custom color set
84    std::vector<osg::Vec4> cs;
85    cs.push_back(osg::Vec4(1.0f,0.0f,0.0f,1.0f));   // R
86    cs.push_back(osg::Vec4(0.0f,1.0f,0.0f,1.0f));   // G
87    cs.push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));   // G
88    cs.push_back(osg::Vec4(0.0f,0.0f,1.0f,1.0f));   // B
89    cs.push_back(osg::Vec4(0.0f,1.0f,1.0f,1.0f));   // R
90
91
92    ColorRange* cr = new ColorRange(0.0f,1.0f,cs);
93    ScalarBar* sb = new ScalarBar(20, 11, cr,
94                      vertical ? "Vertical" : "Horizontal",
95                      vertical ? ScalarBar::VERTICAL : ScalarBar::HORIZONTAL,
96                      0.1f, new MyScalarPrinter);
97    sb->setScalarPrinter(new MyScalarPrinter);
98
99    if ( !vertical )
100    {
101        sb->setPosition( osg::Vec3(0.5f,0.5f,0));
102    }
103
104    return sb;
105#else
106    ScalarBar *sb = new ScalarBar;
107    ScalarBar::TextProperties tp;
108    tp._fontFile = "fonts/times.ttf";
109
110    sb->setTextProperties(tp);
111
112    return sb;
113#endif
114
115}
116
117osg::Node * createScalarBar_HUD()
118{
119    osgSim::ScalarBar * geode = new osgSim::ScalarBar;
120    osgSim::ScalarBar::TextProperties tp;
121    tp._fontFile = "fonts/times.ttf";
122    geode->setTextProperties(tp);
123    osg::StateSet * stateset = geode->getOrCreateStateSet();
124    stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
125
126    stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
127    stateset->setRenderBinDetails(11, "RenderBin");
128
129    osg::MatrixTransform * modelview = new osg::MatrixTransform;
130    modelview->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
131    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
132    modelview->setMatrix(matrix);
133    modelview->addChild(geode);
134
135    osg::Projection * projection = new osg::Projection;
136    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); // or whatever the OSG window res is
137    projection->addChild(modelview);
138
139    return projection; //make sure you delete the return sb line
140}
141
142int main(int , char **)
143{
144    // construct the viewer.
145    osgViewer::Viewer viewer;
146
147    osg::Group* group = new osg::Group;
148
149    group->addChild(createScalarBar_HUD());
150
151    // rotate the scalar from XY plane to XZ so we see them viewing it with the default camera manipulators that look along the Y axis, with Z up.
152    osg::MatrixTransform* transform = new osg::MatrixTransform;
153    group->addChild(transform);
154    transform->setMatrix(osg::Matrix::rotate(osg::inDegrees(90.0),1.0,0.0,0.0));
155    transform->addChild(createScalarBar(true));
156    transform->addChild(createScalarBar(false));
157
158    // add model to viewer.
159    viewer.setSceneData( group );
160
161    return viewer.run();
162}
Note: See TracBrowser for help on using the browser.