55 lines
1.8 KiB
C
55 lines
1.8 KiB
C
#include "tuples.h"
|
|
#include "canvas.h"
|
|
#include "rays.h"
|
|
#include "spheres.h"
|
|
#include "intersections.h"
|
|
#include "transformations.h"
|
|
#include "materials.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();
|
|
shape.material = material();
|
|
shape.material.color = color(1.0f, 0.2f, 1.0f);
|
|
//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;
|
|
|
|
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);
|
|
|
|
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) {
|
|
struct tuple point = ray_position(r, h->t);
|
|
struct tuple normal = sphere_normal_at(h->object, point);
|
|
struct tuple eye = tuple_neg(r.direction);
|
|
|
|
struct tuple color = lighting(h->object->material, light, point, eye, normal);
|
|
canvas_write_pixel(c, x, y, color);
|
|
}
|
|
}
|
|
}
|
|
|
|
canvas_to_ppm(c, "test.ppm");
|
|
}
|