Skip to content

Commit

Permalink
add vec3
Browse files Browse the repository at this point in the history
  • Loading branch information
gau-nernst committed Oct 29, 2023
1 parent ac7737f commit a1d27f7
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
CFLAGS += -Wall
LDLIBS += -lm

OBJECTS = tiff.o main.o
OBJECTS = tiff.o raytracing.o main.o

main: $(OBJECTS)
$(CC) $(OBJECTS) -o $@
$(CC) $(OBJECTS) -o $@ $(LDLIBS)

launch: main
./main
Expand Down
56 changes: 45 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
#include "raytracing.h"
#include "tiff.h"
#include <stdio.h>
#include <stdlib.h>

// scene background
Vec3 ray_color(Ray *ray) {
Vec3 direction = vec3_unit(ray->direction);
float a = 0.5f * (direction.y + 1.0f); // [-1,1] -> [0, 1]

Vec3 WHITE = {1.0f, 1.0f, 1.0f};
Vec3 BLUE = {0.5f, 0.7f, 1.0f};
return vec3_add(vec3_mul(WHITE, 1 - a), vec3_mul(BLUE, a));
}

int main(int argc, char *argv[]) {
int width = 256;
int height = 256;
float aspect_ratio = 16.0f / 9.0f;
int img_width = 256;
int img_height = (int)((float)img_width / aspect_ratio);

float focal_length = 1.0f;
float viewport_height = 2.0f;
float viewport_width = viewport_height * (float)img_width / (float)img_height;
Vec3 camera_pos = {0.0f, 0.0f, 0.0f};

Vec3 viewport_u = {viewport_width, 0.0f, 0.0f}; // scan from left to right
Vec3 viewport_v = {0.0f, -viewport_height, 0.0f}; // scan from top to bottom

Image8 image = {width, height, 3, malloc(width * height * 3)};
Vec3 pixel_delta_u = vec3_mul(viewport_u, 1.0f / (float)img_width);
Vec3 pixel_delta_v = vec3_mul(viewport_v, 1.0f / (float)img_height);

Vec3 camera_direction = {0.0f, 0.0f, focal_length};
Vec3 viewport_upper_left =
vec3_sub(camera_pos, vec3_add(camera_direction, vec3_mul(viewport_u, 0.5f), vec3_mul(viewport_v, 0.5f)));
Vec3 pixel00_pos = vec3_add(viewport_upper_left, vec3_mul(pixel_delta_u, 0.5f), vec3_mul(pixel_delta_v, 0.5f));

Image8 image = {img_width, img_height, 3, malloc(img_width * img_height * 3)};
if (image.data == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return 1;
}

for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++) {
float r = (float)i / (width - 1);
float g = (float)j / (height - 1);
float b = 0;
for (int j = 0; j < img_height; j++) {
fprintf(stderr, "\rScanlines remaining: %d", img_height - j);

for (int i = 0; i < img_width; i++) {
Vec3 pixel_pos = vec3_add(pixel00_pos, vec3_mul(pixel_delta_u, (float)i), vec3_mul(pixel_delta_v, (float)j));
Vec3 ray_direction = vec3_sub(pixel_pos, camera_pos);
Ray ray = {camera_pos, ray_direction};

image.data[(j * width + i) * 3] = (int)(255.999 * r);
image.data[(j * width + i) * 3 + 1] = (int)(255.999 * g);
image.data[(j * width + i) * 3 + 2] = (int)(255.999 * b);
Vec3 pixel_color = ray_color(&ray);

image.data[(j * img_width + i) * 3] = (int)(255.999 * pixel_color.x);
image.data[(j * img_width + i) * 3 + 1] = (int)(255.999 * pixel_color.y);
image.data[(j * img_width + i) * 3 + 2] = (int)(255.999 * pixel_color.z);
}
}
fprintf(stderr, "\nDone\n");

FILE *f = fopen("output.tiff", "wb");
if (f == NULL) {
Expand Down
38 changes: 38 additions & 0 deletions raytracing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "raytracing.h"
#include <math.h>

Vec3 *vec3vec3_add_(Vec3 *self, Vec3 other) {
self->x += other.x;
self->y += other.y;
self->z += other.z;
return self;
}

Vec3 *vec3vec3_sub_(Vec3 *self, Vec3 other) {
self->x -= other.x;
self->y -= other.y;
self->z -= other.z;
return self;
}

Vec3 *vec3float_mul_(Vec3 *self, float other) {
self->x *= other;
self->y *= other;
self->z *= other;
return self;
}

Vec3 vec3vec3_add(Vec3 u, Vec3 v) { return (Vec3){u.x + v.x, u.y + v.y, u.z + v.z}; }
Vec3 vec3vec3_sub(Vec3 u, Vec3 v) { return (Vec3){u.x - v.x, u.y - v.y, u.z - v.z}; }
Vec3 vec3vec3_mul(Vec3 u, Vec3 v) { return (Vec3){u.x * v.x, u.y * v.y, u.z * v.z}; }
Vec3 vec3float_add(Vec3 u, float v) { return (Vec3){u.x + v, u.y + v, u.z + v}; }
Vec3 vec3float_sub(Vec3 u, float v) { return (Vec3){u.x - v, u.y - v, u.z - v}; }
Vec3 vec3float_mul(Vec3 u, float v) { return (Vec3){u.x * v, u.y * v, u.z * v}; }

float vec3_length2(Vec3 u) { return u.x * u.x + u.y * u.y + u.z * u.z; }
float vec3_length(Vec3 u) { return sqrtf(vec3_length2(u)); }
float vec3_dot(Vec3 u, Vec3 v) { return u.x * v.x + u.y * v.y + u.z + v.z; }
Vec3 vec3_cross(Vec3 u, Vec3 v) { return (Vec3){u.y * v.z - v.y * u.z, u.z * v.x - v.z * u.x, u.x * v.y - v.x * u.y}; }
Vec3 vec3_unit(Vec3 u) { return vec3float_mul(u, 1.0f / vec3_length(u)); }

Vec3 ray_at(Ray ray, float t) { return vec3vec3_add(ray.origin, vec3float_mul(ray.direction, t)); }
38 changes: 38 additions & 0 deletions raytracing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
typedef struct Vec3 {
float x;
float y;
float z;
} Vec3;

// https://stackoverflow.com/a/11763277
#define _GET_MACRO(_1, _2, _3, FUNC, ...) FUNC
#define vec3_add2(x, y) _Generic((y), Vec3: vec3vec3_add, float: vec3float_add)(x, y)
#define vec3_add3(x, y, z) vec3_add2(vec3_add2(x, y), z)
#define vec3_add(...) _GET_MACRO(__VA_ARGS__, vec3_add3, vec3_add2)(__VA_ARGS__)

#define vec3_sub(x, y) _Generic((y), Vec3: vec3vec3_sub, float: vec3float_sub)(x, y)
#define vec3_mul(x, y) _Generic((y), Vec3: vec3vec3_mul, float: vec3float_mul)(x, y)

Vec3 *vec3vec3_add_(Vec3 *self, Vec3 other);
Vec3 *vec3vec3_sub_(Vec3 *self, Vec3 other);
Vec3 *vec3float_mul_(Vec3 *self, float other);

Vec3 vec3vec3_add(Vec3 u, Vec3 v);
Vec3 vec3vec3_sub(Vec3 u, Vec3 v);
Vec3 vec3vec3_mul(Vec3 u, Vec3 v);
Vec3 vec3float_add(Vec3 u, float v);
Vec3 vec3float_sub(Vec3 u, float v);
Vec3 vec3float_mul(Vec3 u, float v);

float vec3_length2(Vec3 u);
float vec3_length(Vec3 u);
float vec3_dot(Vec3 u, Vec3 v);
Vec3 vec3_cross(Vec3 u, Vec3 v);
Vec3 vec3_unit(Vec3 u);

typedef struct Ray {
Vec3 origin;
Vec3 direction;
} Ray;

Vec3 ray_at(Ray ray, float t);

0 comments on commit a1d27f7

Please sign in to comment.