遇到的问题
1.项目打开时无法运行:https://blog.csdn.net/m0_51233386/article/details/124341110
2.作业生成的图片可以用ps打开。
作业分析
光追内容的难度真的是直线上升,于是把老师的13视频0.5倍速边看边写了一份文字版的内容。记录完后真的对本次作业没什么说的了,都是老师课上讲了的内容,作业框架也是规定死了的。
Triangle.hpp按照这个逐字对应就好了,限制条件是t大于0,b1,b2,(1-b1-b2)都在(0,1)范围内。
代码
Renderer.cpp 中的 Render():
for (int j = 0; j < scene.height; ++j)
{
for (int i = 0; i < scene.width; ++i)
{
// generate primary ray direction
// 生成主光线方向
float x;
float y;
// TODO: Find the x and y positions of the current pixel to get the direction
// vector that passes through it.
// Also, don't forget to multiply both of them with the variable *scale*, and
// x (horizontal) variable with the *imageAspectRatio*
// 找到当前像素的x和y位置,以获得通过它的方向向量。另外,不要忘记用变量*scale*乘以它们,用x(水平)变量乘以*imageAspectRatio*
x = (i * 2.0 / (float)scene.width-1)*imageAspectRatio*scale;
y = (1 - j * 2.0 / (float)scene.height) * scale;
Vector3f dir = normalize(Vector3f(x, y, -1)); // Don't forget to normalize this direction! 别忘了规范化这个方向!
framebuffer[m++] = castRay(eye_pos, dir, scene, 0);
}
UpdateProgress(j / (float)scene.height);
}
此时已经可以运行了,运行的结果:
Triangle.hpp 中的 rayTriangleIntersect():
bool rayTriangleIntersect(const Vector3f& v0, const Vector3f& v1, const Vector3f& v2, const Vector3f& orig,
const Vector3f& dir, float& tnear, float& u, float& v)
{
// TODO: Implement this function that tests whether the triangle
// that's specified bt v0, v1 and v2 intersects with the ray (whose
// origin is *orig* and direction is *dir*)
// Also don't forget to update tnear, u and v.
// 实现此函数,测试指定的bt v0、v1和v2三角形是否与射线相交(其原点为*orig*,方向为*dir*),也不要忘记更新tnear、u和v。
Vector3f D = dir;
Vector3f O = orig;
Vector3f E1 = v1 - v0;
Vector3f E2 = v2 - v0;
Vector3f S = O - v0;
Vector3f S1 = crossProduct(D, E2);
Vector3f S2 = crossProduct(S, E1);
float t, b1, b2;
t = dotProduct(S2, E2) / dotProduct(S1, E1);
b1= dotProduct(S1, S) / dotProduct(S1, E1);
b2= dotProduct(S2,D) / dotProduct(S1, E1);
tnear = t;
u = b1;
v = b2;
if (t > 0 && b1 >= 0 && b1 <= 1 && b2 >= 0 && b2 <= 1 && 1 - b1 - b2 >= 0 && 1 - b1 - b2 <= 1)
{ return true; }
else
{ return false; }
}
最后的结果: