| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgUtil/Optimizer> |
|---|
| 15 | #include <osgDB/ReadFile> |
|---|
| 16 | #include <osgDB/Registry> |
|---|
| 17 | #include <osgProducer/Viewer> |
|---|
| 18 | |
|---|
| 19 | #include <osg/Geode> |
|---|
| 20 | #include <osg/Projection> |
|---|
| 21 | #include <osg/MatrixTransform> |
|---|
| 22 | |
|---|
| 23 | #include <osgText/Font> |
|---|
| 24 | #include <osgText/Text> |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | osg::Group* createHUDText() |
|---|
| 28 | { |
|---|
| 29 | |
|---|
| 30 | osgText::Font* font = osgText::readFontFile("fonts/arial.ttf"); |
|---|
| 31 | |
|---|
| 32 | osg::Geode* geode = new osg::Geode; |
|---|
| 33 | |
|---|
| 34 | float windowHeight = 1024.0f; |
|---|
| 35 | float windowWidth = 1280.0f; |
|---|
| 36 | float margin = 50.0f; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | osg::Vec4 layoutColor(1.0f,1.0f,0.0f,1.0f); |
|---|
| 40 | float layoutCharacterSize = 20.0f; |
|---|
| 41 | |
|---|
| 42 | { |
|---|
| 43 | osgText::Text* text = new osgText::Text; |
|---|
| 44 | text->setFont(font); |
|---|
| 45 | text->setColor(layoutColor); |
|---|
| 46 | text->setCharacterSize(layoutCharacterSize); |
|---|
| 47 | text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f)); |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | text->setLayout(osgText::Text::LEFT_TO_RIGHT); |
|---|
| 52 | |
|---|
| 53 | text->setText("text->setLayout(osgText::Text::LEFT_TO_RIGHT);"); |
|---|
| 54 | geode->addDrawable(text); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | { |
|---|
| 58 | osgText::Text* text = new osgText::Text; |
|---|
| 59 | text->setFont(font); |
|---|
| 60 | text->setColor(layoutColor); |
|---|
| 61 | text->setCharacterSize(layoutCharacterSize); |
|---|
| 62 | text->setPosition(osg::Vec3(windowWidth-margin,windowHeight-margin,0.0f)); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | text->setLayout(osgText::Text::RIGHT_TO_LEFT); |
|---|
| 66 | |
|---|
| 67 | text->setText("text->setLayout(osgText::Text::RIGHT_TO_LEFT);"); |
|---|
| 68 | geode->addDrawable(text); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | { |
|---|
| 72 | osgText::Text* text = new osgText::Text; |
|---|
| 73 | text->setFont(font); |
|---|
| 74 | text->setColor(layoutColor); |
|---|
| 75 | text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f)); |
|---|
| 76 | text->setCharacterSize(layoutCharacterSize); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | text->setLayout(osgText::Text::VERTICAL); |
|---|
| 80 | |
|---|
| 81 | text->setText("text->setLayout(osgText::Text::VERTICAL);"); |
|---|
| 82 | geode->addDrawable(text); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | osg::Vec4 fontSizeColor(0.0f,1.0f,1.0f,1.0f); |
|---|
| 87 | float fontSizeCharacterSize = 30; |
|---|
| 88 | |
|---|
| 89 | osg::Vec3 cursor = osg::Vec3(margin*2,windowHeight-margin*2,0.0f); |
|---|
| 90 | |
|---|
| 91 | { |
|---|
| 92 | osgText::Text* text = new osgText::Text; |
|---|
| 93 | text->setFont(font); |
|---|
| 94 | text->setColor(fontSizeColor); |
|---|
| 95 | text->setCharacterSize(fontSizeCharacterSize); |
|---|
| 96 | text->setPosition(cursor); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | text->setFontSize(10,10); |
|---|
| 100 | |
|---|
| 101 | text->setText("text->setFontSize(10,10); // blocky but small texture memory usage"); |
|---|
| 102 | geode->addDrawable(text); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | cursor.y() -= fontSizeCharacterSize; |
|---|
| 106 | { |
|---|
| 107 | osgText::Text* text = new osgText::Text; |
|---|
| 108 | text->setFont(font); |
|---|
| 109 | text->setColor(fontSizeColor); |
|---|
| 110 | text->setCharacterSize(fontSizeCharacterSize); |
|---|
| 111 | text->setPosition(cursor); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | text->setFontSize(20,20); |
|---|
| 115 | |
|---|
| 116 | text->setText("text->setFontSize(20,20); // smoother but higher texture memory usage (but still quite low)."); |
|---|
| 117 | geode->addDrawable(text); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | cursor.y() -= fontSizeCharacterSize; |
|---|
| 121 | { |
|---|
| 122 | osgText::Text* text = new osgText::Text; |
|---|
| 123 | text->setFont(font); |
|---|
| 124 | text->setColor(fontSizeColor); |
|---|
| 125 | text->setCharacterSize(fontSizeCharacterSize); |
|---|
| 126 | text->setPosition(cursor); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | text->setFontSize(40,40); |
|---|
| 130 | |
|---|
| 131 | text->setText("text->setFontSize(40,40); // even smoother but again higher texture memory usage."); |
|---|
| 132 | geode->addDrawable(text); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | osg::Vec4 characterSizeColor(1.0f,0.0f,1.0f,1.0f); |
|---|
| 137 | |
|---|
| 138 | cursor.y() -= fontSizeCharacterSize*2.0f; |
|---|
| 139 | |
|---|
| 140 | { |
|---|
| 141 | osgText::Text* text = new osgText::Text; |
|---|
| 142 | text->setFont(font); |
|---|
| 143 | text->setColor(characterSizeColor); |
|---|
| 144 | text->setFontSize(15,15); |
|---|
| 145 | text->setPosition(cursor); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | text->setCharacterSize(15); |
|---|
| 149 | |
|---|
| 150 | text->setText("text->setCharacterSize(15.0f); // tiny"); |
|---|
| 151 | geode->addDrawable(text); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | cursor.y() -= fontSizeCharacterSize; |
|---|
| 155 | { |
|---|
| 156 | osgText::Text* text = new osgText::Text; |
|---|
| 157 | text->setFont(font); |
|---|
| 158 | text->setColor(characterSizeColor); |
|---|
| 159 | text->setFontSize(30,30); |
|---|
| 160 | text->setPosition(cursor); |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | text->setCharacterSize(30.0f); |
|---|
| 164 | |
|---|
| 165 | text->setText("text->setCharacterSize(30.0f); // medium size"); |
|---|
| 166 | geode->addDrawable(text); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | cursor.y() -= fontSizeCharacterSize; |
|---|
| 170 | { |
|---|
| 171 | osgText::Text* text = new osgText::Text; |
|---|
| 172 | text->setFont(font); |
|---|
| 173 | text->setColor(characterSizeColor); |
|---|
| 174 | text->setFontSize(40,40); |
|---|
| 175 | text->setPosition(cursor); |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | text->setCharacterSize(40.0f); |
|---|
| 179 | |
|---|
| 180 | text->setText("text->setCharacterSize(40.0f); // big, but can go much bigger :-)"); |
|---|
| 181 | geode->addDrawable(text); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | if (0) |
|---|
| 187 | { |
|---|
| 188 | osgText::Text* text2 = new osgText::Text; |
|---|
| 189 | text2->setPosition(osg::Vec3(0.0f,50.0f,0.0f)); |
|---|
| 190 | text2->setCharacterSize(12.0f); |
|---|
| 191 | text2->setText("10 PRINT \"Hello World\""); |
|---|
| 192 | geode->addDrawable(text2); |
|---|
| 193 | |
|---|
| 194 | osgText::Text* text3 = new osgText::Text; |
|---|
| 195 | text3->setFont(osgText::readFontFile("fonts/arial.ttf")); |
|---|
| 196 | text3->setPosition(osg::Vec3(100.0f,100.0f,0.0f)); |
|---|
| 197 | text3->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/arial.ttf"); |
|---|
| 198 | geode->addDrawable(text3); |
|---|
| 199 | |
|---|
| 200 | osgText::Text* text4 = new osgText::Text; |
|---|
| 201 | text4->setFont(osgText::readFontFile("fonts/times.ttf")); |
|---|
| 202 | text4->setFontSize(128,128); |
|---|
| 203 | text4->setPosition(osg::Vec3(200.0f,200.0f,0.0f)); |
|---|
| 204 | text4->setLayout(osgText::Text::RIGHT_TO_LEFT); |
|---|
| 205 | text4->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX); |
|---|
| 206 | text4->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/times.ttf"); |
|---|
| 207 | geode->addDrawable(text4); |
|---|
| 208 | |
|---|
| 209 | osgText::Text* text5 = new osgText::Text; |
|---|
| 210 | text5->setFont(osgText::readFontFile("fonts/dirtydoz.ttf")); |
|---|
| 211 | text5->setPosition(osg::Vec3(300.0f,300.0f,0.0f)); |
|---|
| 212 | text5->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f)); |
|---|
| 213 | text5->setLayout(osgText::Text::VERTICAL); |
|---|
| 214 | text5->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX); |
|---|
| 215 | text5->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/dirtydoz.ttf"); |
|---|
| 216 | geode->addDrawable(text5); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | osg::Group* rootNode = new osg::Group; |
|---|
| 234 | rootNode->addChild(geode); |
|---|
| 235 | |
|---|
| 236 | return rootNode; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | osg::Group* create3DText() |
|---|
| 240 | { |
|---|
| 241 | |
|---|
| 242 | osg::Geode* geode = new osg::Geode; |
|---|
| 243 | |
|---|
| 244 | osgText::Text* text1 = new osgText::Text; |
|---|
| 245 | text1->setFont("fonts/times.ttf"); |
|---|
| 246 | text1->setAxisAlignment(osgText::Text::XY_PLANE); |
|---|
| 247 | text1->setText("XY_PLANE"); |
|---|
| 248 | geode->addDrawable(text1); |
|---|
| 249 | |
|---|
| 250 | osgText::Text* text2 = new osgText::Text; |
|---|
| 251 | text2->setFont("fonts/times.ttf"); |
|---|
| 252 | text2->setPosition(osg::Vec3(0.0f,0.0f,0.0f)); |
|---|
| 253 | text2->setAxisAlignment(osgText::Text::YZ_PLANE); |
|---|
| 254 | text2->setText("YZ_PLANE"); |
|---|
| 255 | geode->addDrawable(text2); |
|---|
| 256 | |
|---|
| 257 | osgText::Text* text3 = new osgText::Text; |
|---|
| 258 | text3->setFont("fonts/times.ttf"); |
|---|
| 259 | text3->setPosition(osg::Vec3(00.0f,00.0f,00.0f)); |
|---|
| 260 | text3->setAxisAlignment(osgText::Text::XZ_PLANE); |
|---|
| 261 | text3->setText("XZ_PLANE"); |
|---|
| 262 | geode->addDrawable(text3); |
|---|
| 263 | |
|---|
| 264 | osgText::Text* text4 = new osgText::Text; |
|---|
| 265 | text4->setFont("fonts/times.ttf"); |
|---|
| 266 | text4->setAxisAlignment(osgText::Text::SCREEN); |
|---|
| 267 | text4->setPosition(osg::Vec3(00.0f,00.0f,00.0f)); |
|---|
| 268 | text4->setText("SCREEN"); |
|---|
| 269 | geode->addDrawable(text4); |
|---|
| 270 | |
|---|
| 271 | osg::Group* rootNode = new osg::Group; |
|---|
| 272 | rootNode->addChild(geode); |
|---|
| 273 | |
|---|
| 274 | return rootNode; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | int main( int argc, char **argv ) |
|---|
| 278 | { |
|---|
| 279 | |
|---|
| 280 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] [filename] ..."); |
|---|
| 284 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | osgProducer::Viewer viewer(arguments); |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 298 | { |
|---|
| 299 | arguments.getApplicationUsage()->write(cout); |
|---|
| 300 | return 1; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | if (arguments.errors()) |
|---|
| 308 | { |
|---|
| 309 | arguments.writeErrorMessages(cout); |
|---|
| 310 | return 1; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | osg::ref_ptr<osg::Node> rootNode = osgDB::readNodeFiles(arguments); |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | { |
|---|
| 319 | |
|---|
| 320 | if (rootNode.valid()) |
|---|
| 321 | { |
|---|
| 322 | |
|---|
| 323 | osgUtil::Optimizer optimizer; |
|---|
| 324 | optimizer.optimize(rootNode.get()); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | osg::Group* group = dynamic_cast<osg::Group*>(rootNode.get()); |
|---|
| 329 | if (!group) |
|---|
| 330 | { |
|---|
| 331 | group = new osg::Group; |
|---|
| 332 | |
|---|
| 333 | if (rootNode.valid()) group->addChild(rootNode.get()); |
|---|
| 334 | |
|---|
| 335 | rootNode = group; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | { |
|---|
| 339 | |
|---|
| 340 | osg::Projection* projection = new osg::Projection; |
|---|
| 341 | projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
|---|
| 342 | |
|---|
| 343 | osg::MatrixTransform* modelview_abs = new osg::MatrixTransform; |
|---|
| 344 | modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE); |
|---|
| 345 | modelview_abs->setMatrix(osg::Matrix::identity()); |
|---|
| 346 | |
|---|
| 347 | modelview_abs->addChild(createHUDText()); |
|---|
| 348 | |
|---|
| 349 | projection->addChild(modelview_abs); |
|---|
| 350 | |
|---|
| 351 | group->addChild(projection); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | osg::MatrixTransform* scale = new osg::MatrixTransform; |
|---|
| 355 | scale->setMatrix(osg::Matrix::scale(1.0f,1.0f,1.0f)); |
|---|
| 356 | scale->addChild(create3DText()); |
|---|
| 357 | group->addChild(scale); |
|---|
| 358 | |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | viewer.setSceneData(rootNode.get()); |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | viewer.realize(Producer::CameraGroup::ThreadPerCamera); |
|---|
| 366 | |
|---|
| 367 | while( !viewer.done() ) |
|---|
| 368 | { |
|---|
| 369 | |
|---|
| 370 | viewer.sync(); |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | viewer.update(); |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | viewer.frame(); |
|---|
| 378 | |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | return 0; |
|---|
| 382 | } |
|---|