| 134 | case(osgGA::GUIEventAdapter::MOVE): |
| 135 | case(osgGA::GUIEventAdapter::PUSH): |
| 136 | case(osgGA::GUIEventAdapter::RELEASE): |
| 137 | { |
| 138 | osgProducer::Viewer* viewer = dynamic_cast<osgProducer::Viewer*>(&aa); |
| 139 | osgUtil::IntersectVisitor::HitList hlist; |
| 140 | if (viewer->computeIntersections(ea.getX(),ea.getY(), nv->getNodePath().back(), hlist)) |
| 141 | { |
| 142 | if (!hlist.empty()) |
| 143 | { |
| 144 | // use the nearest intersection |
| 145 | osgUtil::Hit& hit = hlist.front(); |
| 146 | osg::Drawable* drawable = hit.getDrawable(); |
| 147 | osg::Geometry* geometry = drawable ? drawable->asGeometry() : 0; |
| 148 | osg::Vec3Array* vertices = geometry ? dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()) : 0; |
| 149 | |
| 150 | if (vertices) |
| 151 | { |
| 152 | // get the vertex indices. |
| 153 | const osgUtil::Hit::VecIndexList& vil = hit.getVecIndexList(); |
| 154 | |
| 155 | if (vil.size()==3) |
| 156 | { |
| 157 | |
| 158 | int i1 = vil[0]; |
| 159 | int i2 = vil[1]; |
| 160 | int i3 = vil[2]; |
| 161 | osg::Vec3 v1 = (*vertices)[i1]; |
| 162 | osg::Vec3 v2 = (*vertices)[i2]; |
| 163 | osg::Vec3 v3 = (*vertices)[i3]; |
| 164 | osg::Vec3 v = hit.getLocalIntersectPoint(); |
| 165 | osg::Vec3 p1 = hit.getLocalLineSegment()->start(); |
| 166 | osg::Vec3 p2 = hit.getLocalLineSegment()->end(); |
| 167 | |
| 168 | osg::Vec3 p12 = p1-p2; |
| 169 | osg::Vec3 v13 = v1-v3; |
| 170 | osg::Vec3 v23 = v2-v3; |
| 171 | osg::Vec3 p1v3 = p1-v3; |
| 172 | |
| 173 | osg::Matrix matrix(p12.x(), v13.x(), v23.x(), 0.0, |
| 174 | p12.y(), v13.y(), v23.y(), 0.0, |
| 175 | p12.z(), v13.z(), v23.z(), 0.0, |
| 176 | 0.0, 0.0, 0.0, 1.0); |
| 177 | |
| 178 | osg::Matrix inverse; |
| 179 | inverse.invert(matrix); |
| 180 | |
| 181 | osg::Vec3 ratio = inverse*p1v3; |
| 182 | |
| 183 | // extract the baricentric coordinates. |
| 184 | float r1 = ratio.y(); |
| 185 | float r2 = ratio.z(); |
| 186 | float r3 = 1.0f-r1-r2; |
| 187 | |
| 188 | osg::Array* texcoords = (geometry->getNumTexCoordArrays()>0) ? geometry->getTexCoordArray(0) : 0; |
| 189 | osg::Vec2Array* texcoords_Vec2Array = dynamic_cast<osg::Vec2Array*>(texcoords); |
| 190 | if (texcoords_Vec2Array) |
| 191 | { |
| 192 | // we have tex coord array so now we can compute the final tex coord at the point of intersection. |
| 193 | osg::Vec2 tc1 = (*texcoords_Vec2Array)[i1]; |
| 194 | osg::Vec2 tc2 = (*texcoords_Vec2Array)[i2]; |
| 195 | osg::Vec2 tc3 = (*texcoords_Vec2Array)[i3]; |
| 196 | osg::Vec2 tc = tc1*r1 + tc2*r2 + tc3*r3; |
| 197 | |
| 198 | osg::notify(osg::NOTICE)<<"We hit tex coords "<<tc<<std::endl; |
| 199 | |
| 200 | } |
| 201 | |
| 202 | |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | osg::notify(osg::NOTICE)<<"Hit but insufficient indices to work with"; |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | |
| 211 | } |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | osg::notify(osg::NOTICE)<<"No hit"<<std::endl; |
| 216 | } |
| 217 | break; |
| 218 | } |