boolrayTriangleIntersect(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);
//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=(((i+0.5f)/static_cast<float>(scene.width-1)*2.0f)-1.0f)*scale*imageAspectRatio; y=(1.0f-(j+0.5f)/static_cast<float>(scene.height-1)*2.0f)*scale; //其实就是把"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); }