| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| [1933] | 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osgUtil/Optimizer> |
|---|
| 20 | #include <osgDB/ReadFile> |
|---|
| [5905] | 21 | #include <osgViewer/Viewer> |
|---|
| [1933] | 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 | |
|---|
| [7968] | 31 | #include <osgDB/WriteFile> |
|---|
| 32 | |
|---|
| [1933] | 33 | #include <osgText/Text> |
|---|
| 34 | |
|---|
| [5905] | 35 | #include <iostream> |
|---|
| 36 | |
|---|
| [4717] | 37 | osg::Node* createLabel(const osg::Vec3& pos, float size, const std::string& label, osgText::Text::AxisAlignment axisAlignment) |
|---|
| [1933] | 38 | { |
|---|
| 39 | osg::Geode* geode = new osg::Geode(); |
|---|
| 40 | |
|---|
| 41 | std::string timesFont("fonts/arial.ttf"); |
|---|
| 42 | |
|---|
| 43 | { |
|---|
| 44 | osgText::Text* text = new osgText::Text; |
|---|
| 45 | geode->addDrawable( text ); |
|---|
| 46 | |
|---|
| 47 | text->setFont(timesFont); |
|---|
| 48 | text->setPosition(pos); |
|---|
| 49 | text->setCharacterSize(size); |
|---|
| [4717] | 50 | text->setAxisAlignment(axisAlignment); |
|---|
| [1933] | 51 | text->setAlignment(osgText::Text::CENTER_CENTER); |
|---|
| 52 | text->setText(label); |
|---|
| 53 | |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | return geode; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | osg::Node* createLabel3(const osg::Vec3& pos, float size, const std::string& label) |
|---|
| 61 | { |
|---|
| 62 | osg::Geode* geode = new osg::Geode(); |
|---|
| 63 | |
|---|
| 64 | std::string timesFont("fonts/arial.ttf"); |
|---|
| 65 | |
|---|
| 66 | { |
|---|
| 67 | osgText::Text* text = new osgText::Text; |
|---|
| 68 | geode->addDrawable( text ); |
|---|
| 69 | |
|---|
| 70 | text->setFont(timesFont); |
|---|
| 71 | text->setPosition(pos); |
|---|
| 72 | text->setFontResolution(40,40); |
|---|
| 73 | text->setCharacterSize(size); |
|---|
| 74 | text->setAlignment(osgText::Text::CENTER_CENTER); |
|---|
| 75 | text->setAutoRotateToScreen(true); |
|---|
| [1982] | 76 | text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT); |
|---|
| [1933] | 77 | text->setText(label); |
|---|
| 78 | |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return geode; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| [4717] | 84 | 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) |
|---|
| [1933] | 85 | { |
|---|
| 86 | osg::Group* group = new osg::Group; |
|---|
| 87 | |
|---|
| 88 | osg::Vec3 dv = e-s; |
|---|
| 89 | dv /= float(numReps-1); |
|---|
| 90 | |
|---|
| 91 | osg::Vec3 pos = s; |
|---|
| 92 | |
|---|
| [3245] | 93 | bool useAuto = true; |
|---|
| [1933] | 94 | if (useAuto) |
|---|
| 95 | { |
|---|
| 96 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 97 | |
|---|
| 98 | for(int i=0;i<numReps;++i) |
|---|
| 99 | { |
|---|
| 100 | osg::AutoTransform* at = new osg::AutoTransform; |
|---|
| 101 | at->setPosition(pos); |
|---|
| [3245] | 102 | at->setAutoRotateMode(autoRotateMode); |
|---|
| [4717] | 103 | at->addChild(createLabel(osg::Vec3(0.0f,0.0f,0.0f),dv.length()*0.2f,str, axisAlignment)); |
|---|
| [1933] | 104 | vertices->push_back(pos); |
|---|
| 105 | pos += dv; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | group->addChild(at); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 112 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 113 | |
|---|
| 114 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 115 | geom->setVertexArray(vertices); |
|---|
| 116 | geom->setColorArray(colors); |
|---|
| [7968] | 117 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| [1933] | 118 | geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size())); |
|---|
| 119 | |
|---|
| 120 | osg::Geode* geode = new osg::Geode; |
|---|
| 121 | geode->addDrawable(geom); |
|---|
| 122 | |
|---|
| 123 | group->addChild(geode); |
|---|
| 124 | } |
|---|
| 125 | else |
|---|
| 126 | { |
|---|
| 127 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 128 | |
|---|
| 129 | for(int i=0;i<numReps;++i) |
|---|
| 130 | { |
|---|
| [3245] | 131 | group->addChild(createLabel3(osg::Vec3(pos),dv.length()*0.5f,str)); |
|---|
| [1933] | 132 | vertices->push_back(pos); |
|---|
| 133 | pos += dv; |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 139 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 140 | |
|---|
| 141 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 142 | geom->setVertexArray(vertices); |
|---|
| 143 | geom->setColorArray(colors); |
|---|
| [7968] | 144 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| [1933] | 145 | geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size())); |
|---|
| 146 | |
|---|
| 147 | osg::Geode* geode = new osg::Geode; |
|---|
| 148 | geode->addDrawable(geom); |
|---|
| 149 | |
|---|
| 150 | group->addChild(geode); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | return group; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| [7967] | 156 | osg::Node* createAutoScale(const osg::Vec3& position, float characterSize, const std::string& message, float minScale=0.0, float maxScale=FLT_MAX) |
|---|
| 157 | { |
|---|
| 158 | std::string timesFont("fonts/arial.ttf"); |
|---|
| 159 | |
|---|
| 160 | osgText::Text* text = new osgText::Text; |
|---|
| 161 | text->setCharacterSize(characterSize); |
|---|
| 162 | text->setText(message); |
|---|
| 163 | text->setFont(timesFont); |
|---|
| 164 | text->setAlignment(osgText::Text::CENTER_CENTER); |
|---|
| 165 | |
|---|
| 166 | osg::Geode* geode = new osg::Geode; |
|---|
| 167 | geode->addDrawable(text); |
|---|
| 168 | geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
|---|
| 169 | |
|---|
| 170 | osg::AutoTransform* at = new osg::AutoTransform; |
|---|
| 171 | at->addChild(geode); |
|---|
| 172 | |
|---|
| 173 | at->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN); |
|---|
| 174 | at->setAutoScaleToScreen(true); |
|---|
| 175 | at->setMinimumScale(minScale); |
|---|
| 176 | at->setMaximumScale(maxScale); |
|---|
| 177 | at->setPosition(position); |
|---|
| 178 | |
|---|
| 179 | return at; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| [1933] | 182 | osg::Node* createScene() |
|---|
| 183 | { |
|---|
| 184 | osg::Group* root = new osg::Group; |
|---|
| 185 | |
|---|
| [3245] | 186 | |
|---|
| 187 | int numReps = 10; |
|---|
| [4717] | 188 | 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")); |
|---|
| 189 | 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")); |
|---|
| 190 | 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")); |
|---|
| [7967] | 191 | |
|---|
| 192 | root->addChild(createAutoScale(osg::Vec3(500.0,500.0,500.0), 25.0, "AutoScale with no min, max limits")); |
|---|
| 193 | root->addChild(createAutoScale(osg::Vec3(500.0,500.0,300.0), 25.0, "AutoScale with minScale = 1, maxScale = 2.0 ", 1, 2.0)); |
|---|
| 194 | root->addChild(createAutoScale(osg::Vec3(500.0,500.0,700.0), 25.0, "AutoScale with minScale = 0.0, maxScale = 5.0 ", 0.0, 5.0)); |
|---|
| [1933] | 195 | return root; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| [5910] | 198 | int main(int, char**) |
|---|
| [1933] | 199 | { |
|---|
| 200 | |
|---|
| [5905] | 201 | osgViewer::Viewer viewer; |
|---|
| [1933] | 202 | |
|---|
| 203 | |
|---|
| 204 | viewer.setSceneData(createScene()); |
|---|
| 205 | |
|---|
| [5910] | 206 | |
|---|
| [5905] | 207 | return viewer.run(); |
|---|
| [1933] | 208 | } |
|---|