| 39 | | osg::Group* createHUDText() |
| 40 | | { |
| 41 | | |
| 42 | | osg::Group* rootNode = new osg::Group; |
| 43 | | |
| 44 | | osgText::Font* font = osgText::readFontFile("fonts/arial.ttf"); |
| 45 | | |
| 46 | | osg::Geode* geode = new osg::Geode; |
| 47 | | rootNode->addChild(geode); |
| 48 | | |
| 49 | | float windowHeight = 1024.0f; |
| 50 | | float windowWidth = 1280.0f; |
| 51 | | float margin = 50.0f; |
| 52 | | |
| 53 | | |
| 54 | | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 55 | | // |
| 56 | | // Examples of how to set up different text layout |
| 57 | | // |
| 58 | | |
| 59 | | osg::Vec4 layoutColor(1.0f,1.0f,0.0f,1.0f); |
| 60 | | float layoutCharacterSize = 20.0f; |
| 61 | | |
| 62 | | { |
| 63 | | osgText::Text* text = new osgText::Text; |
| 64 | | text->setFont(font); |
| 65 | | text->setColor(layoutColor); |
| 66 | | text->setCharacterSize(layoutCharacterSize); |
| 67 | | text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f)); |
| 68 | | |
| 69 | | // the default layout is left to right, typically used in languages |
| 70 | | // originating from europe such as English, French, German, Spanish etc.. |
| 71 | | text->setLayout(osgText::Text::LEFT_TO_RIGHT); |
| 72 | | |
| 73 | | text->setText("text->setLayout(osgText::Text::LEFT_TO_RIGHT);"); |
| 74 | | geode->addDrawable(text); |
| 75 | | } |
| 76 | | |
| 77 | | { |
| 78 | | osgText::Text* text = new osgText::Text; |
| 79 | | text->setFont(font); |
| 80 | | text->setColor(layoutColor); |
| 81 | | text->setCharacterSize(layoutCharacterSize); |
| 82 | | text->setPosition(osg::Vec3(windowWidth-margin,windowHeight-margin,0.0f)); |
| 83 | | |
| 84 | | // right to left layouts would be used for hebrew or arabic fonts. |
| 85 | | text->setLayout(osgText::Text::RIGHT_TO_LEFT); |
| 86 | | text->setAlignment(osgText::Text::RIGHT_BASE_LINE); |
| 87 | | |
| 88 | | text->setText("text->setLayout(osgText::Text::RIGHT_TO_LEFT);"); |
| 89 | | geode->addDrawable(text); |
| 90 | | } |
| 91 | | |
| 92 | | { |
| 93 | | osgText::Text* text = new osgText::Text; |
| 94 | | text->setFont(font); |
| 95 | | text->setColor(layoutColor); |
| 96 | | text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f)); |
| 97 | | text->setCharacterSize(layoutCharacterSize); |
| 98 | | |
| 99 | | // vertical font layout would be used for asian fonts. |
| 100 | | text->setLayout(osgText::Text::VERTICAL); |
| 101 | | |
| 102 | | text->setText("text->setLayout(osgText::Text::VERTICAL);"); |
| 103 | | geode->addDrawable(text); |
| 104 | | } |
| 105 | | |
| 106 | | |
| 107 | | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 108 | | // |
| 109 | | // Examples of how to set up different font resolution |
| 110 | | // |
| 111 | | |
| 112 | | osg::Vec4 fontSizeColor(0.0f,1.0f,1.0f,1.0f); |
| 113 | | float fontSizeCharacterSize = 30; |
| 114 | | |
| 115 | | osg::Vec3 cursor = osg::Vec3(margin*2,windowHeight-margin*2,0.0f); |
| 116 | | |
| 117 | | { |
| 118 | | osgText::Text* text = new osgText::Text; |
| 119 | | text->setFont(font); |
| 120 | | text->setColor(fontSizeColor); |
| 121 | | text->setCharacterSize(fontSizeCharacterSize); |
| 122 | | text->setPosition(cursor); |
| 123 | | |
| 124 | | // use text that uses 10 by 10 texels as a target resolution for fonts. |
| 125 | | text->setFontResolution(10,10); // blocky but small texture memory usage |
| 126 | | |
| 127 | | text->setText("text->setFontResolution(10,10); // blocky but small texture memory usage"); |
| 128 | | geode->addDrawable(text); |
| 129 | | } |
| 130 | | |
| 131 | | cursor.y() -= fontSizeCharacterSize; |
| 132 | | { |
| 133 | | osgText::Text* text = new osgText::Text; |
| 134 | | text->setFont(font); |
| 135 | | text->setColor(fontSizeColor); |
| 136 | | text->setCharacterSize(fontSizeCharacterSize); |
| 137 | | text->setPosition(cursor); |
| 138 | | |
| 139 | | // use text that uses 20 by 20 texels as a target resolution for fonts. |
| 140 | | text->setFontResolution(20,20); // smoother but higher texture memory usage (but still quite low). |
| 141 | | |
| 142 | | text->setText("text->setFontResolution(20,20); // smoother but higher texture memory usage (but still quite low)."); |
| 143 | | geode->addDrawable(text); |
| 144 | | } |
| 145 | | |
| 146 | | cursor.y() -= fontSizeCharacterSize; |
| 147 | | { |
| 148 | | osgText::Text* text = new osgText::Text; |
| 149 | | text->setFont(font); |
| 150 | | text->setColor(fontSizeColor); |
| 151 | | text->setCharacterSize(fontSizeCharacterSize); |
| 152 | | text->setPosition(cursor); |
| 153 | | |
| 154 | | // use text that uses 40 by 40 texels as a target resolution for fonts. |
| 155 | | text->setFontResolution(40,40); // even smoother but again higher texture memory usage. |
| 156 | | |
| 157 | | text->setText("text->setFontResolution(40,40); // even smoother but again higher texture memory usage."); |
| 158 | | geode->addDrawable(text); |
| 159 | | } |
| 160 | | |
| 161 | | |
| 162 | | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 163 | | // |
| 164 | | // Examples of how to set up different sized text |
| 165 | | // |
| 166 | | |
| 167 | | osg::Vec4 characterSizeColor(1.0f,0.0f,1.0f,1.0f); |
| 168 | | |
| 169 | | cursor.y() -= fontSizeCharacterSize*2.0f; |
| 170 | | |
| 171 | | { |
| 172 | | osgText::Text* text = new osgText::Text; |
| 173 | | text->setFont(font); |
| 174 | | text->setColor(characterSizeColor); |
| 175 | | text->setFontResolution(20,20); |
| 176 | | text->setPosition(cursor); |
| 177 | | |
| 178 | | // use text that is 20 units high. |
| 179 | | text->setCharacterSize(20); // small |
| 180 | | |
| 181 | | text->setText("text->setCharacterSize(20.0f); // small"); |
| 182 | | geode->addDrawable(text); |
| 183 | | } |
| 184 | | |
| 185 | | cursor.y() -= 30.0f; |
| 186 | | { |
| 187 | | osgText::Text* text = new osgText::Text; |
| 188 | | text->setFont(font); |
| 189 | | text->setColor(characterSizeColor); |
| 190 | | text->setFontResolution(30,30); |
| 191 | | text->setPosition(cursor); |
| 192 | | |
| 193 | | // use text that is 30 units high. |
| 194 | | text->setCharacterSize(30.0f); // medium |
| 195 | | |
| 196 | | text->setText("text->setCharacterSize(30.0f); // medium"); |
| 197 | | geode->addDrawable(text); |
| 198 | | } |
| 199 | | |
| 200 | | cursor.y() -= 50.0f; |
| 201 | | { |
| 202 | | osgText::Text* text = new osgText::Text; |
| 203 | | text->setFont(font); |
| 204 | | text->setColor(characterSizeColor); |
| 205 | | text->setFontResolution(40,40); |
| 206 | | text->setPosition(cursor); |
| 207 | | |
| 208 | | // use text that is 60 units high. |
| 209 | | text->setCharacterSize(60.0f); // large |
| 210 | | |
| 211 | | text->setText("text->setCharacterSize(60.0f); // large"); |
| 212 | | geode->addDrawable(text); |
| 213 | | } |
| 214 | | |
| 215 | | |
| 216 | | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 217 | | // |
| 218 | | // Examples of how to set up different alignments |
| 219 | | // |
| 220 | | |
| 221 | | osg::Vec4 alignmentSizeColor(0.0f,1.0f,0.0f,1.0f); |
| 222 | | float alignmentCharacterSize = 25.0f; |
| 223 | | cursor.x() = 640; |
| 224 | | cursor.y() = margin*4.0f; |
| 225 | | |
| 226 | | typedef std::pair<osgText::Text::AlignmentType,std::string> AlignmentPair; |
| 227 | | typedef std::vector<AlignmentPair> AlignmentList; |
| 228 | | AlignmentList alignmentList; |
| 229 | | alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_TOP,"text->setAlignment(\nosgText::Text::LEFT_TOP);")); |
| 230 | | alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_CENTER,"text->setAlignment(\nosgText::Text::LEFT_CENTER);")); |
| 231 | | alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_BOTTOM,"text->setAlignment(\nosgText::Text::LEFT_BOTTOM);")); |
| 232 | | alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_TOP,"text->setAlignment(\nosgText::Text::CENTER_TOP);")); |
| 233 | | alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_CENTER,"text->setAlignment(\nosgText::Text::CENTER_CENTER);")); |
| 234 | | alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_BOTTOM,"text->setAlignment(\nosgText::Text::CENTER_BOTTOM);")); |
| 235 | | alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_TOP,"text->setAlignment(\nosgText::Text::RIGHT_TOP);")); |
| 236 | | alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_CENTER,"text->setAlignment(\nosgText::Text::RIGHT_CENTER);")); |
| 237 | | alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_BOTTOM,"text->setAlignment(\nosgText::Text::RIGHT_BOTTOM);")); |
| 238 | | alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_BASE_LINE,"text->setAlignment(\nosgText::Text::LEFT_BASE_LINE);")); |
| 239 | | alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_BASE_LINE,"text->setAlignment(\nosgText::Text::CENTER_BASE_LINE);")); |
| 240 | | alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_BASE_LINE,"text->setAlignment(\nosgText::Text::RIGHT_BASE_LINE);")); |
| 241 | | alignmentList.push_back(AlignmentPair(osgText::Text::LEFT_BOTTOM_BASE_LINE,"text->setAlignment(\nosgText::Text::LEFT_BOTTOM_BASE_LINE);")); |
| 242 | | alignmentList.push_back(AlignmentPair(osgText::Text::CENTER_BOTTOM_BASE_LINE,"text->setAlignment(\nosgText::Text::CENTER_BOTTOM_BASE_LINE);")); |
| 243 | | alignmentList.push_back(AlignmentPair(osgText::Text::RIGHT_BOTTOM_BASE_LINE,"text->setAlignment(\nosgText::Text::RIGHT_BOTTOM_BASE_LINE);")); |
| 244 | | |
| 245 | | |
| 246 | | osg::Sequence* sequence = new osg::Sequence; |
| 247 | | { |
| 248 | | for(AlignmentList::iterator itr=alignmentList.begin(); |
| 249 | | itr!=alignmentList.end(); |
| 250 | | ++itr) |
| 251 | | { |
| 252 | | osg::Geode* alignmentGeode = new osg::Geode; |
| 253 | | sequence->addChild(alignmentGeode); |
| 254 | | sequence->setTime(sequence->getNumChildren(), 1.0f); |
| 255 | | |
| 256 | | osgText::Text* text = new osgText::Text; |
| 257 | | text->setFont(font); |
| 258 | | text->setColor(alignmentSizeColor); |
| 259 | | text->setCharacterSize(alignmentCharacterSize); |
| 260 | | text->setPosition(cursor); |
| 261 | | text->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX); |
| 262 | | |
| 263 | | text->setAlignment(itr->first); |
| 264 | | text->setText(itr->second); |
| 265 | | |
| 266 | | alignmentGeode->addDrawable(text); |
| 267 | | |
| 268 | | |
| 269 | | } |
| 270 | | |
| 271 | | } |
| 272 | | |
| 273 | | sequence->setMode(osg::Sequence::START); |
| 274 | | sequence->setInterval(osg::Sequence::LOOP, 0, -1); |
| 275 | | sequence->setDuration(1.0f, -1); |
| 276 | | |
| 277 | | rootNode->addChild(sequence); |
| 278 | | |
| 279 | | |
| 280 | | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 281 | | // |
| 282 | | // Examples of how to set up different fonts... |
| 283 | | // |
| 284 | | |
| 285 | | cursor.x() = margin*2.0f; |
| 286 | | cursor.y() = margin*2.0f; |
| 287 | | |
| 288 | | osg::Vec4 fontColor(1.0f,0.5f,0.0f,1.0f); |
| 289 | | float fontCharacterSize = 20.0f; |
| 290 | | float spacing = 40.0f; |
| 291 | | |
| 292 | | { |
| 293 | | osgText::Text* text = new osgText::Text; |
| 294 | | text->setColor(fontColor); |
| 295 | | text->setPosition(cursor); |
| 296 | | text->setCharacterSize(fontCharacterSize); |
| 297 | | |
| 298 | | text->setFont(0); |
| 299 | | text->setText("text->setFont(0); // inbuilt font."); |
| 300 | | geode->addDrawable(text); |
| 301 | | |
| 302 | | cursor.x() = text->getBound().xMax() + spacing ; |
| 303 | | } |
| 304 | | |
| 305 | | { |
| 306 | | osgText::Font* arial = osgText::readFontFile("fonts/arial.ttf"); |
| 307 | | |
| 308 | | osgText::Text* text = new osgText::Text; |
| 309 | | text->setColor(fontColor); |
| 310 | | text->setPosition(cursor); |
| 311 | | text->setCharacterSize(fontCharacterSize); |
| 312 | | |
| 313 | | text->setFont(arial); |
| 314 | | text->setText(arial!=0? |
| 315 | | "text->setFont(\"fonts/arial.ttf\");": |
| 316 | | "unable to load \"fonts/arial.ttf\""); |
| 317 | | geode->addDrawable(text); |
| 318 | | |
| 319 | | cursor.x() = text->getBound().xMax() + spacing ; |
| 320 | | } |
| 321 | | |
| 322 | | { |
| 323 | | osgText::Font* times = osgText::readFontFile("fonts/times.ttf"); |
| 324 | | |
| 325 | | osgText::Text* text = new osgText::Text; |
| 326 | | text->setColor(fontColor); |
| 327 | | text->setPosition(cursor); |
| 328 | | text->setCharacterSize(fontCharacterSize); |
| 329 | | |
| 330 | | geode->addDrawable(text); |
| 331 | | text->setFont(times); |
| 332 | | text->setText(times!=0? |
| 333 | | "text->setFont(\"fonts/times.ttf\");": |
| 334 | | "unable to load \"fonts/times.ttf\""); |
| 335 | | |
| 336 | | cursor.x() = text->getBound().xMax() + spacing ; |
| 337 | | } |
| 338 | | |
| 339 | | cursor.x() = margin*2.0f; |
| 340 | | cursor.y() = margin; |
| 341 | | |
| 342 | | { |
| 343 | | osgText::Font* dirtydoz = osgText::readFontFile("fonts/dirtydoz.ttf"); |
| 344 | | |
| 345 | | osgText::Text* text = new osgText::Text; |
| 346 | | text->setColor(fontColor); |
| 347 | | text->setPosition(cursor); |
| 348 | | text->setCharacterSize(fontCharacterSize); |
| 349 | | |
| 350 | | text->setFont(dirtydoz); |
| 351 | | text->setText(dirtydoz!=0? |
| 352 | | "text->setFont(\"fonts/dirtydoz.ttf\");": |
| 353 | | "unable to load \"fonts/dirtydoz.ttf\""); |
| 354 | | geode->addDrawable(text); |
| 355 | | |
| 356 | | cursor.x() = text->getBound().xMax() + spacing ; |
| 357 | | } |
| 358 | | |
| 359 | | { |
| 360 | | osgText::Font* fudd = osgText::readFontFile("fonts/fudd.ttf"); |
| 361 | | |
| 362 | | osgText::Text* text = new osgText::Text; |
| 363 | | text->setColor(fontColor); |
| 364 | | text->setPosition(cursor); |
| 365 | | text->setCharacterSize(fontCharacterSize); |
| 366 | | |
| 367 | | text->setFont(fudd); |
| 368 | | text->setText(fudd!=0? |
| 369 | | "text->setFont(\"fonts/fudd.ttf\");": |
| 370 | | "unable to load \"fonts/fudd.ttf\""); |
| 371 | | geode->addDrawable(text); |
| 372 | | |
| 373 | | cursor.x() = text->getBound().xMax() + spacing ; |
| 374 | | } |
| 375 | | |
| 376 | | return rootNode; |
| 377 | | } |
| 378 | | |
| 379 | | |
| 380 | | |
| 381 | | |
| 382 | | // create text which sits in 3D space such as would be inserted into a normal model |
| 383 | | osg::Group* create3DText(const osg::Vec3& center,float radius) |
| 384 | | { |
| 385 | | |
| 386 | | osg::Geode* geode = new osg::Geode; |
| 387 | | |
| 388 | | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 389 | | // |
| 390 | | // Examples of how to set up axis/orientation alignments |
| 391 | | // |
| 392 | | |
| 393 | | float characterSize=radius*0.2f; |
| 394 | | |
| 395 | | osg::Vec3 pos(center.x()-radius*.5f,center.y()-radius*.5f,center.z()-radius*.5f); |
| 396 | | |
| 397 | | osgText::Text* text1 = new osgText::Text; |
| 398 | | text1->setFont("fonts/times.ttf"); |
| 399 | | text1->setCharacterSize(characterSize); |
| 400 | | text1->setPosition(pos); |
| 401 | | text1->setAxisAlignment(osgText::Text::XY_PLANE); |
| 402 | | text1->setText("XY_PLANE"); |
| 403 | | geode->addDrawable(text1); |
| 404 | | |
| 405 | | osgText::Text* text2 = new osgText::Text; |
| 406 | | text2->setFont("fonts/times.ttf"); |
| 407 | | text2->setCharacterSize(characterSize); |
| 408 | | text2->setPosition(pos); |
| 409 | | text2->setAxisAlignment(osgText::Text::YZ_PLANE); |
| 410 | | text2->setText("YZ_PLANE"); |
| 411 | | geode->addDrawable(text2); |
| 412 | | |
| 413 | | osgText::Text* text3 = new osgText::Text; |
| 414 | | text3->setFont("fonts/times.ttf"); |
| 415 | | text3->setCharacterSize(characterSize); |
| 416 | | text3->setPosition(pos); |
| 417 | | text3->setAxisAlignment(osgText::Text::XZ_PLANE); |
| 418 | | text3->setText("XZ_PLANE"); |
| 419 | | geode->addDrawable(text3); |
| 420 | | |
| 421 | | |
| 422 | | osgText::Text* text4 = new osgText::Text; |
| 423 | | text4->setFont("fonts/times.ttf"); |
| 424 | | text4->setCharacterSize(characterSize); |
| 425 | | text4->setPosition(center); |
| 426 | | text4->setAxisAlignment(osgText::Text::SCREEN); |
| 427 | | |
| 428 | | osg::Vec4 characterSizeModeColor(1.0f,0.0f,0.5f,1.0f); |
| 429 | | |
| 430 | | osgText::Text* text5 = new osgText::Text; |
| 431 | | text5->setColor(characterSizeModeColor); |
| 432 | | text5->setFont("fonts/times.ttf"); |
| 433 | | //text5->setCharacterSize(characterSize); |
| 434 | | text5->setCharacterSize(32.0f); // medium |
| 435 | | text5->setPosition(center - osg::Vec3(0.0, 0.0, 0.2)); |
| 436 | | text5->setAxisAlignment(osgText::Text::SCREEN); |
| 437 | | text5->setCharacterSizeMode(osgText::Text::SCREEN_COORDS); |
| 438 | | text5->setText("CharacterSizeMode SCREEN_COORDS(size 32.0)"); |
| 439 | | geode->addDrawable(text5); |
| 440 | | |
| 441 | | osgText::Text* text6 = new osgText::Text; |
| 442 | | text6->setColor(characterSizeModeColor); |
| 443 | | text6->setFont("fonts/times.ttf"); |
| 444 | | text6->setCharacterSize(characterSize); |
| 445 | | text6->setPosition(center - osg::Vec3(0.0, 0.0, 0.4)); |
| 446 | | text6->setAxisAlignment(osgText::Text::SCREEN); |
| 447 | | text6->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT); |
| 448 | | text6->setText("CharacterSizeMode OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT"); |
| 449 | | geode->addDrawable(text6); |
| 450 | | |
| 451 | | osgText::Text* text7 = new osgText::Text; |
| 452 | | text7->setColor(characterSizeModeColor); |
| 453 | | text7->setFont("fonts/times.ttf"); |
| 454 | | text7->setCharacterSize(characterSize); |
| 455 | | text7->setPosition(center - osg::Vec3(0.0, 0.0, 0.6)); |
| 456 | | text7->setAxisAlignment(osgText::Text::SCREEN); |
| 457 | | text7->setCharacterSizeMode(osgText::Text::OBJECT_COORDS); |
| 458 | | text7->setText("CharacterSizeMode OBJECT_COORDS (default)"); |
| 459 | | geode->addDrawable(text7); |
| 460 | | |
| 461 | | #if 1 |
| 462 | | // reproduce outline bounding box compute problem with backdrop on. |
| 463 | | text4->setBackdropType(osgText::Text::OUTLINE); |
| 464 | | text4->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX); |
| 465 | | #endif |
| 466 | | |
| 467 | | text4->setText("SCREEN"); |
| 468 | | geode->addDrawable(text4); |
| 469 | | |
| 470 | | osg::ShapeDrawable* shape = new osg::ShapeDrawable(new osg::Sphere(center,characterSize*0.2f)); |
| 471 | | shape->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON); |
| 472 | | geode->addDrawable(shape); |
| 473 | | |
| 474 | | osg::Group* rootNode = new osg::Group; |
| 475 | | rootNode->addChild(geode); |
| 476 | | |
| 477 | | return rootNode; |
| 478 | | } |
| | 39 | |
| | 40 | |
| | 41 | |
| | 42 | |