diff --git a/out.jpg b/out.jpg index 5660126..1cf4f11 100644 Binary files a/out.jpg and b/out.jpg differ diff --git a/tinyraytracer.cpp b/tinyraytracer.cpp index 385df3a..bf5a582 100644 --- a/tinyraytracer.cpp +++ b/tinyraytracer.cpp @@ -5,6 +5,12 @@ #include #include "geometry.h" +struct Light { + Light(const Vec3f &p, const float &i) : position(p), intensity(i) {} + Vec3f position; + float intensity; +}; + struct Material { Material(const Vec3f &color) : diffuse_color(color) {} Material() : diffuse_color() {} @@ -46,7 +52,7 @@ bool scene_intersect(const Vec3f &orig, const Vec3f &dir, const std::vector &spheres) { +Vec3f cast_ray(const Vec3f &orig, const Vec3f &dir, const std::vector &spheres, const std::vector &lights) { Vec3f point, N; Material material; @@ -54,10 +60,15 @@ Vec3f cast_ray(const Vec3f &orig, const Vec3f &dir, const std::vector &s return Vec3f(0.2, 0.7, 0.8); // background color } - return material.diffuse_color; + float diffuse_light_intensity = 0; + for (size_t i=0; i &spheres) { +void render(const std::vector &spheres, const std::vector &lights) { const int width = 1024; const int height = 768; const int fov = M_PI/2.; @@ -69,7 +80,7 @@ void render(const std::vector &spheres) { float x = (2*(i + 0.5)/(float)width - 1)*tan(fov/2.)*width/(float)height; float y = -(2*(j + 0.5)/(float)height - 1)*tan(fov/2.); Vec3f dir = Vec3f(x, y, -1).normalize(); - framebuffer[i+j*width] = cast_ray(Vec3f(0,0,0), dir, spheres); + framebuffer[i+j*width] = cast_ray(Vec3f(0,0,0), dir, spheres, lights); } } @@ -94,7 +105,10 @@ int main() { spheres.push_back(Sphere(Vec3f( 1.5, -0.5, -18), 3, red_rubber)); spheres.push_back(Sphere(Vec3f( 7, 5, -18), 4, ivory)); - render(spheres); + std::vector lights; + lights.push_back(Light(Vec3f(-20, 20, 20), 1.5)); + + render(spheres, lights); return 0; }