目录
作业要求
Render()
这一部分要实现生成一条从人眼到当前像素看过去的光线,要理解这部分强烈推荐下面的链接,图文并茂,非常详尽。如果懒得看英文可以选择谷歌翻译或者直接看图其实效果也就到了。
Ray-Tracing: Generating Camera Rays (Generating Camera Rays)
下面我来简单翻译一下解读一下。最开始我们的i,j是针对光栅化空间而言的,如下图所示。这里建议首先将i+0.5,j+0.5,得到当前像素中心坐标。
而我们要得到的是在世界坐标下,从相机(即原点,通过Vector3f eye_pos(0)进行赋值)到当前屏幕上像素的一条法线,在已经知道相机/人眼坐标为原点的情况下,只需要求出当前屏幕像素在世界坐标下的坐标即可。所以我们要进行Raster space ---> NDC space ----> Screen space ---> Wordl space的变化。
NDC space是将原本的像素坐标i,j均压缩到[0, 1]后的坐标。要实现这个变换,需要将原本的i+0.5,j+0.5分别除以对应纬度的长度:
在之后,则需要变换坐标轴位置,实现如下的屏幕空间坐标。
i方向即宽方向,[0,1]->[-1,1],因为变化是线性的,很容易得到 i = 2*i-1。
j方向即高方向,[0,1]->[1,-1],因为变化是线性的,很容易得到 i = -2*i+1。代入前面的变化,则得:
此时横纵坐标均分布在[-1,1],但是这并不是世界坐标下图像真实映射过来的位置,原因有二:
①横纵方向的长度并不是1:1的;
②物体本身并不是在[-1,1]的,[-1,1]是缩放后的范围。
要解决这个问题需要先理解代码中给出的scale和imageAspectRatio:
①imageAspectRatio即宽高比,是原本scene的宽高比,所以要想恢复原始的比例需要用 宽 * 宽高比,即 x * imageAspectRatio。
②scale为一半可看角度(fov/2)的tan值,即下图tan(α/2),它代表了原本图像屏幕高度与图像到X-Y平面距离的比值,又由代码已给出的 Vector3f dir = Vector3f(x, y, -1) ,可以知道原本的图像屏幕距离X-Y平面为1,所以scale即为原本的图像高度,而现在的图像高度为1,所以令x,y同时乘以scale即可得到原始世界坐标下的坐标。
综上,直接给出以下代码。顺便,注意在本次编程里,我们采用了Vector.hpp中的Vector3f类存储向量,在该类里已经给出了归一化向量的方法normalize,注释里要求的对dir进行归一化只需调用此函数。
// [comment]
// The main render function. This where we iterate over all pixels in the image, generate
// primary rays and cast these rays into the scene. The content of the framebuffer is
// saved to a file.
// [/comment]
void Renderer::Render(const Scene& scene)
{
std::vector<Vector3f> framebuffer(scene.width * scene.height);
//
float scale = std::tan(deg2rad(scene.fov * 0.5f));
//宽高比
float imageAspectRatio = (float)scene.width / (float)scene.height;
// Use this variable as the eye position to start your rays.
Vector3f eye_pos(0);
int m = 0;
for (int j = 0; j < scene.height; ++j)
{
for (int i = 0; i < scene.width; ++i)
{
// generate primary ray direction
float x = (2*(((float)i+0.5)/scene.width-1) * imageAspectRatio * scale;
float y = (1.0-2*((float)j+0.5)/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*
//vector = op,说明scene在z=-1,故znear距离人眼距离=1
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);
}
// 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] = (char)(255 * clamp(0, 1, framebuffer[i].x));
color[1] = (char)(255 * clamp(0, 1, framebuffer[i].y));
color[2] = (char)(255 * clamp(0, 1, framebuffer[i].z));
fwrite(color, 1, 3, fp);
}
fclose(fp);
}
rayTriangleIntersect()
这一部分只要根据闫老师课上讲的公式进行代入即可,注意查看Vector3f类中关于向量操作的相关函数的使用。三角形与光线相交的条件即为t、u、v、1-u-v均为正数/非负数。
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.
Vector3f E1 = v1-v0, E2 = v2-v0, S = orig-v0;
Vector3f S1 = crossProduct(dir,E2), S2 = crossProduct(S,E1);
if (dotProduct(S1,E1)<=0) return false;
tnear = dotProduct(S2,E2)/dotProduct(S1,E1);
u = dotProduct(S1,S)/dotProduct(S1,E1);
v = dotProduct(S2,dir)/dotProduct(S1,E1);
if(tnear>=0 && u>=0 && v>=0 && (1-u-v)>=0) return true;
return false;
}
最终结果:
参考链接
Ray-Tracing: Generating Camera Rays (Generating Camera Rays)