Add a simple camera and antialiasing
This commit is contained in:
parent
ec322b7c16
commit
d2a6aec56c
23
camera.c
Normal file
23
camera.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include "camera.h"
|
||||||
|
|
||||||
|
void camera_init(Camera *camera, double aspect_ratio) {
|
||||||
|
double viewport_height = 2;
|
||||||
|
double viewport_width = aspect_ratio * viewport_height;
|
||||||
|
double focal_length = 1.0;
|
||||||
|
|
||||||
|
camera->origin = (Point3){0, 0, 0};
|
||||||
|
camera->horizontal = (Vec3){viewport_width, 0, 0};
|
||||||
|
camera->vertical = (Vec3){0, viewport_height, 0};
|
||||||
|
Vec3 offset =
|
||||||
|
vec3_add(vec3_div(camera->horizontal, 2), vec3_div(camera->vertical, 2));
|
||||||
|
offset = vec3_add(offset, (Vec3){0, 0, focal_length});
|
||||||
|
camera->lower_left_corner = point3_add(camera->origin, vec3_neg(offset));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ray camera_get_ray(const Camera *camera, double u, double v) {
|
||||||
|
Point3 screen_point = point3_add(
|
||||||
|
camera->lower_left_corner,
|
||||||
|
vec3_add(vec3_mul(u, camera->horizontal), vec3_mul(v, camera->vertical)));
|
||||||
|
Vec3 direction = point3_sub(screen_point, camera->origin);
|
||||||
|
return (Ray){camera->origin, direction};
|
||||||
|
}
|
||||||
19
camera.h
Normal file
19
camera.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef INCLUDED_CAMERA_H
|
||||||
|
#define INCLUDED_CAMERA_H
|
||||||
|
|
||||||
|
#include "point3.h"
|
||||||
|
#include "ray.h"
|
||||||
|
#include "vec3.h"
|
||||||
|
|
||||||
|
typedef struct Camera {
|
||||||
|
Point3 origin;
|
||||||
|
Point3 lower_left_corner;
|
||||||
|
Vec3 horizontal;
|
||||||
|
Vec3 vertical;
|
||||||
|
} Camera;
|
||||||
|
|
||||||
|
void camera_init(Camera *camera, double aspect_ratio);
|
||||||
|
|
||||||
|
Ray camera_get_ray(const Camera *camera, double u, double v);
|
||||||
|
|
||||||
|
#endif /* INCLUDED_CAMERA_H */
|
||||||
20
color.c
20
color.c
@ -1,6 +1,12 @@
|
|||||||
#include "color.h"
|
#include "color.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
Color color_add(Color c1, Color c2) {
|
||||||
|
return (Color){c1.r + c2.r, c1.g + c2.g, c1.b + c2.b};
|
||||||
|
}
|
||||||
|
|
||||||
Color color_lerp(Color c1, Color c2, double t) {
|
Color color_lerp(Color c1, Color c2, double t) {
|
||||||
return (Color){
|
return (Color){
|
||||||
(1.0 - t) * c1.r + t * c2.r,
|
(1.0 - t) * c1.r + t * c2.r,
|
||||||
@ -9,7 +15,15 @@ Color color_lerp(Color c1, Color c2, double t) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void color_write(FILE *out, Color c) {
|
void color_write(FILE *out, Color c, int samples_per_pixel) {
|
||||||
fprintf(out, "%d %d %d\n", (int)(255.999 * c.r), (int)(255.999 * c.g),
|
double scale = 1.0 / samples_per_pixel;
|
||||||
(int)(255.999 * c.b));
|
double r = c.r * scale;
|
||||||
|
double g = c.g * scale;
|
||||||
|
double b = c.b * scale;
|
||||||
|
|
||||||
|
int ir = (int)(256 * clamp(r, 0.0, 0.999));
|
||||||
|
int ig = (int)(256 * clamp(g, 0.0, 0.999));
|
||||||
|
int ib = (int)(256 * clamp(b, 0.0, 0.999));
|
||||||
|
|
||||||
|
fprintf(out, "%d %d %d\n", ir, ig, ib);
|
||||||
}
|
}
|
||||||
|
|||||||
4
color.h
4
color.h
@ -7,8 +7,10 @@ typedef struct Color {
|
|||||||
double r, g, b;
|
double r, g, b;
|
||||||
} Color;
|
} Color;
|
||||||
|
|
||||||
|
Color color_add(Color c1, Color c2);
|
||||||
|
|
||||||
Color color_lerp(Color c1, Color c2, double t);
|
Color color_lerp(Color c1, Color c2, double t);
|
||||||
|
|
||||||
void color_write(FILE *out, Color c);
|
void color_write(FILE *out, Color c, int samples_per_pixel);
|
||||||
|
|
||||||
#endif /* INCLUDED_COLOR_H */
|
#endif /* INCLUDED_COLOR_H */
|
||||||
|
|||||||
56
main.c
56
main.c
@ -3,10 +3,12 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "camera.h"
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "hittable.h"
|
#include "hittable.h"
|
||||||
#include "point3.h"
|
#include "point3.h"
|
||||||
#include "ray.h"
|
#include "ray.h"
|
||||||
|
#include "utils.h"
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
|
||||||
Color ray_color(Ray r, Hittable world) {
|
Color ray_color(Ray r, Hittable world) {
|
||||||
@ -27,54 +29,48 @@ int main(void) {
|
|||||||
const double aspect_ratio = 16.0 / 9.0;
|
const double aspect_ratio = 16.0 / 9.0;
|
||||||
const int image_width = 256;
|
const int image_width = 256;
|
||||||
const int image_height = (int)(image_width / aspect_ratio);
|
const int image_height = (int)(image_width / aspect_ratio);
|
||||||
|
const int samples_per_pixel = 100;
|
||||||
|
|
||||||
/* World */
|
/* World */
|
||||||
HittableList world = {0};
|
HittableList object_list = {0};
|
||||||
hittable_list_add(&world, make_hittable_sphere(&(Sphere){
|
hittable_list_add(&object_list, make_hittable_sphere(&(Sphere){
|
||||||
.center = (Point3){0, 0, -1},
|
.center = (Point3){0, 0, -1},
|
||||||
.radius = 0.5,
|
.radius = 0.5,
|
||||||
}));
|
}));
|
||||||
hittable_list_add(&world, make_hittable_sphere(&(Sphere){
|
hittable_list_add(&object_list, make_hittable_sphere(&(Sphere){
|
||||||
.center = (Point3){0, -100.5, -1},
|
.center = (Point3){0, -100.5, -1},
|
||||||
.radius = 100,
|
.radius = 100,
|
||||||
}));
|
}));
|
||||||
|
Hittable world = make_hittable_list(&object_list);
|
||||||
|
|
||||||
/* Camera parameters */
|
/* Camera */
|
||||||
double viewport_height = 2;
|
Camera camera;
|
||||||
double viewport_width = aspect_ratio * viewport_height;
|
camera_init(&camera, aspect_ratio);
|
||||||
double focal_length = 1.0;
|
|
||||||
|
|
||||||
Point3 origin = {0};
|
|
||||||
Vec3 horizontal = {viewport_width, 0, 0};
|
|
||||||
Vec3 vertical = {0, viewport_height, 0};
|
|
||||||
Vec3 offset = vec3_add(vec3_div(horizontal, 2), vec3_div(vertical, 2));
|
|
||||||
offset = vec3_add(offset, (Vec3){0, 0, focal_length});
|
|
||||||
Point3 lower_left_corner = point3_add(origin, vec3_neg(offset));
|
|
||||||
|
|
||||||
printf("P3\n%u %u\n255\n", image_width, image_height);
|
printf("P3\n%u %u\n255\n", image_width, image_height);
|
||||||
|
|
||||||
for (int j = image_height - 1; j >= 0; --j) {
|
for (int j = image_height - 1; j >= 0; --j) {
|
||||||
fprintf(stderr, "\rScanlines remaining: %d ", j);
|
fprintf(stderr, "\rScanlines remaining: %d ", j);
|
||||||
for (int i = 0; i < image_width; ++i) {
|
for (int i = 0; i < image_width; ++i) {
|
||||||
double u = (double)i / (image_width - 1);
|
Color pixel_color = {0, 0, 0};
|
||||||
double v = (double)j / (image_height - 1);
|
for (int s = 0; s < samples_per_pixel; ++s) {
|
||||||
Point3 screen_point =
|
double u = (i + random_double()) / (image_width - 1);
|
||||||
point3_add(lower_left_corner,
|
double v = (j + random_double()) / (image_height - 1);
|
||||||
vec3_add(vec3_mul(u, horizontal), vec3_mul(v, vertical)));
|
Ray r = camera_get_ray(&camera, u, v);
|
||||||
Vec3 direction = point3_sub(screen_point, origin);
|
pixel_color = color_add(pixel_color, ray_color(r, world));
|
||||||
Ray r = {origin, direction};
|
}
|
||||||
Color pixel_color = ray_color(r, make_hittable_list(&world));
|
color_write(stdout, pixel_color, samples_per_pixel);
|
||||||
color_write(stdout, pixel_color);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "\nDone.\n");
|
fprintf(stderr, "\nDone.\n");
|
||||||
|
|
||||||
hittable_list_free(&world);
|
hittable_list_free(&object_list);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "camera.c"
|
||||||
#include "color.c"
|
#include "color.c"
|
||||||
#include "hittable.c"
|
#include "hittable.c"
|
||||||
#include "point3.c"
|
#include "point3.c"
|
||||||
|
|||||||
17
utils.c
17
utils.c
@ -1,7 +1,20 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
double degrees_to_radians(double degrees) {
|
double degrees_to_radians(double degrees) { return degrees * M_PI / 180.0; }
|
||||||
return degrees * M_PI / 180.0;
|
|
||||||
|
double random_double(void) { return rand() / (RAND_MAX + 1.0); }
|
||||||
|
|
||||||
|
double random_double_in_range(double min, double max) {
|
||||||
|
return min + (max - min) * random_double();
|
||||||
|
}
|
||||||
|
|
||||||
|
double clamp(double x, double min, double max) {
|
||||||
|
if (x < min)
|
||||||
|
return min;
|
||||||
|
if (x > max)
|
||||||
|
return max;
|
||||||
|
return x;
|
||||||
}
|
}
|
||||||
|
|||||||
5
utils.h
5
utils.h
@ -3,4 +3,9 @@
|
|||||||
|
|
||||||
double degrees_to_radians(double degrees);
|
double degrees_to_radians(double degrees);
|
||||||
|
|
||||||
|
double random_double(void);
|
||||||
|
double random_double_in_range(double min, double max);
|
||||||
|
|
||||||
|
double clamp(double x, double min, double max);
|
||||||
|
|
||||||
#endif /* INCLUDED_UTILS_H */
|
#endif /* INCLUDED_UTILS_H */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user