root/OpenSceneGraph/trunk/examples/osgtext/osgtext.cpp @ 1619

Revision 1619, 11.5 kB (checked in by robert, 11 years ago)

Added examples directory, a place for all new OSG demo code.

Added osgcameragroup, osgviewer, osgtext, osgconv to the new examples directory.

  • 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/MatrixTransform>
24#include <osgText/Text>
25
26
27
28///////////////////////////////////////////////////////////////////////////////
29// globals
30#define        TEXT_POLYGON    "Polygon Font - jygq"
31#define        TEXT_OUTLINE    "Outline Font - jygq"
32#define        TEXT_TEXTURE    "Texture Font - jygq"
33#define        TEXT_BITMAP        "Bitmap Font - jygq"
34#define        TEXT_PIXMAP        "Pixmap Font - jygq"
35
36#define        TEXT_COL_2D        osg::Vec4(.9,.9,.9,1)
37#define        TEXT_COL_3D        osg::Vec4(.99,.3,.2,1)
38
39
40std::string    ttfPath("fonts/times.ttf");
41std::string    ttfPath1("fonts/arial.ttf");
42
43int    gFontSize=18;
44int    gFontSize1=24;
45std::vector<osg::ref_ptr<osgText::Text > >    gTextList;
46osgText::Text::AlignmentType    gAlignment=osgText::Text::LEFT_BOTTOM;
47
48osg::Group* createText()
49{
50    osg::Group* rootNode = new osg::Group;
51
52    osgText::Text*    text;
53    osg::Geode*        geode;
54    osg::Material*    textMaterial;
55    osg::StateSet*  textState;
56    double            xOffset=150;
57    double            yOffset=gFontSize+10;
58
59    ///////////////////////////////////////////////////////////////////////////
60    // setup the texts
61
62    ///////////////////////////////////////////////////////////////////////////
63    // BitmapFont
64    osgText::BitmapFont*    bitmapFont= new  osgText::BitmapFont(ttfPath,
65                                                               gFontSize1);
66    text= new  osgText::Text(bitmapFont);
67    gTextList.push_back(text);
68    text->setText(std::string("2d ")+std::string(TEXT_BITMAP));
69    text->setPosition(osg::Vec3(xOffset,yOffset,0));
70    text->setDrawMode( osgText::Text::TEXT |
71                       osgText::Text::BOUNDINGBOX |
72                       osgText::Text::ALIGNMENT );
73    text->setAlignment(gAlignment);
74    geode = new osg::Geode();
75    geode->setName("BitmapFont");
76    geode->addDrawable( text );
77
78    textMaterial = new osg::Material();
79    textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE);
80    textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, TEXT_COL_2D);
81    textState = new osg::StateSet();
82    textState->setAttribute(textMaterial );
83    geode->setStateSet( textState );
84
85    rootNode->addChild(geode);
86
87    xOffset+=90;
88    yOffset+=120;
89
90    ///////////////////////////////////////////////////////////////////////////
91    // PixmapFont
92    osgText::PixmapFont*    pixmapFont= new  osgText::PixmapFont(ttfPath,
93                                                               gFontSize1);
94    text= new  osgText::Text(pixmapFont);
95    gTextList.push_back(text);
96    text->setText(std::string("2d ")+std::string(TEXT_PIXMAP));
97    text->setPosition(osg::Vec3(xOffset,yOffset,0));
98    text->setDrawMode( osgText::Text::TEXT |
99                       osgText::Text::BOUNDINGBOX |
100                       osgText::Text::ALIGNMENT );
101    text->setAlignment(gAlignment);
102    geode = new osg::Geode();
103    geode->setName("PixmapFont");
104    geode->addDrawable( text );
105
106    textMaterial = new osg::Material();
107    textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE);
108    textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK,TEXT_COL_2D);
109    // to get antiaA pixmapFonts we have to draw them with blending
110    osg::BlendFunc    *transp= new  osg::BlendFunc();
111    transp->setFunction(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
112
113
114
115
116    textState = new osg::StateSet();
117    textState->setAttribute(textMaterial );
118    textState->setAttribute(transp);
119    textState->setMode(GL_BLEND,osg::StateAttribute::ON);
120    textState->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
121   
122   
123    geode->setStateSet( textState );
124
125    rootNode->addChild(geode);
126
127    xOffset+=90;
128    yOffset+=120;
129
130    ///////////////////////////////////////////////////////////////////////////
131    // TextureFont
132    osgText::TextureFont*    textureFont= new  osgText::TextureFont(ttfPath1,
133                                                                 gFontSize1);
134    text= new  osgText::Text(textureFont);
135    gTextList.push_back(text);
136    text->setText(std::string("2d ")+std::string(TEXT_TEXTURE));
137    text->setPosition(osg::Vec3(xOffset,yOffset,0));
138    text->setDrawMode( osgText::Text::TEXT |
139                       osgText::Text::BOUNDINGBOX |
140                       osgText::Text::ALIGNMENT );
141    text->setAlignment(gAlignment);
142    geode = new osg::Geode();
143    geode->setName("TextureFont");
144    geode->addDrawable( text );
145
146    textMaterial = new osg::Material();
147    textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE);
148    textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, TEXT_COL_2D);
149    // to get antiaA pixmapFonts we have to draw them with blending
150    transp= new  osg::BlendFunc();
151    transp->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
152
153    textState = new osg::StateSet();
154    textState->setAttribute(textMaterial );
155    textState->setAttribute(transp);
156
157    textState->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::ON);
158    textState->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
159    geode->setStateSet( textState );
160
161    rootNode->addChild(geode);
162
163    xOffset+=90;
164    yOffset+=120;
165
166    ///////////////////////////////////////////////////////////////////////////
167    // PolygonFont
168    osgText::PolygonFont*    polygonFont= new  osgText::PolygonFont(ttfPath,
169                                                                 gFontSize1,
170                                                                 3);
171    text= new  osgText::Text(polygonFont);
172    gTextList.push_back(text);
173    text->setText(std::string("2d ")+std::string("TEXT_POLYGON"));
174    text->setPosition(osg::Vec3(xOffset,yOffset,0));
175    text->setDrawMode( osgText::Text::TEXT |
176                       osgText::Text::BOUNDINGBOX |
177                       osgText::Text::ALIGNMENT );
178    text->setAlignment(gAlignment);
179    geode = new osg::Geode();
180    geode->setName("PolygonFont");
181    geode->addDrawable( text );
182
183    textMaterial = new osg::Material();
184    textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE);
185    textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, TEXT_COL_2D);
186    textState = new osg::StateSet();
187    textState->setAttribute(textMaterial );
188    geode->setStateSet( textState );
189
190    rootNode->addChild(geode);
191
192    xOffset+=90;
193    yOffset+=120;
194
195    ///////////////////////////////////////////////////////////////////////////
196    // OutlineFont
197    osgText::OutlineFont*    outlineFont= new  osgText::OutlineFont(ttfPath,
198                                                                 gFontSize1,
199                                                                 3);
200
201    text= new  osgText::Text(outlineFont);
202    gTextList.push_back(text);
203    text->setText(std::string("2d ")+std::string(TEXT_OUTLINE));
204    text->setPosition(osg::Vec3(xOffset,yOffset,0));
205    text->setDrawMode( osgText::Text::TEXT |
206                       osgText::Text::BOUNDINGBOX |
207                       osgText::Text::ALIGNMENT );
208    text->setAlignment(gAlignment);
209    geode = new osg::Geode();
210    geode->setName("OutlineFont");
211    geode->addDrawable( text );
212
213    textMaterial = new osg::Material();
214    textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE);
215    textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, TEXT_COL_2D);
216    textState = new osg::StateSet();
217    textState->setAttribute(textMaterial );
218    geode->setStateSet( textState );
219
220    rootNode->addChild(geode);
221   
222   
223    // now add a depth attribute to the scene to force it to draw on top.
224    osg::Depth* depth = new osg::Depth;
225    depth->setRange(0.0,0.0);
226   
227    osg::StateSet* rootState = new osg::StateSet();
228    rootState->setAttribute(depth);
229    rootState->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
230   
231    rootNode->setStateSet(rootState);
232
233    return rootNode;   
234}
235
236
237int main( int argc, char **argv )
238{
239
240    // use an ArgumentParser object to manage the program arguments.
241    osg::ArgumentParser arguments(&argc,argv);
242   
243    // set up the usage document, in case we need to print out how to use this program.
244    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] [filename] ...");
245    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
246   
247
248    // construct the viewer.
249    osgProducer::Viewer viewer(arguments);
250
251    // set up the value with sensible default event handlers.
252    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
253
254    // get details on keyboard and mouse bindings used by the viewer.
255    viewer.getUsage(*arguments.getApplicationUsage());
256
257    // if user request help write it out to cout.
258    if (arguments.read("-h") || arguments.read("--help"))
259    {
260        arguments.getApplicationUsage()->write(cout);
261        return 1;
262    }
263
264    // any option left unread are converted into errors to write out later.
265    arguments.reportRemainingOptionsAsUnrecognized();
266
267    // report any errors if they have occured when parsing the program aguments.
268    if (arguments.errors())
269    {
270        arguments.writeErrorMessages(cout);
271        return 1;
272    }
273
274
275    // read the scene from the list of file specified commandline args.
276    osg::ref_ptr<osg::Node> rootNode = osgDB::readNodeFiles(arguments);
277
278    // prepare scene.
279    {
280
281        if (rootNode.valid())
282        {
283            // optimize the scene graph, remove rendundent nodes and state etc.
284            osgUtil::Optimizer optimizer;
285            optimizer.optimize(rootNode.get());
286        }
287
288        // make sure the root node is group so we can add extra nodes to it.
289        osg::Group* group = dynamic_cast<osg::Group*>(rootNode.get());
290        if (!group)
291        {
292            group = new osg::Group;
293           
294            if (rootNode.valid()) group->addChild(rootNode.get());
295           
296            rootNode = group;
297        }
298
299        // create the hud.
300        osg::Projection* projection = new osg::Projection;
301        projection->setMatrix(osg::Matrix::ortho2D(0,1024,0,768));
302
303        osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
304        modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE);
305        modelview_abs->setMatrix(osg::Matrix::identity());
306
307        modelview_abs->addChild(createText());
308
309        projection->addChild(modelview_abs);
310
311        group->addChild(projection);
312
313    }
314
315    // set the scene to render
316    viewer.setSceneData(rootNode.get());
317
318    // create the windows and run the threads.
319    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
320
321    while( !viewer.done() )
322    {
323        // wait for all cull and draw threads to complete.
324        viewer.sync();
325
326        // update the scene by traversing it with the the update visitor which will
327        // call all node update callbacks and animations.
328        viewer.update();
329         
330        // fire off the cull and draw traversals of the scene.
331        viewer.frame();
332       
333    }
334   
335    return 0;
336}
Note: See TracBrowser for help on using the browser.