2024-08-11 22:29:55 -05:00

201 lines
7.0 KiB
C

#define CANVAS_MAX 2048
#include "tuples.h"
#include "canvas.h"
#include "rays.h"
#include "spheres.h"
#include "intersections.h"
#include "transformations.h"
#include "materials.h"
#include "world.h"
#include "camera.h"
static struct canvas _canvas;
int main()
{
struct tuple ray_origin = point(0.0f, 0.0f, -5.0f);
float wall_z = 10.0f;
float wall_size = 7.0f;
int canvas_pixels = 100;
float pixel_size = wall_size / (float)canvas_pixels;
float half = wall_size / 2.0f;
struct shape floor = plane();
floor.material.has_pattern = true;
floor.material.pattern.a = color(1.0f, 0.3f, 0.9f);
floor.material.pattern.b = color(0.2f, 0.9f, 0.5f);
floor.material.pattern.type = PATTERN_CHECKERS;
//floor.material.pattern.transform = scaling(10.0f, 2.0f, 2.0f);
floor.material.specular = 0.0f;
/*
struct shape left_wall = sphere();
{
struct mat4x4 _translation = translation(0.0f, 0.0f, 5.0f);
struct mat4x4 _rotation_y = rotation_y(-pi / 4.0f);
struct mat4x4 _rotation_x = rotation_x(pi / 2.0f);
struct mat4x4 _scaling = scaling(10.0f, 0.05f, 10.0f);
struct mat4x4 t0 = mat4x4_mul_m(_rotation_x, _scaling);
struct mat4x4 t1 = mat4x4_mul_m(_rotation_y, t0);
struct mat4x4 t2 = mat4x4_mul_m(_translation, t1);
left_wall.transform = t2;
}
left_wall.material = floor.material;
struct shape right_wall = sphere();
{
struct mat4x4 _translation = translation(0.0f, 0.0f, 5.0f);
struct mat4x4 _rotation_y = rotation_y(pi / 4.0f);
struct mat4x4 _rotation_x = rotation_x(pi / 2.0f);
struct mat4x4 _scaling = scaling(10.0f, 0.05f, 10.0f);
struct mat4x4 t0 = mat4x4_mul_m(_rotation_x, _scaling);
struct mat4x4 t1 = mat4x4_mul_m(_rotation_y, t0);
struct mat4x4 t2 = mat4x4_mul_m(_translation, t1);
right_wall.transform = t2;
}
right_wall.material = floor.material;
*/
struct shape middle = sphere();
middle.transform = translation(-0.5f, 1.0f, 0.5f);
middle.material.color = color((float)0xfc / 255.f , (float)0x6d / 255.f, (float)0x09 / 255.f);
middle.material.diffuse = 0.7;
middle.material.specular = 0.3;
struct shape right = sphere();
struct mat4x4 right_t = translation(1.5f, 0.5f, -0.5f);
struct mat4x4 right_s = scaling(0.5f, 0.5f, 0.5f);
right.transform = mat4x4_mul_m(right_t, right_s);
right.material.color = color(0.5f, 1.0f, 0.1f);
right.material.diffuse = 0.7;
right.material.specular = 0.3;
struct shape left = sphere();
struct mat4x4 left_t = translation(-1.5f, 0.33f, -0.75f);
struct mat4x4 left_s = scaling(0.33f, 0.33f, 0.33f);
left.transform = mat4x4_mul_m(left_t, left_s);
left.material.color = color((float)0x12 / 255.f, (float)0xc9 / 255.f, (float)0xcc / 255.f);
left.material.diffuse = 0.7;
left.material.specular = 0.3;
struct tuple light_position = point(-10.0f, 10.0f, -10.0f);
struct tuple light_color = color(1.0f, 1.0f, 1.0f);
struct light light = point_light(light_position, light_color);
struct shape h1 = plane();
struct shape h2 = plane();
struct shape h3 = plane();
struct shape h4 = plane();
struct shape h5 = plane();
struct shape h6 = plane();
struct mat4x4 h1_t = translation(0, 8, 8);
struct mat4x4 h2_t = translation(4, 0, 6.9282);
struct mat4x4 h3_t = translation(-4, 0, 6.9282);
struct mat4x4 h4_t = translation(0, -8, 8);
struct mat4x4 h5_t = translation(-4, 0, -6.9282);
struct mat4x4 h6_t = translation(4, 0, -6.9282);
struct mat4x4 h1_r = rotation_x(tau / 4.0f);
struct mat4x4 h2_r = rotation_x(tau / 4.0f);
struct mat4x4 h3_r = rotation_x(tau / 4.0f);
struct mat4x4 h4_r = rotation_x(tau / 4.0f);
struct mat4x4 h5_r = rotation_x(tau / 4.0f);
struct mat4x4 h6_r = rotation_x(tau / 4.0f);
struct mat4x4 h1_rr = rotation_y(tau / 4.0f * 0);
h1.transform = mat4x4_mul_m(h1_t, h1_r);
h1.transform = mat4x4_mul_m(h1_rr, h1.transform);
struct mat4x4 h2_rr = rotation_y(tau / 4.0f * 1);
h2.transform = mat4x4_mul_m(h2_t, h2_r);
h2.transform = mat4x4_mul_m(h2_rr, h2.transform);
struct mat4x4 h3_rr = rotation_y(tau / 4.0f * 2);
h3.transform = mat4x4_mul_m(h3_t, h3_r);
h3.transform = mat4x4_mul_m(h3_rr, h3.transform);
struct mat4x4 h4_rr = rotation_y(tau / 4.0f * 3);
h4.transform = mat4x4_mul_m(h4_t, h4_r);
h4.transform = mat4x4_mul_m(h4_rr, h4.transform);
struct mat4x4 h5_rr = rotation_y(tau / 4.0f);
h5.transform = mat4x4_mul_m(h5_rr, h5_r);
h5.transform = mat4x4_mul_m(h5_t, h5.transform);
struct mat4x4 h6_rr = rotation_y(tau / 4.0f);
h6.transform = mat4x4_mul_m(h6_rr, h6_r);
h6.transform = mat4x4_mul_m(h6_t, h6.transform);
h1.material.specular = 0.0f;
h1.material.color = color(1.0f, 0.0f, 0.0f);
h2.material.specular = 0.0f;
h2.material.color = color(0.0f, 1.0f, 0.0f);
h3.material.specular = 0.0f;
h3.material.color = color(0.0f, 0.0f, 1.0f);
h4.material.specular = 0.0f;
h4.material.color = color(1.0f, 1.0f, 0.0f);
h5.material.specular = 0.0f;
h5.material.color = color(0.0f, 1.0f, 1.0f);
h6.material.specular = 0.0f;
h6.material.color = color(1.0f, 0.0f, 1.0f);
struct world world;
world.light = light;
world.object_count = 4 + 4;
world.objects[0] = floor;
world.objects[1] = middle;
world.objects[2] = right;
world.objects[3] = left;
world.objects[4] = h1;
world.objects[5] = h2;
world.objects[6] = h3;
world.objects[7] = h4;
world.objects[8] = h5;
world.objects[9] = h6;
struct camera _camera = camera(1600.0f, 800.0f, pi / 4.f);
_camera.transform = view_transform(point(0.0f, 1.5f, -5.0f),
point(0.0f, 1.0f, 0.0f),
vector(0.0f, 1.0f, 0.0f));
struct mat4x4 middle_t = world.objects[1].transform;
for (int i = 0; i < 360; i++) {
world.objects[0].material.pattern.transform =
mat4x4_mul_m(rotation_y(-tau / 360 * (float)i * 2.0f), mat4x4_mul_m(translation((float)i / 360.0f * 10.0f, 0.0f, 0.0f), scaling(3.0f, 3.0f, 3.0f)));
_camera.transform = view_transform(point(2.0f * cosf(pi / 360 * (float)i * 0.5),
8.5f,
1.0f * sinf(pi / 360 * (float)i * 0.5)),
point(0.0f, 1.0f, 0.0f),
vector(0.0f, 1.0f, 0.0f));
struct tuple light_position = point(-6.0f * cosf(pi / 360 * (float)i * 3),
10.0f,
-6.0f * sinf(pi / 360 * (float)i * 3));
world.light.position = light_position;
struct mat4x4 middle_s = scaling(1.0f, 0.4f, 1.0f);
struct mat4x4 middle_rz = rotation_z(tau / 360.f * (float)i);
struct mat4x4 middle_ry = rotation_y(tau / 360.f * (float)i);
struct mat4x4 middle_r = mat4x4_mul_m(middle_rz, middle_ry);
struct mat4x4 middle_transform = mat4x4_mul_m(middle_r, middle_s);
world.objects[1].transform = mat4x4_mul_m(middle_t, middle_transform);
camera_render(&_camera, &world, &_canvas);
char s[128];
snprintf(s, 128, "test%03d.ppm", i);
printf("%s\n", s);
canvas_to_ppm(&_canvas, s);
}
}