/usr/share/openscenegraph/examples/osgqfont/osgqfont.cpp is in openscenegraph-examples 3.2.0~rc1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | /* OpenSceneGraph example, osgtext.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <QApplication>
#include <QGridLayout>
#include <QWidget>
#include <osgQt/GraphicsWindowQt>
#include <osgQt/QFontImplementation>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgDB/Registry>
#include <osgGA/StateSetManipulator>
#include <osgGA/TrackballManipulator>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osg/Geode>
#include <osg/Camera>
#include <osg/ShapeDrawable>
#include <osg/Sequence>
#include <osg/PolygonMode>
#include <osgText/Font>
#include <osgText/Text>
osg::Group* createHUDText()
{
osg::Group* rootNode = new osg::Group;
osgText::Font* font = new osgText::Font(new osgQt::QFontImplementation(QFont("Arial")));
osg::Geode* geode = new osg::Geode;
rootNode->addChild(geode);
float windowHeight = 1024.0f;
float windowWidth = 1280.0f;
float margin = 50.0f;
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Examples of how to set up different text layout
//
osg::Vec4 layoutColor(1.0f,1.0f,0.0f,1.0f);
float layoutCharacterSize = 20.0f;
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(layoutColor);
text->setCharacterSize(layoutCharacterSize);
text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f));
// the default layout is left to right, typically used in languages
// originating from europe such as English, French, German, Spanish etc..
text->setLayout(osgText::Text::LEFT_TO_RIGHT);
text->setText("text->setLayout(osgText::Text::LEFT_TO_RIGHT);");
geode->addDrawable(text);
}
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(layoutColor);
text->setCharacterSize(layoutCharacterSize);
text->setPosition(osg::Vec3(windowWidth-margin,windowHeight-margin,0.0f));
// right to left layouts would be used for hebrew or arabic fonts.
text->setLayout(osgText::Text::RIGHT_TO_LEFT);
text->setAlignment(osgText::Text::RIGHT_BASE_LINE);
text->setText("text->setLayout(osgText::Text::RIGHT_TO_LEFT);");
geode->addDrawable(text);
}
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(layoutColor);
text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f));
text->setCharacterSize(layoutCharacterSize);
// vertical font layout would be used for asian fonts.
text->setLayout(osgText::Text::VERTICAL);
text->setText("text->setLayout(osgText::Text::VERTICAL);");
geode->addDrawable(text);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Examples of how to set up different font resolution
//
osg::Vec4 fontSizeColor(0.0f,1.0f,1.0f,1.0f);
float fontSizeCharacterSize = 30;
osg::Vec3 cursor = osg::Vec3(margin*2,windowHeight-margin*2,0.0f);
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(fontSizeColor);
text->setCharacterSize(fontSizeCharacterSize);
text->setPosition(cursor);
// use text that uses 10 by 10 texels as a target resolution for fonts.
text->setFontResolution(10,10); // blocky but small texture memory usage
text->setText("text->setFontResolution(10,10); // blocky but small texture memory usage");
geode->addDrawable(text);
}
cursor.y() -= fontSizeCharacterSize;
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(fontSizeColor);
text->setCharacterSize(fontSizeCharacterSize);
text->setPosition(cursor);
// use text that uses 20 by 20 texels as a target resolution for fonts.
text->setFontResolution(20,20); // smoother but higher texture memory usage (but still quite low).
text->setText("text->setFontResolution(20,20); // smoother but higher texture memory usage (but still quite low).");
geode->addDrawable(text);
}
cursor.y() -= fontSizeCharacterSize;
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(fontSizeColor);
text->setCharacterSize(fontSizeCharacterSize);
text->setPosition(cursor);
// use text that uses 40 by 40 texels as a target resolution for fonts.
text->setFontResolution(40,40); // even smoother but again higher texture memory usage.
text->setText("text->setFontResolution(40,40); // even smoother but again higher texture memory usage.");
geode->addDrawable(text);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Examples of how to set up different sized text
//
osg::Vec4 characterSizeColor(1.0f,0.0f,1.0f,1.0f);
cursor.y() -= fontSizeCharacterSize*2.0f;
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(characterSizeColor);
text->setFontResolution(20,20);
text->setPosition(cursor);
// use text that is 20 units high.
text->setCharacterSize(20); // small
text->setText("text->setCharacterSize(20.0f); // small");
geode->addDrawable(text);
}
cursor.y() -= 30.0f;
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(characterSizeColor);
text->setFontResolution(30,30);
text->setPosition(cursor);
// use text that is 30 units high.
text->setCharacterSize(30.0f); // medium
text->setText("text->setCharacterSize(30.0f); // medium");
geode->addDrawable(text);
}
cursor.y() -= 50.0f;
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(characterSizeColor);
text->setFontResolution(40,40);
text->setPosition(cursor);
// use text that is 60 units high.
text->setCharacterSize(60.0f); // large
text->setText("text->setCharacterSize(60.0f); // large");
geode->addDrawable(text);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Examples of how to set up different alignments
//
osg::Vec4 alignmentSizeColor(0.0f,1.0f,0.0f,1.0f);
float alignmentCharacterSize = 25.0f;
cursor.x() = 640;
cursor.y() = margin*4.0f;
typedef std::pair<osgText::Text::AlignmentType,std::string> AlignmentPair;
typedef std::vector<AlignmentPair> AlignmentList;
AlignmentList alignmentList;
alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_TOP,"text->setAlignment(\nosgText::Text::LEFT_TOP);"));
alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_CENTER,"text->setAlignment(\nosgText::Text::LEFT_CENTER);"));
alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_BOTTOM,"text->setAlignment(\nosgText::Text::LEFT_BOTTOM);"));
alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_TOP,"text->setAlignment(\nosgText::Text::CENTER_TOP);"));
alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_CENTER,"text->setAlignment(\nosgText::Text::CENTER_CENTER);"));
alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_BOTTOM,"text->setAlignment(\nosgText::Text::CENTER_BOTTOM);"));
alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_TOP,"text->setAlignment(\nosgText::Text::RIGHT_TOP);"));
alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_CENTER,"text->setAlignment(\nosgText::Text::RIGHT_CENTER);"));
alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_BOTTOM,"text->setAlignment(\nosgText::Text::RIGHT_BOTTOM);"));
alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_BASE_LINE,"text->setAlignment(\nosgText::Text::LEFT_BASE_LINE);"));
alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_BASE_LINE,"text->setAlignment(\nosgText::Text::CENTER_BASE_LINE);"));
alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_BASE_LINE,"text->setAlignment(\nosgText::Text::RIGHT_BASE_LINE);"));
alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_BOTTOM_BASE_LINE,"text->setAlignment(\nosgText::Text::LEFT_BOTTOM_BASE_LINE);"));
alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_BOTTOM_BASE_LINE,"text->setAlignment(\nosgText::Text::CENTER_BOTTOM_BASE_LINE);"));
alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_BOTTOM_BASE_LINE,"text->setAlignment(\nosgText::Text::RIGHT_BOTTOM_BASE_LINE);"));
osg::Sequence* sequence = new osg::Sequence;
{
for(AlignmentList::iterator itr=alignmentList.begin();
itr!=alignmentList.end();
++itr)
{
osg::Geode* alignmentGeode = new osg::Geode;
sequence->addChild(alignmentGeode);
sequence->setTime(sequence->getNumChildren(), 1.0f);
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(alignmentSizeColor);
text->setCharacterSize(alignmentCharacterSize);
text->setPosition(cursor);
text->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX);
text->setAlignment(itr->first);
text->setText(itr->second);
alignmentGeode->addDrawable(text);
}
}
sequence->setMode(osg::Sequence::START);
sequence->setInterval(osg::Sequence::LOOP, 0, -1);
sequence->setDuration(1.0f, -1);
rootNode->addChild(sequence);
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Examples of how to set up different fonts...
//
cursor.x() = margin*2.0f;
cursor.y() = margin*2.0f;
osg::Vec4 fontColor(1.0f,0.5f,0.0f,1.0f);
float fontCharacterSize = 20.0f;
float spacing = 40.0f;
{
osgText::Text* text = new osgText::Text;
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
text->setFont(0);
text->setText("text->setFont(0); // inbuilt font.");
geode->addDrawable(text);
cursor.x() = text->getBound().xMax() + spacing ;
}
{
osgText::Font* arial = new osgText::Font(new osgQt::QFontImplementation(QFont("Arial")));
osgText::Text* text = new osgText::Text;
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
text->setFont(arial);
text->setText(arial!=0?
"text->setFont(\"fonts/arial.ttf\");":
"unable to load \"fonts/arial.ttf\"");
geode->addDrawable(text);
cursor.x() = text->getBound().xMax() + spacing ;
}
{
osgText::Font* times = new osgText::Font(new osgQt::QFontImplementation(QFont("Times")));
osgText::Text* text = new osgText::Text;
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
geode->addDrawable(text);
text->setFont(times);
text->setText(times!=0?
"text->setFont(\"fonts/times.ttf\");":
"unable to load \"fonts/times.ttf\"");
cursor.x() = text->getBound().xMax() + spacing ;
}
cursor.x() = margin*2.0f;
cursor.y() = margin;
{
osgText::Font* dirtydoz = new osgText::Font(new osgQt::QFontImplementation(QFont("Times")));
osgText::Text* text = new osgText::Text;
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
text->setFont(dirtydoz);
text->setText(dirtydoz!=0?
"text->setFont(\"fonts/dirtydoz.ttf\");":
"unable to load \"fonts/dirtydoz.ttf\"");
geode->addDrawable(text);
cursor.x() = text->getBound().xMax() + spacing ;
}
{
osgText::Font* fudd = new osgText::Font(new osgQt::QFontImplementation(QFont("Times")));
osgText::Text* text = new osgText::Text;
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
text->setFont(fudd);
text->setText(fudd!=0?
"text->setFont(\"fonts/fudd.ttf\");":
"unable to load \"fonts/fudd.ttf\"");
geode->addDrawable(text);
cursor.x() = text->getBound().xMax() + spacing ;
}
return rootNode;
}
// create text which sits in 3D space such as would be inserted into a normal model
osg::Group* create3DText(const osg::Vec3& center,float radius)
{
osg::Geode* geode = new osg::Geode;
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Examples of how to set up axis/orientation alignments
//
float characterSize=radius*0.2f;
osg::Vec3 pos(center.x()-radius*.5f,center.y()-radius*.5f,center.z()-radius*.5f);
osgText::Text* text1 = new osgText::Text;
text1->setFont(new osgText::Font(new osgQt::QFontImplementation(QFont("Times"))));
text1->setCharacterSize(characterSize);
text1->setPosition(pos);
text1->setAxisAlignment(osgText::Text::XY_PLANE);
text1->setText("XY_PLANE");
geode->addDrawable(text1);
osgText::Text* text2 = new osgText::Text;
text2->setFont(new osgText::Font(new osgQt::QFontImplementation(QFont("Times"))));
text2->setCharacterSize(characterSize);
text2->setPosition(pos);
text2->setAxisAlignment(osgText::Text::YZ_PLANE);
text2->setText("YZ_PLANE");
geode->addDrawable(text2);
osgText::Text* text3 = new osgText::Text;
text3->setFont(new osgText::Font(new osgQt::QFontImplementation(QFont("Times"))));
text3->setCharacterSize(characterSize);
text3->setPosition(pos);
text3->setAxisAlignment(osgText::Text::XZ_PLANE);
text3->setText("XZ_PLANE");
geode->addDrawable(text3);
osgText::Text* text4 = new osgText::Text;
text4->setFont(new osgText::Font(new osgQt::QFontImplementation(QFont("Times"))));
text4->setCharacterSize(characterSize);
text4->setPosition(center);
text4->setAxisAlignment(osgText::Text::SCREEN);
osg::Vec4 characterSizeModeColor(1.0f,0.0f,0.5f,1.0f);
osgText::Text* text5 = new osgText::Text;
text5->setColor(characterSizeModeColor);
text5->setFont(new osgText::Font(new osgQt::QFontImplementation(QFont("Times"))));
//text5->setCharacterSize(characterSize);
text5->setCharacterSize(32.0f); // medium
text5->setPosition(center - osg::Vec3(0.0, 0.0, 0.2));
text5->setAxisAlignment(osgText::Text::SCREEN);
text5->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
text5->setText("CharacterSizeMode SCREEN_COORDS(size 32.0)");
geode->addDrawable(text5);
osgText::Text* text6 = new osgText::Text;
text6->setColor(characterSizeModeColor);
text6->setFont(new osgText::Font(new osgQt::QFontImplementation(QFont("Times"))));
text6->setCharacterSize(characterSize);
text6->setPosition(center - osg::Vec3(0.0, 0.0, 0.4));
text6->setAxisAlignment(osgText::Text::SCREEN);
text6->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
text6->setText("CharacterSizeMode OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT");
geode->addDrawable(text6);
osgText::Text* text7 = new osgText::Text;
text7->setColor(characterSizeModeColor);
text7->setFont(new osgText::Font(new osgQt::QFontImplementation(QFont("Times"))));
text7->setCharacterSize(characterSize);
text7->setPosition(center - osg::Vec3(0.0, 0.0, 0.6));
text7->setAxisAlignment(osgText::Text::SCREEN);
text7->setCharacterSizeMode(osgText::Text::OBJECT_COORDS);
text7->setText("CharacterSizeMode OBJECT_COORDS (default)");
geode->addDrawable(text7);
#if 1
// reproduce outline bounding box compute problem with backdrop on.
text4->setBackdropType(osgText::Text::OUTLINE);
text4->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX);
#endif
text4->setText("SCREEN");
geode->addDrawable(text4);
osg::ShapeDrawable* shape = new osg::ShapeDrawable(new osg::Sphere(center,characterSize*0.2f));
shape->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON);
geode->addDrawable(shape);
osg::Group* rootNode = new osg::Group;
rootNode->addChild(geode);
return rootNode;
}
class MainWindow : public QWidget {
public:
MainWindow()
{
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits(osg::DisplaySettings::instance().get());
traits->width = width();
traits->height = height();
traits->doubleBuffer = true;
osgQt::GraphicsWindowQt* graphicsWindow = new osgQt::GraphicsWindowQt(traits.get());
QGridLayout* grid = new QGridLayout;
grid->setMargin(0);
grid->addWidget(graphicsWindow->getGLWidget(), 0, 0);
setLayout(grid);
_viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
osg::Camera* camera = _viewer.getCamera();
camera->setGraphicsContext(graphicsWindow);
camera->setViewport(new osg::Viewport(0, 0, width(), height()));
startTimer(10);
}
virtual void paintEvent(QPaintEvent* event)
{
_viewer.frame();
}
virtual void timerEvent(QTimerEvent* event)
{
_viewer.frame();
}
void setSceneData(osg::Node* node)
{
_viewer.setSceneData(node);
}
void setCameraManipulator(osgGA::CameraManipulator* manipulator, bool resetPosition = true)
{
_viewer.setCameraManipulator(manipulator, resetPosition);
}
private:
osgViewer::Viewer _viewer;
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
// prepare scene.
osg::Vec3 center(0.0f,0.0f,0.0f);
float radius = 1.0f;
// create the hud.
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);
camera->setViewMatrix(osg::Matrix::identity());
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->addChild(createHUDText());
camera->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
// make sure the root node is group so we can add extra nodes to it.
osg::ref_ptr<osg::Group> group = new osg::Group;
group->addChild(camera.get());
group->addChild(create3DText(center, radius));
// The qt window
MainWindow widget;
// set the scene to render
widget.setSceneData(group.get());
widget.setCameraManipulator(new osgGA::TrackballManipulator);
widget.setGeometry(100, 100, 800, 600);
widget.show();
return app.exec();
}
|