Hi Michael,
In the svn or 2.7.x dev series of the OSG there is a convinience event
callback in osgViewer/ViewerEventHandlers called
InteractiveImageHandler, this is used to direct mouse events into
osg::Image subclasses, such as used by the new osgWidget::Browser,
osgWidget::PdfReader and osgWidget::VncClient classes. The code that
is relevant to you is the InteractiveImageHandler::mousePoistion(..)
method. This computes the mouse coords in the local image coords,
accounting for any geometry that the image is textured on to:
bool InteractiveImageHandler::mousePosition(osgViewer::View* view,
osg::NodeVisitor* nv, const osgGA::GUIEventAdapter& ea, int& x, int
&y) const
{
osgUtil::LineSegmentIntersector::Intersections intersections;
bool foundIntersection = view==0 ? false :
(nv==0 ? view->computeIntersections(ea.getX(), ea.getY(),
intersections) :
view->computeIntersections(ea.getX(), ea.getY(),
nv->getNodePath(), intersections));
if (foundIntersection)
{
osg::Vec2 tc(0.5f,0.5f);
// use the nearest intersection
const osgUtil::LineSegmentIntersector::Intersection&
intersection = *(intersections.begin());
osg::Drawable* drawable = intersection.drawable.get();
osg::Geometry* geometry = drawable ? drawable->asGeometry() : 0;
osg::Vec3Array* vertices = geometry ?
dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()) : 0;
if (vertices)
{
// get the vertex indices.
const
osgUtil::LineSegmentIntersector::Intersection::IndexList& indices =
intersection.indexList;
const
osgUtil::LineSegmentIntersector::Intersection::RatioList& ratios =
intersection.ratioList;
if (indices.size()==3 && ratios.size()==3)
{
unsigned int i1 = indices[0];
unsigned int i2 = indices[1];
unsigned int i3 = indices[2];
float r1 = ratios[0];
float r2 = ratios[1];
float r3 = ratios[2];
osg::Array* texcoords =
(geometry->getNumTexCoordArrays()>0) ? geometry->getTexCoordArray(0) :
0;
osg::Vec2Array* texcoords_Vec2Array =
dynamic_cast<osg::Vec2Array*>(texcoords);
if (texcoords_Vec2Array)
{
// we have tex coord array so now we can compute
the final tex coord at the point of intersection.
osg::Vec2 tc1 = (*texcoords_Vec2Array)[i1];
osg::Vec2 tc2 = (*texcoords_Vec2Array)[i2];
osg::Vec2 tc3 = (*texcoords_Vec2Array)[i3];
tc = tc1*r1 + tc2*r2 + tc3*r3;
}
}
}
osg::TexMat* activeTexMat = 0;
osg::Texture* activeTexture = 0;
if (geometry->getStateSet())
{
osg::TexMat* texMat =
dynamic_cast<osg::TexMat*>(geometry->getStateSet()->getTextureAttribute(0,osg::StateAttribute::TEXMAT));
if (texMat) activeTexMat = texMat;
osg::Texture* texture =
dynamic_cast<osg::Texture*>(geometry->getStateSet()->getTextureAttribute(0,osg::StateAttribute::TEXTURE));
if (texture) activeTexture = texture;
}
if (activeTexMat)
{
osg::Vec4 tc_transformed = osg::Vec4(tc.x(), tc.y(),
0.0f,0.0f) * activeTexMat->getMatrix();
tc.x() = tc_transformed.x();
tc.y() = tc_transformed.y();
}
if (dynamic_cast<osg::TextureRectangle*>(activeTexture))
{
x = int( tc.x() );
y = int( tc.y() );
}
else if (_image.valid())
{
x = int( float(_image->s()) * tc.x() );
y = int( float(_image->t()) * tc.y() );
}
return true;
}
return false;
}
Robert.
On Tue, Jan 13, 2009 at 11:56 PM, Michael <[email protected]> wrote:
> Hi
>
> I am using TriangleFunctor to do some very simple collision detection.
> Essentially I have a virtual pen (the collision detection is treated as a
> sphere on the tip of the pen) that I am testing for collisions against other
> geometry in the scene. The geometry I am testing against is loaded from OBJ
> files using osgDB::readNodeFile(). Once I have found a collision with the
> pen, I would like to update a texture image (drawing on the virtual object).
> However, I am unsure how to access the texture coordinates for the triangle
> I have just tested with.
>
> How am I able to do this with TriangleFunctor? Or, is there another method I
> should be using for my collision detection that will give me access to the
> texture coordinates of the triangle.
>
> Thanks
> Michael
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org