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

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

Updates osghud demo.

Compile fixes for VisualStudio?6.0 because its a hideous pile of junk and can't
compile its way out of a paper bag.

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