我试图“填充”我绘制的几何几何体的表面。我正在使用GL_TRIANGLE_FAN
原语。 (例如:1个枢纽(中心)点和12个其他点)。我已经计算了0-1区间内每个顶点的纹理坐标。但结果我得到了这个,它有点困惑。我希望获得像image那样的结果。请帮忙,这里有什么问题?
如何在此类三角测量中计算正确的纹理坐标(GL_TRIANGLE_FAN
)
在image红点是我的要点
*代码 - 代码段:
assert(("CROSS type intersection needs only 5 vertices : center point and "
"rest points in anticlockwise order", (lp->size() > 5) && (lp->size() < 5)));
osg::Vec3 vAlong_1,vAlong_2;
vAlong_1 = (*lp)[1] - (*lp)[4];
vAlong_2 = (*lp)[1] - (*lp)[2];
eps = ((*lp)[2] - (*lp)[4]).length() * 0.2 / 2;
vAlong_1.normalize();
vAlong_2.normalize();
_edgeCoords->push_back((*lp)[0]);
_edgeCoords->push_back((*lp)[1]);
if (CMF::euclidDistance((*lp)[0],(*lp)[1]) <= CMF::euclidDistance((*lp)[0],(*lp)[2])) {
float cosAlpha = -(vAlong_1 * vAlong_2);
float extraLength = ((*lp)[2] - (*lp)[1]).length() * cosAlpha;
_edgeCoords->push_back((*lp)[1] + vAlong_1 * (eps + extraLength));
_edgeCoords->push_back((*lp)[2] + vAlong_1 * eps);
} else {
float cosAlpha = (vAlong_1 * vAlong_2);
float extraLength = ((*lp)[2] - (*lp)[1]).length() * cosAlpha;
_edgeCoords->push_back((*lp)[1] + vAlong_1 * eps);
_edgeCoords->push_back((*lp)[2] + vAlong_1 * (eps + extraLength));
}
_edgeCoords->push_back((*lp)[2]);
if (CMF::euclidDistance((*lp)[0],(*lp)[2]) <= CMF::euclidDistance((*lp)[0],(*lp)[3])) {
float cosAlpha = -(vAlong_1 * vAlong_2);
float extraLength = ((*lp)[3] - (*lp)[2]).length() * cosAlpha;
_edgeCoords->push_back((*lp)[2] - vAlong_2 * (eps + extraLength));
_edgeCoords->push_back((*lp)[3] - vAlong_2 * eps);
} else {
float cosAlpha = (vAlong_1 * vAlong_2);
float extraLength = ((*lp)[3] - (*lp)[2]).length() * cosAlpha;
_edgeCoords->push_back((*lp)[2] - vAlong_2 * eps);
_edgeCoords->push_back((*lp)[3] - vAlong_2 * (eps + extraLength));
}
_edgeCoords->push_back((*lp)[3]);
if (CMF::euclidDistance((*lp)[0],(*lp)[3]) <= CMF::euclidDistance((*lp)[0],(*lp)[4])) {
float cosAlpha = -(vAlong_1 * vAlong_2);
float extraLength = ((*lp)[4] - (*lp)[3]).length() * cosAlpha;
_edgeCoords->push_back((*lp)[3] - vAlong_1 * (eps + extraLength));
_edgeCoords->push_back((*lp)[4] - vAlong_1 * eps);
} else {
float cosAlpha = (vAlong_1 * vAlong_2);
float extraLength = ((*lp)[4] - (*lp)[3]).length() * cosAlpha;
_edgeCoords->push_back((*lp)[3] - vAlong_1 * eps);
_edgeCoords->push_back((*lp)[4] - vAlong_1 * (eps + extraLength));
}
_edgeCoords->push_back((*lp)[4]);
if (CMF::euclidDistance((*lp)[0],(*lp)[1]) <= CMF::euclidDistance((*lp)[0],(*lp)[4])) {
float cosAlpha = -(vAlong_1 * vAlong_2);
float extraLength = ((*lp)[4] - (*lp)[1]).length() * cosAlpha;
_edgeCoords->push_back((*lp)[4] + vAlong_2 * eps);
_edgeCoords->push_back((*lp)[1] + vAlong_2 * (eps + extraLength));
} else {
float cosAlpha = (vAlong_1 * vAlong_2);
float extraLength = ((*lp)[4] - (*lp)[1]).length() * cosAlpha;
_edgeCoords->push_back((*lp)[4] + vAlong_2 * (eps + extraLength));
_edgeCoords->push_back((*lp)[1] + vAlong_2 * eps);
}
_edgeCoords->push_back((*lp)[1]);
_tCoords->push_back(osg::Vec2(0.5,0.5));
_tCoords->push_back(osg::Vec2(0.666,0.666));
_tCoords->push_back(osg::Vec2(0.666,1.0));
_tCoords->push_back(osg::Vec2(0.333,1.0));
_tCoords->push_back(osg::Vec2(0.333,0.666));
_tCoords->push_back(osg::Vec2(0.0,0.666));
_tCoords->push_back(osg::Vec2(0.0,0.333));
_tCoords->push_back(osg::Vec2(0.333,0.333));
_tCoords->push_back(osg::Vec2(0.333,0.0));
_tCoords->push_back(osg::Vec2(0.666,0.0));
_tCoords->push_back(osg::Vec2(0.666,0.333));
_tCoords->push_back(osg::Vec2(1.0,0.333));
_tCoords->push_back(osg::Vec2(1.0,0.666));
_tCoords->push_back(osg::Vec2(0.666,0.666));
答案 0 :(得分:2)
尝试保持2d位置始终等于每个顶点的纹理坐标。这将确保您的几何图形显示为纹理的无失真切口。然后,您可以根据需要重新缩放和居中网格,而不会通过将变换应用于顶点位置来扭曲纹理。
这样做的一种方法是创建一个推动单个顶点的函数,接受顶点的2d坐标和要应用的任何变换。然后,该函数将2d坐标作为texcoords推,然后转换它们并将结果推送到位置。