mirror of
https://github.com/ssloy/tinyraytracer.git
synced 2025-09-03 02:53:17 +02:00
diffuse lighting
This commit is contained in:
BIN
out.jpg
BIN
out.jpg
Binary file not shown.
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 46 KiB |
@@ -5,6 +5,12 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include "geometry.h"
|
#include "geometry.h"
|
||||||
|
|
||||||
|
struct Light {
|
||||||
|
Light(const Vec3f &p, const float &i) : position(p), intensity(i) {}
|
||||||
|
Vec3f position;
|
||||||
|
float intensity;
|
||||||
|
};
|
||||||
|
|
||||||
struct Material {
|
struct Material {
|
||||||
Material(const Vec3f &color) : diffuse_color(color) {}
|
Material(const Vec3f &color) : diffuse_color(color) {}
|
||||||
Material() : diffuse_color() {}
|
Material() : diffuse_color() {}
|
||||||
@@ -46,7 +52,7 @@ bool scene_intersect(const Vec3f &orig, const Vec3f &dir, const std::vector<Sphe
|
|||||||
return spheres_dist<1000;
|
return spheres_dist<1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3f cast_ray(const Vec3f &orig, const Vec3f &dir, const std::vector<Sphere> &spheres) {
|
Vec3f cast_ray(const Vec3f &orig, const Vec3f &dir, const std::vector<Sphere> &spheres, const std::vector<Light> &lights) {
|
||||||
Vec3f point, N;
|
Vec3f point, N;
|
||||||
Material material;
|
Material material;
|
||||||
|
|
||||||
@@ -54,10 +60,15 @@ Vec3f cast_ray(const Vec3f &orig, const Vec3f &dir, const std::vector<Sphere> &s
|
|||||||
return Vec3f(0.2, 0.7, 0.8); // background color
|
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<lights.size(); i++) {
|
||||||
|
Vec3f light_dir = (lights[i].position - point).normalize();
|
||||||
|
diffuse_light_intensity += lights[i].intensity * std::max(0.f, light_dir*N);
|
||||||
|
}
|
||||||
|
return material.diffuse_color * diffuse_light_intensity;
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(const std::vector<Sphere> &spheres) {
|
void render(const std::vector<Sphere> &spheres, const std::vector<Light> &lights) {
|
||||||
const int width = 1024;
|
const int width = 1024;
|
||||||
const int height = 768;
|
const int height = 768;
|
||||||
const int fov = M_PI/2.;
|
const int fov = M_PI/2.;
|
||||||
@@ -69,7 +80,7 @@ void render(const std::vector<Sphere> &spheres) {
|
|||||||
float x = (2*(i + 0.5)/(float)width - 1)*tan(fov/2.)*width/(float)height;
|
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.);
|
float y = -(2*(j + 0.5)/(float)height - 1)*tan(fov/2.);
|
||||||
Vec3f dir = Vec3f(x, y, -1).normalize();
|
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( 1.5, -0.5, -18), 3, red_rubber));
|
||||||
spheres.push_back(Sphere(Vec3f( 7, 5, -18), 4, ivory));
|
spheres.push_back(Sphere(Vec3f( 7, 5, -18), 4, ivory));
|
||||||
|
|
||||||
render(spheres);
|
std::vector<Light> lights;
|
||||||
|
lights.push_back(Light(Vec3f(-20, 20, 20), 1.5));
|
||||||
|
|
||||||
|
render(spheres, lights);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user