| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 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 | #include <osgText/Text> |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | #define TEXT_POLYGON "Polygon Font - jygq" |
|---|
| 31 | #define TEXT_OUTLINE "Outline Font - jygq" |
|---|
| 32 | #define TEXT_TEXTURE "Texture Font - jygq" |
|---|
| 33 | #define TEXT_BITMAP "Bitmap Font - jygq" |
|---|
| 34 | #define TEXT_PIXMAP "Pixmap Font - jygq" |
|---|
| 35 | |
|---|
| 36 | #define TEXT_COL_2D osg::Vec4(.9,.9,.9,1) |
|---|
| 37 | #define TEXT_COL_3D osg::Vec4(.99,.3,.2,1) |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | std::string ttfPath("fonts/times.ttf"); |
|---|
| 41 | std::string ttfPath1("fonts/arial.ttf"); |
|---|
| 42 | |
|---|
| 43 | int gFontSize=18; |
|---|
| 44 | int gFontSize1=24; |
|---|
| 45 | std::vector<osg::ref_ptr<osgText::Text > > gTextList; |
|---|
| 46 | osgText::Text::AlignmentType gAlignment=osgText::Text::LEFT_BOTTOM; |
|---|
| 47 | |
|---|
| 48 | osg::Group* createText() |
|---|
| 49 | { |
|---|
| 50 | osg::Group* rootNode = new osg::Group; |
|---|
| 51 | |
|---|
| 52 | osgText::Text* text; |
|---|
| 53 | osg::Geode* geode; |
|---|
| 54 | osg::Material* textMaterial; |
|---|
| 55 | osg::StateSet* textState; |
|---|
| 56 | double xOffset=150; |
|---|
| 57 | double yOffset=gFontSize+10; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | osgText::BitmapFont* bitmapFont= new osgText::BitmapFont(ttfPath, |
|---|
| 65 | gFontSize1); |
|---|
| 66 | text= new osgText::Text(bitmapFont); |
|---|
| 67 | gTextList.push_back(text); |
|---|
| 68 | text->setText(std::string("2d ")+std::string(TEXT_BITMAP)); |
|---|
| 69 | text->setPosition(osg::Vec3(xOffset,yOffset,0)); |
|---|
| 70 | text->setDrawMode( osgText::Text::TEXT | |
|---|
| 71 | osgText::Text::BOUNDINGBOX | |
|---|
| 72 | osgText::Text::ALIGNMENT ); |
|---|
| 73 | text->setAlignment(gAlignment); |
|---|
| 74 | geode = new osg::Geode(); |
|---|
| 75 | geode->setName("BitmapFont"); |
|---|
| 76 | geode->addDrawable( text ); |
|---|
| 77 | |
|---|
| 78 | textMaterial = new osg::Material(); |
|---|
| 79 | textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE); |
|---|
| 80 | textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, TEXT_COL_2D); |
|---|
| 81 | textState = new osg::StateSet(); |
|---|
| 82 | textState->setAttribute(textMaterial ); |
|---|
| 83 | geode->setStateSet( textState ); |
|---|
| 84 | |
|---|
| 85 | rootNode->addChild(geode); |
|---|
| 86 | |
|---|
| 87 | xOffset+=90; |
|---|
| 88 | yOffset+=120; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | osgText::PixmapFont* pixmapFont= new osgText::PixmapFont(ttfPath, |
|---|
| 93 | gFontSize1); |
|---|
| 94 | text= new osgText::Text(pixmapFont); |
|---|
| 95 | gTextList.push_back(text); |
|---|
| 96 | text->setText(std::string("2d ")+std::string(TEXT_PIXMAP)); |
|---|
| 97 | text->setPosition(osg::Vec3(xOffset,yOffset,0)); |
|---|
| 98 | text->setDrawMode( osgText::Text::TEXT | |
|---|
| 99 | osgText::Text::BOUNDINGBOX | |
|---|
| 100 | osgText::Text::ALIGNMENT ); |
|---|
| 101 | text->setAlignment(gAlignment); |
|---|
| 102 | geode = new osg::Geode(); |
|---|
| 103 | geode->setName("PixmapFont"); |
|---|
| 104 | geode->addDrawable( text ); |
|---|
| 105 | |
|---|
| 106 | textMaterial = new osg::Material(); |
|---|
| 107 | textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE); |
|---|
| 108 | textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK,TEXT_COL_2D); |
|---|
| 109 | |
|---|
| 110 | osg::BlendFunc *transp= new osg::BlendFunc(); |
|---|
| 111 | transp->setFunction(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | textState = new osg::StateSet(); |
|---|
| 117 | textState->setAttribute(textMaterial ); |
|---|
| 118 | textState->setAttribute(transp); |
|---|
| 119 | textState->setMode(GL_BLEND,osg::StateAttribute::ON); |
|---|
| 120 | textState->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | geode->setStateSet( textState ); |
|---|
| 124 | |
|---|
| 125 | rootNode->addChild(geode); |
|---|
| 126 | |
|---|
| 127 | xOffset+=90; |
|---|
| 128 | yOffset+=120; |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | osgText::TextureFont* textureFont= new osgText::TextureFont(ttfPath1, |
|---|
| 133 | gFontSize1); |
|---|
| 134 | text= new osgText::Text(textureFont); |
|---|
| 135 | gTextList.push_back(text); |
|---|
| 136 | text->setText(std::string("2d ")+std::string(TEXT_TEXTURE)); |
|---|
| 137 | text->setPosition(osg::Vec3(xOffset,yOffset,0)); |
|---|
| 138 | text->setDrawMode( osgText::Text::TEXT | |
|---|
| 139 | osgText::Text::BOUNDINGBOX | |
|---|
| 140 | osgText::Text::ALIGNMENT ); |
|---|
| 141 | text->setAlignment(gAlignment); |
|---|
| 142 | geode = new osg::Geode(); |
|---|
| 143 | geode->setName("TextureFont"); |
|---|
| 144 | geode->addDrawable( text ); |
|---|
| 145 | |
|---|
| 146 | textMaterial = new osg::Material(); |
|---|
| 147 | textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE); |
|---|
| 148 | textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, TEXT_COL_2D); |
|---|
| 149 | |
|---|
| 150 | transp= new osg::BlendFunc(); |
|---|
| 151 | transp->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|---|
| 152 | |
|---|
| 153 | textState = new osg::StateSet(); |
|---|
| 154 | textState->setAttribute(textMaterial ); |
|---|
| 155 | textState->setAttribute(transp); |
|---|
| 156 | |
|---|
| 157 | textState->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::ON); |
|---|
| 158 | textState->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 159 | geode->setStateSet( textState ); |
|---|
| 160 | |
|---|
| 161 | rootNode->addChild(geode); |
|---|
| 162 | |
|---|
| 163 | xOffset+=90; |
|---|
| 164 | yOffset+=120; |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | osgText::PolygonFont* polygonFont= new osgText::PolygonFont(ttfPath, |
|---|
| 169 | gFontSize1, |
|---|
| 170 | 3); |
|---|
| 171 | text= new osgText::Text(polygonFont); |
|---|
| 172 | gTextList.push_back(text); |
|---|
| 173 | text->setText(std::string("2d ")+std::string("TEXT_POLYGON")); |
|---|
| 174 | text->setPosition(osg::Vec3(xOffset,yOffset,0)); |
|---|
| 175 | text->setDrawMode( osgText::Text::TEXT | |
|---|
| 176 | osgText::Text::BOUNDINGBOX | |
|---|
| 177 | osgText::Text::ALIGNMENT ); |
|---|
| 178 | text->setAlignment(gAlignment); |
|---|
| 179 | geode = new osg::Geode(); |
|---|
| 180 | geode->setName("PolygonFont"); |
|---|
| 181 | geode->addDrawable( text ); |
|---|
| 182 | |
|---|
| 183 | textMaterial = new osg::Material(); |
|---|
| 184 | textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE); |
|---|
| 185 | textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, TEXT_COL_2D); |
|---|
| 186 | textState = new osg::StateSet(); |
|---|
| 187 | textState->setAttribute(textMaterial ); |
|---|
| 188 | geode->setStateSet( textState ); |
|---|
| 189 | |
|---|
| 190 | rootNode->addChild(geode); |
|---|
| 191 | |
|---|
| 192 | xOffset+=90; |
|---|
| 193 | yOffset+=120; |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | osgText::OutlineFont* outlineFont= new osgText::OutlineFont(ttfPath, |
|---|
| 198 | gFontSize1, |
|---|
| 199 | 3); |
|---|
| 200 | |
|---|
| 201 | text= new osgText::Text(outlineFont); |
|---|
| 202 | gTextList.push_back(text); |
|---|
| 203 | text->setText(std::string("2d ")+std::string(TEXT_OUTLINE)); |
|---|
| 204 | text->setPosition(osg::Vec3(xOffset,yOffset,0)); |
|---|
| 205 | text->setDrawMode( osgText::Text::TEXT | |
|---|
| 206 | osgText::Text::BOUNDINGBOX | |
|---|
| 207 | osgText::Text::ALIGNMENT ); |
|---|
| 208 | text->setAlignment(gAlignment); |
|---|
| 209 | geode = new osg::Geode(); |
|---|
| 210 | geode->setName("OutlineFont"); |
|---|
| 211 | geode->addDrawable( text ); |
|---|
| 212 | |
|---|
| 213 | textMaterial = new osg::Material(); |
|---|
| 214 | textMaterial->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE); |
|---|
| 215 | textMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, TEXT_COL_2D); |
|---|
| 216 | textState = new osg::StateSet(); |
|---|
| 217 | textState->setAttribute(textMaterial ); |
|---|
| 218 | geode->setStateSet( textState ); |
|---|
| 219 | |
|---|
| 220 | rootNode->addChild(geode); |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | osg::Depth* depth = new osg::Depth; |
|---|
| 225 | depth->setRange(0.0,0.0); |
|---|
| 226 | |
|---|
| 227 | osg::StateSet* rootState = new osg::StateSet(); |
|---|
| 228 | rootState->setAttribute(depth); |
|---|
| 229 | rootState->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 230 | |
|---|
| 231 | rootNode->setStateSet(rootState); |
|---|
| 232 | |
|---|
| 233 | return rootNode; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | int main( int argc, char **argv ) |
|---|
| 238 | { |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] [filename] ..."); |
|---|
| 245 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | osgProducer::Viewer viewer(arguments); |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 259 | { |
|---|
| 260 | arguments.getApplicationUsage()->write(cout); |
|---|
| 261 | return 1; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | if (arguments.errors()) |
|---|
| 269 | { |
|---|
| 270 | arguments.writeErrorMessages(cout); |
|---|
| 271 | return 1; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | osg::ref_ptr<osg::Node> rootNode = osgDB::readNodeFiles(arguments); |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | { |
|---|
| 280 | |
|---|
| 281 | if (rootNode.valid()) |
|---|
| 282 | { |
|---|
| 283 | |
|---|
| 284 | osgUtil::Optimizer optimizer; |
|---|
| 285 | optimizer.optimize(rootNode.get()); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | osg::Group* group = dynamic_cast<osg::Group*>(rootNode.get()); |
|---|
| 290 | if (!group) |
|---|
| 291 | { |
|---|
| 292 | group = new osg::Group; |
|---|
| 293 | |
|---|
| 294 | if (rootNode.valid()) group->addChild(rootNode.get()); |
|---|
| 295 | |
|---|
| 296 | rootNode = group; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | osg::Projection* projection = new osg::Projection; |
|---|
| 301 | projection->setMatrix(osg::Matrix::ortho2D(0,1024,0,768)); |
|---|
| 302 | |
|---|
| 303 | osg::MatrixTransform* modelview_abs = new osg::MatrixTransform; |
|---|
| 304 | modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE); |
|---|
| 305 | modelview_abs->setMatrix(osg::Matrix::identity()); |
|---|
| 306 | |
|---|
| 307 | modelview_abs->addChild(createText()); |
|---|
| 308 | |
|---|
| 309 | projection->addChild(modelview_abs); |
|---|
| 310 | |
|---|
| 311 | group->addChild(projection); |
|---|
| 312 | |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | viewer.setSceneData(rootNode.get()); |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | viewer.realize(Producer::CameraGroup::ThreadPerCamera); |
|---|
| 320 | |
|---|
| 321 | while( !viewer.done() ) |
|---|
| 322 | { |
|---|
| 323 | |
|---|
| 324 | viewer.sync(); |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | viewer.update(); |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | viewer.frame(); |
|---|
| 332 | |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | return 0; |
|---|
| 336 | } |
|---|