diff --git a/src/basics.c b/src/basics.c index 7398165..3c28b1a 100644 --- a/src/basics.c +++ b/src/basics.c @@ -112,3 +112,21 @@ rect_init(float x, float y, float w, float h) r.w = w; return r; } + +/* + * Rays + */ + +/* + * ray_init -- + * + * Create a new Ray. + */ +Ray +ray_init(Vector3 location, Vector3 direction) +{ + Ray r; + r.location = location; + r.direction = direction; + return r; +} diff --git a/src/basics.h b/src/basics.h index 9f67924..41c8617 100644 --- a/src/basics.h +++ b/src/basics.h @@ -34,4 +34,17 @@ typedef struct { Rect rect_init(float x, float y, float h, float w); +typedef struct { + Vector3 location; + Vector3 direction; +} Ray; + +Ray ray_init(Vector3 location, Vector3 direction); + + +typedef struct { + float red, green, blue; +} Color; + + #endif diff --git a/src/scene.c b/src/scene.c index b2804a2..55158e8 100644 --- a/src/scene.c +++ b/src/scene.c @@ -6,10 +6,14 @@ */ +#include #include #include "scene.h" +Color _scene_trace(Scene *scene, const Ray ray, const int depth); + + /* * scene_init -- * @@ -67,9 +71,41 @@ scene_load(Scene *scene, FILE *scene_file) void scene_render(Scene *scene) { + Color *pixels = malloc(sizeof(Color) * scene->height * scene->width); + if (pixels == NULL) { + // TODO: Print an error. + return; + } + + float fov = 30.0; + float aspect_ratio = scene->width / scene->height; + float angle = tan(M_PI * 0.5 * fov / 180.0); + + float xx, yy; + Ray primary_ray; + Vector3 direction; for (int y = 0; y < scene->height; y++) { for (int x = 0; x < scene->width; x++) { - // TODO: Process the scene. + // Compute (x, y) of ray direction. + xx = (2 * ((x + 0.5) / scene->width) - 1) * angle * aspect_ratio; + yy = (1 - 2 * ((y + 0.5) / scene->height)) * angle; + + // Assemble a ray and trace it. + direction = vector_init(xx, yy, 1); + primary_ray = ray_init(ZeroVector3, direction); + pixels[y * x] = _scene_trace(scene, primary_ray, 0); } } } + + +Color +_scene_trace(Scene *scene, const Ray ray, const int depth) +{ + Color out_color; + out_color.red = 0.0; + out_color.blue = 0.0; + out_color.green = 0.0; + + return out_color; +}