【发布时间】:2020-04-28 06:21:19
【问题描述】:
我正在尝试转换 Moller Trumbore 算法,如 here 所示。
这段代码的目的是寻找一个向量与一个三角形的交点,同时返回U和V纹理点。
但是,我坚持这一行:
Vector uvHit = uvVectors[0] * u + uvVectors[1] * v + uvVectors[2] * (1 - u - v);
谁也可以看作:
*uvHit = u * uvVectors[0] + v * uvVectors[1] + (1 - u - v) * uvVectors[2];
这是我的代码:
typedef float vec_t;
typedef vec_t vec3_t[3];
qboolean GetIntersection(vec3_t rayOrigin, vec3_t rayDirection, float hitDistance, vec3_t *uv)
{
vec3_t pvec;
float det = 0.0f;
vec3_t tvec;
vec3_t qvec;
vec3_t uvHit;
// begin calculating determinant - also used to calculate U parameter
CrossProduct(rayDirection, edge2, pvec);
// if determinant is near zero, ray lies in plane of triangle
det = DotProduct(edge1, pvec);
const float EPSILON = 0.000001f;
if ((det > -EPSILON) && (det < EPSILON))
return qfalse;
float inv_det = 1.0f / det;
// calculate distance from vertex 0 to ray origin
tvec = rayOrigin - verts[0];
// calculate U parameter and test bounds
u = DotProduct(tvec, pvec) * inv_det;
if ((u < 0.0f) || (u > 1.0f))
return false;
// prepare to test V parameter
CrossProduct(tvec, edge1, qvec);
// calculate V parameter and test bounds
float v = DotProduct(rayDirection, qvec) * inv_det;
if ((v < 0.0f) || (u + v > 1.0f))
return false;
Vector uvHit = uvVectors[0] * u + uvVectors[1] * v + uvVectors[2] * (1 - u - v);// This is the line I don't understand
uv[0] = uvHit[0];
uv[1] = uvHit[1];
uv[2] = 0;
// calculate t, ray intersects triangle
hitDistance = DotProduct(edge2, qvec) * inv_det;
// only allow intersections in the forward ray direction
return hitDistance >= 0.0f;
}
【问题讨论】:
-
你为什么认为是矩阵乘法?它看起来像普通的算术。
-
你需要展示
Vector类型是如何定义的,以及所有变量的声明。 -
这里没有任何东西看起来像 C++
std::vector。 -
Vector类与 C++std::vector完全无关。它代表矢量图形。 -
好吧,我的错。但是无论如何请如何转换它?