| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include <Producer/RenderSurface> |
|---|
| 7 | #include <Producer/KeyboardMouse> |
|---|
| 8 | #include <Producer/Trackball> |
|---|
| 9 | |
|---|
| 10 | #include <osg/Timer> |
|---|
| 11 | #include <osg/io_utils> |
|---|
| 12 | |
|---|
| 13 | #include <osgUtil/SceneView> |
|---|
| 14 | #include <osgUtil/IntersectVisitor> |
|---|
| 15 | |
|---|
| 16 | #include <osgDB/ReadFile> |
|---|
| 17 | |
|---|
| 18 | #include <osgFX/Scribe> |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback |
|---|
| 22 | { |
|---|
| 23 | public: |
|---|
| 24 | |
|---|
| 25 | MyKeyboardMouseCallback(osgUtil::SceneView* sceneView) : |
|---|
| 26 | Producer::KeyboardMouseCallback(), |
|---|
| 27 | _mx(0.0f),_my(0.0f),_mbutton(0), |
|---|
| 28 | _done(false), |
|---|
| 29 | _trackBall(new Producer::Trackball), |
|---|
| 30 | _sceneView(sceneView) |
|---|
| 31 | { |
|---|
| 32 | resetTrackball(); |
|---|
| 33 | _mouseMovingOnPreviousRelease = false; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | virtual void specialKeyPress( Producer::KeyCharacter key ) |
|---|
| 37 | { |
|---|
| 38 | if (key==Producer::KeyChar_Escape) |
|---|
| 39 | shutdown(); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | virtual void shutdown() |
|---|
| 43 | { |
|---|
| 44 | _done = true; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | virtual void keyPress( Producer::KeyCharacter key) |
|---|
| 48 | { |
|---|
| 49 | if (key==' ') resetTrackball(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | virtual void mouseMotion( float mx, float my ) |
|---|
| 53 | { |
|---|
| 54 | _mx = mx; |
|---|
| 55 | _my = my; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | virtual void buttonPress( float mx, float my, unsigned int mbutton ) |
|---|
| 59 | { |
|---|
| 60 | _mx = mx; |
|---|
| 61 | _my = my; |
|---|
| 62 | _mbutton |= (1<<(mbutton-1)); |
|---|
| 63 | |
|---|
| 64 | _mx_buttonPress = _mx; |
|---|
| 65 | _my_buttonPress = _my; |
|---|
| 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 | if (_mx==_mx_buttonPress && _my_buttonPress==_my) |
|---|
| 74 | { |
|---|
| 75 | if (!_mouseMovingOnPreviousRelease) |
|---|
| 76 | { |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | pick(_mx,_my); |
|---|
| 80 | } |
|---|
| 81 | _mouseMovingOnPreviousRelease = false; |
|---|
| 82 | } |
|---|
| 83 | else |
|---|
| 84 | { |
|---|
| 85 | _mouseMovingOnPreviousRelease = true; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | bool done() { return _done; } |
|---|
| 90 | float mx() { return _mx; } |
|---|
| 91 | float my() { return _my; } |
|---|
| 92 | unsigned int mbutton() { return _mbutton; } |
|---|
| 93 | |
|---|
| 94 | void resetTrackball() |
|---|
| 95 | { |
|---|
| 96 | osg::Node* scene = _sceneView->getSceneData(); |
|---|
| 97 | if (scene) |
|---|
| 98 | { |
|---|
| 99 | const osg::BoundingSphere& bs = scene->getBound(); |
|---|
| 100 | if (bs.valid()) |
|---|
| 101 | { |
|---|
| 102 | _trackBall->reset(); |
|---|
| 103 | _trackBall->setOrientation( Producer::Trackball::Z_UP ); |
|---|
| 104 | _trackBall->setDistance(bs.radius()*2.0f); |
|---|
| 105 | _trackBall->translate(-bs.center().x(),-bs.center().y(),-bs.center().z()); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | osg::Matrixd getViewMatrix() |
|---|
| 111 | { |
|---|
| 112 | _trackBall->input( mx(), my(), mbutton() ); |
|---|
| 113 | return osg::Matrixd(_trackBall->getMatrix().ptr()); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | void pick(float x, float y) |
|---|
| 117 | { |
|---|
| 118 | osg::Node* scene = _sceneView->getSceneData(); |
|---|
| 119 | if (scene) |
|---|
| 120 | { |
|---|
| 121 | std::cout<<"Picking "<<x<<"\t"<<y<<std::endl; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | int origX, origY, width, height; |
|---|
| 125 | _sceneView->getViewport(origX,origY,width,height); |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | int winX = (int)((x+1.0f)*0.5f*(float)width); |
|---|
| 129 | int winY = (int)((y+1.0f)*0.5f*(float)height); |
|---|
| 130 | |
|---|
| 131 | osg::Vec3 nearPoint, farPoint; |
|---|
| 132 | _sceneView->projectWindowXYIntoObject(winX,winY,nearPoint,farPoint); |
|---|
| 133 | |
|---|
| 134 | std::cout<<"nearPoint "<<nearPoint<<" farPoint "<<farPoint<<std::endl; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | osg::ref_ptr<osg::LineSegment> lineSegment = new osg::LineSegment(nearPoint,farPoint); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | osgUtil::IntersectVisitor intersector; |
|---|
| 141 | intersector.addLineSegment(lineSegment.get()); |
|---|
| 142 | |
|---|
| 143 | scene->accept(intersector); |
|---|
| 144 | |
|---|
| 145 | osgUtil::IntersectVisitor::HitList& hits=intersector.getHitList(lineSegment.get()); |
|---|
| 146 | if (!hits.empty()) |
|---|
| 147 | { |
|---|
| 148 | std::cout<<"Got hits"<<std::endl; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | osgUtil::Hit& hit = hits.front(); |
|---|
| 152 | |
|---|
| 153 | osg::NodePath& nodePath = hit._nodePath; |
|---|
| 154 | osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
|---|
| 155 | osg::Group* parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
|---|
| 156 | |
|---|
| 157 | if (parent && node) |
|---|
| 158 | { |
|---|
| 159 | |
|---|
| 160 | std::cout<<"Hits "<<node->className()<<std::endl; |
|---|
| 161 | std::cout<<" parent "<<parent->className()<<std::endl; |
|---|
| 162 | |
|---|
| 163 | osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent); |
|---|
| 164 | if (!parentAsScribe) |
|---|
| 165 | { |
|---|
| 166 | |
|---|
| 167 | osgFX::Scribe* scribe = new osgFX::Scribe(); |
|---|
| 168 | scribe->addChild(node); |
|---|
| 169 | parent->replaceChild(node,scribe); |
|---|
| 170 | } |
|---|
| 171 | else |
|---|
| 172 | { |
|---|
| 173 | |
|---|
| 174 | osg::Node::ParentList parentList = parentAsScribe->getParents(); |
|---|
| 175 | for(osg::Node::ParentList::iterator itr=parentList.begin(); |
|---|
| 176 | itr!=parentList.end(); |
|---|
| 177 | ++itr) |
|---|
| 178 | { |
|---|
| 179 | (*itr)->replaceChild(parentAsScribe,node); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | private: |
|---|
| 191 | |
|---|
| 192 | float _mx, _my; |
|---|
| 193 | float _mx_buttonPress, _my_buttonPress; |
|---|
| 194 | unsigned int _mbutton; |
|---|
| 195 | bool _mouseMovingOnPreviousRelease; |
|---|
| 196 | |
|---|
| 197 | bool _done; |
|---|
| 198 | |
|---|
| 199 | osg::ref_ptr<Producer::Trackball> _trackBall; |
|---|
| 200 | osg::ref_ptr<osgUtil::SceneView> _sceneView; |
|---|
| 201 | }; |
|---|
| 202 | |
|---|
| 203 | int main( int argc, char **argv ) |
|---|
| 204 | { |
|---|
| 205 | if (argc<2) |
|---|
| 206 | { |
|---|
| 207 | std::cout << argv[0] <<": requires filename argument." << std::endl; |
|---|
| 208 | return 1; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]); |
|---|
| 213 | if (!loadedModel) |
|---|
| 214 | { |
|---|
| 215 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 216 | return 1; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface; |
|---|
| 221 | renderSurface->setWindowName("osgkeyboardmouse"); |
|---|
| 222 | renderSurface->setWindowRectangle(100,100,800,600); |
|---|
| 223 | renderSurface->useBorder(true); |
|---|
| 224 | renderSurface->realize(); |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView; |
|---|
| 229 | sceneView->setDefaults(); |
|---|
| 230 | sceneView->setSceneData(loadedModel.get()); |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | osg::ref_ptr<Producer::KeyboardMouse> kbm = new Producer::KeyboardMouse(renderSurface.get()); |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback(sceneView.get()); |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 242 | |
|---|
| 243 | unsigned int frameNum = 0; |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | while( renderSurface->isRealized() && !kbmcb->done()) |
|---|
| 247 | { |
|---|
| 248 | |
|---|
| 249 | osg::FrameStamp* frameStamp = new osg::FrameStamp; |
|---|
| 250 | frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick())); |
|---|
| 251 | frameStamp->setFrameNumber(frameNum++); |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | sceneView->setFrameStamp(frameStamp); |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | kbm->update( *kbmcb ); |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | sceneView->setViewMatrix(kbmcb->getViewMatrix()); |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | sceneView->setViewport(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight()); |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | sceneView->update(); |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | sceneView->cull(); |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | sceneView->draw(); |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | renderSurface->swapBuffers(); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | return 0; |
|---|
| 279 | } |
|---|