-
Notifications
You must be signed in to change notification settings - Fork 0
/
minirt.h
337 lines (287 loc) · 6.89 KB
/
minirt.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minirt.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: shongou <shongou@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 23:34:10 by shongou #+# #+# */
/* Updated: 2023/01/04 23:34:12 by shongou ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINIRT_H
# define MINIRT_H
# include <limits.h>
# include <math.h>
# include <stdbool.h>
# include "vector_utils.h"
# include "libft.h"
# include "get_next_line.h"
# include <stdlib.h>
# include <stdio.h>
# include <float.h>
# include "mlx_int.h"
# include "mlx.h"
# define SHININESS 8.0
# define SPECULAR_REF 0.3
# define __GNU_SOURCE 1
# define EXPOSE 12
# define ESC_KEY 65307
# define HEIGHT 600
# define WIDTH 800
# define NO_ERROR 0
# define MLX_INIT_ERR 1
# define NEW_WINDOW_ERR 2
# define NEW_IMAGE_ERR 4
typedef struct s_err t_err;
struct s_err
{
int err_flag;
int scene_obj;
} ;
typedef enum e_scene_obj t_scene_obj;
enum e_scene_obj{
A = 1,
C = 2,
L = 4,
PL = 8,
SP = 16,
CY = 32,
} ;
typedef union s_my_double
{
unsigned long long ulnum;
double dnum;
} t_my_double;
typedef struct s_image t_image;
struct s_image
{
void *img_ptr;
char *addr;
int size_line;
int bpp;
int endian;
} ;
//typedef struct s_status t_status;
//struct s_status
//{
// void *mlx;
// void *mlx_win;
// t_image img;
// t_config config;
//};
typedef struct s_color
{
double r;
double g;
double b;
} t_color;
typedef struct s_ray
{
t_vec start;
t_vec direction;
} t_ray;
typedef struct s_sphere
{
t_vec center;
double radius;
} t_sphere;
typedef struct s_plane
{
t_vec normal;
t_vec pos;
} t_plane;
typedef struct s_cylinder t_cylinder;
struct s_cylinder
{
t_vec normal;
t_vec pos;
double radius;
double height;
};
typedef struct s_material
{
t_color diffuse_ref;
t_color specular_ref;
double shininess;
} t_material;
typedef enum e_shape_type
{
ST_SPHERE,
ST_PLANE,
ST_CYLINDER,
ST_NONE
} t_shape_type;
typedef struct s_intersection
{
double distance;
t_vec pos;
t_vec normal;
} t_intersection;
typedef struct s_shape t_shape;
struct s_shape
{
t_shape_type type;
t_sphere sphere;
t_plane plane;
t_cylinder cylinder;
t_material material;
t_shape *next;
} ;
typedef struct s_nearest
{
t_intersection i_point;
t_shape shape;
bool flag;
} t_nearest;
typedef enum e_light_type
{
POINT,
DIRECTIONAL
} t_light_type;
typedef struct s_light
{
t_light_type type;
t_vec vec;
t_color illuminance;
double brightness_ratio;
} t_light;
typedef struct s_ambient
{
t_color ambient_illuminance;
t_color ambient_ref;
} t_ambient;
typedef struct s_camera
{
t_vec pos;
double fov;
t_vec orientation;
} t_camera;
typedef struct s_config
{
t_shape *shape_list;
t_material *material;
size_t num_shapes;
t_light light;
t_ambient ambient;
t_camera camera;
} t_config;
typedef struct s_status t_status;
struct s_status
{
void *mlx;
void *mlx_win;
t_image img;
t_config config;
};
typedef struct s_quadratic t_quadratic;
struct s_quadratic
{
double a;
double b;
double c;
double sol1;
double sol2;
double sol;
} ;
typedef enum e_error_type t_error_type;
enum e_error_type
{
MALLOC_ERROR = 1,
IDENTIFIER_ERROR = 2,
NUMBER_OF_ELEMENT_ERROR = 4,
NULL_STR = 8,
INVALID_COLOR = 16,
INVALID_VECTOR = 32,
INVALID_ORIENTATION = 64,
INVALID_FOV = 128,
INVALID_RATIO = 256,
INVALID_SIZE = 512,
INVALID_FILE = 1024,
OPEN_ERROR = 2048,
};
/* --------------------------------
config {
t_shape *shape_list;
size_t num_shapes;
t_light light;
t_color ambient_illuminance;
}
---------------------------------- */
// init.c
t_config init_config(char **argv);
// ray_trace.c
void ray_trace(t_config config, t_status *status);
// draw.c
void draw(t_color color, int x, int y, t_image img);
// window_utils.c
t_status *mlx_run(t_config config);
void init_image(t_status *status);
int delete_window(t_status *status);
int key_hook(int key, t_status *status);
int rendering(t_status *status);
void free_list(t_config config);
// get_shadow_ray.c
t_nearest get_shadow_ray(t_config config, t_nearest nearest, \
t_vec light_dir);
// utils.c
void print_vector(t_vec vec, char *msg);
void add_list_last(t_shape **shape_list, t_shape *node);
char *remove_nl(char *str, t_err *err);
void free_strs(char **strs);
// atod.c
double atod(char *str);
// luminance.c
t_color get_luminance(t_config config, t_nearest nearest, t_ray ray);
// color_utils.c
t_color add_color(t_color n, t_color m);
t_color set_color(char *rgb, int *err_flag);
// get_nearest.c
t_nearest get_nearest(t_config config, t_ray ray, double max_d, bool shadow);
// is_hittable.c
bool is_hittable(t_shape shape, t_ray ray, t_intersection *i_point);
// equation.c
double get_solution_of_quadratic_equation(t_sphere sph, t_ray ray, \
double d);
double discriminant(t_sphere sph, t_ray ray);
double cy_get_solution_of_quadratic_equation(double d, t_quadratic *quad);
double cy_discriminant(t_cylinder cyl, t_ray ray, t_quadratic *quad);
// camera_ray.c
t_ray get_camera_ray(double x, double y, t_camera camera);
//void get_basis_vector(t_vec *esx, t_vec *esy, t_vec cam_dir);
// set_scene.c
void set_ambient(char **strs, t_config *config, int *err_flag, \
int *scene_obj);
void set_light(char **strs, t_config *config, int *err_flag, \
int *scene_obj);
void set_camera(char **strs, t_config *config, int *err_flag, \
int *scene_obj);
// set_shapes.c
void set_sphere(char **strs, t_config *config, int *err_flag, \
int *scene_obj);
void set_cylinder(char **strs, t_config *config, int *err_flag, \
int *scene_obj);
void set_plane(char **strs, t_config *config, int *err_flag, \
int *scene_obj);
//set_utils.c
t_vec set_orientation(char *str, int *err_flag);
t_vec set_coordinates(char *str, int *err_flag);
t_color set_rgb(char *str, int *err_flag);
//set_utils2.c
double set_size(char *str, int *err_flag);
double set_ratio(char *str, int *err_flag);
double set_fov(char *str, int *err_flag);
//valid_utils.c
bool valid_color(char *str);
bool valid_digit(char *str);
bool valid_vector(char *str);
bool valid_float(char *str);
//error.c
void check_obj(int scene_objs);
void before_set_config_err_handler(int err_flag);
void display_usage(void);
void error_handler(int fd, char *line, size_t line_n, int error_flag);
//mlx_error.c
void print_mlx_error(int flag);
void mlx_error(t_status *status, t_config config, int flag);
#endif