作业5
问题描述
实现两个部分:光线的生成和光线与三角的相交。
Möller-Trumbore 算法推导
光线与三角形相交时,可得如下等式:
//orig起点 direction射线方向
//v0,v1,v2 是三角形三个顶点
Vector3f E1 = v1 - v0;
Vector3f E2 = v2 - v0;
Vector3f S = orig - v0;
Vector3f S1 = crossProduct(dir, E2);
Vector3f S2 = crossProduct(S, E1);
//tnear为时间 沿着三角形方向 t的为正
//u,v 为重心坐标前的参数 都得为非负的还得小于1
float S1E1 = dotProduct(S1, E1);
tnear = 1.0f / S1E1 * dotProduct(S2, E2);
u = 1.0f / S1E1 * dotProduct(S1, S);
v = 1.0f / S1E1 * dotProduct(S2, dir);
if (tnear > 0 && v >= 0 && v <= 1 && u >= 0 && u <= 1)
{
return true;
}
return false;
// 产生主射线方向
float x = i + 0.5f - scene.width / 2;
float y = scene.height / 2 - j - 0.5f;
float z = -scene.height / 2;
// TODO: 查找当前像素的x和y位置以获得方向
// 穿过它的向量
// 此外,不要忘记将它们与变量*scale*相乘
// x(水平)变量与*imageAspectRatio*
Vector3f dir = normalize(Vector3f(x , y , z)); // Don't forget to normalize this direction!
framebuffer[m++] = castRay(eye_pos, dir, scene, 0);