root/OpenSceneGraph/trunk/examples/osgautotransform/osgautotransform.cpp @ 1933

Revision 1933, 7.0 kB (checked in by robert, 12 years ago)

Addd support for maximum screen text size into osgText when auto scale to
screen is active.

Added osgautotransform demo.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osgUtil/Optimizer>
15#include <osgDB/ReadFile>
16#include <osgProducer/Viewer>
17
18#include <osg/Material>
19#include <osg/Geode>
20#include <osg/BlendFunc>
21#include <osg/Depth>
22#include <osg/Projection>
23#include <osg/AutoTransform>
24#include <osg/Geometry>
25
26#include <osgText/Text>
27
28osg::Node* createLabel(const osg::Vec3& pos, float size, const std::string& label)
29{
30    osg::Geode* geode = new osg::Geode();
31   
32    std::string timesFont("fonts/arial.ttf");
33
34    {
35        osgText::Text* text = new  osgText::Text;
36        geode->addDrawable( text );
37
38        text->setFont(timesFont);
39        text->setPosition(pos);
40        text->setCharacterSize(size);
41        text->setAlignment(osgText::Text::CENTER_CENTER);
42        text->setText(label);
43       
44    }
45   
46    return geode;   
47}
48
49osg::Node* createLabel2(const osg::Vec3& pos, float size, const std::string& label)
50{
51    osg::Geode* geode = new osg::Geode();
52   
53    std::string timesFont("fonts/arial.ttf");
54
55    {
56        osgText::Text* text = new  osgText::Text;
57        geode->addDrawable( text );
58
59        text->setFont(timesFont);
60        text->setPosition(pos);
61        text->setFontResolution(40,40);
62        text->setCharacterSize(size);
63        text->setAlignment(osgText::Text::CENTER_CENTER);
64        text->setAutoRotateToScreen(true);
65        text->setAutoScaleToScreen(true);
66        text->setDrawMode(osgText::Text::TEXT_PIXMAP);
67        text->setText(label);
68       
69    }
70   
71    return geode;   
72}
73
74osg::Node* createLabel3(const osg::Vec3& pos, float size, const std::string& label)
75{
76    osg::Geode* geode = new osg::Geode();
77   
78    std::string timesFont("fonts/arial.ttf");
79
80    {
81        osgText::Text* text = new  osgText::Text;
82        geode->addDrawable( text );
83
84        text->setFont(timesFont);
85        text->setPosition(pos);
86        text->setFontResolution(40,40);
87        text->setCharacterSize(size);
88        text->setAlignment(osgText::Text::CENTER_CENTER);
89        text->setAutoRotateToScreen(true);
90        text->setAutoScaleToScreen(true);
91        text->setText(label);
92       
93    }
94   
95    return geode;   
96}
97
98osg::Node* createAxis(const osg::Vec3& s, const osg::Vec3& e, int numReps)
99{
100    osg::Group* group = new osg::Group;
101
102    osg::Vec3 dv = e-s;
103    dv /= float(numReps-1);
104
105    osg::Vec3 pos = s;
106
107    bool useAuto = false;
108    if (useAuto)
109    {
110        osg::Vec3Array* vertices = new osg::Vec3Array;
111
112        for(int i=0;i<numReps;++i)
113        {
114            osg::AutoTransform* at = new osg::AutoTransform;
115            at->setPosition(pos);
116            at->setAutoRotateToScreen(true);
117            at->setAutoScaleToScreen(true);
118            at->addChild(createLabel(osg::Vec3(0.0f,0.0f,0.0f),40.0f,"Test 2"));
119            vertices->push_back(pos);
120            pos += dv;
121
122
123            group->addChild(at);
124        }
125
126        osg::Vec4Array* colors = new osg::Vec4Array;
127        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
128
129        osg::Geometry* geom = new osg::Geometry;
130        geom->setVertexArray(vertices);
131        geom->setColorArray(colors);
132        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
133
134        osg::Geode* geode = new osg::Geode;
135        geode->addDrawable(geom);
136
137        group->addChild(geode);
138    }
139    else
140    {
141        osg::Vec3Array* vertices = new osg::Vec3Array;
142
143        for(int i=0;i<numReps;++i)
144        {
145            group->addChild(createLabel3(osg::Vec3(pos),dv.length()*0.5f,"Test 2"));
146            vertices->push_back(pos);
147            pos += dv;
148
149
150        }
151
152        osg::Vec4Array* colors = new osg::Vec4Array;
153        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
154
155        osg::Geometry* geom = new osg::Geometry;
156        geom->setVertexArray(vertices);
157        geom->setColorArray(colors);
158        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
159
160        osg::Geode* geode = new osg::Geode;
161        geode->addDrawable(geom);
162
163        group->addChild(geode);
164    }
165   
166    return group;
167}
168
169osg::Node* createScene()
170{
171    osg::Group* root = new osg::Group;
172   
173    int numReps = 3333;
174//    int numReps = 10;
175    root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(1000.0,0.0,0.0),numReps));
176    root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,1000.0,0.0),numReps));
177    root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,0.0,1000.0),numReps));   
178   
179    return root;
180}
181
182int main( int argc, char **argv )
183{
184
185    // use an ArgumentParser object to manage the program arguments.
186    osg::ArgumentParser arguments(&argc,argv);
187   
188    // set up the usage document, in case we need to print out how to use this program.
189    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays.");
190    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename] ...");
191    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
192   
193
194    // construct the viewer.
195    osgProducer::Viewer viewer(arguments);
196
197    // set up the value with sensible default event handlers.
198    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
199
200    // get details on keyboard and mouse bindings used by the viewer.
201    viewer.getUsage(*arguments.getApplicationUsage());
202
203    // if user request help write it out to cout.
204    if (arguments.read("-h") || arguments.read("--help"))
205    {
206        arguments.getApplicationUsage()->write(std::cout);
207        return 1;
208    }
209
210    // any option left unread are converted into errors to write out later.
211    arguments.reportRemainingOptionsAsUnrecognized();
212
213    // report any errors if they have occured when parsing the program aguments.
214    if (arguments.errors())
215    {
216        arguments.writeErrorMessages(std::cout);
217        return 1;
218    }
219   
220
221    // set the scene to render
222    viewer.setSceneData(createScene());
223
224    // create the windows and run the threads.
225    viewer.realize();
226
227    while( !viewer.done() )
228    {
229        // wait for all cull and draw threads to complete.
230        viewer.sync();
231
232        // update the scene by traversing it with the the update visitor which will
233        // call all node update callbacks and animations.
234        viewer.update();
235         
236        // fire off the cull and draw traversals of the scene.
237        viewer.frame();
238       
239    }
240   
241    // wait for all cull and draw threads to complete before exit.
242    viewer.sync();
243   
244    return 0;
245}
Note: See TracBrowser for help on using the browser.