| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include <osgViewer/Viewer> |
|---|
| 21 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 22 | #include <osgDB/ReadFile> |
|---|
| 23 | |
|---|
| 24 | #include <osgGA/TrackballManipulator> |
|---|
| 25 | #include <osgGA/StateSetManipulator> |
|---|
| 26 | |
|---|
| 27 | #include <osg/Geode> |
|---|
| 28 | #include <osg/Geometry> |
|---|
| 29 | #include <osg/Material> |
|---|
| 30 | #include <osg/Shape> |
|---|
| 31 | #include <osg/ShapeDrawable> |
|---|
| 32 | #include <osgText/Text3D> |
|---|
| 33 | |
|---|
| 34 | #include <iostream> |
|---|
| 35 | #include <sstream> |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | osg::Group* create3DText(const osg::Vec3& center,float radius) |
|---|
| 41 | { |
|---|
| 42 | |
|---|
| 43 | osg::Geode* geode = new osg::Geode; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | float characterSize=radius*0.2f; |
|---|
| 51 | float characterDepth=characterSize*0.2f; |
|---|
| 52 | |
|---|
| 53 | osg::Vec3 pos(center.x()-radius*.5f,center.y()-radius*.5f,center.z()-radius*.5f); |
|---|
| 54 | |
|---|
| 55 | osgText::Text3D* text1 = new osgText::Text3D; |
|---|
| 56 | text1->setFont("fonts/arial.ttf"); |
|---|
| 57 | text1->setCharacterSize(characterSize); |
|---|
| 58 | text1->setCharacterDepth(characterDepth); |
|---|
| 59 | text1->setPosition(pos); |
|---|
| 60 | text1->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX); |
|---|
| 61 | text1->setAxisAlignment(osgText::Text3D::XY_PLANE); |
|---|
| 62 | text1->setText("XY_PLANE"); |
|---|
| 63 | geode->addDrawable(text1); |
|---|
| 64 | |
|---|
| 65 | osgText::Text3D* text2 = new osgText::Text3D; |
|---|
| 66 | text2->setFont("fonts/times.ttf"); |
|---|
| 67 | text2->setCharacterSize(characterSize); |
|---|
| 68 | text2->setCharacterDepth(characterDepth); |
|---|
| 69 | text2->setPosition(pos); |
|---|
| 70 | text2->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX); |
|---|
| 71 | text2->setAxisAlignment(osgText::Text3D::YZ_PLANE); |
|---|
| 72 | text2->setText("YZ_PLANE"); |
|---|
| 73 | geode->addDrawable(text2); |
|---|
| 74 | |
|---|
| 75 | osgText::Text3D* text3 = new osgText::Text3D; |
|---|
| 76 | text3->setFont("fonts/dirtydoz.ttf"); |
|---|
| 77 | text3->setCharacterSize(characterSize); |
|---|
| 78 | text3->setCharacterDepth(characterDepth); |
|---|
| 79 | text3->setPosition(pos); |
|---|
| 80 | text3->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX); |
|---|
| 81 | text3->setAxisAlignment(osgText::Text3D::XZ_PLANE); |
|---|
| 82 | text3->setText("XZ_PLANE"); |
|---|
| 83 | geode->addDrawable(text3); |
|---|
| 84 | |
|---|
| 85 | osgText::Text3D* text7 = new osgText::Text3D; |
|---|
| 86 | text7->setFont("fonts/times.ttf"); |
|---|
| 87 | text7->setCharacterSize(characterSize); |
|---|
| 88 | text7->setCharacterDepth(characterSize*0.2f); |
|---|
| 89 | text7->setPosition(center - osg::Vec3(0.0, 0.0, 0.6)); |
|---|
| 90 | text7->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX); |
|---|
| 91 | text7->setAxisAlignment(osgText::Text3D::SCREEN); |
|---|
| 92 | text7->setCharacterSizeMode(osgText::Text3D::OBJECT_COORDS); |
|---|
| 93 | text7->setText("CharacterSizeMode OBJECT_COORDS (default)"); |
|---|
| 94 | geode->addDrawable(text7); |
|---|
| 95 | |
|---|
| 96 | osg::ShapeDrawable* shape = new osg::ShapeDrawable(new osg::Sphere(center,characterSize*0.2f)); |
|---|
| 97 | shape->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON); |
|---|
| 98 | geode->addDrawable(shape); |
|---|
| 99 | |
|---|
| 100 | osg::Group* rootNode = new osg::Group; |
|---|
| 101 | rootNode->addChild(geode); |
|---|
| 102 | |
|---|
| 103 | osg::Material* front = new osg::Material; |
|---|
| 104 | front->setAlpha(osg::Material::FRONT_AND_BACK,1); |
|---|
| 105 | front->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(0.2,0.2,0.2,1.0)); |
|---|
| 106 | front->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(.0,.0,1.0,1.0)); |
|---|
| 107 | rootNode->getOrCreateStateSet()->setAttributeAndModes(front); |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | return rootNode; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | int main(int, char**) |
|---|
| 114 | { |
|---|
| 115 | osgViewer::Viewer viewer; |
|---|
| 116 | |
|---|
| 117 | osg::Vec3 center(0.0f,0.0f,0.0f); |
|---|
| 118 | float radius = 1.0f; |
|---|
| 119 | |
|---|
| 120 | osg::Group* root = new osg::Group; |
|---|
| 121 | root->addChild(create3DText(center, radius)); |
|---|
| 122 | |
|---|
| 123 | viewer.setSceneData(root); |
|---|
| 124 | viewer.setCameraManipulator(new osgGA::TrackballManipulator()); |
|---|
| 125 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 126 | |
|---|
| 127 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 128 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 129 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | viewer.run(); |
|---|
| 133 | |
|---|
| 134 | return 0; |
|---|
| 135 | } |
|---|
| 136 | |
|---|