Assignment5的要求
在這次作業中,我們需要實現兩個部分:光線的生成和光線與三角的相交。本次代碼框架的工作流程為:
- 從main 函數開始。我們定義場景的參數,添加物體(球體或三形)到場景中,並設置其材質,然后將光源添加到場景中。
- 調用Render(scene) 函數。在遍歷所有像素的循環里,生成對應的光線並將返回的顏色保存在幀緩沖區(framebuffer)中。在渲染過程結束后,幀緩沖區中的信息將被保存為圖像。
- 在生成像素對應的光線后,我們調用CastRay 函數,該函數調用trace 來查詢光線與場景中最近的對象的交點。
- 然后,我們在此交點執行着色。我們設置了三種不同的着色情況,並且已經為你提供了代碼。
你需要修改的函數是:
- Renderer.cpp 中的Render():這里你需要為每個像素生成一條對應的光線,然后調用函數castRay() 來得到顏色,最后將顏色存儲在幀緩沖區的相應像素中。
- Triangle.hpp 中的rayTriangleIntersect(): v0, v1, v2 是三角形的三個頂點,orig 是光線的起點,dir 是光線單位化的方向向量。tnear, u, v 是你需要使用我們課上推導的Moller-Trumbore 算法來更新的參數。
具體步驟
1.光線生成:實現光線生成部分,並且能夠看到圖像中的兩個球體。
需要注意的是此時的相機是看向負z方向的。此處的[i,j]即為成像平面上的像素位置,目的是為了得到實際的坐標。首先要將像素水平坐標i除以圖像寬度從而將其映射回[0,1],然后乘以縮放因子和圖像寬高比將其映射回[-1,1] ,對y同樣處理,但是y需要進行翻轉。
// [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 = 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
// 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*
float x=(2 * (i + 0.5) / (float)scene.width - 1)*scale*imageAspectRatio;
float y=(1 - 2 * (j + 0.5) / (float)scene.height)*scale;
Vector3f dir = Vector3f(x, y, -1); // Don't forget to normalize this direction!
dir=normalize(dir);
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);
}
2.光線與三角形相交:實現了Moller-Trumbore 算法,並且能夠看到圖像中的地面。
如上圖所示,逐個帶入計算即可。
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.
auto e1=v1-v0,e2=v2-v0,s=orig-v0;
auto s1=crossProduct(dir,e2),s2=crossProduct(s,e1);
float co=1/(dotProduct(s1,e1));
float t=co*dotProduct(s2,e2);
float b1=co*dotProduct(s1,s);
float b2=co*dotProduct(s2,dir);
if(t>0.0 && b1>0.0 && b2>0.0 && (1-b1-b2)>=0.0){
tnear=t;
u=b1;
v=b2;
return true;
}
return false;
}