| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osgUtil/Optimizer> |
|---|
| 20 | #include <osgDB/ReadFile> |
|---|
| 21 | #include <osgViewer/Viewer> |
|---|
| 22 | |
|---|
| 23 | #include <osg/Material> |
|---|
| 24 | #include <osg/Geode> |
|---|
| 25 | #include <osg/BlendFunc> |
|---|
| 26 | #include <osg/Depth> |
|---|
| 27 | #include <osg/Projection> |
|---|
| 28 | #include <osg/AutoTransform> |
|---|
| 29 | #include <osg/Geometry> |
|---|
| 30 | |
|---|
| 31 | #include <osgText/Text> |
|---|
| 32 | |
|---|
| 33 | #include <iostream> |
|---|
| 34 | |
|---|
| 35 | osg::Node* createLabel(const osg::Vec3& pos, float size, const std::string& label, osgText::Text::AxisAlignment axisAlignment) |
|---|
| 36 | { |
|---|
| 37 | osg::Geode* geode = new osg::Geode(); |
|---|
| 38 | |
|---|
| 39 | std::string timesFont("fonts/arial.ttf"); |
|---|
| 40 | |
|---|
| 41 | { |
|---|
| 42 | osgText::Text* text = new osgText::Text; |
|---|
| 43 | geode->addDrawable( text ); |
|---|
| 44 | |
|---|
| 45 | text->setFont(timesFont); |
|---|
| 46 | text->setPosition(pos); |
|---|
| 47 | text->setCharacterSize(size); |
|---|
| 48 | text->setAxisAlignment(axisAlignment); |
|---|
| 49 | text->setAlignment(osgText::Text::CENTER_CENTER); |
|---|
| 50 | text->setText(label); |
|---|
| 51 | |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | return geode; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | osg::Node* createLabel3(const osg::Vec3& pos, float size, const std::string& label) |
|---|
| 59 | { |
|---|
| 60 | osg::Geode* geode = new osg::Geode(); |
|---|
| 61 | |
|---|
| 62 | std::string timesFont("fonts/arial.ttf"); |
|---|
| 63 | |
|---|
| 64 | { |
|---|
| 65 | osgText::Text* text = new osgText::Text; |
|---|
| 66 | geode->addDrawable( text ); |
|---|
| 67 | |
|---|
| 68 | text->setFont(timesFont); |
|---|
| 69 | text->setPosition(pos); |
|---|
| 70 | text->setFontResolution(40,40); |
|---|
| 71 | text->setCharacterSize(size); |
|---|
| 72 | text->setAlignment(osgText::Text::CENTER_CENTER); |
|---|
| 73 | text->setAutoRotateToScreen(true); |
|---|
| 74 | text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT); |
|---|
| 75 | text->setText(label); |
|---|
| 76 | |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | return geode; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | osg::Node* createAxis(const osg::Vec3& s, const osg::Vec3& e, int numReps, osg::AutoTransform::AutoRotateMode autoRotateMode, osgText::Text::AxisAlignment axisAlignment, const std::string& str) |
|---|
| 83 | { |
|---|
| 84 | osg::Group* group = new osg::Group; |
|---|
| 85 | |
|---|
| 86 | osg::Vec3 dv = e-s; |
|---|
| 87 | dv /= float(numReps-1); |
|---|
| 88 | |
|---|
| 89 | osg::Vec3 pos = s; |
|---|
| 90 | |
|---|
| 91 | bool useAuto = true; |
|---|
| 92 | if (useAuto) |
|---|
| 93 | { |
|---|
| 94 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 95 | |
|---|
| 96 | for(int i=0;i<numReps;++i) |
|---|
| 97 | { |
|---|
| 98 | osg::AutoTransform* at = new osg::AutoTransform; |
|---|
| 99 | at->setPosition(pos); |
|---|
| 100 | at->setAutoRotateMode(autoRotateMode); |
|---|
| 101 | at->addChild(createLabel(osg::Vec3(0.0f,0.0f,0.0f),dv.length()*0.2f,str, axisAlignment)); |
|---|
| 102 | vertices->push_back(pos); |
|---|
| 103 | pos += dv; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | group->addChild(at); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 110 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 111 | |
|---|
| 112 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 113 | geom->setVertexArray(vertices); |
|---|
| 114 | geom->setColorArray(colors); |
|---|
| 115 | geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size())); |
|---|
| 116 | |
|---|
| 117 | osg::Geode* geode = new osg::Geode; |
|---|
| 118 | geode->addDrawable(geom); |
|---|
| 119 | |
|---|
| 120 | group->addChild(geode); |
|---|
| 121 | } |
|---|
| 122 | else |
|---|
| 123 | { |
|---|
| 124 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 125 | |
|---|
| 126 | for(int i=0;i<numReps;++i) |
|---|
| 127 | { |
|---|
| 128 | group->addChild(createLabel3(osg::Vec3(pos),dv.length()*0.5f,str)); |
|---|
| 129 | vertices->push_back(pos); |
|---|
| 130 | pos += dv; |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 136 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 137 | |
|---|
| 138 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 139 | geom->setVertexArray(vertices); |
|---|
| 140 | geom->setColorArray(colors); |
|---|
| 141 | geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size())); |
|---|
| 142 | |
|---|
| 143 | osg::Geode* geode = new osg::Geode; |
|---|
| 144 | geode->addDrawable(geom); |
|---|
| 145 | |
|---|
| 146 | group->addChild(geode); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | return group; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | osg::Node* createScene() |
|---|
| 153 | { |
|---|
| 154 | osg::Group* root = new osg::Group; |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | int numReps = 10; |
|---|
| 158 | root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(1000.0,0.0,0.0),numReps,osg::AutoTransform::ROTATE_TO_CAMERA,osgText::Text::XY_PLANE, "ROTATE_TO_CAMERA")); |
|---|
| 159 | root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,1000.0,0.0),numReps,osg::AutoTransform::ROTATE_TO_SCREEN,osgText::Text::XY_PLANE, "ROTATE_TO_SCREEN")); |
|---|
| 160 | root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,0.0,1000.0),numReps,osg::AutoTransform::NO_ROTATION,osgText::Text::XZ_PLANE, "NO_ROTATION")); |
|---|
| 161 | |
|---|
| 162 | return root; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | int main(int, char**) |
|---|
| 166 | { |
|---|
| 167 | |
|---|
| 168 | osgViewer::Viewer viewer; |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | viewer.setSceneData(createScene()); |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | return viewer.run(); |
|---|
| 175 | } |
|---|