mirror of
https://github.com/ssloy/tinyraytracer.git
synced 2025-01-17 06:08:14 +01:00
adding more spheres to the scene; rudimentary materials
This commit is contained in:
parent
5806eb45e9
commit
c19c430151
BIN
out.jpg
BIN
out.jpg
Binary file not shown.
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 34 KiB |
@ -5,11 +5,18 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include "geometry.h"
|
#include "geometry.h"
|
||||||
|
|
||||||
|
struct Material {
|
||||||
|
Material(const Vec3f &color) : diffuse_color(color) {}
|
||||||
|
Material() : diffuse_color() {}
|
||||||
|
Vec3f diffuse_color;
|
||||||
|
};
|
||||||
|
|
||||||
struct Sphere {
|
struct Sphere {
|
||||||
Vec3f center;
|
Vec3f center;
|
||||||
float radius;
|
float radius;
|
||||||
|
Material material;
|
||||||
|
|
||||||
Sphere(const Vec3f &c, const float &r) : center(c), radius(r) {}
|
Sphere(const Vec3f &c, const float &r, const Material &m) : center(c), radius(r), material(m) {}
|
||||||
|
|
||||||
bool ray_intersect(const Vec3f &orig, const Vec3f &dir, float &t0) const {
|
bool ray_intersect(const Vec3f &orig, const Vec3f &dir, float &t0) const {
|
||||||
Vec3f L = center - orig;
|
Vec3f L = center - orig;
|
||||||
@ -25,16 +32,32 @@ struct Sphere {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Vec3f cast_ray(const Vec3f &orig, const Vec3f &dir, const Sphere &sphere) {
|
bool scene_intersect(const Vec3f &orig, const Vec3f &dir, const std::vector<Sphere> &spheres, Vec3f &hit, Vec3f &N, Material &material) {
|
||||||
float sphere_dist = std::numeric_limits<float>::max();
|
float spheres_dist = std::numeric_limits<float>::max();
|
||||||
if (!sphere.ray_intersect(orig, dir, sphere_dist)) {
|
for (size_t i=0; i < spheres.size(); i++) {
|
||||||
|
float dist_i;
|
||||||
|
if (spheres[i].ray_intersect(orig, dir, dist_i) && dist_i < spheres_dist) {
|
||||||
|
spheres_dist = dist_i;
|
||||||
|
hit = orig + dir*dist_i;
|
||||||
|
N = (hit - spheres[i].center).normalize();
|
||||||
|
material = spheres[i].material;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return spheres_dist<1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vec3f cast_ray(const Vec3f &orig, const Vec3f &dir, const std::vector<Sphere> &spheres) {
|
||||||
|
Vec3f point, N;
|
||||||
|
Material material;
|
||||||
|
|
||||||
|
if (!scene_intersect(orig, dir, spheres, point, N, material)) {
|
||||||
return Vec3f(0.2, 0.7, 0.8); // background color
|
return Vec3f(0.2, 0.7, 0.8); // background color
|
||||||
}
|
}
|
||||||
|
|
||||||
return Vec3f(0.4, 0.4, 0.3);
|
return material.diffuse_color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(const Sphere &sphere) {
|
void render(const std::vector<Sphere> &spheres) {
|
||||||
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.;
|
||||||
@ -46,7 +69,7 @@ void render(const Sphere &sphere) {
|
|||||||
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, sphere);
|
framebuffer[i+j*width] = cast_ray(Vec3f(0,0,0), dir, spheres);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,8 +85,16 @@ void render(const Sphere &sphere) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Sphere sphere(Vec3f(-3, 0, -16), 2);
|
Material ivory(Vec3f(0.4, 0.4, 0.3));
|
||||||
render(sphere);
|
Material red_rubber(Vec3f(0.3, 0.1, 0.1));
|
||||||
|
|
||||||
|
std::vector<Sphere> spheres;
|
||||||
|
spheres.push_back(Sphere(Vec3f(-3, 0, -16), 2, ivory));
|
||||||
|
spheres.push_back(Sphere(Vec3f(-1.0, -1.5, -12), 2, 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));
|
||||||
|
|
||||||
|
render(spheres);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user