// 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; 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 = (2 * (i + 0.5) / (float)scene.width - 1) * imageAspectRatio * scale; y = (1 - 2 * (j + 0.5) / (float)scene.height) * scale; Vector3f dir = Vector3f(x, y, -1) - eye_pos; // Don't forget to normalize this direction! dir = normalize(dir); framebuffer[m++] = castRay(eye_pos, dir, scene, 0); } //UpdateProgress(j / (float)scene.height); }
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; auto e2 = v2 - v0; auto s = orig - v0; auto s1 = crossProduct(dir, e2); auto s2 = crossProduct(s, e1); auto coeff = 1 / dotProduct(s1, e1); auto t = coeff * dotProduct(s2, e2); auto b1 = coeff * dotProduct(s1, s); auto b2 = coeff * dotProduct(s2, dir); if (t >= 0 && b1 >= 0 && b2 >= 0 && (1 - b1 - b2) >= 0) { tnear = t; u = b1; v = b2; returntrue; } returnfalse; }