aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2020-12-20 16:50:33 +0100
committerladevieq <ladevie.quentin@gmail.com>2020-12-20 16:50:33 +0100
commitfc601fd503ec1ebd08e4daffeb9aa0812aff2b71 (patch)
tree63e428b39a9c5e829bcf232e13bbccda07bc4449 /src
parentfa931762847d46baf8eb3ff2f53cab74ca34ef0e (diff)
Render a sphere
Fix errors in the renderer
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp45
-rw-r--r--src/vec3.cpp21
2 files changed, 47 insertions, 19 deletions
diff --git a/src/main.cpp b/src/main.cpp
index b964aeb..e2ef071 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,42 +4,63 @@
#include "vec3.hpp"
#include "ray.hpp"
+color ground_color { 1.0, 1.0, 1.0 };
+color sky_color { 0.5, 0.7, 1.0 };
+
+bool hit_sphere(const point3& sphere_pos, double sphere_radius, const ray& r) {
+ // Vector from the ray origin to sphere position
+ vec3 origin_center = r.origin - sphere_pos;
+ double a = r.direction.dot(r.direction);
+ double b = 2.0 * r.direction.dot(origin_center);
+ double c = origin_center.dot(origin_center) - sphere_radius * sphere_radius;
+ double discriminant = b * b - 4.0 * a * c;
+
+ return (discriminant > 0);
+}
+
color ray_color(ray& r) {
auto unit_dir = r.direction.unit();
auto t = (unit_dir.y + 1.0) * 0.5;
- return lerp(color(1.0, 1.0, 1.0), color(0.5, 0.7, 1.0), t);
+ auto c = lerp(ground_color, sky_color, t);
+
+ if (hit_sphere(point3{ 0.0, 0.0, -1.0 }, .5, r)) {
+ c = color { 1.0, 0.0, 0.0 };
+ }
+
+ return c;
}
int main() {
// Image dimensions
+ const double aspect_ratio = 16.0 / 9.0;
const size_t width = 400;
- const size_t height = width / (16.0 / 9.0);
+ const size_t height = width / aspect_ratio;
// const size_t width = 256;
// const size_t height = 256;
- const double aspect_ratio = width / height;
+ // const double aspect_ratio = width / height;
// Viewport dimensions
- const double viewport_width = 2.0;
- const double viewport_height = viewport_width / aspect_ratio;
+ const double viewport_height = 2.0;
+ const double viewport_width = viewport_height * aspect_ratio;
// Unit vectors for viewport pixels
const vec3 horizontal { viewport_width / width, 0.0, 0.0 };
const vec3 vertical { 0.0, viewport_height / height, 0.0 };
- std::cerr << "Horizontal : " << horizontal << std::endl;
- std::cerr << "Vertical : " << vertical << std::endl;
// We use a right handed coord system
const point3 eye_pos {};
const double focal_length = 1.0;
- const point3 viewport_first_pixel =
- horizontal * -(viewport_width / 2.0) +
- vertical * -(viewport_height / 2.0) +
- point3{ 0.0, 0.0, -focal_length };
+ const point3 viewport_first_pixel {
+ -viewport_width / 2.0,
+ -viewport_height / 2.0,
+ -focal_length
+ };
const point3 first_pixel = eye_pos + viewport_first_pixel;
+ std::cerr << viewport_first_pixel << std::endl;
// PPM format header
std::cout << "P3\n" << width << '\n' << height << "\n 255" << std::endl;
@@ -58,6 +79,8 @@ int main() {
ray ray { eye_pos, direction };
write_color(std::cout, ray_color(ray));
+ // write_color(std::cout, color(direction.unit().y, direction.unit().y, direction.unit().y));
+ // write_color(std::cout, color(y.y, y.y, y.y));
}
}
diff --git a/src/vec3.cpp b/src/vec3.cpp
index 8164aeb..fdd8969 100644
--- a/src/vec3.cpp
+++ b/src/vec3.cpp
@@ -42,15 +42,16 @@ double vec3::length_sq() {
return x * x + y * y + z * z;
}
-double vec3::dot(vec3& vec) {
- return x * vec.x + y + vec.y + z + vec.z;
+double vec3::dot(const vec3& vec) const {
+ return x * vec.x + y * vec.y + z * vec.z;
}
-vec3 vec3::cross(vec3& vec) {
- return vec3(y * vec.z - z * vec.y,
- z * vec.x - x * vec.z,
- x * vec.y - y * vec.y
- );
+vec3 vec3::cross(const vec3& vec) const {
+ return vec3(
+ y * vec.z - z * vec.y,
+ z * vec.x - x * vec.z,
+ x * vec.y - y * vec.y
+ );
}
vec3 vec3::unit() {
@@ -74,7 +75,11 @@ vec3 operator*(vec3 u, vec3 v) {
}
vec3 operator*(vec3 u, double scale) {
- return vec3(u.x * scale, u.y * scale, u.z * scale);
+ return vec3(
+ u.x * scale,
+ u.y * scale,
+ u.z * scale
+ );
}
vec3 operator*(double scale, vec3 u) {