1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| auto sph2 = std::make_unique<Sphere>(Vector3f(0.5, -0.5, -8), 1.5); sph2->ior = 1.5; sph2->materialType = REFLECTION_AND_REFRACTION; scene.Add(std::move(mesh)); scene.Add(std::make_unique<Light>(Vector3f(-20, 70, 20), 0.5)); scene.Add(std::make_unique<Light>(Vector3f(30, 50, -12), 0.5));
void Renderer::Render(const Scene& scene) { std::vector<Vector3f> framebuffer(scene.width * scene.height); std::vector<Vector3f> framebuffer2(scene.width * scene.height);
float scale = std::tan(deg2rad(scene.fov * 0.5f)); float imageAspectRatio = scene.width / (float)scene.height;
Vector3f eye_pos(0); int m = 0; for (int j = 0; j < scene.height; ++j) { for (int i = 0; i < scene.width; ++i) { float x; float y; x = (float)i / scene.width - 0.5; y = (float)(scene.height - j) / scene.height - 0.5; x *= scale * imageAspectRatio; y *= scale;
Vector3f dir = Vector3f(x, y, -1); dir = normalize(dir); framebuffer[m++] = castRay(eye_pos, dir, scene, 0); } UpdateProgress(j / (float)scene.height); }
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); }
Vector3f castRay( const Vector3f &orig, const Vector3f &dir, const Scene& scene, int depth) { if (depth > scene.maxDepth) { return Vector3f(0.0,0.0,0.0); }
Vector3f hitColor = scene.backgroundColor; if (auto payload = trace(orig, dir, scene.get_objects()); payload) { Vector3f hitPoint = orig + dir * payload->tNear; Vector3f N; Vector2f st; payload->hit_obj->getSurfaceProperties(hitPoint, dir, payload->index, payload->uv, N, st); switch (payload->hit_obj->materialType) { case REFLECTION_AND_REFRACTION: { Vector3f reflectionDirection = normalize(reflect(dir, N)); Vector3f refractionDirection = normalize(refract(dir, N, payload->hit_obj->ior)); Vector3f reflectionRayOrig = (dotProduct(reflectionDirection, N) < 0) ? hitPoint - N * scene.epsilon : hitPoint + N * scene.epsilon; Vector3f refractionRayOrig = (dotProduct(refractionDirection, N) < 0) ? hitPoint - N * scene.epsilon : hitPoint + N * scene.epsilon; Vector3f reflectionColor = castRay(reflectionRayOrig, reflectionDirection, scene, depth + 1); Vector3f refractionColor = castRay(refractionRayOrig, refractionDirection, scene, depth + 1); float kr = fresnel(dir, N, payload->hit_obj->ior); hitColor = reflectionColor * kr + refractionColor * (1 - kr); break; } case REFLECTION: { float kr = fresnel(dir, N, payload->hit_obj->ior); Vector3f reflectionDirection = reflect(dir, N); Vector3f reflectionRayOrig = (dotProduct(reflectionDirection, N) < 0) ? hitPoint + N * scene.epsilon : hitPoint - N * scene.epsilon; hitColor = castRay(reflectionRayOrig, reflectionDirection, scene, depth + 1) * kr; break; } default: { Vector3f lightAmt = 0, specularColor = 0; Vector3f shadowPointOrig = (dotProduct(dir, N) < 0) ? hitPoint + N * scene.epsilon : hitPoint - N * scene.epsilon; for (auto& light : scene.get_lights()) { Vector3f lightDir = light->position - hitPoint; float lightDistance2 = dotProduct(lightDir, lightDir); lightDir = normalize(lightDir); float LdotN = std::max(0.f, dotProduct(lightDir, N)); auto shadow_res = trace(shadowPointOrig, lightDir, scene.get_objects()); bool inShadow = shadow_res && (shadow_res->tNear * shadow_res->tNear < lightDistance2);
lightAmt += inShadow ? 0 : light->intensity * LdotN; Vector3f reflectionDirection = reflect(-lightDir, N);
specularColor += powf(std::max(0.f, -dotProduct(reflectionDirection, dir)), payload->hit_obj->specularExponent) * light->intensity; }
hitColor = lightAmt * payload->hit_obj->evalDiffuseColor(st) * payload->hit_obj->Kd + specularColor * payload->hit_obj->Ks; break; } } }
return hitColor; }
|