#include #include #include #include "vec3.h" #include "ray.h" using namespace std; //skybox float hit_sphere(const vec3 ¢er, float radius, const ray& r) { float a, b, c; a = dot(r.D, r.D); b = 2 * (dot(r.D, (r.O - center))); c = dot(r.O - center, r.O - center) - radius * radius; return (((-b) + sqrt((b * b) - (4 * a * c))) / (2 * a)); } vec3 color(ray& r){ vec3 center = vec3(0, 0, -1); float t = hit_sphere(center, 0.5, r); if (t > 0.0) { vec3 N = unit_vector(r.point_at_parameter(t) - center); return 0.5 * vec3(N.x()+1, N.y()+1, N.z()+1); } vec3 unit_direction = unit_vector(r.direction()); t = 0.5 * (unit_direction.y() + 1.0); return (1.0-t)* vec3(1, 1, 1) + t* vec3(0.5, 0.7, 1.0); } int main(){ int width = 200; int height = 100; fstream file; file.open("ray.ppm", ios::out); vec3 lower_left_corner(-2, -1, -1); vec3 origin(0, 0, 0); vec3 horizontal(4, 0, 0); vec3 vertical(0, 2, 0); file << "P3\n" << width << " " << height << "\n255\n"; for (int j = height - 1; j >= 0; j--) { for (int i = 0; i < width; i++) { float u = float(i) / float(width); float v = float(j) / float(height); ray r = ray(origin, lower_left_corner + u*horizontal + v*vertical); vec3 color2 = color(r); file << int(color2.r() * 255) << " " << int(color2.g() * 255) << " " << int(color2.b() * 255) << "\n"; } } file.close(); }