feat: add sphere
This commit is contained in:
parent
2d913f4fa2
commit
ddad81b6f8
48
main.cc
48
main.cc
@ -1,20 +1,60 @@
|
||||
#include "color.h"
|
||||
#include "ray.h"
|
||||
#include "vec3.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
double hit_sphere(const point3& center, double radius, const ray& r) {
|
||||
vec3 oc = r.origin() - center;
|
||||
auto a = r.direction().length_squared();
|
||||
auto half_b = dot(oc, r.direction());
|
||||
auto c = oc.length_squared() - radius*radius;
|
||||
auto discriminant = half_b*half_b - a*c;
|
||||
|
||||
if (discriminant < 0) {
|
||||
return -1.0;
|
||||
} else {
|
||||
return (-half_b - sqrt(discriminant)) / a;
|
||||
}
|
||||
}
|
||||
|
||||
color ray_color(const ray& r) {
|
||||
auto t = hit_sphere(point3(0, 0, -1), 0.5, r);
|
||||
if (t > 0.0) {
|
||||
vec3 N = unit_vector(r.at(t) - vec3(0, 0, -1));
|
||||
return 0.5*color(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)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Image
|
||||
const int image_width = 256;
|
||||
const int image_height = 256;
|
||||
const auto aspect_ratio = 16.0 / 9.0;
|
||||
const int image_width = 400;
|
||||
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
||||
|
||||
// Camera
|
||||
auto viewport_height = 2.0;
|
||||
auto viewport_width = aspect_ratio * viewport_height;
|
||||
auto focal_length = 1.0;
|
||||
|
||||
auto origin = point3(0, 0, 0);
|
||||
auto horizontal = vec3(viewport_width, 0, 0);
|
||||
auto vertical = vec3(0, viewport_height, 0);
|
||||
auto lower_left_corner = origin - horizontal/2 - vertical/2 - vec3(0, 0, focal_length);
|
||||
|
||||
// Render
|
||||
std::cout<<"P3\n"<<image_width<<" "<<image_height<<"\n255\n";
|
||||
for (int j = image_height-1; j >= 0; --j) {
|
||||
std::cerr<<"\rScanlines remaining: "<<j<<" "<<std::flush;
|
||||
for (int i = 0; i < image_width; ++i) {
|
||||
color pixel_color(double(i) / (image_width-1), double(j) / (image_height-1), 0.25);
|
||||
|
||||
auto u = double(i) / (image_width-1);
|
||||
auto v = double(j) / (image_height-1);
|
||||
ray r(origin, lower_left_corner + u*horizontal + v*vertical - origin);
|
||||
color pixel_color = ray_color(r);
|
||||
write_color(std::cout, pixel_color);
|
||||
}
|
||||
}
|
||||
|
25
ray.h
Normal file
25
ray.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef RAY_H
|
||||
#define RAY_H
|
||||
|
||||
#include "vec3.h"
|
||||
|
||||
class ray {
|
||||
public:
|
||||
ray() {}
|
||||
ray(const point3& origin, const vec3& direction)
|
||||
: orig(origin), dir(direction)
|
||||
{}
|
||||
|
||||
point3 origin() const { return orig; }
|
||||
vec3 direction() const { return dir; }
|
||||
|
||||
point3 at(double t) const {
|
||||
return orig + t*dir;
|
||||
}
|
||||
|
||||
public:
|
||||
point3 orig;
|
||||
vec3 dir;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user