| 1 | #include <osgProducer/Viewer> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Transform> |
|---|
| 4 | #include <osg/Billboard> |
|---|
| 5 | #include <osg/Geode> |
|---|
| 6 | #include <osg/Group> |
|---|
| 7 | #include <osg/Notify> |
|---|
| 8 | |
|---|
| 9 | #include <osgDB/Registry> |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | |
|---|
| 12 | #include <osgGA/TrackballManipulator> |
|---|
| 13 | #include <osgGA/FlightManipulator> |
|---|
| 14 | #include <osgGA/DriveManipulator> |
|---|
| 15 | |
|---|
| 16 | #include <osgUtil/Optimizer> |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | class UpdateCallback : public osg::NodeCallback |
|---|
| 20 | { |
|---|
| 21 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 22 | { |
|---|
| 23 | std::cout<<"update callback - pre traverse"<<node<<std::endl; |
|---|
| 24 | traverse(node,nv); |
|---|
| 25 | std::cout<<"update callback - post traverse"<<node<<std::endl; |
|---|
| 26 | } |
|---|
| 27 | }; |
|---|
| 28 | |
|---|
| 29 | class CullCallback : public osg::NodeCallback |
|---|
| 30 | { |
|---|
| 31 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 32 | { |
|---|
| 33 | std::cout<<"cull callback - pre traverse"<<node<<std::endl; |
|---|
| 34 | traverse(node,nv); |
|---|
| 35 | std::cout<<"cull callback - post traverse"<<node<<std::endl; |
|---|
| 36 | } |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | class DrawableDrawCallback : public osg::Drawable::DrawCallback |
|---|
| 40 | { |
|---|
| 41 | virtual void drawImplementation(osg::State& state,const osg::Drawable* drawable) const |
|---|
| 42 | { |
|---|
| 43 | std::cout<<"draw call back - pre drawImplementation"<<drawable<<std::endl; |
|---|
| 44 | drawable->drawImplementation(state); |
|---|
| 45 | std::cout<<"draw call back - post drawImplementation"<<drawable<<std::endl; |
|---|
| 46 | } |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | struct TransformCallback : public osg::Transform::ComputeTransformCallback |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,const osg::Transform* transform, osg::NodeVisitor* nv) const |
|---|
| 53 | { |
|---|
| 54 | std::cout<<"computeLocalToWorldMatrix - pre transform->computeLocalToWorldMatrix"<<std::endl; |
|---|
| 55 | bool result = transform->computeLocalToWorldMatrix(matrix,nv); |
|---|
| 56 | std::cout<<"computeLocalToWorldMatrix - post transform->computeLocalToWorldMatrix"<<std::endl; |
|---|
| 57 | return result; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,const osg::Transform* transform, osg::NodeVisitor* nv) const |
|---|
| 62 | { |
|---|
| 63 | std::cout<<"computeWorldToLocalMatrix - pre transform->computeWorldToLocalMatrix"<<std::endl; |
|---|
| 64 | bool result = transform->computeWorldToLocalMatrix(matrix,nv); |
|---|
| 65 | std::cout<<"computeWorldToLocalMatrix - post transform->computeWorldToLocalMatrix"<<std::endl; |
|---|
| 66 | return result; |
|---|
| 67 | } |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | struct DrawableUpdateCallback : public osg::Drawable::UpdateCallback |
|---|
| 71 | { |
|---|
| 72 | virtual void update(osg::NodeVisitor*, osg::Drawable* drawable) |
|---|
| 73 | { |
|---|
| 74 | std::cout<<"Drawable update callback "<<drawable<<std::endl; |
|---|
| 75 | } |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | struct DrawableCullCallback : public osg::Drawable::CullCallback |
|---|
| 79 | { |
|---|
| 80 | |
|---|
| 81 | virtual bool cull(osg::NodeVisitor*, osg::Drawable* drawable, osg::State* ) const |
|---|
| 82 | { |
|---|
| 83 | std::cout<<"Drawable cull callback "<<drawable<<std::endl; |
|---|
| 84 | return false; |
|---|
| 85 | } |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | class InsertCallbacksVisitor : public osg::NodeVisitor |
|---|
| 89 | { |
|---|
| 90 | |
|---|
| 91 | public: |
|---|
| 92 | |
|---|
| 93 | InsertCallbacksVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 94 | { |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | virtual void apply(osg::Node& node) |
|---|
| 98 | { |
|---|
| 99 | node.setUpdateCallback(new UpdateCallback()); |
|---|
| 100 | node.setCullCallback(new CullCallback()); |
|---|
| 101 | traverse(node); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | virtual void apply(osg::Geode& geode) |
|---|
| 105 | { |
|---|
| 106 | geode.setUpdateCallback(new UpdateCallback()); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
|---|
| 115 | { |
|---|
| 116 | geode.getDrawable(i)->setUpdateCallback(new DrawableUpdateCallback()); |
|---|
| 117 | geode.getDrawable(i)->setCullCallback(new DrawableCullCallback()); |
|---|
| 118 | geode.getDrawable(i)->setDrawCallback(new DrawableDrawCallback()); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | virtual void apply(osg::Transform& node) |
|---|
| 123 | { |
|---|
| 124 | node.setComputeTransformCallback(new TransformCallback()); |
|---|
| 125 | apply((osg::Node&)node); |
|---|
| 126 | } |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | int main( int argc, char **argv ) |
|---|
| 130 | { |
|---|
| 131 | |
|---|
| 132 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ..."); |
|---|
| 136 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | osgProducer::Viewer viewer(arguments); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 149 | { |
|---|
| 150 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 151 | return 1; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | if (arguments.errors()) |
|---|
| 159 | { |
|---|
| 160 | arguments.writeErrorMessages(std::cout); |
|---|
| 161 | return 1; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | osg::Node* rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 166 | if (!rootnode) |
|---|
| 167 | { |
|---|
| 168 | |
|---|
| 169 | return 1; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | osgUtil::Optimizer optimzer; |
|---|
| 174 | optimzer.optimize(rootnode); |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | InsertCallbacksVisitor icv; |
|---|
| 178 | rootnode->accept(icv); |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | viewer.setSceneData(rootnode); |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | viewer.realize(Producer::CameraGroup::ThreadPerCamera); |
|---|
| 185 | |
|---|
| 186 | while( !viewer.done() ) |
|---|
| 187 | { |
|---|
| 188 | |
|---|
| 189 | viewer.sync(); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | viewer.update(); |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | viewer.frame(); |
|---|
| 197 | |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | viewer.sync(); |
|---|
| 202 | |
|---|
| 203 | return 0; |
|---|
| 204 | } |
|---|