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

Revision 1629, 7.4 kB (checked in by robert, 12 years ago)

First cut of new osgText implementation.

  • 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
25#include <osgText/Text>
26
27
28
29osg::Group* createHUDText()
30{
31
32    osg::Geode* geode  = new osg::Geode;
33
34    osgText::Text* text1 = new osgText::Text;
35    text1->setCharacterSize(12.0f);
36    text1->setText("20 GOTO 10");
37    geode->addDrawable(text1);
38
39    osgText::Text* text2 = new osgText::Text;
40    text2->setPosition(osg::Vec3(0.0f,50.0f,0.0f));
41    text2->setCharacterSize(12.0f);
42    text2->setText("10 PRINT \"Hello World\"");
43    geode->addDrawable(text2);
44
45    osgText::Text* text3 = new osgText::Text;
46    text3->setFont(osgText::readFontFile("fonts/arial.ttf"));
47    text3->setPosition(osg::Vec3(100.0f,100.0f,0.0f));
48    text3->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/arial.ttf");
49    geode->addDrawable(text3);
50
51    osgText::Text* text4 = new osgText::Text;
52    text4->setFont(osgText::readFontFile("fonts/times.ttf"));
53    text4->setFontSize(64,64);
54    text4->setPosition(osg::Vec3(200.0f,200.0f,0.0f));
55    text4->setLayout(osgText::Text::RIGHT_TO_LEFT);
56    text4->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX);
57    text4->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/times.ttf");
58    geode->addDrawable(text4);
59
60    osgText::Text* text5 = new osgText::Text;
61    text5->setFont(osgText::readFontFile("fonts/dirtydoz.ttf"));
62    text5->setPosition(osg::Vec3(300.0f,300.0f,0.0f));
63    text5->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
64    text5->setLayout(osgText::Text::VERTICAL);
65    text5->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX);
66    text5->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/dirtydoz.ttf");
67    geode->addDrawable(text5);
68
69
70//     osgText::Text::TextString string;
71//     for(int i=0;i<2048;++i)
72//         string.push_back(i);
73//
74//     osgText::Text* text6 = new osgText::Text;
75//     text6->setFont(osgText::readFontFile("/home/robert/Documents/GuopingSun/msmincho.ttc"));
76//     text6->setFontSize(64,64);
77//     text6->setText(string);
78//     text6->setPosition(osg::Vec3(00.0f,400.0f,0.0f));
79//     geode->addDrawable(text6);
80
81   
82
83
84    osg::Group* rootNode = new osg::Group;
85    rootNode->addChild(geode);
86
87    return rootNode;   
88}
89
90osg::Group* create3DText()
91{
92
93    osg::Geode* geode  = new osg::Geode;
94
95    osgText::Text* text1 = new osgText::Text;
96    text1->setFont("fonts/times.ttf");
97    text1->setAxisAlignment(osgText::Text::XY_PLANE);
98    text1->setText("XY_PLANE");
99    geode->addDrawable(text1);
100
101    osgText::Text* text2 = new osgText::Text;
102    text2->setFont("fonts/times.ttf");
103    text2->setPosition(osg::Vec3(0.0f,0.0f,0.0f));
104    text2->setAxisAlignment(osgText::Text::YZ_PLANE);
105    text2->setText("YZ_PLANE");
106    geode->addDrawable(text2);
107
108    osgText::Text* text3 = new osgText::Text;
109    text3->setFont("fonts/times.ttf");
110    text3->setPosition(osg::Vec3(00.0f,00.0f,00.0f));
111    text3->setAxisAlignment(osgText::Text::XZ_PLANE);
112    text3->setText("XZ_PLANE");
113    geode->addDrawable(text3);
114
115    osgText::Text* text4 = new osgText::Text;
116    text4->setFont("fonts/times.ttf");
117    text4->setAxisAlignment(osgText::Text::SCREEN);
118    text4->setPosition(osg::Vec3(00.0f,00.0f,00.0f));
119    text4->setText("SCREEN");
120    geode->addDrawable(text4);
121
122    osg::Group* rootNode = new osg::Group;
123    rootNode->addChild(geode);
124
125    return rootNode;   
126}
127
128int main( int argc, char **argv )
129{
130
131    // use an ArgumentParser object to manage the program arguments.
132    osg::ArgumentParser arguments(&argc,argv);
133   
134    // set up the usage document, in case we need to print out how to use this program.
135    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] [filename] ...");
136    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
137   
138
139    // construct the viewer.
140    osgProducer::Viewer viewer(arguments);
141
142    // set up the value with sensible default event handlers.
143    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
144
145    // get details on keyboard and mouse bindings used by the viewer.
146    viewer.getUsage(*arguments.getApplicationUsage());
147
148    // if user request help write it out to cout.
149    if (arguments.read("-h") || arguments.read("--help"))
150    {
151        arguments.getApplicationUsage()->write(cout);
152        return 1;
153    }
154
155    // any option left unread are converted into errors to write out later.
156    arguments.reportRemainingOptionsAsUnrecognized();
157
158    // report any errors if they have occured when parsing the program aguments.
159    if (arguments.errors())
160    {
161        arguments.writeErrorMessages(cout);
162        return 1;
163    }
164
165
166    // read the scene from the list of file specified commandline args.
167    osg::ref_ptr<osg::Node> rootNode = osgDB::readNodeFiles(arguments);
168
169    // prepare scene.
170    {
171
172        if (rootNode.valid())
173        {
174            // optimize the scene graph, remove rendundent nodes and state etc.
175            osgUtil::Optimizer optimizer;
176            optimizer.optimize(rootNode.get());
177        }
178
179        // make sure the root node is group so we can add extra nodes to it.
180        osg::Group* group = dynamic_cast<osg::Group*>(rootNode.get());
181        if (!group)
182        {
183            group = new osg::Group;
184           
185            if (rootNode.valid()) group->addChild(rootNode.get());
186           
187            rootNode = group;
188        }
189
190        {
191            // create the hud.
192            osg::Projection* projection = new osg::Projection;
193            projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
194
195            osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
196            modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE);
197            modelview_abs->setMatrix(osg::Matrix::identity());
198
199            modelview_abs->addChild(createHUDText());
200
201            projection->addChild(modelview_abs);
202
203            group->addChild(projection);
204        }
205
206        osg::MatrixTransform* scale = new osg::MatrixTransform;
207        scale->setMatrix(osg::Matrix::scale(1.0f,1.0f,1.0f));
208        scale->addChild(create3DText());
209        group->addChild(scale);
210
211    }
212
213    // set the scene to render
214    viewer.setSceneData(rootNode.get());
215
216    // create the windows and run the threads.
217    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
218
219    while( !viewer.done() )
220    {
221        // wait for all cull and draw threads to complete.
222        viewer.sync();
223
224        // update the scene by traversing it with the the update visitor which will
225        // call all node update callbacks and animations.
226        viewer.update();
227         
228        // fire off the cull and draw traversals of the scene.
229        viewer.frame();
230       
231    }
232   
233    return 0;
234}
Note: See TracBrowser for help on using the browser.