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

Revision 3547, 5.5 kB (checked in by robert, 10 years ago)

Added setCullingActive false to the absolute transform.

  • 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 ScalarBar::ScalarPrinter::printScalar(scalar)+" Bottom";
51            else if(scalar==0.5f) return ScalarBar::ScalarPrinter::printScalar(scalar)+" Middle";
52            else if(scalar==1.0f) return ScalarBar::ScalarPrinter::printScalar(scalar)+" Top";
53            else return ScalarBar::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::ABSOLUTE_RF);
88    modelview->setCullingActive(false);
89    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
90    modelview->setMatrix(matrix);
91    modelview->addChild(geode);
92
93    osg::Projection * projection = new osg::Projection;
94    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); // or whatever the OSG window res is
95    projection->addChild(modelview);
96
97    return projection; //make sure you delete the return sb line
98}
99
100int main( int argc, char **argv )
101{
102    // use an ArgumentParser object to manage the program arguments.
103    osg::ArgumentParser arguments(&argc,argv);
104
105    // set up the usage document, in case we need to print out how to use this program.
106    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates both text, animation and billboard via custom transform to create the OpenSceneGraph logo..");
107    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
108    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
109    arguments.getApplicationUsage()->addCommandLineOption("ps","Render the Professional Services logo");
110
111    // construct the viewer.
112    osgProducer::Viewer viewer(arguments);
113
114    // set up the value with sensible default event handlers.
115    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
116
117    // get details on keyboard and mouse bindings used by the viewer.
118    viewer.getUsage(*arguments.getApplicationUsage());
119
120    // if user request help write it out to cout.
121    if (arguments.read("-h") || arguments.read("--help"))
122    {
123        arguments.getApplicationUsage()->write(std::cout);
124        return 1;
125    }
126
127    // any option left unread are converted into errors to write out later.
128    arguments.reportRemainingOptionsAsUnrecognized();
129
130    // report any errors if they have occured when parsing the program aguments.
131    if (arguments.errors())
132    {
133        arguments.writeErrorMessages(std::cout);
134        return 1;
135    }
136
137    osg::Group* group = new osg::Group;
138    group->addChild(createScalarBar());
139    group->addChild(createScalarBar_HUD());
140
141    // add model to viewer.
142    viewer.setSceneData( group );
143
144    // create the windows and run the threads.
145    viewer.realize();
146
147    while( !viewer.done() )
148    {
149       
150        // wait for all cull and draw threads to complete.
151        viewer.sync();
152
153        // update the scene by traversing it with the the update visitor which will
154        // call all node update callbacks and animations.
155        viewer.update();
156
157        // fire off the cull and draw traversals of the scene.
158        viewer.frame();
159    }
160
161    // wait for all cull and draw threads to complete before exit.
162    viewer.sync();
163
164    return 0;
165}
Note: See TracBrowser for help on using the browser.