Hi Daniel,
the following piece of code calculates the textureCoordinates for a
(user picked)point in 3d space, so this might be useful for what you are
trying to do.
Laurens.
if (transparentImage) {
const osg::Geometry* geometry = hit.drawable->asGeometry();
if (geometry)
{
const osg::Vec3Array* vertices = dynamic_cast<const
osg::Vec3Array*>(geometry->getVertexArray());
const osg::Vec2Array* texCoords =
dynamic_cast<const
osg::Vec2Array*>(geometry->getTexCoordArray(multiTexIndex));
if (vertices && texCoords)
{
const osg::Vec3* firstVertex =
&(vertices->front());
const osg::Vec2* firstTc = &(texCoords->front());
const
osgUtil::LineSegmentIntersector::Intersection::IndexList &vIndexList =
hit.indexList;
if (vIndexList.size()) {//not present on
indexed vertices
const osg::Vec3& hp =
hit.getLocalIntersectPoint();
const osg::Vec3* v0 = firstVertex +
vIndexList[0];
const osg::Vec3* v1 = firstVertex +
vIndexList[1];
const osg::Vec3* v2 = firstVertex +
vIndexList[2];
osg::Vec3 hitL = hp - *v0;
osg::Vec3 aL = *v1 - *v0;
osg::Vec3 bL = *v2 - *v0;
float dot00 = aL * aL;
float dot01 = aL * bL;
float dot02 = aL * hitL;
float dot11 = bL * bL;
float dot12 = bL * hitL;
float invDenom = 1 / (dot00 * dot11 - dot01
* dot01);
float weightA = (dot11 * dot02 - dot01 *
dot12) * invDenom;
float weightB = (dot00 * dot12 - dot01 *
dot02) * invDenom;
const osg::Vec2* tc0 = firstTc + vIndexList[0];
const osg::Vec2* tc1 = firstTc + vIndexList[1];
const osg::Vec2* tc2 = firstTc + vIndexList[2];
osg::Vec2 tc(*tc0);
tc += (*tc1 - *tc0) * weightA;
tc += (*tc2 - *tc0) * weightB;
// look at the border clamp mode
switch(transparentTexture->getWrap(osg::Texture::WRAP_S)){
case(osg::Texture::CLAMP):
if(tc[0] < 0.0) tc[0] = 0.0;
if(tc[0] > 1.0) tc[0] = 1.0;
break;
case(osg::Texture::CLAMP_TO_EDGE):
{
double minS =
0.5/(double)transparentImage->s();
if(tc[0] < minS) tc[0] = minS;
if(tc[0] > 1.0 - minS) tc[0] = 1.0
- minS;
}
break;
case(osg::Texture::CLAMP_TO_BORDER):
{
double minS =
0.5/(double)transparentImage->s();
if(tc[0] < minS || tc[0] > 1.0 - minS){
float imgAlpha =
transparentTexture->getBorderColor().a();
if (imgAlpha < aMinVal) {
transparentHit = true;
return true;
}
}
}
break;
case(osg::Texture::REPEAT):
tc[0] = tc[0] - floorf(tc[0]);
if(tc[0] < 0.0) tc[0] = 1.0 - tc[0];
break;
case(osg::Texture::MIRROR):
if(osg::absolute((int)tc[0])%(int)2 == 0){
tc[0] = tc[0] - floorf(tc[0]);
if(tc[0] < 0.0) tc[0] = 1.0 - tc[0];
} else {
tc[0] = tc[0] - floorf(tc[0]);
if(tc[0] < 0.0) tc[0] = tc[0];
else tc[0] = 1.0 - tc[0];
}
break;
}
switch(transparentTexture->getWrap(osg::Texture::WRAP_T)){
case(osg::Texture::CLAMP):
if(tc[1] < 0.0) tc[1] = 0.0;
if(tc[1] > 1.0) tc[1] = 1.0;
break;
case(osg::Texture::CLAMP_TO_EDGE):
{
double minT =
0.5/(double)transparentImage->t();
if(tc[1] < minT) tc[1] = minT;
if(tc[1] > 1.0 - minT) tc[1] = 1.0
- minT;
}
break;
case(osg::Texture::CLAMP_TO_BORDER):
{
double minT =
0.5/(double)transparentImage->t();
if(tc[1] < minT || tc[1] > 1.0 - minT){
float imgAlpha =
transparentTexture->getBorderColor().a();
if (imgAlpha < aMinVal) {
transparentHit = true;
return true;
}
}
}
break;
case(osg::Texture::REPEAT):
tc[1] = tc[1] - floorf(tc[1]);
if(tc[1] < 0.0) tc[0] = 1.0 - tc[1];
break;
case(osg::Texture::MIRROR):
if(osg::absolute((int)tc[1])%(int)2 == 0){
tc[1] = tc[1] - floorf(tc[1]);
if(tc[1] < 0.0) tc[1] = 1.0 - tc[1];
} else {
tc[1] = tc[1] - floorf(tc[1]);
if(tc[1] < 0.0) tc[0] = tc[1];
else tc[1] = 1.0 - tc[1];
}
break;
}
float imgAlpha =
transparentImage->getColor(tc).a();
if (imgAlpha < aMinVal) {
transparentHit = true;
return true;
}
}
}
}
}
On 9/6/2013 1:38 PM, Robert Osfield wrote:
Hi Daniel,
I haven't specifically tried doing what are trying to do, but my
inclination would be to compute the bari-centric coordinates of the
the new mesh points relative to the triangle corners, then use thse
bar-centric coordinates to compute the texture coordinates. Have a
look online for docs on bari-centric coordinates.
Robert.
On 6 September 2013 11:26, Daniel Schmid
<[email protected] <mailto:[email protected]>>
wrote:
Hi all
As you can see in the attached illustration, I have created a
configurable algorithm that takes the Triangles of an existing
Geode and creates a mesh of quads (yellow).
Everything works fine so far.
Now I have the following problem: I want the reuse the texture
that was originally applied to the triangle to be applied to the
mesh again, to have the same rendered appearance.
How can I generate the texture coordinates for the mesh points?
They should somehow be interpolated, but I don't know how to do
this...
Anybody?
Regards
Daniel
_______________________________________________
osg-users mailing list
[email protected]
<mailto:[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
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org