-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniRT.h
285 lines (243 loc) · 8.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* miniRT.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenamar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/28 18:08:16 by abenamar #+# #+# */
/* Updated: 2024/04/24 14:11:41 by abenamar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINIRT_H
# define MINIRT_H
# include <fcntl.h>
# include "float.h"
# include <math.h>
# include <stdbool.h>
# include <X11/Xlib.h>
# include <X11/Xutil.h>
# include "mlx.h"
# include "libft.h"
/* ************************************************************************** */
/* */
/* math */
/* */
/* ************************************************************************** */
# define __M_PIF 3.14159265358979323846F
typedef struct s_vec3f
{
float x;
float y;
float z;
} t_vec3f;
typedef struct s_quat4f
{
t_vec3f xyz;
float w;
} t_quat4f;
t_vec3f ft_vec3f(float const x, float const y, float const z);
t_vec3f ft_vec3f_sum(t_vec3f const u, t_vec3f const v);
t_vec3f ft_vec3f_diff(t_vec3f const u, t_vec3f const v);
t_vec3f ft_vec3f_prod(t_vec3f const u, float const t);
t_vec3f ft_vec3f_unit(t_vec3f const u);
float ft_vec3f_dot(t_vec3f const u, t_vec3f const v);
t_vec3f ft_vec3f_cross(t_vec3f const u, t_vec3f const v);
t_quat4f ft_quat4f(t_vec3f const xyz, float const w);
t_quat4f ft_quat4f_prod(t_quat4f const q, t_quat4f const p);
t_quat4f ft_quat4f_unit(t_quat4f const q);
/* ************************************************************************** */
/* */
/* utils */
/* */
/* ************************************************************************** */
# define __USAGE "Usage: miniRT file.rt\n"
# define __ERR_01 "Error: invalid window width (not greater to 0)\n"
# define __ERR_02 "Error: invalid window height (not greater to 0)\n"
# define __ERR_03 "Error: invalid samples per pixel (not greater to 0)\n"
# define __ERR_04 "Error: invalid shadow bias (not greater or equal to 0)\n"
# define __ERR_05 "Error: wrong file extension (not *.rt)\n"
# define __ERR_06 "Error: out of memory\n"
# define __ERR_07 "Error: duplicated capital identifier (not unique A,C,L)\n"
# define __ERR_08 "Error: wrong number of element informations\n"
# define __ERR_09 "Error: wrong floating-point value (not finite number)\n"
# define __ERR_10 "Error: invalid lightning ratio (not in range [0,1])\n"
# define __ERR_11 "Error: wrong number of RGB colors (not exactly 3)\n"
# define __ERR_12 "Error: invalid RGB value (not in range [0,255])\n"
# define __ERR_13 "Error: wrong number of 3D coordinates (not exactly 3)\n"
# define __ERR_14 "Error: invalid normalized value (not in range [-1,1])\n"
# define __ERR_15 "Error: forbidden null vector (not normalized vector)\n"
# define __ERR_16 "Error: invalid field of view (not in range [0,180])\n"
# define __ERR_17 "Error: wrong element identifier (not A,C,L,sp,pl,cy)\n"
# define __ERR_18 "Error: invalid distance (not greater or equal to 0)\n"
# define __ERR_19 "Error: missing capital identifier (not one of A,C,L)\n"
typedef struct s_radius
{
float value;
float twice;
float square;
float reciprocal;
} t_radius;
typedef t_vec3f t_point3f;
typedef struct s_arrow
{
t_point3f start;
t_point3f center;
t_point3f end;
float height;
} t_arrow;
typedef struct s_focus
{
int type;
t_point3f *position;
t_quat4f *rotation;
t_vec3f *orientation;
t_vec3f *vup;
t_radius *radius;
t_arrow *arrow;
t_list *next;
} t_focus;
typedef t_vec3f t_color3f;
ssize_t ft_pstderr(char const *str);
void ft_perror(char const *str);
void ft_tab_free(char **tab);
size_t ft_tab_size(char *const *tab);
float ft_str_to_float(char const *nptr);
t_color3f ft_str_to_color3f(char const *str);
t_vec3f ft_str_to_vec3f(char const *str);
bool ft_vec3f_isnormalized(t_vec3f const *const u);
t_vec3f ft_vec3f_rotate(t_vec3f const u, t_quat4f const q);
t_radius ft_radius(float const diameter);
t_arrow ft_arrow(t_point3f const center, \
t_vec3f const axis, float const height);
/* ************************************************************************** */
/* */
/* scene */
/* */
/* ************************************************************************** */
typedef struct s_camera
{
t_point3f position;
t_vec3f orientation;
float fov;
t_quat4f rotation;
t_vec3f vup;
} t_camera;
typedef struct s_light
{
t_point3f position;
float brightness;
t_color3f color;
} t_light;
typedef struct s_sphere
{
t_point3f center;
t_radius radius;
t_color3f color;
} t_sphere;
typedef struct s_plane
{
t_point3f point;
t_vec3f normal;
t_color3f color;
t_quat4f rotation;
} t_plane;
typedef struct s_cylinder
{
t_vec3f axis;
t_radius radius;
t_arrow arrow;
t_color3f color;
t_quat4f rotation;
} t_cylinder;
typedef struct s_scene
{
t_color3f *ambiance;
t_camera *camera;
t_light *light;
t_list *spheres;
t_list *planes;
t_list *cylinders;
t_focus focus;
} t_scene;
bool ft_camera_init(t_scene *const scene, char *const *info);
bool ft_light_init(t_scene *const scene, char *const *info);
bool ft_sphere_add(t_scene *const scene, char *const *info);
bool ft_plane_add(t_scene *const scene, char *const *info);
bool ft_cylinder_add(t_scene *const scene, char *const *info);
void ft_focus_camera(t_scene *const scene);
void ft_focus_light(t_scene *const scene);
void ft_focus_sphere(t_scene *const scene);
void ft_focus_plane(t_scene *const scene);
void ft_focus_cylinder(t_scene *const scene);
void ft_scene_free(t_scene *scene);
t_scene *ft_scene_new(char const *file);
/* ************************************************************************** */
/* */
/* render */
/* */
/* ************************************************************************** */
# ifndef _WIDTH
# define _WIDTH 640
# endif
# ifndef _HEIGHT
# define _HEIGHT 360
# endif
# ifndef _TITLE
# define _TITLE "MiniRT"
# endif
# ifndef _SAMPLES_PER_PIXEL
# define _SAMPLES_PER_PIXEL 1
# endif
# ifndef _SHADOW_BIAS
# define _SHADOW_BIAS 1.0E-4F
# endif
typedef struct s_xclient
{
void *mlx;
void *win;
void *img;
char *data;
int bpp;
int lsize;
int endian;
bool update;
t_scene *scene;
} t_xclient;
typedef struct s_viewport
{
t_vec3f u;
t_vec3f v;
t_vec3f pdu;
t_vec3f pdv;
t_point3f p00;
} t_viewport;
typedef struct s_ray
{
t_point3f origin;
t_vec3f direction;
} t_ray;
typedef struct s_hit
{
float t;
float bias;
t_point3f point;
t_vec3f normal;
t_color3f color;
} t_hit;
void ft_xclient_free(t_xclient *xclient);
t_xclient *ft_xclient_new(t_scene *const scene);
t_vec3f ft_face_normal(t_vec3f const direction, t_vec3f const normal);
void ft_sphere_hit(t_sphere const *const sp, \
t_ray const *const r, t_hit *const h);
void ft_plane_hit(t_plane const *const pl, \
t_ray const *const r, t_hit *const h);
void ft_cylinder_hit(t_cylinder const *const cy, \
t_ray const *const r, t_hit *const h);
t_color3f ft_ray_tracing(t_scene const *const scene, \
int const i, int const j);
void ft_pixel_put(t_xclient const *const xclient, \
int const x, int const y, t_color3f const color);
bool ft_key_rotate(int const keycode, t_scene *const scene);
int ft_key_press(int const keycode, t_xclient *const xclient);
#endif