| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osgUtil/Optimizer> |
|---|
| 20 | |
|---|
| 21 | #include <osgDB/ReadFile> |
|---|
| 22 | #include <osgDB/WriteFile> |
|---|
| 23 | #include <osgDB/Registry> |
|---|
| 24 | |
|---|
| 25 | #include <osgViewer/Viewer> |
|---|
| 26 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 27 | |
|---|
| 28 | #include <osg/Geode> |
|---|
| 29 | #include <osg/Camera> |
|---|
| 30 | #include <osg/ShapeDrawable> |
|---|
| 31 | #include <osg/Sequence> |
|---|
| 32 | #include <osg/PolygonMode> |
|---|
| 33 | #include <osg/io_utils> |
|---|
| 34 | |
|---|
| 35 | #include <osgText/Font> |
|---|
| 36 | #include <osgText/Text> |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | class UpdateTextOperation : public osg::Operation |
|---|
| 45 | { |
|---|
| 46 | public: |
|---|
| 47 | |
|---|
| 48 | UpdateTextOperation(osg::Group* group): |
|---|
| 49 | Operation("UpdateTextOperation", true), |
|---|
| 50 | _group(group), |
|---|
| 51 | _maxNumChildren(200), |
|---|
| 52 | _maxNumTextPerGeode(10) |
|---|
| 53 | { |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | virtual void operator () (osg::Object* callingObject) |
|---|
| 57 | { |
|---|
| 58 | |
|---|
| 59 | osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(callingObject); |
|---|
| 60 | |
|---|
| 61 | if (viewer) update(); |
|---|
| 62 | else load(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | void update() |
|---|
| 66 | { |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 70 | |
|---|
| 71 | if (_mergeSubgraph.valid()) |
|---|
| 72 | { |
|---|
| 73 | _group->addChild(_mergeSubgraph.get()); |
|---|
| 74 | |
|---|
| 75 | _mergeSubgraph = 0; |
|---|
| 76 | |
|---|
| 77 | if (_group->getNumChildren()>_maxNumChildren) |
|---|
| 78 | { |
|---|
| 79 | osg::Geode* geode = dynamic_cast<osg::Geode*>(_group->getChild(0)); |
|---|
| 80 | if (geode) |
|---|
| 81 | { |
|---|
| 82 | _availableSubgraph.push_back(geode); |
|---|
| 83 | geode->removeDrawables(0,geode->getNumDrawables()); |
|---|
| 84 | } |
|---|
| 85 | _group->removeChild(0,1); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | _waitOnMergeBlock.release(); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void load() |
|---|
| 93 | { |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | osg::ref_ptr<osg::Geode> geode; |
|---|
| 98 | { |
|---|
| 99 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 100 | if (!_availableSubgraph.empty()) |
|---|
| 101 | { |
|---|
| 102 | geode = _availableSubgraph.front(); |
|---|
| 103 | _availableSubgraph.pop_front(); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | if (!geode) geode = new osg::Geode; |
|---|
| 108 | |
|---|
| 109 | for(unsigned int i=0; i<_maxNumTextPerGeode; ++i) |
|---|
| 110 | { |
|---|
| 111 | osg::Vec3 position(float(rand()) / float(RAND_MAX), float(rand()) / float(RAND_MAX), float(i)/float(_maxNumTextPerGeode)); |
|---|
| 112 | |
|---|
| 113 | std::string str; |
|---|
| 114 | unsigned int _numCharacters = 5; |
|---|
| 115 | for(unsigned int ni=0; ni<_numCharacters;++ni) |
|---|
| 116 | { |
|---|
| 117 | str.push_back(char(32.0 + (float(rand())/float(RAND_MAX))*128.0f)); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | osgText::Text* text = new osgText::Text; |
|---|
| 121 | text->setDataVariance(osg::Object::DYNAMIC); |
|---|
| 122 | text->setPosition(position); |
|---|
| 123 | text->setFont("times.ttf"); |
|---|
| 124 | text->setText(str); |
|---|
| 125 | text->setCharacterSize(0.025f); |
|---|
| 126 | text->setAxisAlignment(osgText::Text::SCREEN); |
|---|
| 127 | |
|---|
| 128 | geode->addDrawable(text); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | { |
|---|
| 133 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 134 | _mergeSubgraph = geode; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | _waitOnMergeBlock.block(); |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | virtual void release() |
|---|
| 144 | { |
|---|
| 145 | _waitOnMergeBlock.release(); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | typedef std::list< osg::ref_ptr<osg::Geode> > AvailableList; |
|---|
| 149 | |
|---|
| 150 | unsigned int _maxNumChildren; |
|---|
| 151 | unsigned int _maxNumTextPerGeode; |
|---|
| 152 | |
|---|
| 153 | OpenThreads::Mutex _mutex; |
|---|
| 154 | osg::ref_ptr<osg::Group> _group; |
|---|
| 155 | osg::ref_ptr<osg::Geode> _mergeSubgraph; |
|---|
| 156 | AvailableList _availableSubgraph; |
|---|
| 157 | OpenThreads::Block _waitOnMergeBlock; |
|---|
| 158 | |
|---|
| 159 | unsigned int _counter; |
|---|
| 160 | |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | int main(int argc, char** argv) |
|---|
| 165 | { |
|---|
| 166 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | osg::Referenced::setThreadSafeReferenceCounting(true); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | osgViewer::Viewer viewer(arguments); |
|---|
| 173 | |
|---|
| 174 | typedef std::list< osg::ref_ptr<osg::OperationThread> > Threads; |
|---|
| 175 | |
|---|
| 176 | Threads operationThreads; |
|---|
| 177 | osg::ref_ptr<UpdateTextOperation> updateOperation; |
|---|
| 178 | |
|---|
| 179 | unsigned int numThreads = 0; |
|---|
| 180 | if (arguments.read("--mt", numThreads) || arguments.read("--mt")) |
|---|
| 181 | { |
|---|
| 182 | |
|---|
| 183 | if (numThreads==0) numThreads = 1; |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | osg::Group* mainGroup = new osg::Group; |
|---|
| 187 | |
|---|
| 188 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 189 | mainGroup->addChild(loadedModel.get()); |
|---|
| 190 | |
|---|
| 191 | for(unsigned int i=0; i<numThreads; ++i) |
|---|
| 192 | { |
|---|
| 193 | osg::Group* textGroup = new osg::Group; |
|---|
| 194 | mainGroup->addChild(textGroup); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | osg::OperationThread* operationThread = new osg::OperationThread; |
|---|
| 198 | |
|---|
| 199 | operationThreads.push_back(operationThread); |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | updateOperation = new UpdateTextOperation(textGroup); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | operationThread->add(updateOperation.get()); |
|---|
| 207 | operationThread->startThread(); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | viewer.addUpdateOperation(updateOperation.get()); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | osg::Geode* geode = new osg::Geode; |
|---|
| 215 | geode->getOrCreateStateSet()->setAttribute(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE)); |
|---|
| 216 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.5f,0.5f,0.5f),1.0))); |
|---|
| 217 | |
|---|
| 218 | mainGroup->addChild(geode); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | viewer.setSceneData(mainGroup); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | #if 0 |
|---|
| 226 | osgDB::writeNodeFile(*viewer.getSceneData(),"text.osg"); |
|---|
| 227 | #endif |
|---|
| 228 | |
|---|
| 229 | viewer.addEventHandler(new osgViewer::StatsHandler()); |
|---|
| 230 | viewer.addEventHandler( new osgViewer::ThreadingHandler ); |
|---|
| 231 | viewer.addEventHandler( new osgViewer::WindowSizeHandler ); |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | viewer.run(); |
|---|
| 235 | |
|---|
| 236 | if (!operationThreads.empty()) |
|---|
| 237 | { |
|---|
| 238 | for(Threads::iterator itr = operationThreads.begin(); |
|---|
| 239 | itr != operationThreads.begin(); |
|---|
| 240 | ++itr) |
|---|
| 241 | { |
|---|
| 242 | (*itr)->cancel(); |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | return 0; |
|---|
| 247 | } |
|---|