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

Revision 14059, 5.6 kB (checked in by robert, 4 hours ago)

From Mattias Helsing, "Seems I was only half right given what you asked for. CMP0017 only
says that modules that are found and ran from cmake modules dir should
prefer cmake-provided modules. find_package() and include() still look
in CMAKE_MODULE_PATH first.

After some investigating I've come up with a proposal examplified in
the attached FindGDAL.cmake script. It simply calls the cmake provided
FindGDAL.cmake if it exists and returns if it succeeds in finding GDAL
using that, otherwise continue with our local cmake code.
Pro: Wont clutter our root CMakeLists.txt
Con: If we begin to write more advanced Findxxx modules (using
COMPONENTS, REQUIRED etc.) we may have to revise this scheme.
"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[6941]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
[2213]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>
[3012]27#include <osg/Projection>
[2213]28
29#include <osgUtil/CullVisitor>
30
31#include <osgGA/TrackballManipulator>
[5954]32#include <osgViewer/Viewer>
[2213]33#include <osgDB/ReadFile>
34
35#include <osgSim/ScalarsToColors>
36#include <osgSim/ColorRange>
37#include <osgSim/ScalarBar>
38
39#include <sstream>
[5954]40#include <iostream>
[2213]41#include <math.h>
42
43using namespace osgSim;
[2688]44using osgSim::ScalarBar;
[2213]45
[3645]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
[14047]77osg::Node* createScalarBar(bool vertical)
[2213]78{
[2390]79#if 1
80    //ScalarsToColors* stc = new ScalarsToColors(0.0f,1.0f);
81    //ScalarBar* sb = new ScalarBar(2,3,stc,"STC_ScalarBar");
[2213]82
[2390]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);
[14047]93    ScalarBar* sb = new ScalarBar(20, 11, cr,
94                      vertical ? "Vertical" : "Horizontal",
95                      vertical ? ScalarBar::VERTICAL : ScalarBar::HORIZONTAL,
96                      0.1f, new MyScalarPrinter);
[2390]97    sb->setScalarPrinter(new MyScalarPrinter);
98
[14047]99    if ( !vertical )
100    {
[14059]101        sb->setPosition( osg::Vec3(0.5f,0.5f,0));
[14047]102    }
103
[2390]104    return sb;
105#else
[2213]106    ScalarBar *sb = new ScalarBar;
107    ScalarBar::TextProperties tp;
[2390]108    tp._fontFile = "fonts/times.ttf";
[2213]109
[2390]110    sb->setTextProperties(tp);
[2213]111
112    return sb;
[2390]113#endif
[2213]114
115}
116
[3012]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;
[3528]130    modelview->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
[3012]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
[5954]142int main(int , char **)
[2213]143{
144    // construct the viewer.
[5954]145    osgViewer::Viewer viewer;
[2213]146
[3012]147    osg::Group* group = new osg::Group;
[14059]148
[3012]149    group->addChild(createScalarBar_HUD());
[2213]150
[14059]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
[2213]158    // add model to viewer.
[3012]159    viewer.setSceneData( group );
[2213]160
[5954]161    return viewer.run();
[2213]162}
Note: See TracBrowser for help on using the browser.