| 31 | | #include <osgUtil/PickVisitor> |
| 32 | | |
| 33 | | |
| 34 | | |
| 35 | | static Producer::CameraConfig *BuildConfig(void) |
| 36 | | { |
| 37 | | Producer::RenderSurface *rs1 = new Producer::RenderSurface; |
| 38 | | rs1->setScreenNum(0); |
| 39 | | //rs1->useBorder(false); |
| 40 | | rs1->setWindowRect(0,0,640,480); |
| 41 | | Producer::Camera *camera1 = new Producer::Camera; |
| 42 | | camera1->setRenderSurface(rs1); |
| 43 | | camera1->setOffset( 1.0, 0.0 ); |
| 44 | | |
| 45 | | Producer::RenderSurface *rs2 = new Producer::RenderSurface; |
| 46 | | rs2->setScreenNum(0); |
| 47 | | //rs2->useBorder(false); |
| 48 | | rs2->setWindowRect(640,0,640,480); |
| 49 | | Producer::Camera *camera2 = new Producer::Camera; |
| 50 | | camera2->setRenderSurface(rs2); |
| 51 | | camera2->setOffset( -1.0, 0.0 ); |
| 52 | | |
| 53 | | Producer::InputArea *ia = new Producer::InputArea; |
| 54 | | ia->addInputRectangle( rs1, Producer::InputRectangle(0.0,0.5,0.0,1.0)); |
| 55 | | ia->addInputRectangle( rs2, Producer::InputRectangle(0.5,1.0,0.0,1.0)); |
| 56 | | // ia->addInputRectangle( rs1, Producer::InputRectangle(-1.0,0.0,-1.0,1.0)); |
| 57 | | // ia->addInputRectangle( rs2, Producer::InputRectangle(0.0,1.0,-1.0,1.0)); |
| 58 | | |
| 59 | | |
| 60 | | Producer::CameraConfig *cfg = new Producer::CameraConfig; |
| 61 | | cfg->addCamera("Camera 1",camera1); |
| 62 | | cfg->addCamera("Camera 2", camera2); |
| 63 | | cfg->setInputArea(ia); |
| 64 | | return cfg; |
| 65 | | } |
| 109 | | // OK here is the interesting bit - How To Pick a Geode |
| 110 | | // including geodes in a HUD under a Projection Matrix |
| 111 | | osg::Node *scene=_viewer->getSceneData();//main node of the scene. |
| 112 | | if (scene) |
| 113 | | { |
| 114 | | float x=ea.getX(); |
| 115 | | float y=ea.getY(); |
| 116 | | |
| 117 | | |
| 118 | | Producer::KeyboardMouse* km = _viewer->getKeyboardMouse(); |
| 119 | | |
| 120 | | |
| 121 | | std::string gdlist=""; |
| 122 | | |
| 123 | | for(unsigned int i=0;i<_viewer->getNumberOfCameras();++i) |
| | 74 | osgUtil::IntersectVisitor::HitList hlist; |
| | 75 | |
| | 76 | std::string gdlist=""; |
| | 77 | if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist)) |
| | 78 | { |
| | 79 | for(osgUtil::IntersectVisitor::HitList::iterator hitr=hlist.begin(); |
| | 80 | hitr!=hlist.end(); |
| | 81 | ++hitr) |
| 125 | | Producer::Camera* cmm=_viewer->getCamera(i); |
| 126 | | Producer::RenderSurface* rs = cmm->getRenderSurface(); |
| 127 | | |
| 128 | | //std::cout << "checking camara "<<i<<std::endl; |
| 129 | | |
| 130 | | float pixel_x,pixel_y; |
| 131 | | if (km->computePixelCoords(x,y,rs,pixel_x,pixel_y)) |
| | 83 | //osg::Vec3 ip = hitr->getLocalIntersectPoint(); |
| | 84 | //osg::Vec3 in = hitr->getLocalIntersectNormal(); |
| | 85 | osg::Geode* geode = hitr->_geode.get(); |
| | 86 | // the geodes are identified by name. |
| | 87 | if (geode) |
| 133 | | //std::cout << " compute pixel coords "<<pixel_x<<" "<<pixel_y<<std::endl; |
| 134 | | |
| 135 | | int pr_wx, pr_wy; |
| 136 | | unsigned int pr_width, pr_height; |
| 137 | | cmm->getProjectionRect( pr_wx, pr_wy, pr_width, pr_height ); |
| 138 | | |
| 139 | | int rs_wx, rs_wy; |
| 140 | | unsigned int rs_width, rs_height; |
| 141 | | rs->getWindowRect( rs_wx, rs_wy, rs_width, rs_height ); |
| 142 | | |
| 143 | | pr_wx += rs_wx; |
| 144 | | pr_wy += rs_wy; |
| 145 | | |
| 146 | | //std::cout << " wx = "<<pr_wx<<" wy = "<<pr_wy<<" width="<<pr_width<<" height="<<pr_height<<std::endl; |
| 147 | | |
| 148 | | |
| 149 | | |
| 150 | | if (pixel_x<(float)pr_wx) break; |
| 151 | | if (pixel_x>(float)(pr_wx+pr_width)) break; |
| 152 | | |
| 153 | | if (pixel_y<(float)pr_wy) break; |
| 154 | | if (pixel_y>(float)(pr_wy+pr_height)) break; |
| 155 | | |
| 156 | | |
| 157 | | float rx = 2.0f*(pixel_x - (float)pr_wx)/(float)pr_width-1.0f; |
| 158 | | float ry = 2.0f*(pixel_y - (float)pr_wy)/(float)pr_height-1.0f; |
| 159 | | |
| 160 | | //std::cout << " rx "<<rx<<" "<<ry<<std::endl; |
| 161 | | |
| 162 | | osgProducer::OsgSceneHandler* sh = dynamic_cast<osgProducer::OsgSceneHandler*>(cmm->getSceneHandler()); |
| 163 | | osg::Matrix vum; |
| 164 | | if (sh!=0 && sh->getModelViewMatrix()!=0 && sh->getProjectionMatrix()!=0) |
| | 89 | if (!geode->getName().empty()) |
| 176 | | |
| 177 | | osgUtil::PickVisitor iv; |
| 178 | | osgUtil::IntersectVisitor::HitList& hlist=iv.getHits(scene, vum, rx,ry); |
| 179 | | if (iv.hits()) |
| 180 | | { |
| 181 | | for(osgUtil::IntersectVisitor::HitList::iterator hitr=hlist.begin(); |
| 182 | | hitr!=hlist.end(); |
| 183 | | ++hitr) |
| 184 | | { |
| 185 | | //osg::Vec3 ip = hitr->getLocalIntersectPoint(); |
| 186 | | //osg::Vec3 in = hitr->getLocalIntersectNormal(); |
| 187 | | osg::Geode* geode = hitr->_geode.get(); |
| 188 | | // the geodes are identified by name. |
| 189 | | if (geode) |
| 190 | | { |
| 191 | | if (!geode->getName().empty()) |
| 192 | | { |
| 193 | | gdlist=gdlist+" "+geode->getName(); |
| 194 | | } |
| 195 | | else |
| 196 | | { |
| 197 | | gdlist=gdlist+" geode"; |
| 198 | | } |
| 199 | | } |
| 200 | | } |
| 201 | | } |
| 202 | | } |
| | 97 | } |