Assignment 6
1. Basic
1.1 Render() in Renderer.cpp
这一部分间接依照作业 5 即可,在应用 castRay 函数时须要进行改变。
留神:记得对 dir 向量归一化。
void Renderer::Render(const Scene& scene)
{std::vector<Vector3f> framebuffer(scene.width * scene.height);
float scale = tan(deg2rad(scene.fov * 0.5));
float imageAspectRatio = scene.width / (float)scene.height;
Vector3f eye_pos(-1, 5, 10);
int m = 0;
for (uint32_t j = 0; j < scene.height; ++j) {for (uint32_t i = 0; i < scene.width; ++i) {
// generate primary ray direction
float x = (2 * (i + 0.5) / (float)scene.width - 1) *
imageAspectRatio * scale;
float y = (1 - 2 * (j + 0.5) / (float)scene.height) * scale;
// 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 = (2 * (i + 0.5f) / (float)(scene.width-1) - 1) * imageAspectRatio * scale;
// y = (1 - 2 * (j + 0.5f) / (float)(scene.height-1)) * scale;
/* 作业 6 增加局部 */
Vector3f dir = Vector3f(x, y, -1); // Don't forget to normalize this direction!
Ray ray(eye_pos, normalize(dir));
framebuffer[m++] = scene.castRay(ray, 0);
/* 作业 6 增加局部 */
// Don't forget to normalize this direction!
}
UpdateProgress(j / (float)scene.height);
}
UpdateProgress(1.f);
// save framebuffer to file
FILE* fp = fopen("binary.ppm", "wb");
(void)fprintf(fp, "P6\n%d %d\n255\n", scene.width, scene.height);
for (auto i = 0; i < scene.height * scene.width; ++i) {static unsigned char color[3];
color[0] = (unsigned char)(255 * clamp(0, 1, framebuffer[i].x));
color[1] = (unsigned char)(255 * clamp(0, 1, framebuffer[i].y));
color[2] = (unsigned char)(255 * clamp(0, 1, framebuffer[i].z));
fwrite(color, 1, 3, fp);
}
fclose(fp);
}
1.2 Triangle::getIntersection in Triangle.hpp
这一部分出错较多。首先须要浏览一下框架中的计算,将其中参加运算的变量对应上之前作业 5 中的计算两头变量。须要编写的局部其实很简略,然而须要留神要把 inter 的各个成员变量都进行赋值,尤其留神不能漏掉 m,normal,obj 这些,同时赋值时须要留神参看下面的类的定义。
inline Intersection Triangle::getIntersection(Ray ray)
{
Intersection inter;
if (dotProduct(ray.direction, normal) > 0)
return inter;
double u, v, t_tmp = 0;
Vector3f pvec = crossProduct(ray.direction, e2); //s1
double det = dotProduct(e1, pvec); //dotProduct(s1, e1)
if (fabs(det) < EPSILON)
return inter;
double det_inv = 1. / det;
Vector3f tvec = ray.origin - v0; //s
u = dotProduct(tvec, pvec) * det_inv; //b1
if (u < 0 || u > 1)
return inter;
Vector3f qvec = crossProduct(tvec, e1); //s2
v = dotProduct(ray.direction, qvec) * det_inv; //b2
if (v < 0 || u + v > 1)
return inter;
t_tmp = dotProduct(e2, qvec) * det_inv;
// TODO find ray triangle intersection
if (t_tmp>0.0f)
{
inter.distance = t_tmp;
// inter.coords = Vector3f(u, v, 1.0); //**Wrong.
// inter.coords = ray(t_tmp);
inter.coords = ray.origin + ray.direction * t_tmp;
inter.happened = true;
inter.m = m; //**Missed. Pay attention to the "m" above!(int class definition)
inter.normal = normal; //*Missed. But what is "normal" referring to ?
inter.obj = this; //*Missed. obj? this?
}
return inter;
}
1.3 IntersectP(const Ray& ray, const Vector3f& invDir,const std::array<int, 3>& dirIsNeg) in the Bounds3.hpp
这部分没出什么问题,只须要留神一下 eigen 库的应用,不要把中括号和小括号搞反。
inline bool Bounds3::IntersectP(const Ray& ray, const Vector3f& invDir,
const std::array<int, 3>& dirIsNeg) const
{// invDir: ray direction(x,y,z), invDir=(1.0/x,1.0/y,1.0/z), use this because Multiply
// is faster that Division
// dirIsNeg: ray direction(x,y,z), dirIsNeg=[int(x>0),int(y>0),int(z>0)],
// use this to simplify your logic
// TODO test if ray bound intersects
double tenter, texit, tmin, tmax;
texit = std::numeric_limits<double>::max();
tenter = 0.0;
for (int i = 0;i < 3;i++)
{double tmin = std::min((pMin[i] - ray.origin[i]) * invDir[i],
(pMax[i] - ray.origin[i]) * invDir[i]);
double tmax = std::max((pMin[i] - ray.origin[i]) * invDir[i],
(pMax[i] - ray.origin[i]) * invDir[i]);
tenter = std::max(tenter, tmin);
texit = std::min(texit, tmax);
}
if (tenter<texit && texit>0)
return true;
return false;
}
1.4 getIntersection(BVHBuildNode* node, const Ray ray)in BVH.cpp
这部分也绝对简略,出错的中央是对于叶子节点返回其中所有物体外表与光线相交的最近点时没写对,这也须要留神相干类的定义。
Intersection BVHAccel::getIntersection(BVHBuildNode* node, const Ray& ray) const
{
// TODO Traverse the BVH to find intersection
Intersection isect;
std::array<int, 3> dirIsNeg;
for (int i=0;i<3;i++)
dirIsNeg[i] = int(ray.direction[i]>0);
if (!node->bounds.IntersectP(ray, ray.direction_inv, dirIsNeg))
return isect; // 若光线与 bounding box 不相交,完结
// 若相交
// 叶子节点,判断节点外部所有物体,返回最近的交点
if (node->left == nullptr && node->right == nullptr)
// return Intersect(ray); *Wrong.
return node->object->getIntersection(ray); //Pay attention.
Intersection hit1 = getIntersection(node->left, ray);
Intersection hit2 = getIntersection(node->right, ray);
// 若节点不是叶子节点,返回两个子节点交点的最近值
if (hit1.distance < hit2.distance)
return hit1;
else
return hit2;
}
1.5 根底局部实现成果