| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | #include <stdio.h> |
|---|
| 10 | |
|---|
| 11 | #include <Producer/Camera> |
|---|
| 12 | #include <Producer/CameraConfig> |
|---|
| 13 | #include <Producer/InputArea> |
|---|
| 14 | #include <Producer/KeyboardMouse> |
|---|
| 15 | #include <Producer/Trackball> |
|---|
| 16 | |
|---|
| 17 | #include <osg/Timer> |
|---|
| 18 | |
|---|
| 19 | #include <osgUtil/UpdateVisitor> |
|---|
| 20 | |
|---|
| 21 | #include <osgDB/ReadFile> |
|---|
| 22 | |
|---|
| 23 | #include <osg/ShapeDrawable> |
|---|
| 24 | #include <osg/PositionAttitudeTransform> |
|---|
| 25 | |
|---|
| 26 | #include <osgProducer/OsgCameraGroup> |
|---|
| 27 | #include <osgProducer/OsgSceneHandler> |
|---|
| 28 | |
|---|
| 29 | #include "DepthPartitionNode.h" |
|---|
| 30 | |
|---|
| 31 | class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | |
|---|
| 35 | MyKeyboardMouseCallback() : |
|---|
| 36 | Producer::KeyboardMouseCallback(), |
|---|
| 37 | _mx(0.0f),_my(0.0f),_mbutton(0), |
|---|
| 38 | _done(false) |
|---|
| 39 | {} |
|---|
| 40 | |
|---|
| 41 | virtual void specialKeyPress( Producer::KeyCharacter key ) |
|---|
| 42 | { |
|---|
| 43 | if (key==Producer::KeyChar_Escape) |
|---|
| 44 | shutdown(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | virtual void shutdown() |
|---|
| 48 | { |
|---|
| 49 | _done = true; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | virtual void keyPress( Producer::KeyCharacter ) |
|---|
| 53 | { |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | virtual void mouseMotion( float mx, float my ) |
|---|
| 57 | { |
|---|
| 58 | _mx = mx; |
|---|
| 59 | _my = my; |
|---|
| 60 | } |
|---|
| 61 | virtual void buttonPress( float mx, float my, unsigned int mbutton ) |
|---|
| 62 | { |
|---|
| 63 | _mx = mx; |
|---|
| 64 | _my = my; |
|---|
| 65 | _mbutton |= (1<<(mbutton-1)); |
|---|
| 66 | } |
|---|
| 67 | virtual void buttonRelease( float mx, float my, unsigned int mbutton ) |
|---|
| 68 | { |
|---|
| 69 | _mx = mx; |
|---|
| 70 | _my = my; |
|---|
| 71 | _mbutton &= ~(1<<(mbutton-1)); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | bool done() { return _done; } |
|---|
| 75 | float mx() { return _mx; } |
|---|
| 76 | float my() { return _my; } |
|---|
| 77 | unsigned int mbutton() { return _mbutton; } |
|---|
| 78 | |
|---|
| 79 | private: |
|---|
| 80 | |
|---|
| 81 | float _mx, _my; |
|---|
| 82 | unsigned int _mbutton; |
|---|
| 83 | bool _done; |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | const double r_earth = 6378.137; |
|---|
| 87 | const double r_sun = 695990.0; |
|---|
| 88 | const double AU = 149697900.0; |
|---|
| 89 | |
|---|
| 90 | osg::Node* createScene() |
|---|
| 91 | { |
|---|
| 92 | |
|---|
| 93 | osg::ShapeDrawable *earth_sd = new osg::ShapeDrawable; |
|---|
| 94 | osg::Sphere* earth_sphere = new osg::Sphere; |
|---|
| 95 | earth_sphere->setRadius(r_earth); |
|---|
| 96 | earth_sd->setShape(earth_sphere); |
|---|
| 97 | earth_sd->setColor(osg::Vec4(0, 0, 1.0, 1.0)); |
|---|
| 98 | |
|---|
| 99 | osg::Geode* earth = new osg::Geode; |
|---|
| 100 | earth->setName("earth"); |
|---|
| 101 | earth->addDrawable(earth_sd); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | osg::ShapeDrawable *sun_sd = new osg::ShapeDrawable; |
|---|
| 105 | osg::Sphere* sun_sphere = new osg::Sphere; |
|---|
| 106 | sun_sphere->setRadius(r_sun); |
|---|
| 107 | sun_sd->setShape(sun_sphere); |
|---|
| 108 | sun_sd->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0)); |
|---|
| 109 | |
|---|
| 110 | osg::Geode* sun = new osg::Geode; |
|---|
| 111 | sun->setName("sun"); |
|---|
| 112 | sun->addDrawable(sun_sd); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | osg::PositionAttitudeTransform *pat = new osg::PositionAttitudeTransform; |
|---|
| 116 | pat->setPosition(osg::Vec3d(0.0, AU, 0.0)); |
|---|
| 117 | |
|---|
| 118 | osg::Group* scene = new osg::Group; |
|---|
| 119 | scene->addChild(earth); |
|---|
| 120 | scene->addChild(pat); |
|---|
| 121 | pat->addChild(sun); |
|---|
| 122 | |
|---|
| 123 | return scene; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | int main( int argc, char **argv ) |
|---|
| 127 | { |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 134 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard example of using osgProducer::CameraGroup."); |
|---|
| 135 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] ..."); |
|---|
| 136 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | osgProducer::OsgCameraGroup cg(arguments); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 143 | { |
|---|
| 144 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 145 | return 1; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | if (arguments.errors()) |
|---|
| 153 | { |
|---|
| 154 | arguments.writeErrorMessages(std::cout); |
|---|
| 155 | return 1; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | Producer::InputArea *ia = cg.getCameraConfig()->getInputArea(); |
|---|
| 160 | Producer::KeyboardMouse *kbm = ia ? |
|---|
| 161 | (new Producer::KeyboardMouse(ia)) : |
|---|
| 162 | (new Producer::KeyboardMouse(cg.getCamera(0)->getRenderSurface())); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback; |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | kbm->setCallback( kbmcb.get() ); |
|---|
| 169 | kbm->startThread(); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | DepthPartitionNode *dpn = new DepthPartitionNode; |
|---|
| 173 | osg::Node* scene = createScene(); |
|---|
| 174 | dpn->addChild(scene); |
|---|
| 175 | dpn->setActive(true); |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | cg.setSceneData(dpn); |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | osg::BoundingSphere bs; |
|---|
| 182 | bs._radius = r_earth; |
|---|
| 183 | |
|---|
| 184 | osg::ref_ptr<Producer::Trackball> tb = new Producer::Trackball; |
|---|
| 185 | tb->setOrientation( Producer::Trackball::Z_UP ); |
|---|
| 186 | tb->setDistance(bs.radius()*10.0f); |
|---|
| 187 | tb->translate(-bs.center().x(),-bs.center().y(),-bs.center().z()); |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | cg.realize(); |
|---|
| 191 | |
|---|
| 192 | osgUtil::UpdateVisitor update; |
|---|
| 193 | |
|---|
| 194 | while( !kbmcb->done() ) |
|---|
| 195 | { |
|---|
| 196 | |
|---|
| 197 | cg.sync(); |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | cg.getSceneData()->accept(update); |
|---|
| 202 | |
|---|
| 203 | tb->input( kbmcb->mx(), kbmcb->my(), kbmcb->mbutton() ); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | cg.setViewByMatrix(tb->getMatrix()); |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | cg.frame(); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | cg.sync(); |
|---|
| 214 | |
|---|
| 215 | return 0; |
|---|
| 216 | } |
|---|