-
Notifications
You must be signed in to change notification settings - Fork 0
/
raytracer.h
269 lines (179 loc) · 5.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#ifndef __RAYTRACER_H__
#define __RAYTRACER_H__
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
/* ============================= */
/* ============================= */
/* === STRUCTURE DEFINITIONS === */
/* ============================= */
/* ============================= */
typedef unsigned char byte;
typedef struct vec {
double x;
double y;
double z;
} vec;
typedef struct rgb {
/* each value must be on interval [0, 1] */
double r;
double g;
double b;
} rgb;
typedef struct ray {
vec *origin;
vec *direction;
} ray;
typedef struct sphere {
vec *center;
double radius;
rgb *color;
} sphere;
typedef struct sphere_list sphere_list;
/* convention: NULL is the empty sphere list */
struct sphere_list {
sphere *s;
sphere_list *next;
};
typedef struct light {
vec *direction;
rgb *color;
} light;
typedef struct scene {
rgb *bg;
sphere_list *spheres;
light *light;
rgb *amb;
} scene;
typedef struct hit_test {
double dist;
rgb *surf_color;
vec *surf_norm;
} hit_test;
/* hit_test convention: NULL is a miss */
typedef struct camera {
vec *loc;
unsigned int h;
unsigned int w;
} camera;
typedef struct stage {
camera *c;
scene *s;
} stage;
/* ================== */
/* ================== */
/* === OPERATIONS === */
/* ================== */
/* ================== */
/* Please note: the following is not necessarily every operation you will need,
* nor will you necessarily use every operation here in your implementation.
*/
/* ================================== */
/* === vector operations => vec.c === */
/* ================================== */
vec *vec_new(double x, double y, double z);
vec *vec_dup(vec *v);
vec *vec_add(vec *v1, vec *v2);
vec *vec_sub(vec *v1, vec *v2);
vec *vec_neg(vec *v);
vec *vec_scale(double s, vec *v);
double vec_dot(vec *v1, vec *v2);
double vec_mag(vec *v);
/* if a vector's magnitude is zero, it can't be normalized; simply return it */
vec *vec_norm(vec *v);
char *vec_tos(vec *v);
void vec_print(vec *v);
void vec_free(vec *v);
/* =============================== */
/* === rgb operations => rgb.c === */
/* =============================== */
rgb *rgb_new(double r, double g, double b);
rgb *rgb_dup(rgb *c);
rgb *rgb_modulate(rgb *c1, rgb *c2);
rgb *rgb_scale(double s, rgb *c);
rgb *rgb_add(rgb *c1, rgb *c2);
char *rgb_tos(rgb *c);
void rgb_print(rgb *c);
void rgb_free(rgb *c);
/* print three integer values on [0,255] with spaces in between and a newline */
void rgb_print_bytes(rgb *c);
/* =============================== */
/* === ray operations => ray.c === */
/* =============================== */
ray *ray_new(vec *origin, vec *direction);
ray *ray_dup(ray *r);
vec *ray_position(ray *r, double time);
char *ray_tos(ray *r);
void ray_print(ray *r);
void ray_free(ray *r);
/* ===================================== */
/* === sphere operations => sphere.c === */
/* ===================================== */
sphere *sphere_new(vec *center, double radius, rgb *color);
sphere *sphere_dup(sphere *s);
char *sphere_tos(sphere *s);
void sphere_print(sphere *s);
void sphere_free(sphere *s);
/* ======================================== */
/* === sphere list ops => sphere_list.c === */
/* ======================================== */
sphere_list *sl_cons(sphere *s, sphere_list *ss);
sphere_list *sl_singleton(sphere *s);
void sl_print(sphere_list *ss);
void sl_free(sphere_list *ss);
/* =================================== */
/* === light operations => light.c === */
/* =================================== */
light *light_new(vec *direction, rgb *color);
light *light_dup(light *l);
void light_print(light *l);
void light_free(light *l);
/* =================================== */
/* === scene operations => scene.c === */
/* =================================== */
scene *scene_new(rgb *bg, sphere_list *ss, light *light, rgb *amb);
void scene_print(scene *s);
void scene_free(scene *s);
/* ========================================= */
/* === hit_test operations => hit_test.c === */
/* ========================================= */
hit_test *hit_test_new(double dist, rgb *surf_color, vec *surf_norm);
hit_test *hit_test_dup(hit_test *t);
void hit_test_print(hit_test *t);
void hit_test_free(hit_test *t);
/* ===================================== */
/* === camera operations => camera.c === */
/* ===================================== */
camera *camera_new(vec *loc, unsigned int h, unsigned int w);
camera *camera_dup(camera *c);
void camera_print(camera *c);
void camera_free(camera *c);
/* =================================== */
/* === stage operations => stage.c === */
/* =================================== */
stage *stage_new(camera *c, scene *s);
stage *stage_dup(stage *c);
void stage_print(stage *s);
void stage_free(stage *s);
/* The function get_stage just needs to return a valid stage. */
/* Any valid stage. */
/* The string future_expansion is not used in project 1. */
stage *get_stage(char *future_expansion);
/* ================================================ */
/* ================================================ */
/* === RAY TRACER LOGIC and MAIN => raytracer.c === */
/* ================================================ */
/* ================================================ */
hit_test *intersect(ray *r, sphere *s);
int shadow(vec *v, light *l, sphere_list *ss);
rgb *lighting(scene *s, ray *r, hit_test *h);
vec *logical_loc(camera *c, vec *pos);
rgb *trace_ray(scene *s, ray *r);
void render(stage *g);
/* main should simply call get_stage */
/* and render the stage it gets */
/* (printing the resulting ppm to stdout) */
int main(int argc, char *argv[]);
#endif /* __RAYTRACER_H__ */