ray-tracer-challenge/raytracer.c
2024-08-05 21:26:31 -05:00

42 lines
1.2 KiB
C

#include "tuples.h"
#include "canvas.h"
#include "rays.h"
#include "spheres.h"
#include "intersections.h"
#include "transformations.h"
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 canvas c = canvas(canvas_pixels, canvas_pixels);
struct tuple red = color(1.0f, 0.0f, 0.0f);
struct sphere shape = sphere();
struct mat4x4 shear = shearing(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
struct mat4x4 scale = scaling(0.5f, 1.0f, 1.0f);
struct mat4x4 m = mat4x4_mul_m(&shear, &scale);
shape.transform = m;
for (int y = 0; y < canvas_pixels; y++) {
float world_y = half - pixel_size * y;
for (int x = 0; x < canvas_pixels; x++) {
float world_x = -half + pixel_size * x;
struct tuple position = point(world_x, world_y, wall_z);
struct ray r = ray(ray_origin, tuple_normalize(tuple_sub(position, ray_origin)));
struct intersections2 xs = intersect(&shape, r);
struct intersection * h = hit((struct intersections *)&xs);
if (h != NULL) {
canvas_write_pixel(c, x, y, red);
}
}
}
canvas_to_ppm(c, "test.ppm");
}