-
Notifications
You must be signed in to change notification settings - Fork 1
/
raytracer.h
168 lines (131 loc) · 3.66 KB
/
raytracer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef RAYTRACER_H
#define RAYTRACER_H
/*==================[inclusions]============================================*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <tgmath.h>
#include <time.h>
#include <float.h>
#include <stdint.h>
#include <string.h>
#include <omp.h>
#include "vector.h"
/*==================[macros]================================================*/
#ifndef PI
#define PI 3.14159265359
#endif
#define EPSILON 1e-8
#define MAX_DEPTH 5
#define MONTE_CARLO_SAMPLES 1
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define CLAMP(x) (MAX(0, MIN(x, 1)))
#define CLAMP_BETWEEN(x, min_v, max_v) (MAX(min_v, MIN(max_v, 1)))
#define ABS(x) ((x < 0) ? (-x) : (x))
#define VECTOR(x, y, z) ((vec3) {(x), (y), (z)})
#define RGB(r, g, b) (VECTOR((r) / 255.0, (g) / 255.0, (b) / 255.0))
#define EQ(a, b) (ABS((a) - (b)) < EPSILON)
#define RAY(o, d) ((Ray) { .origin = o, .direction = d })
#define RED RGB(255, 0, 0)
#define GREEN RGB(0, 192, 48)
#define BLUE RGB(0, 0, 255)
#define WHITE RGB(255, 255, 255)
#define BLACK RGB(0, 0, 0)
//#define BACKGROUND RGB(0, 0x80, 0x80)
#define BACKGROUND RGB(10, 10, 10)
#define ZERO_VECTOR RGB(0, 0, 0)
#define ONE_VECTOR (VECTOR(1.0, 1.0, 1.0))
#define RANDOM_COLOR \
(vec3) { random_double(), random_double(), random_double() }
#define M_DEFAULT ((uint)1 << 1)
#define M_REFLECTION ((uint)1 << 2)
#define M_REFRACTION ((uint)1 << 3)
#define M_CHECKERED ((uint)1 << 4)
/*==================[type definitions]======================================*/
typedef uint32_t uint;
typedef struct { vec3 pos; vec2 tex; } Vertex;
typedef struct { vec3 origin, direction; } Ray;
typedef struct
{
uint flags;
vec3 color, emission;
double ka, ks, kd;
} Material;
typedef struct
{
vec3 center;
double radius;
} Sphere;
typedef struct
{
size_t num_triangles;
Vertex *vertices;
} TriangleMesh;
typedef union
{
TriangleMesh *mesh;
Sphere *sphere;
} Geometry;
typedef enum
{
GEOMETRY_SPHERE,
GEOMETRY_MESH,
} GeometryType;
/*
typedef struct
{
GeometryType type;
Material material;
Geometry geometry;
} Object;
*/
typedef struct
{
uint flags;
double radius;
vec3 center;
vec3 color;
vec3 emission;
} Object;
typedef struct
{
double t, u, v;
vec3 point;
vec3 normal;
uint object_id;
} Hit;
typedef struct
{
vec3 position, horizontal, vertical, lower_left_corner;
} Camera;
typedef struct
{
vec3 background;
char *result, *obj;
int width, height, samples;
} Options;
/*==================[external function declarations]========================*/
double random_double();
double random_range(double, double);
vec3 point_at(const Ray *ray, double t);
vec3 calculate_surface_normal(vec3 v0, vec3 v1, vec3 v2);
bool intersect_sphere(const Ray *ray, vec3 center, double radius, Hit *hit);
bool intersect_triangle(const Ray *ray, Vertex vertex0, Vertex vertex1, Vertex vertex2, Hit *hit);
#if 0
mat4 scale(vec3);
mat4 rotate(vec3);
mat4 translate(vec3);
#endif
void print_v(const char* msg, const vec3 v);
void print_m(const mat4 m);
void init_camera(Camera *camera, vec3 position, vec3 target, Options *options);
void render(uint8_t *framebuffer, Object *objects, size_t n_objects, Camera *camera, Options *options);
bool load_obj(const char *filename, TriangleMesh *mesh);
/*==================[external constants]====================================*/
/*==================[external data]=========================================*/
extern long long ray_count;
extern long long intersection_test_count;
/*==================[end of file]===========================================*/
#endif /* RAYTRACER_H */