├── textures ├── .DS_Store ├── billard_1.png ├── billard_10.png ├── billard_11.png ├── billard_12.png ├── billard_13.png ├── billard_14.png ├── billard_15.png ├── billard_2.png ├── billard_3.png ├── billard_4.png ├── billard_5.png ├── billard_6.png ├── billard_7.png ├── billard_8.png ├── billard_9.png └── 2k_earth_daymap.png ├── lib ├── libmlx │ ├── libglfw3.a │ ├── .gitattributes │ ├── SECURITY.md │ ├── shaders │ │ ├── default.vert │ │ └── default.frag │ ├── .github │ │ ├── ISSUE_TEMPLATE │ │ │ ├── feature_request.md │ │ │ └── bug_report.md │ │ └── workflows │ │ │ └── ci.yml │ ├── .gitignore │ ├── CONTRIBUTING.md │ ├── src │ │ ├── mlx_monitor.c │ │ ├── textures │ │ │ ├── mlx_png.c │ │ │ └── mlx_texture.c │ │ ├── mlx_put_pixel.c │ │ ├── mlx_cursor.c │ │ ├── mlx_exit.c │ │ ├── mlx_keys.c │ │ ├── utils │ │ │ └── mlx_error.c │ │ └── font │ │ │ └── mlx_font.c │ ├── Makefile_Unix.mk │ ├── Makefile_WindowsNT.mk │ ├── Makefile │ └── tools │ │ ├── compile_shader.sh │ │ └── compile_shader.bat └── libft │ └── src │ ├── additional │ ├── ft_strcmp.c │ ├── ft_split_count_str.c │ ├── ft_rand.c │ ├── ft_str_count_chr.c │ ├── ft_free_split.c │ ├── ft_int_from_str.c │ ├── ft_strncmp_rev.c │ ├── ft_gnl_without_buffer.c │ └── ft_double_from_str.c │ ├── mandatory │ ├── ft_putchar_fd.c │ ├── ft_putstr_fd.c │ ├── ft_isdigit.c │ ├── ft_isprint.c │ ├── ft_isascii.c │ ├── ft_tolower.c │ ├── ft_toupper.c │ ├── ft_isalpha.c │ ├── ft_isalnum.c │ ├── ft_putendl_fd.c │ ├── ft_strlen.c │ ├── ft_calloc.c │ ├── ft_memset.c │ ├── ft_bzero.c │ ├── ft_strchr.c │ ├── ft_strdup.c │ ├── ft_putnbr_fd.c │ ├── ft_memchr.c │ ├── ft_strrchr.c │ ├── ft_memcpy.c │ ├── ft_strncmp.c │ ├── ft_memcmp.c │ ├── ft_atoi.c │ ├── ft_memmove.c │ ├── ft_strjoin.c │ ├── ft_strlcpy.c │ ├── ft_memccpy.c │ ├── ft_strmapi.c │ ├── ft_strlcat.c │ ├── ft_substr.c │ ├── ft_strtrim.c │ ├── ft_strnstr.c │ └── ft_itoa.c │ └── bonus │ ├── ft_lstadd_front.c │ ├── ft_lstdelone.c │ ├── ft_lstsize.c │ ├── ft_lstlast.c │ ├── ft_lstiter.c │ ├── ft_lstadd_back.c │ ├── ft_lstclear.c │ ├── ft_lstnew.c │ └── ft_lstmap.c ├── .gitignore └── src ├── print ├── mrt_print_vec3.c ├── mrt_print_obj_plane.c ├── mrt_print_color.c ├── mrt_print_obj_disc.c ├── mrt_print_obj_sphere.c ├── mrt_print_textures.c ├── mrt_print_obj_tube.c ├── mrt_print_obj_rectangle.c ├── mrt_print_material.c ├── mrt_print_light.c ├── mrt_print_error.c └── mrt_print.h ├── trace ├── mrt_trace_onb.h ├── mrt_trace_onb.c ├── mrt_trace_specular.c ├── mrt_trace_obj_normal_1.c ├── mrt_trace_random.c ├── mrt_trace_light_pdf.c ├── mrt_trace_dielectric.c ├── mrt_trace_obj_normal_2.c ├── mrt_trace_obj_intersect_2.c ├── mrt_trace_diffuse.c └── mrt_trace_obj_intersect_2_utils.c ├── mrt_light.c ├── math ├── mrt_math_color.c ├── mrt_math_vec3_2.c ├── mrt_math_vec3_1.c └── mrt_math.h ├── parse ├── mrt_parse_vec3.c ├── mrt_parse_bg.c ├── mrt_parse_res.c ├── mrt_parse_color.c ├── mrt_parse_amb.c ├── mrt_parse_cam.c ├── mrt_parse_obj_plane.c ├── mrt_parse_obj_disc.c ├── mrt_parse_sampling.c ├── mrt_parse_obj_tube.c ├── mrt_parse_utils.c ├── mrt_parse_material.c └── mrt_parse_texture.c ├── graphic ├── mrt_graphic_move.c └── mrt_graphic.h ├── mrt_obj.c ├── mrt_obj_texture_utils.c ├── mrt_vec3.h ├── mrt_light.h ├── mrt_scene_utils.c ├── mrt_color.h └── mrt_color.c /textures/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/.DS_Store -------------------------------------------------------------------------------- /lib/libmlx/libglfw3.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/lib/libmlx/libglfw3.a -------------------------------------------------------------------------------- /textures/billard_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_1.png -------------------------------------------------------------------------------- /textures/billard_10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_10.png -------------------------------------------------------------------------------- /textures/billard_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_11.png -------------------------------------------------------------------------------- /textures/billard_12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_12.png -------------------------------------------------------------------------------- /textures/billard_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_13.png -------------------------------------------------------------------------------- /textures/billard_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_14.png -------------------------------------------------------------------------------- /textures/billard_15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_15.png -------------------------------------------------------------------------------- /textures/billard_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_2.png -------------------------------------------------------------------------------- /textures/billard_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_3.png -------------------------------------------------------------------------------- /textures/billard_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_4.png -------------------------------------------------------------------------------- /textures/billard_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_5.png -------------------------------------------------------------------------------- /textures/billard_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_6.png -------------------------------------------------------------------------------- /textures/billard_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_7.png -------------------------------------------------------------------------------- /textures/billard_8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_8.png -------------------------------------------------------------------------------- /textures/billard_9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/billard_9.png -------------------------------------------------------------------------------- /textures/2k_earth_daymap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tjensen42/42-miniRT/HEAD/textures/2k_earth_daymap.png -------------------------------------------------------------------------------- /lib/libmlx/.gitattributes: -------------------------------------------------------------------------------- 1 | # See https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings 2 | 3 | # Set the default behavior, in case people don't have core.autocrlf set 4 | * text=auto 5 | 6 | # Declare files that will always have a certain EOL 7 | *.sh text eol=lf 8 | shaders/** text eol=lf 9 | -------------------------------------------------------------------------------- /lib/libmlx/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | | Version | Supported | 6 | | ------- | ------------------ | 7 | | 2.0.x | ✅ | 8 | | 1.0.x | ❌ | 9 | 10 | ## Reporting a Vulnerability 11 | 12 | For security issues please refrain from opening an issue! 13 | Instead write an email to [w2.wizzard@gmail.com](mailto:w2.wizzard@gmail.com) 14 | -------------------------------------------------------------------------------- /lib/libmlx/shaders/default.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | layout(location = 0) in vec3 aPos; 4 | layout(location = 1) in vec2 aTexCoord; 5 | layout(location = 2) in int aTexIndex; 6 | 7 | out vec2 TexCoord; 8 | flat out int TexIndex; 9 | 10 | uniform mat4 ProjMatrix; 11 | 12 | void main() 13 | { 14 | gl_Position = ProjMatrix * vec4(aPos, 1.0); 15 | TexCoord = aTexCoord; 16 | TexIndex = aTexIndex; 17 | } 18 | -------------------------------------------------------------------------------- /lib/libmlx/.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "[REQUEST]" 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /lib/libmlx/.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "[BUG]" 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. MacOS] 28 | - Version [e.g. BigSur] 29 | 30 | **Additional context** 31 | Add any other context about the problem here. 32 | -------------------------------------------------------------------------------- /lib/libmlx/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *mlx.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | # Misc 55 | main.c 56 | *.xpm 57 | *.png 58 | *.xpm42 59 | *.png 60 | .vscode/ 61 | lib/glfw/ 62 | 63 | # Special shader files 64 | mlx_*_shader.c 65 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # exe 2 | miniRT 3 | 4 | # libs 5 | libft.a 6 | libmlx*.a 7 | 8 | # vs_code specific files 9 | .vscode/ 10 | 11 | # OS generated files 12 | .DS_Store 13 | .DS_Store? 14 | ._* 15 | .Spotlight-V100 16 | .Trashes 17 | ehthumbs.db 18 | Thumbs.db 19 | 20 | # Prerequisites 21 | *.d 22 | 23 | # Object files 24 | *.o 25 | *.ko 26 | *.obj 27 | *.elf 28 | 29 | # Linker output 30 | *.ilk 31 | *.map 32 | *.exp 33 | 34 | # Precompiled Headers 35 | *.gch 36 | *.pch 37 | 38 | # Shared objects (inc. Windows DLLs) 39 | *.dll 40 | *.so 41 | *.so.* 42 | *.dylib 43 | 44 | # Executables 45 | *.exe 46 | *.out 47 | *.app 48 | *.i*86 49 | *.x86_64 50 | *.hex 51 | 52 | # Debug files 53 | *.dSYM/ 54 | *.su 55 | *.idb 56 | *.pdb 57 | 58 | # Kernel Module Compile Results 59 | *.mod* 60 | *.cmd 61 | .tmp_versions/ 62 | modules.order 63 | Module.symvers 64 | Mkfile.old 65 | dkms.conf 66 | -------------------------------------------------------------------------------- /lib/libmlx/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to contribute to MLX42 2 | 3 | ## Read the wiki for repo codestyle! 4 | 5 | For any questions, suggestions or help [Contact Me](mailto:lde-la-h@student.codam.nl) 6 | 7 | ## **Found a bug?** 8 | 9 | * Avoid opening any new issues without having checked if your problem has already been reported. If there are no currently open issues that fit your problem's description, feel free to [add it](https://github.com/W2Codam/MLX42/issues/new/choose). 10 | 11 | * When writing an issue make sure to include a clear title and description as well as having filled out all the necessary information: System info, OS, OS-Version, ... 12 | 13 | * If possible add pictures of the issue. 14 | 15 | * Maybe fix it yourself :D ? 16 | 17 | ## Contributing 18 | 19 | Before thinking of adding a contribution, think. Is it necessary? Will this actually be a useful/required feature? Is your implementation good? 20 | Provide clear and documented explanation as to what was changed. 21 | -------------------------------------------------------------------------------- /lib/libft/src/additional/ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/15 11:24:04 by tjensen #+# #+# */ 9 | /* Updated: 2022/04/15 11:27:19 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strcmp(const char *s1, const char *s2) 16 | { 17 | return (ft_strncmp(s1, s2, ft_strlen(s1) + 1)); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/21 18:07:47 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:58:15 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that writes the character 'c' to the file descriptor 'fd'. 17 | */ 18 | void ft_putchar_fd(char c, int fd) 19 | { 20 | write(fd, &c, 1); 21 | } 22 | -------------------------------------------------------------------------------- /src/print/mrt_print_vec3.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_vec3.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:15 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:16 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | void print_vec3(t_vec3 vec3, const char *str, const char *color) 16 | { 17 | printf("\t%s%-10s %8.3f %8.3f %8.3f%s\n", 18 | color, str, vec3.x, vec3.y, vec3.z, "\033[m"); 19 | } 20 | -------------------------------------------------------------------------------- /lib/libft/src/additional/ft_split_count_str.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split_count.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/18 11:37:37 by tjensen #+# #+# */ 9 | /* Updated: 2022/04/18 11:43:46 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_split_count_str(char **split) 16 | { 17 | int i; 18 | 19 | if (split == NULL) 20 | return (-1); 21 | i = 0; 22 | while (split[i]) 23 | i++; 24 | return (i); 25 | } 26 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/21 18:19:01 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 11:00:57 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that writes the string 's' to the file descriptor 'fd'. 17 | */ 18 | void ft_putstr_fd(char *s, int fd) 19 | { 20 | if (s != NULL) 21 | write(fd, s, ft_strlen(s)); 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/src/bonus/ft_lstadd_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/24 13:15:52 by hepple #+# #+# */ 9 | /* Updated: 2021/10/29 11:03:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that adds the element 'new' at the front of the linked list 'lst'. 17 | */ 18 | void ft_lstadd_front(t_list **lst, t_list *new) 19 | { 20 | new->next = *lst; 21 | *lst = new; 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/15 16:40:18 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:27:39 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns 1 if 'c' is a digit (0 through 9) 17 | * and 0 if it isn't. 18 | */ 19 | int ft_isdigit(int c) 20 | { 21 | if (c >= '0' && c <= '9') 22 | return (1); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/15 15:06:30 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:28:10 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns 1 if 'c' is any printable character 17 | * and 0 if it isn't. 18 | */ 19 | int ft_isprint(int c) 20 | { 21 | if (c >= 32 && c < 127) 22 | return (1); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/15 15:49:26 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:27:23 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns 1 if 'c' is from the ASCII character set 17 | * and 0 if it isn't. 18 | */ 19 | int ft_isascii(int c) 20 | { 21 | if (c >= 0 && c <= 127) 22 | return (1); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/additional/ft_rand.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_rand.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/08 12:19:42 by hepple #+# #+# */ 9 | /* Updated: 2022/06/08 12:29:11 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | double ft_rand(void) 16 | { 17 | return (rand() / ((double) RAND_MAX + 1.0)); 18 | } 19 | 20 | double ft_rand_range(double min, double max) 21 | { 22 | return (min + (rand() / (((double) RAND_MAX + 1.0) / (max - min)))); 23 | } 24 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/15 14:46:36 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 13:26:31 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that converts an uppercase letter 'c' to lowercase. If 'c' is no 17 | * uppercase letter, 'c' is returned unchanged. 18 | */ 19 | int ft_tolower(int c) 20 | { 21 | if (c >= 'A' && c <= 'Z') 22 | c += 32; 23 | return (c); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/15 15:01:36 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 13:27:05 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that converts a lowercase letter 'c' to uppercase. If 'c' is no 17 | * lowercase letter, 'c' is returned unchanged. 18 | */ 19 | int ft_toupper(int c) 20 | { 21 | if (c >= 'a' && c <= 'z') 22 | c -= 32; 23 | return (c); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/15 16:46:36 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:27:13 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns 1 if 'c' is an alphabetic character 17 | * (uppercase or lowercase) and 0 if it isn't. 18 | */ 19 | int ft_isalpha(int c) 20 | { 21 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 22 | return (1); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/15 16:30:38 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:27:05 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns 1 if 'c' is an alphanumeric character 17 | * and 0 if it isn't. 18 | */ 19 | int ft_isalnum(int c) 20 | { 21 | if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') 22 | || (c >= 'a' && c <= 'z')) 23 | return (1); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/21 18:23:50 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:59:35 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that writes the string 's' to the file descriptor 'fd', 17 | * followed by a newline character. 18 | */ 19 | void ft_putendl_fd(char *s, int fd) 20 | { 21 | if (s != NULL) 22 | { 23 | write(fd, s, ft_strlen(s)); 24 | write(fd, "\n", 1); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/src/bonus/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/24 16:08:44 by hepple #+# #+# */ 9 | /* Updated: 2021/10/29 11:03:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that deletes the list element 'lst' and frees its content, using 17 | * the function 'del'. 18 | */ 19 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 20 | { 21 | if (lst != NULL && del != NULL) 22 | { 23 | del(lst->content); 24 | free(lst); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/15 14:18:36 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 12:55:30 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns the length of the string 'str', excluding the 17 | * terminating null byte. 18 | */ 19 | size_t ft_strlen(const char *str) 20 | { 21 | size_t length; 22 | 23 | length = 0; 24 | while (str[length] != '\0') 25 | length++; 26 | return (length); 27 | } 28 | -------------------------------------------------------------------------------- /src/print/mrt_print_obj_plane.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_obj_plane.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:26 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:26 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | void print_obj_plane(t_list *obj) 16 | { 17 | t_obj *c_obj; 18 | 19 | c_obj = obj_cont(obj); 20 | printf("PLANE: (%p)\n", c_obj); 21 | print_vec3(c_obj->pl.pos, "pos:", COLOR_BL); 22 | print_vec3(c_obj->pl.dir, "dir:", COLOR_CY); 23 | print_material(&(c_obj->material)); 24 | printf("\n"); 25 | } 26 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_onb.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_onb.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:55:18 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:55:19 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MRT_TRACE_ONB_H 14 | # define MRT_TRACE_ONB_H 15 | 16 | # include "mrt_vec3.h" 17 | # include "math/mrt_math.h" 18 | 19 | typedef struct s_onb 20 | { 21 | t_vec3 u; 22 | t_vec3 v; 23 | t_vec3 w; 24 | } t_onb; 25 | 26 | t_onb onb_build(t_vec3 in); 27 | t_vec3 onb_transform(t_onb onb, t_vec3 in); 28 | 29 | #endif // MRT_TRACE_ONB_H 30 | -------------------------------------------------------------------------------- /lib/libft/src/additional/ft_str_count_chr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_str_count_chr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/18 11:36:42 by tjensen #+# #+# */ 9 | /* Updated: 2022/04/18 11:44:18 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_str_count_chr(const char *str, const char c) 16 | { 17 | int i; 18 | int chr_count; 19 | 20 | if (str == NULL) 21 | return (-1); 22 | chr_count = 0; 23 | i = 0; 24 | while (str[i]) 25 | { 26 | if (str[i] == c) 27 | chr_count++; 28 | i++; 29 | } 30 | return (chr_count); 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/src/bonus/ft_lstsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/24 13:41:59 by hepple #+# #+# */ 9 | /* Updated: 2021/10/29 11:03:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns the number of elements in the list 'lst'. 17 | */ 18 | int ft_lstsize(t_list *lst) 19 | { 20 | t_list *current; 21 | int i; 22 | 23 | i = 0; 24 | current = lst; 25 | while (current != NULL) 26 | { 27 | current = current->next; 28 | i++; 29 | } 30 | return (i); 31 | } 32 | -------------------------------------------------------------------------------- /src/mrt_light.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_light.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:54:02 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:54:13 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_light.h" 14 | 15 | t_list *light_new(void) 16 | { 17 | t_light *c_light; 18 | 19 | c_light = malloc(sizeof(t_light)); 20 | if (c_light == NULL) 21 | return (NULL); 22 | ft_bzero(c_light, sizeof(t_light)); 23 | return (ft_lstnew(c_light)); 24 | } 25 | 26 | inline t_light *light_cont(t_list *is) 27 | { 28 | return ((t_light *)is->content); 29 | } 30 | -------------------------------------------------------------------------------- /src/print/mrt_print_color.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_color.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:32 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:32 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | inline void print_color(t_color color) 16 | { 17 | printf("\t%-10s %s%8d %s%8d %s%8d%s\n", 18 | "color:", 19 | COLOR_RE, (int)(256 * color_clamp(color.r, 0.0, 0.999)), 20 | COLOR_GR, (int)(256 * color_clamp(color.g, 0.0, 0.999)), 21 | COLOR_BL, (int)(256 * color_clamp(color.b, 0.0, 0.999)), 22 | "\033[m"); 23 | } 24 | -------------------------------------------------------------------------------- /lib/libft/src/bonus/ft_lstlast.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/24 13:51:56 by hepple #+# #+# */ 9 | /* Updated: 2021/10/29 11:03:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns a pointer to the last element of the list 'lst'. 17 | */ 18 | t_list *ft_lstlast(t_list *lst) 19 | { 20 | t_list *current; 21 | 22 | current = lst; 23 | if (current == NULL) 24 | return (NULL); 25 | while (current->next != NULL) 26 | current = current->next; 27 | return (current); 28 | } 29 | -------------------------------------------------------------------------------- /lib/libft/src/additional/ft_free_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_free_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/21 13:47:29 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 14:30:00 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that frees an array of strings generated with ft_split. 17 | */ 18 | void ft_free_split(char ***split) 19 | { 20 | int i; 21 | 22 | i = 0; 23 | while ((*split)[i] != NULL) 24 | { 25 | free((*split)[i]); 26 | (*split)[i] = NULL; 27 | i++; 28 | } 29 | free(*split); 30 | *split = NULL; 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/src/bonus/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/24 16:52:15 by hepple #+# #+# */ 9 | /* Updated: 2021/10/29 11:03:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that iterates through the list 'lst' and applies the function 'f' 17 | * to the content of each element. 18 | */ 19 | void ft_lstiter(t_list *lst, void (*f)(void *)) 20 | { 21 | if (lst != NULL && f != NULL) 22 | { 23 | while (lst != NULL) 24 | { 25 | f(lst->content); 26 | lst = lst->next; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/17 15:09:04 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:19:26 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that uses malloc to allocate memory for 'count' elements of size 17 | * 'size' and sets this memory to zero. 18 | */ 19 | void *ft_calloc(size_t count, size_t size) 20 | { 21 | void *ptr; 22 | 23 | ptr = malloc(count * size); 24 | if (ptr == NULL) 25 | return (NULL); 26 | ft_bzero(ptr, count * size); 27 | return (ptr); 28 | } 29 | -------------------------------------------------------------------------------- /src/print/mrt_print_obj_disc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_obj_disc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:27 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:28 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | void print_obj_disc(t_list *obj) 16 | { 17 | t_obj *c_obj; 18 | 19 | c_obj = obj_cont(obj); 20 | printf("DISC: (%p)\n", c_obj); 21 | print_vec3(c_obj->di.pos, "pos:", COLOR_BL); 22 | print_vec3(c_obj->di.dir, "dir:", COLOR_CY); 23 | print_scene_double(c_obj->di.radius, "radius:", COLOR_NO); 24 | print_material(&(c_obj->material)); 25 | printf("\n"); 26 | } 27 | -------------------------------------------------------------------------------- /src/print/mrt_print_obj_sphere.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_obj_sphere.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:23 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:23 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | void print_obj_sphere(t_list *obj) 16 | { 17 | t_obj *c_obj; 18 | 19 | c_obj = obj_cont(obj); 20 | printf("SPHERE: (%p)\n", c_obj); 21 | print_vec3(c_obj->sp.pos, "pos:", COLOR_BL); 22 | print_scene_double(c_obj->sp.radius, "radius:", COLOR_NO); 23 | print_vec3(c_obj->sp.rot, "rot:", COLOR_NO); 24 | print_material(&(c_obj->material)); 25 | printf("\n"); 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/17 12:12:43 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:56:03 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that fills the first 'len' bytes of the memory area pointed to 17 | * by 's' with the constant byte 'c'. 18 | */ 19 | void *ft_memset(void *b, int c, size_t len) 20 | { 21 | unsigned char *dst; 22 | size_t i; 23 | 24 | dst = b; 25 | i = 0; 26 | while (i < len) 27 | { 28 | dst[i] = (unsigned char) c; 29 | i++; 30 | } 31 | return (b); 32 | } 33 | -------------------------------------------------------------------------------- /lib/libft/src/bonus/ft_lstadd_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/24 14:05:13 by hepple #+# #+# */ 9 | /* Updated: 2021/10/29 11:03:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that adds the element 'new' at the end of the linked list 'lst'. 17 | */ 18 | void ft_lstadd_back(t_list **lst, t_list *new) 19 | { 20 | t_list *current; 21 | 22 | if (*lst == NULL) 23 | *lst = new; 24 | else 25 | { 26 | current = *lst; 27 | while (current->next != NULL) 28 | current = current->next; 29 | current->next = new; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/17 12:24:00 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:20:14 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that erases the data in the next 'n' bytes of memory starting from 17 | * the location pointed to by 's', by writing null bytes to that area. 18 | */ 19 | void ft_bzero(void *s, size_t n) 20 | { 21 | unsigned char *dst; 22 | size_t i; 23 | 24 | dst = (unsigned char *)s; 25 | i = 0; 26 | while (i < n) 27 | { 28 | dst[i] = '\0'; 29 | i++; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/src/additional/ft_int_from_str.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_int_from_str.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/19 15:49:48 by tjensen #+# #+# */ 9 | /* Updated: 2022/04/19 15:56:26 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_int_from_str(const char *str, int min, int max, int *res) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if ((str[0] == '+' || str[0] == '-') && str[1] != '\0') 21 | i++; 22 | while (str[i]) 23 | { 24 | if (!ft_isdigit(str[i])) 25 | return (-1); 26 | i++; 27 | } 28 | *res = ft_atoi(str); 29 | if (*res < min || *res > max) 30 | return (-1); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /src/math/mrt_math_color.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_math_color.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/19 11:11:53 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/08 12:21:45 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_math.h" 14 | 15 | inline t_color color_add(t_color c1, t_color c2) 16 | { 17 | return ((t_color){c1.r + c2.r, c1.g + c2.g, c1.b + c2.b}); 18 | } 19 | 20 | inline t_color color_scale(double f, t_color c1) 21 | { 22 | return ((t_color){f * c1.r, f * c1.g, f * c1.b}); 23 | } 24 | 25 | inline t_color color_multi(t_color c1, t_color c2) 26 | { 27 | return ((t_color){c1.r * c2.r, c1.g * c2.g, c1.b * c2.b}); 28 | } 29 | -------------------------------------------------------------------------------- /src/print/mrt_print_textures.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_textures.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:17 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:17 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | void print_textures(t_list *l_texture) 16 | { 17 | t_texture *c_texture; 18 | 19 | if (l_texture == NULL) 20 | return ; 21 | printf("TEXTURES:\n"); 22 | while (l_texture) 23 | { 24 | c_texture = l_texture->content; 25 | printf("\t%s: width: %zu height: %zu\n", 26 | c_texture->name, c_texture->width, c_texture->height); 27 | l_texture = l_texture->next; 28 | } 29 | printf("\n"); 30 | } 31 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/16 13:45:32 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 11:06:37 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns a pointer to the first occurrence of the character 'c' 17 | * in the string 's' or NULL if the character is not found. 18 | */ 19 | char *ft_strchr(const char *s, int c) 20 | { 21 | int i; 22 | 23 | i = 0; 24 | while (s[i] != '\0') 25 | { 26 | if (s[i] == c) 27 | return ((char *)s + i); 28 | i++; 29 | } 30 | if (c == '\0') 31 | return ((char *)s + i); 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /src/print/mrt_print_obj_tube.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_obj_tube.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:21 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:21 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | void print_obj_tube(t_list *obj) 16 | { 17 | t_obj *c_obj; 18 | 19 | c_obj = obj_cont(obj); 20 | printf("TUBE: (%p)\n", c_obj); 21 | print_vec3(c_obj->tb.pos, "pos:", COLOR_BL); 22 | print_vec3(c_obj->tb.dir, "dir:", COLOR_CY); 23 | print_scene_double(c_obj->tb.radius, "radius:", COLOR_NO); 24 | print_scene_double(c_obj->tb.height, "height:", COLOR_NO); 25 | print_material(&(c_obj->material)); 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /lib/libft/src/bonus/ft_lstclear.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/24 16:41:18 by hepple #+# #+# */ 9 | /* Updated: 2021/10/29 11:03:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that deletes and frees the list element 'lst' and all the elements 17 | * after it and setting the list pointer to NULL. For the deleting, the 18 | * function 'del' is used. 19 | */ 20 | void ft_lstclear(t_list **lst, void (*del)(void *)) 21 | { 22 | t_list *tmp; 23 | 24 | while (*lst != NULL) 25 | { 26 | tmp = (*lst)->next; 27 | ft_lstdelone(*lst, del); 28 | *lst = tmp; 29 | } 30 | *lst = NULL; 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/17 15:25:34 by hepple #+# #+# */ 9 | /* Updated: 2021/11/25 13:25:37 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns a pointer to a new string which is a duplicate of the 17 | * string 's1'. Memory for the new string is obtained with malloc. 18 | */ 19 | char *ft_strdup(const char *s1) 20 | { 21 | size_t len_s1; 22 | char *s2; 23 | 24 | len_s1 = ft_strlen(s1); 25 | s2 = malloc(sizeof(char) * (len_s1 + 1)); 26 | if (s2 == NULL) 27 | return (NULL); 28 | s2 = (char *)ft_memcpy(s2, s1, len_s1 + 1); 29 | return (s2); 30 | } 31 | -------------------------------------------------------------------------------- /lib/libft/src/bonus/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/24 13:08:43 by hepple #+# #+# */ 9 | /* Updated: 2021/10/29 11:03:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that uses malloc to create a new list element, where the 'content' 17 | * pointer of the element is initialized with the value of the parameter 18 | * 'content' and the 'next' pointer is set to NULL. 19 | */ 20 | t_list *ft_lstnew(void *content) 21 | { 22 | t_list *new; 23 | 24 | new = malloc(sizeof(t_list)); 25 | if (new == NULL) 26 | return (NULL); 27 | new->content = content; 28 | new->next = NULL; 29 | return (new); 30 | } 31 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/22 08:11:42 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 11:00:15 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that writes the integer 'n' to the file descriptor 'fd'. 17 | */ 18 | void ft_putnbr_fd(int n, int fd) 19 | { 20 | if (n == INT_MIN) 21 | ft_putstr_fd("-2147483648", fd); 22 | else 23 | { 24 | if (n < 0) 25 | { 26 | ft_putchar_fd('-', fd); 27 | n = n * (-1); 28 | } 29 | if (n < 10) 30 | ft_putchar_fd(n + '0', fd); 31 | else 32 | { 33 | ft_putnbr_fd(n / 10, fd); 34 | ft_putnbr_fd(n % 10, fd); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/17 13:06:25 by hepple #+# #+# */ 9 | /* Updated: 2021/11/19 00:07:32 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that scans the initial 'n' bytes of the memory area pointed to by 17 | * 's' for the first instance of 'c' and returns a pointer to this byte 18 | * (or NULL if no matching byte is found). 19 | */ 20 | void *ft_memchr(const void *s, int c, size_t n) 21 | { 22 | size_t i; 23 | 24 | i = 0; 25 | while (i < n) 26 | { 27 | if (((unsigned char *)s)[i] == (unsigned char) c) 28 | return ((void *)(s + i)); 29 | i++; 30 | } 31 | return (NULL); 32 | } 33 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/16 15:45:44 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 13:15:51 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns a pointer to the last occurrence of the character 'c' 17 | * in the string 's' or NULL if the character is not found. 18 | */ 19 | char *ft_strrchr(const char *s, int c) 20 | { 21 | int i; 22 | char *result; 23 | 24 | i = 0; 25 | result = NULL; 26 | while (s[i] != '\0') 27 | { 28 | if (s[i] == c) 29 | result = ((char *)(s + i)); 30 | i++; 31 | } 32 | if (c == '\0') 33 | return ((char *)(s + i)); 34 | return (result); 35 | } 36 | -------------------------------------------------------------------------------- /src/print/mrt_print_obj_rectangle.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_obj_rectangle.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:24 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:25 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | void print_obj_rectangle(t_list *obj) 16 | { 17 | t_obj *c_obj; 18 | 19 | c_obj = obj_cont(obj); 20 | printf("RECTANGLE: (%p)\n", c_obj); 21 | print_vec3(c_obj->rt.pos, "pos:", COLOR_BL); 22 | print_vec3(c_obj->rt.dir, "dir:", COLOR_CY); 23 | print_scene_double(c_obj->rt.width, "width:", COLOR_NO); 24 | print_scene_double(c_obj->rt.height, "height:", COLOR_NO); 25 | print_vec3(c_obj->rt.rot, "rot:", COLOR_NO); 26 | print_material(&(c_obj->material)); 27 | printf("\n"); 28 | } 29 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_vec3.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_vec3.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:19 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 17:03:52 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | 15 | int parse_vec3(const char *str, t_vec3 *vec3) 16 | { 17 | int error; 18 | char **split; 19 | 20 | split = ft_split(str, ','); 21 | if (split == NULL) 22 | return (-1); 23 | error = (ft_split_count_str(split) != 3); 24 | if (!error) 25 | error = double_from_str(split[0], 6, 4, &((*vec3).x)); 26 | if (!error) 27 | error = double_from_str(split[1], 6, 4, &((*vec3).y)); 28 | if (!error) 29 | error = double_from_str(split[2], 6, 4, &((*vec3).z)); 30 | ft_free_split(&split); 31 | return (error); 32 | } 33 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_onb.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_onb.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:55:21 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:55:21 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace_onb.h" 14 | 15 | // in must be normalized 16 | t_onb onb_build(t_vec3 in) 17 | { 18 | t_onb onb; 19 | 20 | onb.w = in; 21 | if (fabs(vec3_dot(onb.w, (t_vec3){1.0, 0.0, 0.0})) > 0.9) 22 | onb.v = vec3_norm(vec3_cross(onb.w, (t_vec3){0.0, 1.0, 0.0})); 23 | else 24 | onb.v = vec3_norm(vec3_cross(onb.w, (t_vec3){1.0, 0.0, 0.0})); 25 | onb.u = vec3_cross(onb.w, onb.v); 26 | return (onb); 27 | } 28 | 29 | t_vec3 onb_transform(t_onb onb, t_vec3 in) 30 | { 31 | return (vec3_norm(vec3_lin_comb(1.0, 32 | vec3_lin_comb(in.x, onb.u, in.y, onb.v), in.z, onb.w))); 33 | } 34 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/17 12:34:29 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:52:01 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that copies 'n' bytes from memory area 'src' to memory area 'dst'. 17 | * For overlapping memory areas, the behaviour is undefined. 18 | */ 19 | void *ft_memcpy(void *dst, const void *src, size_t n) 20 | { 21 | unsigned char *dst_copy; 22 | unsigned char *src_copy; 23 | size_t i; 24 | 25 | dst_copy = (unsigned char *)dst; 26 | src_copy = (unsigned char *)src; 27 | if (dst_copy == NULL && src_copy == NULL) 28 | return (dst); 29 | i = 0; 30 | while (i < n) 31 | { 32 | dst_copy[i] = src_copy[i]; 33 | i++; 34 | } 35 | return (dst); 36 | } 37 | -------------------------------------------------------------------------------- /lib/libmlx/src/mlx_monitor.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* :::::::: */ 4 | /* mlx_monitor.c :+: :+: */ 5 | /* +:+ */ 6 | /* By: W2Wizard +#+ */ 7 | /* +#+ */ 8 | /* Created: 2022/01/19 17:18:59 by W2Wizard #+# #+# */ 9 | /* Updated: 2022/04/13 00:25:43 by w2wizard ######## odam.nl */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "MLX42/MLX42_Int.h" 14 | 15 | //= Public =// 16 | 17 | void mlx_get_monitor_size(int32_t index, int32_t* width, int32_t* height) 18 | { 19 | MLX_ASSERT(index < 0); 20 | MLX_ASSERT(!width); 21 | MLX_ASSERT(!height); 22 | 23 | *width = 0; 24 | *height = 0; 25 | 26 | int32_t monitor_count; 27 | GLFWmonitor** monitors = glfwGetMonitors(&monitor_count); 28 | if (index > monitor_count || !monitors) 29 | return; 30 | 31 | const GLFWvidmode* vidmode; 32 | if ((vidmode = glfwGetVideoMode(monitors[index]))) 33 | { 34 | *width = vidmode->width; 35 | *height = vidmode->height; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_bg.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_bg.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:55 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/10 09:50:22 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | 15 | int parse_bg(t_scene *scene, char **split, int line_num) 16 | { 17 | if (scene->bg.set == true) 18 | return (print_error_scene(line_num, ERR_PARSE, ERR_DUP_ID, ID_BG)); 19 | if (ft_split_count_str(split) != 3) 20 | return (print_error_scene(line_num, ERR_PARSE, ERR_NUM_PARA, NULL)); 21 | if (parse_color(split[1], &(scene->bg.color[0]))) 22 | return (print_error_scene(line_num, ERR_PARSE, ERR_COLOR, NULL)); 23 | if (parse_color(split[2], &(scene->bg.color[1]))) 24 | return (print_error_scene(line_num, ERR_PARSE, ERR_COLOR, NULL)); 25 | scene->bg.set = true; 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/16 09:46:39 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 13:04:32 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that compares 'n' bytes of the two strings 'str1' and 'str2'. 17 | * If no difference is found, 0 is returned. Else, it returns the difference 18 | * between the first pair of bytes that differ in 'str1' and 'str2'. 19 | */ 20 | int ft_strncmp(const char *str1, const char *str2, size_t n) 21 | { 22 | size_t i; 23 | 24 | i = 0; 25 | while (i < n && str1[i] == str2[i]) 26 | { 27 | if (str1[i] == '\0' && str2[i] == '\0') 28 | return (0); 29 | i++; 30 | } 31 | if (i == n) 32 | return (0); 33 | else 34 | return ((unsigned char) str1[i] - (unsigned char) str2[i]); 35 | } 36 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/17 13:19:11 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:56:33 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that compares the first 'n' bytes of the memory areas 's1' 17 | * and 's2'. If no difference is found, 0 is returned. Else, it returns the 18 | * difference between the first pair of bytes that differ in 's1' and 's2'. 19 | */ 20 | int ft_memcmp(const void *s1, const void *s2, size_t n) 21 | { 22 | unsigned char *str1; 23 | unsigned char *str2; 24 | size_t i; 25 | 26 | str1 = (unsigned char *)s1; 27 | str2 = (unsigned char *)s2; 28 | i = 0; 29 | while (i < n && str1[i] == str2[i]) 30 | i++; 31 | if (i == n) 32 | return (0); 33 | else 34 | return (str1[i] - str2[i]); 35 | } 36 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_res.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_res.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:29 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/10 09:50:22 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | 15 | int parse_res(t_scene *scene, char **split, int line_num) 16 | { 17 | if (scene->img.res_set == true) 18 | return (print_error_scene(line_num, ERR_PARSE, ERR_DUP_ID, ID_RES)); 19 | if (ft_split_count_str(split) != 3) 20 | return (print_error_scene(line_num, ERR_PARSE, ERR_NUM_PARA, NULL)); 21 | if (int_from_str(split[1], 10, 10000, &(scene->img.width))) 22 | return (print_error_scene(line_num, ERR_PARSE, ERR_RES, NULL)); 23 | if (int_from_str(split[2], 10, 10000, &(scene->img.height))) 24 | return (print_error_scene(line_num, ERR_PARSE, ERR_RES, NULL)); 25 | scene->img.res_set = true; 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/15 16:55:36 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:26:37 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that converts the initial part of a string 'str' to an integer. 17 | */ 18 | int ft_atoi(const char *str) 19 | { 20 | int i; 21 | int sign; 22 | long int value; 23 | 24 | i = 0; 25 | value = 0; 26 | sign = 1; 27 | while (str[i] == ' ' || str[i] == '\t' || str[i] == '\n' 28 | || str[i] == '\v' || str[i] == '\f' || str[i] == '\r') 29 | i++; 30 | if (str[i] == '-') 31 | sign = -1; 32 | if (str[i] == '-' || str[i] == '+') 33 | i++; 34 | while (str[i] >= '0' && str[i] <= '9') 35 | { 36 | value = value * 10 + (str[i] - '0'); 37 | i++; 38 | } 39 | return (sign * value); 40 | } 41 | -------------------------------------------------------------------------------- /lib/libmlx/src/textures/mlx_png.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* :::::::: */ 4 | /* mlx_png.c :+: :+: */ 5 | /* +:+ */ 6 | /* By: W2Wizard +#+ */ 7 | /* +#+ */ 8 | /* Created: 2022/02/16 23:11:29 by W2Wizard #+# #+# */ 9 | /* Updated: 2022/04/13 00:32:31 by w2wizard ######## odam.nl */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "MLX42/MLX42_Int.h" 14 | 15 | //= Public =// 16 | 17 | mlx_texture_t* mlx_load_png(const char* path) 18 | { 19 | MLX_ASSERT(!path); 20 | 21 | mlx_texture_t* image; 22 | if (!(image = malloc(sizeof(mlx_texture_t)))) 23 | return ((void*)mlx_error(MLX_MEMFAIL)); 24 | 25 | uint32_t error; 26 | image->bytes_per_pixel = BPP; 27 | if ((error = lodepng_decode32_file(&image->pixels, &image->width, &image->height, path))) 28 | { 29 | free(image); 30 | // Explicitly print error on purpose 31 | fprintf(stderr, "MLX42: LodePNG: %s\n", lodepng_error_text(error)); 32 | return ((void*)mlx_error(MLX_INVPNG)); 33 | } 34 | return (image); 35 | } 36 | -------------------------------------------------------------------------------- /src/math/mrt_math_vec3_2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_math_vec3_2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/19 10:30:37 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/08 11:50:08 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_math.h" 14 | 15 | bool vec3_equal(t_vec3 a, t_vec3 b) 16 | { 17 | return ((a.x == b.x) && (a.y == b.y) && (a.z == b.z)); 18 | } 19 | 20 | double vec3_len(t_vec3 a) 21 | { 22 | return (sqrt(a.x * a.x + a.y * a.y + a.z * a.z)); 23 | } 24 | 25 | double vec3_dist(t_vec3 a, t_vec3 b) 26 | { 27 | return (vec3_len(vec3_sub(a, b))); 28 | } 29 | 30 | double vec3_dot(t_vec3 a, t_vec3 b) 31 | { 32 | return (a.x * b.x + a.y * b.y + a.z * b.z); 33 | } 34 | 35 | t_vec3 vec3_cross(t_vec3 a, t_vec3 b) 36 | { 37 | t_vec3 c; 38 | 39 | c.x = a.y * b.z - a.z * b.y; 40 | c.y = a.z * b.x - a.x * b.z; 41 | c.z = a.x * b.y - a.y * b.x; 42 | return (c); 43 | } 44 | -------------------------------------------------------------------------------- /lib/libmlx/src/mlx_put_pixel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mlx_put_pixel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/28 03:30:13 by W2Wizard #+# #+# */ 9 | /* Updated: 2022/04/21 12:49:48 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "MLX42/MLX42_Int.h" 14 | 15 | // BUG: Linux may experience a red hue instead due to endiannes 16 | void mlx_draw_pixel(uint8_t* pixel, uint32_t color) 17 | { 18 | *(pixel++) = (uint8_t)(color >> 24); 19 | *(pixel++) = (uint8_t)(color >> 16); 20 | *(pixel++) = (uint8_t)(color >> 8); 21 | *(pixel++) = (uint8_t)(color & 0xFF); 22 | } 23 | 24 | //= Public =// 25 | 26 | void mlx_put_pixel(mlx_image_t* image, int32_t x, int32_t y, uint32_t color) 27 | { 28 | MLX_ASSERT(!image); 29 | MLX_ASSERT(x < 0); 30 | MLX_ASSERT(y < 0); 31 | 32 | uint8_t* pixelstart = &image->pixels[(y * image->width + x) * BPP]; 33 | mlx_draw_pixel(pixelstart, color); 34 | } 35 | -------------------------------------------------------------------------------- /src/print/mrt_print_material.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_material.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:29 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:29 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | void print_material(t_material *material) 16 | { 17 | print_color(material->color); 18 | print_scene_double(material->surface[DIFFUSE], "DIFFUSE:", COLOR_PL); 19 | print_scene_double(material->surface[SPECULAR], "SPECULAR:", COLOR_PL); 20 | print_scene_double(material->surface[DIELECTRIC], "DIELEC:", COLOR_PL); 21 | print_scene_double(material->fuzz, "FUZZ:", NULL); 22 | print_scene_double(material->refraction_index, "REFRAC:", NULL); 23 | if (material->cb_factor > 0) 24 | print_scene_int(material->cb_factor, "CB:", COLOR_NO); 25 | if (material->c_texture) 26 | printf("\tTEXTURE: %s\n", material->c_texture->name); 27 | } 28 | -------------------------------------------------------------------------------- /src/graphic/mrt_graphic_move.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_graphic_move.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:55:47 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:55:47 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_graphic.h" 14 | 15 | void move(t_scene *scene, t_vec3 new_pos) 16 | { 17 | scene->cam.pos = new_pos; 18 | scene->img.pos = vec3_add(vec3_add(scene->cam.pos, scene->cam.dir), 19 | vec3_add(scene->img.qx, scene->img.qy)); 20 | reset_sampling(scene); 21 | } 22 | 23 | void reset_cam(t_scene *scene) 24 | { 25 | scene->cam.pos = scene->cam.pos_initial; 26 | scene->cam.dir = scene->cam.dir_initial; 27 | scene_calc_img_pos(scene); 28 | reset_sampling(scene); 29 | } 30 | 31 | void reset_sampling(t_scene *scene) 32 | { 33 | scene->sampling.samp = 0; 34 | ft_bzero(scene->img.pixel, scene->img.width 35 | * scene->img.height * sizeof(t_color)); 36 | } 37 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/17 12:49:56 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:54:11 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that copies 'n' bytes from memory area 'src' to memory area 'dst'. 17 | * The memory areas may overlap. 18 | */ 19 | void *ft_memmove(void *dst, const void *src, size_t len) 20 | { 21 | size_t i; 22 | 23 | if (len == 0) 24 | return (dst); 25 | if (src < dst) 26 | { 27 | i = len - 1; 28 | while (i > 0) 29 | { 30 | ((char *)dst)[i] = ((char *)src)[i]; 31 | i--; 32 | } 33 | ((char *)dst)[0] = ((char *)src)[0]; 34 | } 35 | else if (src > dst) 36 | { 37 | i = 0; 38 | while (i < len) 39 | { 40 | ((char *)dst)[i] = ((char *)src)[i]; 41 | i++; 42 | } 43 | } 44 | return (dst); 45 | } 46 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/22 15:14:29 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 13:18:52 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns a new string which is the result of the concatenation 17 | * of the strings 's1' and 's2'. Memory for the new string is obtained with 18 | * malloc. 19 | */ 20 | char *ft_strjoin(char const *s1, char const *s2) 21 | { 22 | size_t len_s1; 23 | size_t len_s2; 24 | char *str; 25 | 26 | if (s1 == NULL || s2 == NULL) 27 | return (NULL); 28 | len_s1 = ft_strlen(s1); 29 | len_s2 = ft_strlen(s2); 30 | str = malloc(sizeof(char) * (len_s1 + len_s2 + 1)); 31 | if (str == NULL) 32 | return (NULL); 33 | ft_memcpy(str, s1, len_s1); 34 | ft_memcpy(str + len_s1, s2, len_s2 + 1); 35 | return (str); 36 | } 37 | -------------------------------------------------------------------------------- /lib/libft/src/additional/ft_strncmp_rev.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp_rev.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/18 10:53:55 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/08 15:10:43 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp_rev(const char *s1, const char *s2, size_t n) 16 | { 17 | size_t i; 18 | size_t s1_len; 19 | size_t s2_len; 20 | 21 | s1_len = ft_strlen(s1); 22 | s2_len = ft_strlen(s2); 23 | i = 0; 24 | while (i < n && i < s1_len && i < s2_len) 25 | { 26 | if (s1[s1_len - 1 - i] != s2[s2_len - 1 - i]) 27 | return ((unsigned char)s1[s1_len - 1 - i] 28 | - (unsigned char)s2[s2_len - 1 - i]); 29 | i++; 30 | } 31 | if (i != n && i == s1_len && i < s2_len) 32 | return (-(unsigned char)s2[s2_len - 1 - i]); 33 | if (i != n && i == s2_len && i < s1_len) 34 | return ((unsigned char)s1[s1_len - 1 - i]); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/16 15:53:29 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 12:55:48 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that copies up to 'dstsize' - 1 characters from the null-terminated 17 | * string 'src' to 'dst' and null-terminates the result. The return value is 18 | * the total length of the resulting string. 19 | */ 20 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) 21 | { 22 | size_t i; 23 | 24 | i = 0; 25 | if (dst == NULL && src == NULL) 26 | return (0); 27 | if (dstsize == 0) 28 | return (ft_strlen(src)); 29 | while (src[i] != '\0' && i < dstsize - 1 && dstsize > 0) 30 | { 31 | dst[i] = src[i]; 32 | i++; 33 | } 34 | if (i < dstsize) 35 | dst[i] = '\0'; 36 | while (src[i] != '\0') 37 | i++; 38 | return (i); 39 | } 40 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_color.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_color.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:52 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 17:44:13 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | 15 | int parse_color(const char *str, t_color *color) 16 | { 17 | char **split; 18 | int primary; 19 | int error; 20 | 21 | split = ft_split(str, ','); 22 | if (split == NULL) 23 | return (-1); 24 | primary = 0; 25 | error = (ft_split_count_str(split) != 3); 26 | if (!error) 27 | error = int_from_str(split[0], 0, 255, &primary); 28 | (*color).r = (double)primary / 255.0; 29 | if (!error) 30 | error = int_from_str(split[1], 0, 255, &primary); 31 | (*color).g = (double)primary / 255.0; 32 | if (!error) 33 | error = int_from_str(split[2], 0, 255, &primary); 34 | (*color).b = (double)primary / 255.0; 35 | ft_free_split(&split); 36 | return (error); 37 | } 38 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/18 16:34:21 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:50:58 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that copies up to 'n' bytes from memory area 'src' 17 | * to memory area 'dst', stopping when the character 'c' is found. 18 | * For overlapping memory areas, the behaviour is undefined. 19 | */ 20 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 21 | { 22 | unsigned char *dst_copy; 23 | unsigned char *src_copy; 24 | size_t i; 25 | 26 | dst_copy = (unsigned char *)dst; 27 | src_copy = (unsigned char *)src; 28 | i = 0; 29 | while (i < n) 30 | { 31 | dst_copy[i] = src_copy[i]; 32 | if (src_copy[i] == (unsigned char) c) 33 | break ; 34 | i++; 35 | } 36 | if (i == n) 37 | return (NULL); 38 | else 39 | return (dst + i + 1); 40 | } 41 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_specular.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_specular.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:55:12 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:55:13 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace.h" 14 | 15 | t_vec3 specular(t_list *obj, t_ray *ray, t_hit *hit) 16 | { 17 | t_vec3 dir; 18 | 19 | if (obj_cont(obj)->material.fuzz == 0.0) 20 | return (vec3_norm(vec3_add(ray->dir, vec3_scale(-2.0 21 | * vec3_dot(ray->dir, hit->normal), hit->normal)))); 22 | dir = vec3_add(ray->dir, vec3_add(vec3_scale(-2.0 23 | * vec3_dot(ray->dir, hit->normal), hit->normal), 24 | vec3_scale(obj_material(obj)->fuzz, random_in_unit_sphere()))); 25 | if (hit->side == OUTSIDE && vec3_dot(dir, hit->normal) <= 0.0) 26 | return ((t_vec3){0.0, 0.0, 0.0}); 27 | else if (hit->side == INSIDE && vec3_dot(dir, hit->normal) >= 0.0) 28 | return ((t_vec3){0.0, 0.0, 0.0}); 29 | return (vec3_norm(dir)); 30 | } 31 | -------------------------------------------------------------------------------- /lib/libmlx/src/mlx_cursor.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* :::::::: */ 4 | /* mlx_cursor.c :+: :+: */ 5 | /* +:+ */ 6 | /* By: W2Wizard +#+ */ 7 | /* +#+ */ 8 | /* Created: 2022/01/18 20:10:54 by W2Wizard #+# #+# */ 9 | /* Updated: 2022/04/13 00:13:17 by w2wizard ######## odam.nl */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "MLX42/MLX42_Int.h" 14 | 15 | //= Public =// 16 | 17 | void* mlx_create_cursor(mlx_t* mlx, mlx_texture_t* image) 18 | { 19 | MLX_ASSERT(!mlx); 20 | MLX_ASSERT(!image); 21 | 22 | GLFWcursor* cursor; 23 | if ((cursor = glfwCreateCursor((GLFWimage*)image, 0, 0))) 24 | { 25 | glfwSetCursor(mlx->window, cursor); 26 | return (cursor); 27 | } 28 | return ((void *)mlx_error(MLX_MEMFAIL)); 29 | } 30 | 31 | void mlx_set_cursor(mlx_t* mlx, void* cursor) 32 | { 33 | MLX_ASSERT(!mlx); 34 | MLX_ASSERT(!cursor); 35 | 36 | glfwSetCursor(mlx->window, cursor); 37 | } 38 | 39 | void mlx_set_cursor_mode(mlx_t* mlx, mouse_mode_t mode) 40 | { 41 | MLX_ASSERT(!mlx); 42 | 43 | glfwSetInputMode(mlx->window, GLFW_CURSOR, mode); 44 | } 45 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/22 15:47:04 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 13:19:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that applies the function 'f' to each character of the string 's' 17 | * to create a new string (with malloc), resulting from successive 18 | * applications of 'f' (using the string index as the first argument of 'f'). 19 | */ 20 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 21 | { 22 | unsigned int i; 23 | size_t len; 24 | char *str; 25 | 26 | if (s == NULL || f == NULL) 27 | return (NULL); 28 | i = 0; 29 | len = ft_strlen(s); 30 | str = malloc(sizeof(char) * (len + 1)); 31 | if (str == NULL) 32 | return (NULL); 33 | ft_memcpy(str, s, len + 1); 34 | while (str[i]) 35 | { 36 | str[i] = f(i, str[i]); 37 | i++; 38 | } 39 | return (str); 40 | } 41 | -------------------------------------------------------------------------------- /src/math/mrt_math_vec3_1.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_math_vec3_1.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/19 10:31:25 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/08 11:48:54 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_math.h" 14 | 15 | t_vec3 vec3_add(t_vec3 a, t_vec3 b) 16 | { 17 | return ((t_vec3){a.x + b.x, a.y + b.y, a.z + b.z}); 18 | } 19 | 20 | t_vec3 vec3_sub(t_vec3 a, t_vec3 b) 21 | { 22 | return ((t_vec3){a.x - b.x, a.y - b.y, a.z - b.z}); 23 | } 24 | 25 | t_vec3 vec3_scale(double alpha, t_vec3 a) 26 | { 27 | return ((t_vec3){alpha * a.x, alpha * a.y, alpha * a.z}); 28 | } 29 | 30 | t_vec3 vec3_lin_comb(double alpha, t_vec3 a, double beta, t_vec3 b) 31 | { 32 | return (vec3_add(vec3_scale(alpha, a), vec3_scale(beta, b))); 33 | } 34 | 35 | t_vec3 vec3_norm(t_vec3 a) 36 | { 37 | double alpha; 38 | 39 | alpha = 1.0 / sqrt(a.x * a.x + a.y * a.y + a.z * a.z); 40 | return ((t_vec3){alpha * a.x, alpha * a.y, alpha * a.z}); 41 | } 42 | -------------------------------------------------------------------------------- /src/mrt_obj.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_obj.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:54:30 by tjensen #+# #+# */ 9 | /* Updated: 2022/12/19 12:43:34 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_obj.h" 14 | #include "libmlx/include/MLX42/MLX42.h" 15 | 16 | t_list *obj_new(void) 17 | { 18 | t_obj *c_obj; 19 | 20 | c_obj = malloc(sizeof(t_obj)); 21 | if (c_obj == NULL) 22 | return (NULL); 23 | ft_bzero(c_obj, sizeof(t_obj)); 24 | return (ft_lstnew(c_obj)); 25 | } 26 | 27 | inline t_material *obj_material(t_list *obj) 28 | { 29 | return (&(obj_cont(obj)->material)); 30 | } 31 | 32 | inline t_texture *obj_c_texture(t_list *obj) 33 | { 34 | return (obj_cont(obj)->material.c_texture); 35 | } 36 | 37 | inline t_obj *obj_cont(t_list *obj) 38 | { 39 | return ((t_obj *)obj->content); 40 | } 41 | 42 | inline t_color obj_color(t_list *obj, t_hit *hit __attribute__((unused))) 43 | { 44 | return (obj_cont(obj)->material.color); 45 | } 46 | -------------------------------------------------------------------------------- /src/math/mrt_math.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_math.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/07 09:49:49 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 13:19:14 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MRT_MATH_H 14 | # define MRT_MATH_H 15 | 16 | # include "mrt_color.h" 17 | # include "mrt_vec3.h" 18 | # include "mrt_scene.h" 19 | 20 | # include 21 | 22 | t_color color_add(t_color c1, t_color c2); 23 | t_color color_scale(double f, t_color c1); 24 | t_color color_multi(t_color c1, t_color c2); 25 | 26 | t_vec3 vec3_add(t_vec3 a, t_vec3 b); 27 | t_vec3 vec3_sub(t_vec3 a, t_vec3 b); 28 | t_vec3 vec3_scale(double alpha, t_vec3 a); 29 | t_vec3 vec3_lin_comb(double alpha, t_vec3 a, double beta, t_vec3 b); 30 | t_vec3 vec3_norm(t_vec3 a); 31 | 32 | bool vec3_equal(t_vec3 a, t_vec3 b); 33 | double vec3_len(t_vec3 a); 34 | double vec3_dist(t_vec3 a, t_vec3 b); 35 | double vec3_dot(t_vec3 a, t_vec3 b); 36 | t_vec3 vec3_cross(t_vec3 a, t_vec3 b); 37 | 38 | #endif // MRT_MATH_H 39 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_amb.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_amb.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:57 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/10 09:50:22 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | 15 | int parse_amb(t_scene *scene, char **split, int line_num) 16 | { 17 | if (scene->amb.set == true) 18 | return (print_error_scene(line_num, ERR_PARSE, ERR_DUP_ID, ID_AMB)); 19 | if (ft_split_count_str(split) != 3) 20 | return (print_error_scene(line_num, ERR_PARSE, ERR_NUM_PARA, NULL)); 21 | if (double_from_str(split[1], 1, 2, &(scene->amb.brightness)) 22 | || scene->amb.brightness < 0.0 || scene->amb.brightness > 1.0) 23 | return (print_error_scene(line_num, ERR_PARSE, ERR_RATIO, NULL)); 24 | if (parse_color(split[2], &(scene->amb.color))) 25 | return (print_error_scene(line_num, ERR_PARSE, ERR_COLOR, NULL)); 26 | scene->amb.color = color_scale(scene->amb.brightness, scene->amb.color); 27 | scene->amb.set = true; 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /src/mrt_obj_texture_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_obj_texture_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:54:24 by tjensen #+# #+# */ 9 | /* Updated: 2022/12/19 12:43:17 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft/inc/libft.h" 14 | #include "mrt_color.h" 15 | #include "mrt_vec3.h" 16 | #include "mrt_obj.h" 17 | #include "math/mrt_math.h" 18 | #include "trace/mrt_trace.h" 19 | 20 | t_list *texture_new(void) 21 | { 22 | t_texture *c_texture; 23 | 24 | c_texture = malloc(sizeof(t_texture)); 25 | if (c_texture == NULL) 26 | return (NULL); 27 | ft_bzero(c_texture, sizeof(t_texture)); 28 | return (ft_lstnew(c_texture)); 29 | } 30 | 31 | void c_texture_destroy(void *in) 32 | { 33 | t_texture *texture; 34 | 35 | texture = in; 36 | free(texture->name); 37 | texture->name = NULL; 38 | free(texture->color); 39 | texture->color = NULL; 40 | free(texture); 41 | } 42 | 43 | inline t_texture *texture_cont(t_list *texture) 44 | { 45 | return ((t_texture *)texture->content); 46 | } 47 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/17 13:28:55 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 12:56:01 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that appends the null-terminated string 'src' to the end of 'dst', 17 | * appending at most 'dstsize' - strlen(dst) - 1 bytes and null-terminating 18 | * the result. The return value is the total length of the resulting string. 19 | */ 20 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize) 21 | { 22 | size_t len_dst; 23 | size_t len_src; 24 | size_t i; 25 | 26 | len_dst = ft_strlen(dst); 27 | len_src = ft_strlen(src); 28 | i = 0; 29 | while (src[i] != '\0' && len_dst + i + 1 < dstsize) 30 | { 31 | dst[len_dst + i] = src[i]; 32 | i++; 33 | } 34 | if (dstsize > 0 && dstsize > len_dst) 35 | dst[len_dst + i] = '\0'; 36 | if (dstsize > len_dst) 37 | return (len_dst + len_src); 38 | else 39 | return (dstsize + len_src); 40 | } 41 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_obj_normal_1.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_obj_normal_1.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:55:26 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:55:27 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace.h" 14 | 15 | t_vec3 normal_plane(t_list *obj, t_hit *hit __attribute__((unused))) 16 | { 17 | return (obj_cont(obj)->pl.dir); 18 | } 19 | 20 | t_vec3 normal_disc(t_list *obj, t_hit *hit __attribute__((unused))) 21 | { 22 | return (obj_cont(obj)->di.dir); 23 | } 24 | 25 | t_vec3 normal_sphere(t_list *obj, t_hit *hit) 26 | { 27 | return (vec3_scale(1.0 / obj_cont(obj)->sp.radius, 28 | vec3_sub(hit->p, obj_cont(obj)->sp.pos))); 29 | } 30 | 31 | t_vec3 normal_tube(t_list *obj, t_hit *hit) 32 | { 33 | t_vec3 normal; 34 | double h; 35 | 36 | h = vec3_dot(vec3_sub(hit->p, obj_cont(obj)->tb.pos), 37 | obj_cont(obj)->tb.dir); 38 | normal = vec3_norm(vec3_sub(hit->p, vec3_add(obj_cont(obj)->tb.pos, 39 | vec3_scale(h, obj_cont(obj)->tb.dir)))); 40 | return (normal); 41 | } 42 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/21 09:20:03 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 13:23:21 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that returns a substring of the string 's', begins at index 'start' 17 | * and being of maximum size 'len'. Memory for the substring is obtained with 18 | * malloc. 19 | */ 20 | char *ft_substr(char const *s, unsigned int start, size_t len) 21 | { 22 | char *subs; 23 | unsigned int i; 24 | 25 | if (s == NULL) 26 | return (NULL); 27 | if (start >= ft_strlen(s)) 28 | { 29 | subs = malloc(sizeof(char)); 30 | if (subs == NULL) 31 | return (NULL); 32 | subs[0] = '\0'; 33 | return (subs); 34 | } 35 | subs = malloc(sizeof(char) * (len + 1)); 36 | if (subs == NULL) 37 | return (NULL); 38 | i = 0; 39 | while (i < len && s[start + i] != '\0') 40 | { 41 | subs[i] = s[start + i]; 42 | i++; 43 | } 44 | subs[i] = '\0'; 45 | return (subs); 46 | } 47 | -------------------------------------------------------------------------------- /lib/libmlx/Makefile_Unix.mk: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile_Unix.mk :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: tjensen +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2022/02/26 21:36:38 by W2Wizard #+# #+# # 9 | # Updated: 2022/04/15 11:46:38 by tjensen ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | #//= Colors =//# 14 | BOLD := \033[1m 15 | BLACK := \033[30;1m 16 | RED := \033[31;1m 17 | GREEN := \033[32;1m 18 | YELLOW := \033[33;1m 19 | BLUE := \033[34;1m 20 | MAGENTA := \033[35;1m 21 | CYAN := \033[36;1m 22 | WHITE := \033[37;1m 23 | RESET := \033[0m 24 | 25 | #//= Make Rules =//# 26 | $(NAME): $(OBJS) 27 | @ar rc $(NAME) $(OBJS) 28 | @echo "$(GREEN)$(BOLD)Done$(RESET)" 29 | 30 | %.o: %.c 31 | @echo "$(GREEN)$(BOLD)Compiling: $(notdir $<)$(RESET)" 32 | @$(CC) $(CFLAGS) -o $@ -c $< $(HEADERS) 33 | 34 | # Convert shaders to .c files 35 | $(SRC_DIR)/mlx_%_shader.c: $(SHADER_DIR)/default.% 36 | @echo "$(GREEN)$(BOLD)Shader to C: $< -> $@$(RESET)" 37 | @bash tools/compile_shader.sh $< > $@ 38 | 39 | clean: 40 | @echo "$(RED)Cleaning$(RESET)" 41 | @rm -f $(OBJS) $(SHDR) 42 | 43 | fclean: clean 44 | @rm -f $(NAME) 45 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_random.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_random.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:55:15 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:55:16 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace.h" 14 | 15 | t_vec3 random_in_unit_sphere(void) 16 | { 17 | t_vec3 vec3; 18 | 19 | while (true) 20 | { 21 | vec3.x = -1.0 + (rand() / (double)(RAND_MAX / 2.0)); 22 | vec3.y = -1.0 + (rand() / (double)(RAND_MAX / 2.0)); 23 | vec3.z = -1.0 + (rand() / (double)(RAND_MAX / 2.0)); 24 | if (vec3_dot(vec3, vec3) < 1.0) 25 | return (vec3); 26 | } 27 | } 28 | 29 | t_vec3 random_cosine_direction_onb(t_vec3 normal) 30 | { 31 | t_onb uvw; 32 | 33 | uvw = onb_build(normal); 34 | return (onb_transform(uvw, random_cosine_direction())); 35 | } 36 | 37 | t_vec3 random_cosine_direction(void) 38 | { 39 | t_vec3 vec3; 40 | double r1; 41 | double r2; 42 | double phi; 43 | 44 | r1 = ft_rand(); 45 | r2 = ft_rand(); 46 | vec3.z = sqrt(1 - r2); 47 | phi = 2 * M_PI * r1; 48 | vec3.x = cos(phi) * sqrt(r2); 49 | vec3.y = sin(phi) * sqrt(r2); 50 | return (vec3); 51 | } 52 | -------------------------------------------------------------------------------- /lib/libmlx/src/mlx_exit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* :::::::: */ 4 | /* mlx_exit.c :+: :+: */ 5 | /* +:+ */ 6 | /* By: W2Wizard +#+ */ 7 | /* +#+ */ 8 | /* Created: 2021/12/28 02:43:22 by W2Wizard #+# #+# */ 9 | /* Updated: 2022/04/13 00:08:15 by w2wizard ######## odam.nl */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "MLX42/MLX42_Int.h" 14 | 15 | //= Private =// 16 | 17 | static void mlx_free_image(void* content) 18 | { 19 | mlx_image_t* img = content; 20 | 21 | mlx_freen(3, img->context, img->pixels, img->instances); 22 | } 23 | 24 | //= Public =// 25 | 26 | void mlx_close_window(mlx_t* mlx) 27 | { 28 | MLX_ASSERT(!mlx); 29 | glfwSetWindowShouldClose(mlx->window, true); 30 | } 31 | 32 | /** 33 | * All of glfw & glads resources are cleaned up by the terminate function. 34 | * Now its time to cleanup our own mess. 35 | */ 36 | void mlx_terminate(mlx_t* mlx) 37 | { 38 | MLX_ASSERT(!mlx); 39 | 40 | mlx_ctx_t *const mlxctx = mlx->context; 41 | 42 | glfwTerminate(); 43 | mlx_lstclear((mlx_list_t**)(&mlxctx->hooks), &free); 44 | mlx_lstclear((mlx_list_t**)(&mlxctx->render_queue), &free); 45 | mlx_lstclear((mlx_list_t**)(&mlxctx->images), &mlx_free_image); 46 | mlx_freen(2, mlxctx, mlx); 47 | } 48 | -------------------------------------------------------------------------------- /src/graphic/mrt_graphic.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_graphic.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/07 09:50:23 by tjensen #+# #+# */ 9 | /* Updated: 2022/12/19 12:43:34 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MRT_GRAPHIC_H 14 | # define MRT_GRAPHIC_H 15 | 16 | # include "mrt_color.h" 17 | # include "mrt_vec3.h" 18 | # include "mrt_obj.h" 19 | # include "mrt_scene.h" 20 | # include "math/mrt_math.h" 21 | # include "trace/mrt_trace.h" 22 | # include "libmlx/include/MLX42/MLX42.h" 23 | 24 | # define WIN_NAME "miniRT" 25 | 26 | typedef struct s_graphic_data 27 | { 28 | mlx_t *mlx; 29 | mlx_image_t *mlx_img; 30 | t_scene *scene; 31 | } t_graphic_data; 32 | 33 | int graphic(t_scene *scene); 34 | 35 | void move(t_scene *scene, t_vec3 new_pos); 36 | void rotate_horizontal(t_scene *scene, double rot); 37 | void rotate_vertical(t_scene *scene, bool up, double rot); 38 | 39 | void graphic_render(t_graphic_data *graphic); 40 | void *draw_thread(void *thread_data); 41 | void img_to_mlx_img(t_scene *scene, mlx_image_t *mlx_img); 42 | 43 | void reset_cam(t_scene *scene); 44 | void reset_sampling(t_scene *scene); 45 | 46 | #endif // MRT_GRAPHIC_H 47 | -------------------------------------------------------------------------------- /lib/libmlx/src/mlx_keys.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* :::::::: */ 4 | /* mlx_keys.c :+: :+: */ 5 | /* +:+ */ 6 | /* By: W2Wizard +#+ */ 7 | /* +#+ */ 8 | /* Created: 2022/01/01 21:06:45 by W2Wizard #+# #+# */ 9 | /* Updated: 2022/04/13 00:24:08 by w2wizard ######## odam.nl */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "MLX42/MLX42_Int.h" 14 | 15 | //= Private =// 16 | 17 | static void mlx_key_callback(GLFWwindow* window, int32_t key, int32_t scancode, int32_t action, int32_t mods) 18 | { 19 | const mlx_t* mlx = glfwGetWindowUserPointer(window); 20 | const mlx_key_t key_hook = ((mlx_ctx_t*)mlx->context)->key_hook; 21 | const mlx_key_data_t callback_data = { 22 | key, 23 | action, 24 | scancode, 25 | mods, 26 | }; 27 | 28 | key_hook.func(callback_data, key_hook.param); 29 | } 30 | 31 | //= Public =// 32 | 33 | void mlx_key_hook(mlx_t* mlx, mlx_keyfunc func, void* param) 34 | { 35 | MLX_ASSERT(!mlx ); 36 | MLX_ASSERT(!func); 37 | 38 | mlx_ctx_t* mlxctx = mlx->context; 39 | mlxctx->key_hook.func = func; 40 | mlxctx->key_hook.param = param; 41 | glfwSetKeyCallback(mlx->window, mlx_key_callback); 42 | } 43 | 44 | bool mlx_is_key_down(mlx_t* mlx, keys_t key) 45 | { 46 | MLX_ASSERT(!mlx); 47 | 48 | return (glfwGetKey(mlx->window, key) == GLFW_PRESS); 49 | } 50 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/22 16:24:58 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 13:21:22 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that creates and returns a copy of string 's1' with the characters 17 | * specified in 'set' removed from the beginning and the end of the string. 18 | * Memory for the new string is obtained with malloc. 19 | */ 20 | char *ft_strtrim(char const *s1, char const *set) 21 | { 22 | size_t min; 23 | size_t max; 24 | char *s2; 25 | 26 | if (s1 == NULL) 27 | return (NULL); 28 | min = 0; 29 | max = ft_strlen(s1) - 1; 30 | while (s1[min] != '\0' && ft_strchr(set, s1[min]) != NULL) 31 | min++; 32 | while (max >= min && ft_strchr(set, s1[max]) != NULL) 33 | max--; 34 | if (max < min || s1[0] == '\0') 35 | s2 = (char *)ft_calloc(1, sizeof(char)); 36 | else 37 | s2 = malloc(sizeof(char) * (max - min + 2)); 38 | if (s2 == NULL) 39 | return (NULL); 40 | if (max >= min && s1[0] != '\0') 41 | { 42 | ft_memcpy(s2, s1 + min, max - min + 1); 43 | s2[max - min + 1] = '\0'; 44 | } 45 | return (s2); 46 | } 47 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/16 11:05:29 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 13:11:34 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that locates the first occurrence of the null-terminated string 17 | * 'needle' in the null-terminated string 'haystack', searching at most 'len' 18 | * bytes. The return value is either a pointer to the first character of the 19 | * first occurrence of 'needle' in 'haystack', or NULL if 'needle' isn't 20 | * contained in 'haystack'. 21 | */ 22 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 23 | { 24 | size_t i; 25 | size_t j; 26 | 27 | if (ft_strlen(needle) == 0) 28 | return ((char *)haystack); 29 | if (len == 0) 30 | return (NULL); 31 | i = 0; 32 | while (haystack[i] != '\0' && i < len - ft_strlen(needle) + 1) 33 | { 34 | j = 0; 35 | while (needle[j] != '\0') 36 | { 37 | if (needle[j] == haystack[i + j]) 38 | j++; 39 | else 40 | break ; 41 | } 42 | if (needle[j] == '\0') 43 | return ((char *)haystack + i); 44 | i++; 45 | } 46 | return (NULL); 47 | } 48 | -------------------------------------------------------------------------------- /lib/libmlx/Makefile_WindowsNT.mk: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # :::::::: # 4 | # Makefile_WindowsNT.mk :+: :+: # 5 | # +:+ # 6 | # By: W2Wizard +#+ # 7 | # +#+ # 8 | # Created: 2022/02/26 21:32:00 by W2Wizard #+# #+# # 9 | # Updated: 2022/02/26 21:32:00 by W2Wizard ######## odam.nl # 10 | # # 11 | # **************************************************************************** # 12 | 13 | # While windows does have support for nmake it offers nowhere near the amount of 14 | # features GnuWin does. 15 | 16 | #//= Colors =//# 17 | # Nope :( 18 | 19 | CC := gcc # We need to explicitly mention GCC/CC here. 20 | WINSTFU := > NUL 2>&1 # In some cases we want windows to just stfu 21 | 22 | # Switch file paths to windows \ delimiter 23 | SHDR := $(subst /,\,$(SHDR)) 24 | LIB := $(subst /,\,$(LIB)) 25 | SRCS := $(subst /,\,$(SRCS)) 26 | OBJS := $(subst /,\,$(OBJS)) 27 | 28 | #//= Make Rules =//# 29 | $(NAME): $(OBJS) 30 | @ar rc $(NAME) $(OBJS) 31 | @echo Done 32 | 33 | %.o: %.c 34 | @echo Compiling: $(notdir $<) 35 | @$(CC) $(CFLAGS) -o $@ -c $< $(HEADERS) 36 | 37 | # Convert shaders to .c files 38 | $(SRC_DIR)\mlx_%_shader.c: $(SHADER_DIR)\default.% 39 | @echo Shader to C: $< -^> $@ 40 | @.\tools\compile_shader.bat $< > $@ 41 | 42 | clean: 43 | @echo Cleaning 44 | @del /F /Q $(OBJS) $(SHDR) $(WINSTFU) 45 | 46 | fclean: clean 47 | @del /F /Q $(NAME) $(WINSTFU) 48 | -------------------------------------------------------------------------------- /lib/libft/src/bonus/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/24 17:03:58 by hepple #+# #+# */ 9 | /* Updated: 2021/10/29 11:03:16 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Function that iterates through the list 'lst' and creates a copy of the 17 | * whole list, where the function 'f' is applied to the content of each 18 | * element. If needed, the function 'del' is used to delete the content of 19 | * an element. 20 | */ 21 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 22 | { 23 | t_list *new_lst; 24 | t_list *new_element; 25 | 26 | if (lst == NULL || f == NULL) 27 | return (NULL); 28 | new_lst = ft_lstnew(f(lst->content)); 29 | if (new_lst == NULL) 30 | { 31 | ft_lstclear(&new_lst, del); 32 | return (NULL); 33 | } 34 | lst = lst->next; 35 | while (lst != NULL) 36 | { 37 | new_element = ft_lstnew(f(lst->content)); 38 | if (new_element == NULL) 39 | { 40 | ft_lstclear(&new_lst, del); 41 | ft_lstclear(&new_element, del); 42 | return (NULL); 43 | } 44 | ft_lstadd_back(&new_lst, new_element); 45 | lst = lst->next; 46 | } 47 | return (new_lst); 48 | } 49 | -------------------------------------------------------------------------------- /lib/libmlx/.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | workflow_dispatch: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | schedule: 9 | - cron: '0 0 * * FRI' 10 | 11 | jobs: 12 | build-unix: 13 | timeout-minutes: 10 14 | runs-on: ${{ matrix.os }} 15 | strategy: 16 | matrix: 17 | os: [ubuntu-latest, macos-latest] 18 | 19 | steps: 20 | - name: Clone repository 21 | uses: actions/checkout@v2 22 | 23 | - name: Install Dependencies 24 | run: | 25 | set -x 26 | if [ "$RUNNER_OS" == "Linux" ]; then 27 | sudo apt-get update -qq 28 | sudo apt-get install -y -qq gcc make xorg build-essential cmake xorg-dev libx11-dev libglu1-mesa-dev freeglut3-dev libglew1.5 libglew1.5-dev libglu1-mesa libgl1-mesa-glx libgl1-mesa-dev libglfw3-dev libglfw3 29 | elif [ "$RUNNER_OS" == "macOS" ]; then 30 | brew update 31 | brew install glfw 32 | else 33 | echo "$RUNNER_OS not supported" 34 | exit 1 35 | fi 36 | 37 | - name: Build 38 | run: make 39 | 40 | build-win: 41 | timeout-minutes: 20 42 | runs-on: windows-latest 43 | 44 | steps: 45 | - name: Clone repository 46 | uses: actions/checkout@v2 47 | 48 | - name: Install MinGW 49 | uses: egor-tensin/setup-mingw@v2 50 | 51 | - name: Download GLFW binaries 52 | uses: carlosperate/download-file-action@v1.1.1 53 | with: 54 | file-url: 'https://github.com/glfw/glfw/releases/download/3.3.6/glfw-3.3.6.bin.WIN64.zip' 55 | file-name: 'glfw.zip' 56 | location: '${{ github.workspace }}\lib' 57 | 58 | - name: Unpack GLFW binaries 59 | run: | 60 | unzip 'lib/glfw.zip' -d 'lib' 61 | rm 'lib\glfw.zip' 62 | mv 'lib\glfw*' 'lib\glfw' 63 | 64 | - name: Build 65 | run: make HEADERS="-I lib/glfw/include" WIN_UNIX=1 66 | -------------------------------------------------------------------------------- /lib/libmlx/shaders/default.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | in vec2 TexCoord; 4 | flat in int TexIndex; 5 | 6 | out vec4 FragColor; 7 | 8 | uniform sampler2D Texture0; 9 | uniform sampler2D Texture1; 10 | uniform sampler2D Texture2; 11 | uniform sampler2D Texture3; 12 | uniform sampler2D Texture4; 13 | uniform sampler2D Texture5; 14 | uniform sampler2D Texture6; 15 | uniform sampler2D Texture7; 16 | uniform sampler2D Texture8; 17 | uniform sampler2D Texture9; 18 | uniform sampler2D Texture10; 19 | uniform sampler2D Texture11; 20 | uniform sampler2D Texture12; 21 | uniform sampler2D Texture13; 22 | uniform sampler2D Texture14; 23 | uniform sampler2D Texture15; 24 | 25 | void main() 26 | { 27 | vec4 outColor = vec4(1.0, 0.0, 0.0, 1.0); 28 | switch (int(TexIndex)) { 29 | case 0: outColor = texture(Texture0, TexCoord); break; 30 | case 1: outColor = texture(Texture1, TexCoord); break; 31 | case 2: outColor = texture(Texture2, TexCoord); break; 32 | case 3: outColor = texture(Texture3, TexCoord); break; 33 | case 4: outColor = texture(Texture4, TexCoord); break; 34 | case 5: outColor = texture(Texture5, TexCoord); break; 35 | case 6: outColor = texture(Texture6, TexCoord); break; 36 | case 7: outColor = texture(Texture7, TexCoord); break; 37 | case 8: outColor = texture(Texture8, TexCoord); break; 38 | case 9: outColor = texture(Texture9, TexCoord); break; 39 | case 10: outColor = texture(Texture10, TexCoord); break; 40 | case 11: outColor = texture(Texture11, TexCoord); break; 41 | case 12: outColor = texture(Texture12, TexCoord); break; 42 | case 13: outColor = texture(Texture13, TexCoord); break; 43 | case 14: outColor = texture(Texture14, TexCoord); break; 44 | case 15: outColor = texture(Texture15, TexCoord); break; 45 | default: outColor = vec4(1.0, 0.0, 0.0, 1.0); break; 46 | } 47 | FragColor = outColor; 48 | } 49 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_cam.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_cam.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:54 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/10 09:50:22 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | 15 | int parse_cam(t_scene *scene, char **split, int line_num) 16 | { 17 | t_vec3 look_at; 18 | 19 | if (scene->cam.set == true) 20 | return (print_error_scene(line_num, ERR_PARSE, ERR_DUP_ID, ID_CAM)); 21 | if (ft_split_count_str(split) != 4) 22 | return (print_error_scene(line_num, ERR_PARSE, ERR_NUM_PARA, NULL)); 23 | if (parse_vec3(split[1], &(scene->cam.pos))) 24 | return (print_error_scene(line_num, ERR_PARSE, ERR_POS, VEC3_RANGE)); 25 | scene->cam.pos_initial = scene->cam.pos; 26 | if (parse_vec3(split[2], &look_at)) 27 | return ( 28 | print_error_scene(line_num, ERR_PARSE, ERR_LOOK_AT, VEC3_RANGE)); 29 | if (vec3_equal(scene->cam.pos, look_at)) 30 | return (print_error_scene(line_num, ERR_PARSE, ERR_CAM_LOOK, NULL)); 31 | scene->cam.dir = vec3_norm(vec3_sub(look_at, scene->cam.pos)); 32 | scene->cam.dir_initial = scene->cam.dir; 33 | if (int_from_str(split[3], 1, 179, &(scene->cam.fov))) 34 | return (print_error_scene(line_num, ERR_PARSE, ERR_FOV, NULL)); 35 | scene->cam.set = true; 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /lib/libmlx/src/utils/mlx_error.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* :::::::: */ 4 | /* mlx_error.c :+: :+: */ 5 | /* +:+ */ 6 | /* By: W2Wizard +#+ */ 7 | /* +#+ */ 8 | /* Created: 2021/12/28 02:51:54 by W2Wizard #+# #+# */ 9 | /* Updated: 2022/04/13 00:16:51 by w2wizard ######## odam.nl */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "MLX42/MLX42_Int.h" 14 | 15 | //= Private =// 16 | 17 | // English description of the error codes. 18 | static const char* mlx_errors[] = { 19 | "No Errors", 20 | "File has invalid extension", 21 | "Failed to open the file", 22 | "PNG file is invalid or corrupted", 23 | "XPM42 file is invalid or corrupted", 24 | "Font atlas data is invalid", 25 | "Texture area out of range", 26 | "Failed to compile shader", 27 | "Failed to allocate memory", 28 | "Failed to initialize GLAD", 29 | "Failed to initialize GLFW", 30 | "Failed to create window", 31 | "Image size is too big", 32 | "Image size is too small", 33 | "Texture is larger than image", 34 | }; 35 | 36 | 37 | /** 38 | * Functions to set the error number, simply for convenience. 39 | * 40 | * @param val The error value. 41 | * @return Always false 42 | */ 43 | bool mlx_error(mlx_errno_t val) 44 | { 45 | mlx_errno = val; 46 | return (false); 47 | } 48 | 49 | //= Public =// 50 | 51 | const char* mlx_strerror(mlx_errno_t val) 52 | { 53 | MLX_ASSERT(val < 0); 54 | MLX_ASSERT(val >= MLX_ERRMAX); 55 | 56 | return (mlx_errors[val]); 57 | } 58 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_obj_plane.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_obj_plane.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:37 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:56:38 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | #include "print/mrt_print.h" 15 | 16 | int parse_obj_plane(t_scene *scene, char **split, int line_num) 17 | { 18 | t_list *obj; 19 | t_obj *c_obj; 20 | 21 | if (ft_split_count_str(split) != 7) 22 | return (print_error_scene(line_num, ERR_PARSE, ERR_NUM_PARA, NULL)); 23 | obj = obj_new(); 24 | if (obj == NULL) 25 | return (print_error_scene(line_num, ERR_PARSE, strerror(errno), NULL)); 26 | ft_lstadd_back(&(scene->l_obj), obj); 27 | c_obj = obj_cont(obj); 28 | if (parse_vec3(split[1], &(c_obj->pl.pos))) 29 | return (print_error_scene(line_num, ERR_PARSE, ERR_POS, VEC3_RANGE)); 30 | if (parse_vec3(split[2], &(c_obj->pl.dir))) 31 | return (print_error_scene(line_num, ERR_PARSE, ERR_DIR, VEC3_RANGE)); 32 | c_obj->pl.dir = vec3_norm(c_obj->pl.dir); 33 | if (parse_material(&(c_obj->material), &split[3], line_num)) 34 | return (-1); 35 | if (c_obj->material.surface[DIELECTRIC] != 0.0) 36 | return (print_error_scene(line_num, ERR_PARSE, ERR_DIELECTRIC, NULL)); 37 | c_obj->print = &print_obj_plane; 38 | c_obj->intersect = &intersect_plane; 39 | c_obj->normal = &normal_plane; 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /lib/libft/src/additional/ft_gnl_without_buffer.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_gnl_without_buffer.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/15 13:14:08 by tjensen #+# #+# */ 9 | /* Updated: 2022/04/15 13:15:10 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *gnl_free_line(char *line); 16 | static char *str_append_chr(char *str, char append); 17 | 18 | char *ft_gnl_without_buffer(int fd) 19 | { 20 | char *line; 21 | char buffer; 22 | int check; 23 | 24 | line = ft_strdup(""); 25 | if (line == NULL) 26 | return (NULL); 27 | check = read(fd, &buffer, 1); 28 | if (check == -1 || check == 0) 29 | return (gnl_free_line(line)); 30 | while (check > 0) 31 | { 32 | line = str_append_chr(line, buffer); 33 | if (line == NULL) 34 | return (NULL); 35 | if (buffer == '\n') 36 | return (line); 37 | check = read(fd, &buffer, 1); 38 | } 39 | if (check == -1) 40 | return (gnl_free_line(line)); 41 | return (line); 42 | } 43 | 44 | static char *gnl_free_line(char *line) 45 | { 46 | free(line); 47 | return (NULL); 48 | } 49 | 50 | static char *str_append_chr(char *str, char append) 51 | { 52 | char *new_str; 53 | int i; 54 | 55 | if (str == NULL) 56 | return (NULL); 57 | new_str = malloc(ft_strlen(str) + 2); 58 | if (new_str != NULL) 59 | { 60 | i = 0; 61 | while (str[i]) 62 | { 63 | new_str[i] = str[i]; 64 | i++; 65 | } 66 | new_str[i] = append; 67 | new_str[i + 1] = '\0'; 68 | } 69 | free(str); 70 | return (new_str); 71 | } 72 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_obj_disc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_obj_disc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:39 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/10 09:32:00 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | #include "print/mrt_print.h" 15 | 16 | int parse_obj_disc(t_scene *scene, char **split, int line_num) 17 | { 18 | t_list *obj; 19 | t_disc *di; 20 | 21 | if (ft_split_count_str(split) != 8) 22 | return (print_error_scene(line_num, ERR_PARSE, ERR_NUM_PARA, NULL)); 23 | obj = obj_new(); 24 | if (obj == NULL) 25 | return (print_error_scene(line_num, ERR_PARSE, strerror(errno), NULL)); 26 | ft_lstadd_back(&(scene->l_obj), obj); 27 | di = &(obj_cont(obj)->di); 28 | if (parse_vec3(split[1], &(di->pos))) 29 | return (print_error_scene(line_num, ERR_PARSE, ERR_POS, VEC3_RANGE)); 30 | if (parse_vec3(split[2], &(di->dir))) 31 | return (print_error_scene(line_num, ERR_PARSE, ERR_DIR, VEC3_RANGE)); 32 | di->dir = vec3_norm(di->dir); 33 | if (double_from_str(split[3], 6, 4, &(di->radius)) || di->radius <= 0) 34 | return (print_error_scene(line_num, ERR_PARSE, ERR_RAD, NULL)); 35 | if (parse_material(obj_material(obj), &split[4], line_num)) 36 | return (-1); 37 | if (obj_material(obj)->surface[DIELECTRIC] != 0.0) 38 | return (print_error_scene(line_num, ERR_PARSE, ERR_DIELECTRIC, NULL)); 39 | obj_cont(obj)->print = &print_obj_disc; 40 | obj_cont(obj)->intersect = &intersect_disc; 41 | obj_cont(obj)->normal = &normal_disc; 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /src/mrt_vec3.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_vec3.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/19 10:27:56 by tjensen #+# #+# */ 9 | /* Updated: 2022/12/19 12:42:53 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MRT_VEC3_H 14 | # define MRT_VEC3_H 15 | 16 | # include "libft/inc/libft.h" 17 | 18 | # include 19 | 20 | /* ************************************************************************** */ 21 | /* STRUCTS */ 22 | /* ************************************************************************** */ 23 | 24 | typedef struct s_vec3 25 | { 26 | double x; 27 | double y; 28 | double z; 29 | } t_vec3; 30 | 31 | /* ************************************************************************** */ 32 | /* FUNCTION PROTOTYPES */ 33 | /* ************************************************************************** */ 34 | 35 | // VEC3_PARSE 36 | int parse_vec3(const char *str, t_vec3 *vec3); 37 | 38 | // VEC3_PRINT 39 | void print_vec3(t_vec3 vec3, const char *str, const char *color); 40 | 41 | // VEC3_MATH_1 42 | t_vec3 vec3_add(t_vec3 a, t_vec3 b); 43 | t_vec3 vec3_sub(t_vec3 a, t_vec3 b); 44 | t_vec3 vec3_scale(double alpha, t_vec3 a); 45 | t_vec3 vec3_lin_comb(double alpha, t_vec3 a, double beta, t_vec3 b); 46 | t_vec3 vec3_norm(t_vec3 a); 47 | 48 | // VEC3_MATH_2 49 | bool vec3_equal(t_vec3 a, t_vec3 b); 50 | double vec3_len(t_vec3 a); 51 | double vec3_dist(t_vec3 a, t_vec3 b); 52 | double vec3_dot(t_vec3 a, t_vec3 b); 53 | t_vec3 vec3_cross(t_vec3 a, t_vec3 b); 54 | 55 | #endif // MRT_VEC3_H 56 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_sampling.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_sampling.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:27 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/10 09:50:22 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | 15 | int parse_sampling(t_scene *scene, char **split, int line_num) 16 | { 17 | if (scene->sampling.set == true) 18 | return (print_error_scene(line_num, ERR_PARSE, ERR_DUP_ID, 19 | ID_SAMPLING)); 20 | if (ft_split_count_str(split) != 6) 21 | return (print_error_scene(line_num, ERR_PARSE, ERR_NUM_PARA, NULL)); 22 | if (int_from_str(split[1], 1, 9999, &(scene->sampling.max_samp))) 23 | return (print_error_scene(line_num, ERR_PARSE, ERR_MAX_SAMP, NULL)); 24 | if (int_from_str(split[2], 1, 1000, &(scene->sampling.recursion_depth))) 25 | return (print_error_scene(line_num, ERR_PARSE, ERR_REC_DEPTH, NULL)); 26 | if (double_from_str(split[3], 1, 4, &(scene->sampling.cosine))) 27 | return (print_error_scene(line_num, ERR_PARSE, ERR_COSINE_SAMP, NULL)); 28 | if (double_from_str(split[4], 1, 4, &(scene->sampling.light))) 29 | return (print_error_scene(line_num, ERR_PARSE, ERR_IMPORT_SAMP, NULL)); 30 | if (scene->sampling.cosine + scene->sampling.light != 1.0) 31 | return (print_error_scene(line_num, ERR_PARSE, ERR_SAMP_SUM, NULL)); 32 | if (ft_strcmp(split[5], "true") != 0 && ft_strcmp(split[5], "false") != 0) 33 | return (print_error_scene(line_num, ERR_PARSE, ERR_GAMMA, NULL)); 34 | scene->sampling.gamma_correction = (ft_strcmp(split[5], "true") == 0); 35 | scene->sampling.set = true; 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /src/print/mrt_print_light.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_light.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:30 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:31 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | void print_light_sphere(t_list *light) 16 | { 17 | t_light *c_light; 18 | 19 | c_light = light_cont(light); 20 | printf("LIGHT SPHERE: (%p) -> (%p)\n", c_light, c_light->linked_c_obj); 21 | print_vec3(c_light->linked_c_obj->sp.pos, "pos:", COLOR_BL); 22 | print_scene_double( 23 | c_light->linked_c_obj->material.brightness, "bright:", COLOR_NO); 24 | print_scene_double(c_light->weight, "weight:", COLOR_NO); 25 | printf("\n"); 26 | } 27 | 28 | void print_light_rectangle(t_list *light) 29 | { 30 | t_light *c_light; 31 | 32 | c_light = light_cont(light); 33 | printf("LIGHT RECTANGLE: (%p) -> (%p)\n", c_light, c_light->linked_c_obj); 34 | print_vec3(c_light->linked_c_obj->di.pos, "pos:", COLOR_BL); 35 | print_scene_double( 36 | c_light->linked_c_obj->material.brightness, "bright:", COLOR_NO); 37 | print_scene_double(c_light->weight, "weight:", COLOR_NO); 38 | printf("\n"); 39 | } 40 | 41 | void print_light_disc(t_list *light) 42 | { 43 | t_light *c_light; 44 | 45 | c_light = light_cont(light); 46 | printf("LIGHT DISC: (%p) -> (%p)\n", c_light, c_light->linked_c_obj); 47 | print_vec3(c_light->linked_c_obj->di.pos, "pos:", COLOR_BL); 48 | print_scene_double( 49 | c_light->linked_c_obj->material.brightness, "bright:", COLOR_NO); 50 | print_scene_double(c_light->weight, "weight:", COLOR_NO); 51 | printf("\n"); 52 | } 53 | -------------------------------------------------------------------------------- /src/mrt_light.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_light.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:54:06 by tjensen #+# #+# */ 9 | /* Updated: 2022/12/19 12:43:17 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MRT_LIGHT_H 14 | #define MRT_LIGHT_H 15 | 16 | /* ************************************************************************** */ 17 | /* INCLUDES 18 | */ 19 | /* ************************************************************************** */ 20 | 21 | #include "libft/inc/libft.h" 22 | #include "mrt_color.h" 23 | #include "mrt_obj.h" 24 | #include "mrt_vec3.h" 25 | #include "trace/mrt_trace.h" 26 | 27 | /* ************************************************************************** */ 28 | /* STRUCTS 29 | */ 30 | /* ************************************************************************** */ 31 | 32 | typedef struct s_obj t_obj; 33 | typedef struct s_scene t_scene; 34 | typedef struct s_hit t_hit; 35 | 36 | typedef void (*t_f_print)(t_list *is); 37 | typedef double (*t_f_pdf_val)(t_list *is, t_hit *hit); 38 | typedef t_vec3 (*t_f_random_dir)(t_list *is, t_hit *hit); 39 | 40 | typedef struct s_light { 41 | double weight; 42 | t_f_print print; 43 | t_f_pdf_val pdf_value; 44 | t_f_random_dir random_dir; 45 | t_obj *linked_c_obj; 46 | } t_light; 47 | 48 | /* ************************************************************************** */ 49 | /* FUNCTION PROTOTYPES 50 | */ 51 | /* ************************************************************************** */ 52 | 53 | // OBJECTS 54 | t_list *light_new(void); 55 | t_light *light_cont(t_list *is); 56 | 57 | #endif // MRT_LIGHT_H 58 | -------------------------------------------------------------------------------- /lib/libft/src/mandatory/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hepple +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/06/22 08:43:54 by hepple #+# #+# */ 9 | /* Updated: 2021/10/21 10:34:01 by hepple ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | * Helper function for ft_itoa that counts the number of digits 17 | * of an integer 'i' in base 10. 18 | */ 19 | static size_t num_digits(int i) 20 | { 21 | if (i == INT_MIN) 22 | return (11); 23 | if (i < 0) 24 | return (num_digits(i * (-1)) + 1); 25 | if (i < 10) 26 | return (1); 27 | return (num_digits(i / 10) + 1); 28 | } 29 | 30 | /* 31 | * Helper function for ft_itoa that writes the digits of the integer 'n' 32 | * to the string 'str' one by one. 33 | */ 34 | static char *itoa_main_part(int n, char *str, size_t size) 35 | { 36 | size_t i; 37 | int n_is_negative; 38 | 39 | str[size] = '\0'; 40 | if (n < 0) 41 | { 42 | n_is_negative = 1; 43 | n = n * (-1); 44 | } 45 | else 46 | n_is_negative = 0; 47 | i = size - 1; 48 | while (i > 0) 49 | { 50 | str[i] = n % 10 + '0'; 51 | n = n / 10; 52 | i--; 53 | } 54 | if (n_is_negative) 55 | str[0] = '-'; 56 | else 57 | str[0] = n + '0'; 58 | return (str); 59 | } 60 | 61 | /* 62 | * Function that converts an integer 'n' to a null-terminated string. 63 | */ 64 | char *ft_itoa(int n) 65 | { 66 | char *str; 67 | size_t size; 68 | 69 | size = num_digits(n); 70 | str = malloc(sizeof(char) * (size + 1)); 71 | if (str == NULL) 72 | return (NULL); 73 | if (n == INT_MIN) 74 | ft_memcpy(str, "-2147483648\0", size + 1); 75 | else 76 | str = itoa_main_part(n, str, size); 77 | return (str); 78 | } 79 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_light_pdf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_light_pdf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:54:56 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:54:56 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace.h" 14 | 15 | double pdf_sphere(t_list *light, t_hit *hit) 16 | { 17 | t_sphere *sp; 18 | t_vec3 light_dir; 19 | double cos_theta_max; 20 | double solid_angle; 21 | 22 | sp = &(light_cont(light)->linked_c_obj->sp); 23 | light_dir = vec3_sub(sp->pos, hit->p); 24 | cos_theta_max = sqrt(1.0 - sp->radius * sp->radius 25 | / vec3_dot(light_dir, light_dir)); 26 | solid_angle = 2.0 * M_PI * (1.0 - cos_theta_max); 27 | return (1.0 / solid_angle); 28 | } 29 | 30 | double pdf_disc(t_list *light, t_hit *hit) 31 | { 32 | t_disc *di; 33 | t_vec3 light_dir; 34 | double cosine; 35 | double length_squared; 36 | 37 | di = &(light_cont(light)->linked_c_obj->di); 38 | light_dir = vec3_sub(di->pos, hit->p); 39 | length_squared = vec3_dot(light_dir, light_dir); 40 | light_dir = vec3_norm(light_dir); 41 | cosine = fabs(vec3_dot(light_dir, di->dir)); 42 | return (length_squared / (cosine * M_PI * di->radius * di->radius)); 43 | } 44 | 45 | double pdf_rectangle(t_list *light, t_hit *hit) 46 | { 47 | t_rectangle *rt; 48 | t_vec3 light_dir; 49 | double cosine; 50 | double length_squared; 51 | 52 | rt = &(light_cont(light)->linked_c_obj->rt); 53 | light_dir = vec3_sub(rt->pos, hit->p); 54 | length_squared = vec3_dot(light_dir, light_dir); 55 | light_dir = vec3_norm(light_dir); 56 | cosine = fabs(vec3_dot(light_dir, rt->dir)); 57 | return (length_squared / (cosine * rt->width * rt->height)); 58 | } 59 | -------------------------------------------------------------------------------- /src/print/mrt_print_error.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print_error.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:44 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:45 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_print.h" 14 | 15 | int print_error(char *s1, char *s2, char *s3, char *s4) 16 | { 17 | fprintf(stderr, "%sError%s\n", COLOR_RE_1, COLOR_NO); 18 | fflush(stderr); 19 | if (s1) 20 | ft_putstr_fd(s1, STDERR_FILENO); 21 | if (s2) 22 | { 23 | if (s1) 24 | ft_putstr_fd(": ", STDERR_FILENO); 25 | ft_putstr_fd(s2, STDERR_FILENO); 26 | } 27 | if (s3) 28 | { 29 | if (s1 || s2) 30 | ft_putstr_fd(": ", STDERR_FILENO); 31 | ft_putstr_fd(s3, STDERR_FILENO); 32 | } 33 | if (s4) 34 | { 35 | if (s1 || s2 || s3) 36 | ft_putstr_fd(": ", STDERR_FILENO); 37 | ft_putstr_fd(s4, STDERR_FILENO); 38 | } 39 | ft_putchar_fd('\n', STDERR_FILENO); 40 | return (-1); 41 | } 42 | 43 | int print_error_scene(int line_num, const char *msg1, 44 | const char *msg2, const char *msg3) 45 | { 46 | fprintf(stderr, "%sError%s\n", COLOR_RE_1, COLOR_NO); 47 | fflush(stderr); 48 | if (line_num > 0) 49 | { 50 | write(STDERR_FILENO, "Line ", 5); 51 | ft_putnbr_fd(line_num, STDERR_FILENO); 52 | write(STDERR_FILENO, ": ", 2); 53 | } 54 | if (msg1) 55 | write(STDERR_FILENO, msg1, ft_strlen(msg1)); 56 | if (msg2) 57 | { 58 | if (msg1) 59 | write(STDERR_FILENO, ": ", 2); 60 | write(STDERR_FILENO, msg2, ft_strlen(msg2)); 61 | } 62 | if (msg3) 63 | { 64 | if (msg1 || msg2) 65 | write(STDERR_FILENO, ": ", 2); 66 | write(STDERR_FILENO, msg3, ft_strlen(msg3)); 67 | } 68 | write(STDERR_FILENO, "\n", 1); 69 | return (-1); 70 | } 71 | -------------------------------------------------------------------------------- /src/mrt_scene_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_scene_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:54:37 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:54:39 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_scene.h" 14 | #include "parse/mrt_parse.h" 15 | #include "print/mrt_print.h" 16 | 17 | int scene_norm_light_weights(t_list *l_light) 18 | { 19 | t_list *iter; 20 | double weight_sum; 21 | 22 | if (l_light == NULL) 23 | return (0); 24 | weight_sum = 0; 25 | iter = l_light; 26 | while (iter) 27 | { 28 | weight_sum += light_cont(iter)->weight; 29 | iter = iter->next; 30 | } 31 | if (weight_sum <= 0) 32 | return (-1); 33 | iter = l_light; 34 | while (iter) 35 | { 36 | light_cont(iter)->weight /= weight_sum; 37 | iter = iter->next; 38 | } 39 | return (0); 40 | } 41 | 42 | void scene_calc_img_pos(t_scene *scene) 43 | { 44 | if (fabs(scene->cam.dir.y) > 1.0 - 1e-4) 45 | scene->img.qx = vec3_scale( 46 | tan((double)scene->cam.fov / 2.0 * (M_PI / 180.0)), 47 | vec3_norm( 48 | vec3_cross((t_vec3){0, 0, -1.0}, 49 | scene->cam.dir))); 50 | else 51 | scene->img.qx = vec3_scale( 52 | tan((double)scene->cam.fov / 2.0 * (M_PI / 180.0)), 53 | vec3_norm( 54 | vec3_cross((t_vec3){0, 1.0, 0}, 55 | scene->cam.dir))); 56 | scene->img.qy = vec3_scale(vec3_len(scene->img.qx) 57 | * scene->img.height / scene->img.width, 58 | vec3_norm(vec3_cross(scene->img.qx, 59 | scene->cam.dir))); 60 | scene->img.px = vec3_scale(-2.0 / (scene->img.width - 1), scene->img.qx); 61 | scene->img.py = vec3_scale(-2.0 / (scene->img.height - 1), scene->img.qy); 62 | scene->img.pos = vec3_add(vec3_add(scene->cam.pos, scene->cam.dir), 63 | vec3_add(scene->img.qx, scene->img.qy)); 64 | } 65 | -------------------------------------------------------------------------------- /lib/libft/src/additional/ft_double_from_str.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_double_from_str.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/19 15:50:04 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/08 15:11:12 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static double get_double(const char *str); 16 | 17 | int ft_double_from_str(const char *str, int before_dec, 18 | int after_dec, double *res) 19 | { 20 | int i; 21 | 22 | if (ft_strchr(str, '.') != ft_strrchr(str, '.')) 23 | return (-1); 24 | if (ft_strchr(str, '.') && (int)(ft_strchr(str, '.') - str) > before_dec) 25 | return (-1); 26 | if (ft_strchr(str, '.') 27 | && (int)(ft_strlen(ft_strchr(str, '.') + 1)) > after_dec) 28 | return (-1); 29 | if (ft_strchr(str, '.') && !ft_isdigit(*(ft_strchr(str, '.') + 1))) 30 | return (-1); 31 | if (!ft_strchr(str, '.') && (int)(ft_strlen(str)) > before_dec) 32 | return (-1); 33 | i = 0; 34 | if ((str[0] == '+' || str[0] == '-') && str[1] != '\0') 35 | i++; 36 | while (str[i]) 37 | { 38 | if (!ft_isdigit(str[i]) && str[i] != '.') 39 | return (-1); 40 | i++; 41 | } 42 | *res = get_double(str); 43 | return (0); 44 | } 45 | 46 | static double get_double(const char *str) 47 | { 48 | double value; 49 | double factor; 50 | int i; 51 | 52 | i = 0; 53 | value = 0.0; 54 | factor = 1; 55 | if (str[i] == '-') 56 | factor = -1; 57 | if (str[i] == '-' || str[i] == '+') 58 | i++; 59 | while (str[i] >= '0' && str[i] <= '9') 60 | { 61 | value = value * 10 + (str[i] - '0'); 62 | i++; 63 | } 64 | if (str[i] == '.') 65 | i++; 66 | while (str[i] >= '0' && str[i] <= '9') 67 | { 68 | value = value * 10.0 + (str[i] - '0'); 69 | factor = factor / 10.0; 70 | i++; 71 | } 72 | return (factor * value); 73 | } 74 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_dielectric.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_dielectric.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:54:51 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:54:52 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace.h" 14 | 15 | static double reflectance(double cos_theta, double ref_ratio); 16 | static t_vec3 refract(t_list *obj, t_ray *ray, t_hit *hit, double ref_ratio); 17 | 18 | t_vec3 dielectric(t_list *obj, t_ray *ray, t_hit *hit) 19 | { 20 | double ref_ratio; 21 | 22 | if (hit->side == OUTSIDE) 23 | ref_ratio = 1.0 / obj_material(obj)->refraction_index; 24 | else 25 | ref_ratio = obj_material(obj)->refraction_index; 26 | return (refract(obj, ray, hit, ref_ratio)); 27 | } 28 | 29 | static double reflectance(double cos_theta, double ref_ratio) 30 | { 31 | double r; 32 | 33 | r = (1.0 - ref_ratio) / (1.0 + ref_ratio); 34 | r = r * r; 35 | return (r + (1.0 - r) * pow((1.0 - cos_theta), 5.0)); 36 | } 37 | 38 | static t_vec3 refract(t_list *obj, t_ray *ray, t_hit *hit, double ref_ratio) 39 | { 40 | t_vec3 normal; 41 | t_vec3 r_out_perp; 42 | t_vec3 r_out_para; 43 | double cos_theta; 44 | bool cannot_refract; 45 | 46 | if (hit->side == OUTSIDE) 47 | normal = hit->normal; 48 | else 49 | normal = vec3_scale(-1.0, hit->normal); 50 | cos_theta = fmin(vec3_dot(vec3_scale(-1.0, ray->dir), normal), 1.0); 51 | cannot_refract = (ref_ratio * sqrt(1.0 - cos_theta * cos_theta) > 1.0); 52 | if (cannot_refract 53 | || reflectance(cos_theta, ref_ratio) > ft_rand()) 54 | return (specular(obj, ray, hit)); 55 | else 56 | { 57 | r_out_perp = vec3_scale(ref_ratio, 58 | vec3_add(ray->dir, vec3_scale(cos_theta, normal))); 59 | r_out_para = vec3_scale( 60 | -sqrt(fabs(1.0 - vec3_dot(r_out_perp, r_out_perp))), normal); 61 | return (vec3_norm(vec3_add(r_out_perp, r_out_para))); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/print/mrt_print.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_print.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:57:14 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:57:14 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MRT_PRINT_H 14 | # define MRT_PRINT_H 15 | 16 | /* ************************************************************************** */ 17 | /* INCLUDES */ 18 | /* ************************************************************************** */ 19 | 20 | # include "mrt_color.h" 21 | # include "mrt_vec3.h" 22 | # include "mrt_scene.h" 23 | # include "mrt_obj.h" 24 | # include "mrt_light.h" 25 | 26 | # include 27 | # include 28 | # include 29 | # include 30 | 31 | /* ************************************************************************** */ 32 | /* FUNCTION PROTOTYPES */ 33 | /* ************************************************************************** */ 34 | 35 | void print_color(t_color color); 36 | 37 | void print_vec3(t_vec3 vec3, const char *str, const char *color); 38 | 39 | void print_material(t_material *material); 40 | 41 | int print_scene_debug(t_scene *scene); 42 | void print_scene_int(int num, const char *str, const char *color); 43 | void print_scene_double(double num, const char *str, const char *color); 44 | 45 | void print_textures(t_list *l_texture); 46 | 47 | void print_obj_plane(t_list *obj); 48 | void print_obj_disc(t_list *obj); 49 | void print_obj_sphere(t_list *obj); 50 | void print_obj_tube(t_list *obj); 51 | void print_obj_rectangle(t_list *obj); 52 | 53 | void print_light_sphere(t_list *is); 54 | void print_light_rectangle(t_list *light); 55 | void print_light_disc(t_list *light); 56 | 57 | int print_error(char *s1, char *s2, char *s3, char *s4); 58 | int print_error_scene(int line_num, const char *msg1, 59 | const char *msg2, const char *msg3); 60 | 61 | #endif // MRT_PRINT_H 62 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_obj_tube.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_obj_tube.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:30 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/10 09:34:02 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | #include "print/mrt_print.h" 15 | 16 | static void parse_obj_tube_function_ptr(t_list *obj); 17 | 18 | int parse_obj_tube(t_scene *scene, char **split, int line_num) 19 | { 20 | t_list *obj; 21 | 22 | if (ft_split_count_str(split) != 9) 23 | return (print_error_scene(line_num, ERR_PARSE, ERR_NUM_PARA, NULL)); 24 | obj = obj_new(); 25 | if (obj == NULL) 26 | return (print_error_scene(line_num, ERR_PARSE, strerror(errno), NULL)); 27 | ft_lstadd_back(&(scene->l_obj), obj); 28 | if (parse_vec3(split[1], &(obj_cont(obj)->tb.pos))) 29 | return (print_error_scene(line_num, ERR_PARSE, ERR_POS, VEC3_RANGE)); 30 | if (parse_vec3(split[2], &(obj_cont(obj)->tb.dir))) 31 | return (print_error_scene(line_num, ERR_PARSE, ERR_DIR, VEC3_RANGE)); 32 | obj_cont(obj)->tb.dir = vec3_norm(obj_cont(obj)->tb.dir); 33 | if (double_from_str(split[3], 6, 4, &(obj_cont(obj)->tb.radius)) 34 | || obj_cont(obj)->tb.radius <= 0) 35 | return (print_error_scene(line_num, ERR_PARSE, ERR_RAD, NULL)); 36 | if (double_from_str(split[4], 6, 4, &(obj_cont(obj)->tb.height)) 37 | || obj_cont(obj)->tb.height <= 0) 38 | return (print_error_scene(line_num, ERR_PARSE, ERR_HEIGHT, NULL)); 39 | if (parse_material(&(obj_cont(obj)->material), &split[5], line_num)) 40 | return (-1); 41 | if (obj_material(obj)->surface[DIELECTRIC] != 0.0) 42 | return (print_error_scene(line_num, ERR_PARSE, ERR_DIELECTRIC, NULL)); 43 | parse_obj_tube_function_ptr(obj); 44 | return (0); 45 | } 46 | 47 | static void parse_obj_tube_function_ptr(t_list *obj) 48 | { 49 | obj_cont(obj)->print = &print_obj_tube; 50 | obj_cont(obj)->intersect = &intersect_tube; 51 | obj_cont(obj)->normal = &normal_tube; 52 | } 53 | -------------------------------------------------------------------------------- /src/mrt_color.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_color.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/19 10:32:01 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:17:35 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MRT_COLOR_H 14 | # define MRT_COLOR_H 15 | 16 | /* ************************************************************************** */ 17 | /* INCLUDES */ 18 | /* ************************************************************************** */ 19 | 20 | # include 21 | 22 | /* ************************************************************************** */ 23 | /* DEFINES */ 24 | /* ************************************************************************** */ 25 | 26 | # define COLOR_NO "\033[m" 27 | # define COLOR_RE "\033[0;31m" 28 | # define COLOR_RE_1 "\033[1;31m" 29 | # define COLOR_YE "\033[0;33m" 30 | # define COLOR_YE_1 "\033[1;33m" 31 | # define COLOR_GR "\033[0;32m" 32 | # define COLOR_GR_1 "\033[1;32m" 33 | # define COLOR_BL "\033[0;34m" 34 | # define COLOR_BL_1 "\033[1;34m" 35 | # define COLOR_PL "\033[0;35m" 36 | # define COLOR_PL_1 "\033[1;35m" 37 | # define COLOR_CY "\033[0;36m" 38 | # define COLOR_CY_1 "\033[1;36m" 39 | 40 | /* ************************************************************************** */ 41 | /* STRUCTS */ 42 | /* ************************************************************************** */ 43 | 44 | typedef struct s_color 45 | { 46 | double r; 47 | double g; 48 | double b; 49 | } t_color; 50 | 51 | /* ************************************************************************** */ 52 | /* FUNCTION PROTOTYPES */ 53 | /* ************************************************************************** */ 54 | 55 | // COLOR_UTILS 56 | double color_clamp(double color, double min, double max); 57 | t_color color_blend(double f, t_color c1, t_color c2); 58 | t_color color_gamma_encode(t_color color); 59 | int color_to_rgba_int(t_color color, bool gamma_correction); 60 | t_color color_max(t_color c1, t_color c2); 61 | 62 | #endif // MRT_COLOR_H 63 | -------------------------------------------------------------------------------- /src/mrt_color.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_color.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/19 11:12:06 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/06 17:33:00 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_color.h" 14 | #include "math/mrt_math.h" 15 | 16 | #include 17 | 18 | inline t_color color_max(t_color c1, t_color c2) 19 | { 20 | t_color color; 21 | 22 | if (c1.r > c2.r) 23 | color.r = c1.r; 24 | else 25 | color.r = c2.r; 26 | if (c1.g > c2.g) 27 | color.g = c1.g; 28 | else 29 | color.g = c2.g; 30 | if (c1.b > c2.b) 31 | color.b = c1.b; 32 | else 33 | color.b = c2.b; 34 | return (color); 35 | } 36 | 37 | inline double color_clamp(double color, double min, double max) 38 | { 39 | if (color < min) 40 | return (min); 41 | if (color > max) 42 | return (max); 43 | return (color); 44 | } 45 | 46 | inline t_color color_blend(double f, t_color c1, t_color c2) 47 | { 48 | return ((t_color){ 49 | (1.0 - f) * c1.r + f * c2.r, 50 | (1.0 - f) * c1.g + f * c2.g, 51 | (1.0 - f) * c1.b + f * c2.b 52 | }); 53 | } 54 | 55 | inline t_color color_gamma_encode(t_color color) 56 | { 57 | return ((t_color){sqrt(color.r), sqrt(color.g), sqrt(color.b)}); 58 | } 59 | 60 | int color_to_rgba_int(t_color color, bool gamma_correction) 61 | { 62 | if (color.r != color.r) 63 | color.r = 0; 64 | if (color.g != color.g) 65 | color.g = 0; 66 | if (color.b != color.b) 67 | color.b = 0; 68 | if (color.b > 1.0 && color.b >= color.r && color.b >= color.g) 69 | color = color_scale(1.0 / color.b, color); 70 | if (color.g > 1.0 && color.g >= color.r && color.g >= color.b) 71 | color = color_scale(1.0 / color.g, color); 72 | if (color.r > 1.0 && color.r >= color.g && color.r >= color.b) 73 | color = color_scale(1.0 / color.r, color); 74 | if (gamma_correction) 75 | color = color_gamma_encode(color); 76 | return ( 77 | (int)(256.0 * color_clamp(color.r, 0.0, 0.999)) << 24 | 78 | (int)(256.0 * color_clamp(color.g, 0.0, 0.999)) << 16 | 79 | (int)(256.0 * color_clamp(color.b, 0.0, 0.999)) << 8 | 80 | (int)(0xFF) 81 | ); 82 | } 83 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_obj_normal_2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_obj_normal_2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:55:24 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:55:24 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace.h" 14 | 15 | static void normal_rectangle_rotate_x(t_vec3 *normal, double rot_x); 16 | static void normal_rectangle_rotate_y(t_vec3 *normal, double rot_y); 17 | static void normal_rectangle_rotate_z(t_vec3 *normal, double rot_z); 18 | 19 | t_vec3 normal_rectangle(t_list *obj, t_hit *hit __attribute__((unused))) 20 | { 21 | t_vec3 tmp_normal; 22 | 23 | tmp_normal = obj_cont(obj)->rt.dir; 24 | if (obj_cont(obj)->rt.rot.x != 0.0) 25 | normal_rectangle_rotate_x(&tmp_normal, obj_cont(obj)->rt.rot.x); 26 | if (obj_cont(obj)->rt.rot.y != 0.0) 27 | normal_rectangle_rotate_y(&tmp_normal, obj_cont(obj)->rt.rot.y); 28 | if (obj_cont(obj)->rt.rot.z != 0.0) 29 | normal_rectangle_rotate_z(&tmp_normal, obj_cont(obj)->rt.rot.z); 30 | return (tmp_normal); 31 | } 32 | 33 | static void normal_rectangle_rotate_x(t_vec3 *normal, double rot_x) 34 | { 35 | double tmp; 36 | double sine; 37 | double cosine; 38 | 39 | sine = sin(rot_x * (M_PI / 180.0)); 40 | cosine = cos(rot_x * (M_PI / 180.0)); 41 | tmp = cosine * normal->y + sine * normal->z; 42 | normal->z = -sine * normal->y + cosine * normal->z; 43 | normal->y = tmp; 44 | } 45 | 46 | static void normal_rectangle_rotate_y(t_vec3 *normal, double rot_y) 47 | { 48 | double tmp; 49 | double sine; 50 | double cosine; 51 | 52 | sine = sin(rot_y * (M_PI / 180.0)); 53 | cosine = cos(rot_y * (M_PI / 180.0)); 54 | tmp = cosine * normal->x - sine * normal->z; 55 | normal->z = sine * normal->x + cosine * normal->z; 56 | normal->x = tmp; 57 | } 58 | 59 | static void normal_rectangle_rotate_z(t_vec3 *normal, double rot_z) 60 | { 61 | double tmp; 62 | double sine; 63 | double cosine; 64 | 65 | sine = sin(rot_z * (M_PI / 180.0)); 66 | cosine = cos(rot_z * (M_PI / 180.0)); 67 | tmp = cosine * normal->x + sine * normal->y; 68 | normal->y = -sine * normal->x + cosine * normal->y; 69 | normal->x = tmp; 70 | } 71 | -------------------------------------------------------------------------------- /lib/libmlx/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: tjensen +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2022/02/26 21:32:49 by W2Wizard #+# #+# # 9 | # Updated: 2022/06/09 22:33:40 by tjensen ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME := libmlx42.a 14 | LIB_DIR := lib 15 | SRC_DIR := src 16 | SHADER_DIR := shaders 17 | INCLUDE_DIR := include 18 | 19 | override HEADERS += -I $(INCLUDE_DIR) 20 | 21 | CFLAGS := -Wextra -Wall -Werror -Wunreachable-code -Wno-char-subscripts 22 | ifdef DEBUG 23 | CFLAGS += -g 24 | else 25 | CFLAGS += -Ofast -D NDEBUG 26 | endif 27 | 28 | # Recursive wildcard/find function, the subst is to guarantee unix file paths 29 | rwildcard = $(subst \,/,$(sort $(foreach d,$(wildcard $1/*),$(call rwildcard,$d,$2) $(wildcard $1/$2)))) 30 | 31 | SHDR := $(call rwildcard,$(SHADER_DIR),default.*) 32 | SHDR := $(SHDR:$(SHADER_DIR)/default.%=$(SRC_DIR)/mlx_%_shader.c) 33 | LIB := $(call rwildcard,$(LIB_DIR),*.c) 34 | SRCS := $(call rwildcard,$(SRC_DIR),*.c) 35 | OBJS := $(sort $(patsubst %.c,%.o,$(SRCS) $(LIB) $(SHDR))) 36 | 37 | ifeq ($(OS), Windows_NT) 38 | ifdef WIN_UNIX 39 | include Makefile_Unix.mk 40 | else 41 | include Makefile_WindowsNT.mk 42 | endif 43 | else 44 | UNAME_S := $(shell uname -s) 45 | ifeq ($(UNAME_S), Linux) 46 | include Makefile_Unix.mk 47 | else ifeq ($(UNAME_S), Darwin) 48 | # Default 49 | DYLIB_EXISTS := test -e /usr/local/lib/libglfw.3.dylib || echo "false" 50 | 51 | # Generic Homebrew path 52 | ifneq ($(DYLIB_EXISTS), false) 53 | BREW_GLFW_PREFIX := $(shell brew --prefix glfw) 54 | DYLIB_EXISTS := test -e $(BREW_GLFW_PREFIX)/lib/libglfw.3.dylib || echo "false" 55 | ifneq ($(DYLIB_EXISTS), false) 56 | override HEADERS += -I $(BREW_GLFW_PREFIX)/include 57 | endif 58 | endif 59 | include Makefile_Unix.mk 60 | else 61 | $(error OS: $(OS) - Is not supported!) 62 | endif 63 | endif 64 | 65 | #//= Make Rules =//# 66 | all: $(SHDR) $(NAME) 67 | 68 | # Run make as part of the recipe to allow for multi-threading to be used (-j) 69 | re: fclean 70 | @$(MAKE) -e 71 | 72 | #//= Misc =//# 73 | .DEFAULT_GOAL := all 74 | .PHONY: all clean fclean re 75 | -------------------------------------------------------------------------------- /lib/libmlx/tools/compile_shader.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # **************************************************************************** # 3 | # # 4 | # :::::::: # 5 | # compile_shader.sh :+: :+: # 6 | # +:+ # 7 | # By: fbes +#+ # 8 | # +#+ # 9 | # Created: 2022/03/03 02:38:19 by fbes #+# #+# # 10 | # Updated: 2022/03/15 20:34:51 by lde-la-h ######## odam.nl # 11 | # # 12 | # **************************************************************************** # 13 | 14 | # If no arguments have been given, exit with error code 1 15 | if [ "$#" -ne 1 ]; then 16 | echo "ERROR: missing arguments, use as follows: $0 " 1>&2 17 | exit 1 18 | fi 19 | 20 | # If file cannot be found, exit with error code 2 21 | if [ ! -f "$1" ]; then 22 | echo "ERROR: shader file not found: $1" 1>&2 23 | exit 2 24 | fi 25 | 26 | SHADERTYPE="${1##*.}" 27 | 28 | echo "/* ************************************************************************** */" 29 | echo "/* */" 30 | echo "/* :::::::: */" 31 | echo "/* lol.c :+: :+: */" 32 | echo "/* +:+ */" 33 | echo "/* By: W2wizard +#+ */" 34 | echo "/* +#+ */" 35 | echo "/* Created: 2022/02/17 22:34:59 by W2wizard #+# #+# */" 36 | echo "/* Updated: 2022/02/17 22:34:59 by W2wizard ######## odam.nl */" 37 | echo "/* */" 38 | echo "/* ************************************************************************** */" 39 | echo "" 40 | echo "// If you wish to modify this file edit the .vert or .frag file!" 41 | echo "" 42 | echo "#include \"MLX42/MLX42_Int.h\"" 43 | echo "" 44 | echo "const char* ${SHADERTYPE}_shader = \"$(sed -n '1{p;q;}' $1)\\n\"" 45 | { 46 | # Skip over first line 47 | read 48 | while IFS= read -r LINE; do 49 | if [ ! "${LINE}" = "" ]; then 50 | if [ "${LINE}" = "}" ]; then 51 | echo " \"${LINE}\";" 52 | else 53 | echo " \"${LINE}\"" 54 | fi 55 | fi 56 | done 57 | } < "$1" 58 | exit 0 59 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:21 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:56:21 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | 15 | static double get_double(const char *str); 16 | 17 | int int_from_str(const char *str, int min, int max, int *res) 18 | { 19 | int i; 20 | 21 | i = 0; 22 | if ((str[0] == '+' || str[0] == '-') && str[1] != '\0') 23 | i++; 24 | while (str[i]) 25 | { 26 | if (!ft_isdigit(str[i])) 27 | return (-1); 28 | i++; 29 | } 30 | *res = ft_atoi(str); 31 | if (*res < min || *res > max) 32 | return (-1); 33 | return (0); 34 | } 35 | 36 | int double_from_str(const char *str, int b_p, 37 | int a_p, double *res) 38 | { 39 | int i; 40 | 41 | i = 0; 42 | if ((str[0] == '+' || str[0] == '-') && str[1] != '\0') 43 | i++; 44 | while (str[i]) 45 | { 46 | if (!ft_isdigit(str[i]) && str[i] != '.') 47 | return (-1); 48 | i++; 49 | } 50 | *res = get_double(str); 51 | if (str[0] == '+' || str[0] == '-') 52 | str = str + 1; 53 | if (ft_strchr(str, '.') != ft_strrchr(str, '.')) 54 | return (-1); 55 | if (ft_strchr(str, '.') && (int)(ft_strchr(str, '.') - str) > b_p) 56 | return (-1); 57 | if (ft_strchr(str, '.') && (int)(ft_strlen(ft_strchr(str, '.') + 1)) > a_p) 58 | return (-1); 59 | if (ft_strchr(str, '.') && !ft_isdigit(*(ft_strchr(str, '.') + 1))) 60 | return (-1); 61 | if (!ft_strchr(str, '.') && (int)(ft_strlen(str)) > b_p) 62 | return (-1); 63 | return (0); 64 | } 65 | 66 | static double get_double(const char *str) 67 | { 68 | double value; 69 | double factor; 70 | int i; 71 | 72 | i = 0; 73 | value = 0.0; 74 | factor = 1; 75 | if (str[i] == '-') 76 | factor = -1; 77 | if (str[i] == '-' || str[i] == '+') 78 | i++; 79 | while (str[i] >= '0' && str[i] <= '9') 80 | { 81 | value = value * 10 + (str[i] - '0'); 82 | i++; 83 | } 84 | if (str[i] == '.') 85 | i++; 86 | while (str[i] >= '0' && str[i] <= '9') 87 | { 88 | value = value * 10.0 + (str[i] - '0'); 89 | factor = factor / 10.0; 90 | i++; 91 | } 92 | return (factor * value); 93 | } 94 | -------------------------------------------------------------------------------- /lib/libmlx/src/font/mlx_font.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* :::::::: */ 4 | /* mlx_font.c :+: :+: */ 5 | /* +:+ */ 6 | /* By: W2Wizard +#+ */ 7 | /* +#+ */ 8 | /* Created: 2022/02/22 12:01:37 by W2Wizard #+# #+# */ 9 | /* Updated: 2022/04/13 00:34:10 by w2wizard ######## odam.nl */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "font.h" 14 | #include "MLX42/MLX42_Int.h" 15 | 16 | //= Private =// 17 | 18 | /** 19 | * Retrieves the X offset of the given char in the font texture strip. 20 | * 21 | * @param c The char to find. 22 | * @return Non-negative if found or -1 if not found. 23 | */ 24 | static int32_t mlx_get_texoffset(char c) 25 | { 26 | const bool _isprint = isprint(c); 27 | 28 | // NOTE: Cheesy branchless operation :D 29 | return (-1 * !_isprint + ((FONT_WIDTH + 2) * (c - 32)) * _isprint); 30 | } 31 | 32 | /** 33 | * Does the actual copying of pixels form the atlas buffer to the 34 | * image buffer. 35 | * 36 | * Skips any non-printable characters. 37 | * 38 | * @param image The image to draw on. 39 | * @param texture The font_atlas. 40 | * @param texoffset The character texture X offset. 41 | * @param imgoffset The image X offset. 42 | */ 43 | static void mlx_draw_char(mlx_image_t* image, int32_t texoffset, int32_t imgoffset) 44 | { 45 | if (texoffset < 0) 46 | return; 47 | 48 | uint8_t* pixelx; 49 | uint8_t* pixeli; 50 | for (uint32_t y = 0; y < FONT_HEIGHT; y++) 51 | { 52 | pixelx = &font_atlas.pixels[(y * font_atlas.width + texoffset) * BPP]; 53 | pixeli = image->pixels + ((y * image->width + imgoffset) * BPP); 54 | memcpy(pixeli, pixelx, FONT_WIDTH * BPP); 55 | } 56 | } 57 | 58 | //= Public =// 59 | 60 | mlx_image_t* mlx_put_string(mlx_t* mlx, const char* str, int32_t x, int32_t y) 61 | { 62 | MLX_ASSERT(!mlx); 63 | MLX_ASSERT(!str); 64 | 65 | mlx_image_t* strimage; 66 | if (!(strimage = mlx_new_image(mlx, strlen(str) * FONT_WIDTH, FONT_HEIGHT))) 67 | return (NULL); 68 | 69 | // Draw the text itself 70 | int32_t imgoffset = 0; 71 | for (size_t i = 0; str[i] != '\0'; i++, imgoffset += FONT_WIDTH) 72 | mlx_draw_char(strimage, mlx_get_texoffset(str[i]), imgoffset); 73 | 74 | if (mlx_image_to_window(mlx, strimage, x, y) == -1) 75 | return (mlx_delete_image(mlx, strimage), NULL); 76 | return (strimage); 77 | } 78 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_obj_intersect_2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_obj_intersect_2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:55:28 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:55:29 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace.h" 14 | 15 | double intersect_rectangle_z(t_list *obj, t_ray *ray, double t_min, 16 | double t_max) 17 | { 18 | t_ray tmp_ray; 19 | double t; 20 | double x; 21 | double y; 22 | 23 | tmp_ray = *ray; 24 | intersect_rectangle_move_rotate_ray(&tmp_ray, obj); 25 | if (fabs(tmp_ray.dir.z) < 1e-6) 26 | return (-1.0); 27 | t = -tmp_ray.pos.z / tmp_ray.dir.z; 28 | if (t < t_min || t > t_max) 29 | return (-1.0); 30 | x = tmp_ray.pos.x + t * tmp_ray.dir.x; 31 | y = tmp_ray.pos.y + t * tmp_ray.dir.y; 32 | if (fabs(x) > obj_cont(obj)->rt.width / 2.0 33 | || fabs(y) > obj_cont(obj)->rt.height / 2.0) 34 | return (-1.0); 35 | return (t); 36 | } 37 | 38 | double intersect_rectangle_y(t_list *obj, t_ray *ray, double t_min, 39 | double t_max) 40 | { 41 | t_ray tmp_ray; 42 | double t; 43 | double x; 44 | double z; 45 | 46 | tmp_ray = *ray; 47 | intersect_rectangle_move_rotate_ray(&tmp_ray, obj); 48 | if (fabs(tmp_ray.dir.y) < 1e-6) 49 | return (-1.0); 50 | t = -tmp_ray.pos.y / tmp_ray.dir.y; 51 | if (t < t_min || t > t_max) 52 | return (-1.0); 53 | x = tmp_ray.pos.x + t * tmp_ray.dir.x; 54 | z = tmp_ray.pos.z + t * tmp_ray.dir.z; 55 | if (fabs(x) > obj_cont(obj)->rt.width / 2.0 56 | || fabs(z) > obj_cont(obj)->rt.height / 2.0) 57 | return (-1.0); 58 | return (t); 59 | } 60 | 61 | double intersect_rectangle_x(t_list *obj, t_ray *ray, double t_min, 62 | double t_max) 63 | { 64 | t_ray tmp_ray; 65 | double t; 66 | double y; 67 | double z; 68 | 69 | tmp_ray = *ray; 70 | intersect_rectangle_move_rotate_ray(&tmp_ray, obj); 71 | if (fabs(tmp_ray.dir.x) < 1e-6) 72 | return (-1.0); 73 | t = -tmp_ray.pos.x / tmp_ray.dir.x; 74 | if (t < t_min || t > t_max) 75 | return (-1.0); 76 | y = tmp_ray.pos.y + t * tmp_ray.dir.y; 77 | z = tmp_ray.pos.z + t * tmp_ray.dir.z; 78 | if (fabs(z) > obj_cont(obj)->rt.width / 2.0 79 | || fabs(y) > obj_cont(obj)->rt.height / 2.0) 80 | return (-1.0); 81 | return (t); 82 | } 83 | -------------------------------------------------------------------------------- /lib/libmlx/tools/compile_shader.bat: -------------------------------------------------------------------------------- 1 | :: ************************************************************************** :: 2 | :: :: 3 | :: :::::::: :: 4 | :: compile_shader.bat :+: :+: :: 5 | :: +:+ :: 6 | :: By: fbes +#+ :: 7 | :: +#+ :: 8 | :: Created: 2022/03/07 16:24:06 by fbes #+# #+# :: 9 | :: Updated: 2022/03/07 18:12:51 by fbes ######## odam.nl :: 10 | :: :: 11 | :: ************************************************************************** :: 12 | 13 | @echo off 14 | SETLOCAL DisableDelayedExpansion 15 | 16 | :: go to usage function if no arguments have been given to the script 17 | IF [%1]==[] GOTO usage 18 | 19 | :: check if input file exists before continuing 20 | IF NOT EXIST %1 GOTO fnotfound 21 | 22 | SET SHADERTYPE=%~x1 23 | SET SHADERTYPE=%SHADERTYPE:~1% 24 | 25 | echo /* ************************************************************************** */ 26 | echo /* */ 27 | echo /* :::::::: */ 28 | echo /* lol.c :+: :+: */ 29 | echo /* +:+ */ 30 | echo /* By: W2wizard ^ +#+ */ 31 | echo /* +#+ */ 32 | echo /* Created: 2022/02/17 22:34:59 by W2wizard #+# #+# */ 33 | echo /* Updated: 2022/02/17 22:34:59 by W2wizard ######## odam.nl */ 34 | echo /* */ 35 | echo /* ************************************************************************** */ 36 | echo. 37 | echo // If you wish to modify this file edit the .vert or .frag file! 38 | echo. 39 | echo #include "MLX42/MLX42_Int.h" 40 | echo. 41 | 42 | FOR /F "delims=" %%A IN (%1) DO IF NOT DEFINED VERSIONLINE set "VERSIONLINE=%%A" 43 | echo const char* %SHADERTYPE%_shader = "%VERSIONLINE%\n" 44 | FOR /F "skip=1 delims=" %%A IN (%1) DO ( 45 | IF "%%A" == "}" (echo "%%A";) ELSE (echo "%%A") 46 | ) 47 | 48 | ENDLOCAL 49 | EXIT /B 0 50 | 51 | :: usage function exits the script with exit code 3 (path not found) 52 | :usage 53 | echo ERROR: missing arguments, use as follows: %0 ^ 1>&2 54 | ENDLOCAL 55 | EXIT /B 3 56 | 57 | :: fnotfound function exits the script with exit code 2 (file not found) 58 | :fnotfound 59 | echo ERROR: shader file not found: %1 1>&2 60 | ENDLOCAL 61 | EXIT /B 2 62 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_diffuse.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_diffuse.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:54:54 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:54:54 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace.h" 14 | 15 | static double mixed_sampling_pdf(t_scene *scene, t_ray *ray, t_hit *hit); 16 | 17 | t_vec3 diffuse_light_sampling(t_list *l_light, t_hit *hit) 18 | { 19 | double random; 20 | double weight_sum; 21 | 22 | weight_sum = 0; 23 | random = ft_rand(); 24 | while (l_light) 25 | { 26 | weight_sum += light_cont(l_light)->weight; 27 | if (random <= weight_sum) 28 | return (light_cont(l_light)->random_dir(l_light, hit)); 29 | l_light = l_light->next; 30 | } 31 | return (hit->normal); 32 | } 33 | 34 | t_vec3 diffuse_cosine_sampling(t_hit *hit) 35 | { 36 | if (hit->side == INSIDE) 37 | return (random_cosine_direction_onb(vec3_scale(-1.0, hit->normal))); 38 | else 39 | return (random_cosine_direction_onb(hit->normal)); 40 | } 41 | 42 | double pdf_scaling(t_scene *scene, t_ray *ray, t_hit *hit) 43 | { 44 | double cosine; 45 | double mixed_sampling_pdf_value; 46 | double scattering_pdf; 47 | 48 | mixed_sampling_pdf_value = mixed_sampling_pdf(scene, ray, hit); 49 | if (mixed_sampling_pdf_value == 0.0) 50 | return (0.0); 51 | cosine = vec3_dot(hit->normal, ray->dir); 52 | if ((cosine < 0 && hit->side == OUTSIDE) 53 | || (cosine > 0 && hit->side == INSIDE)) 54 | scattering_pdf = 0; 55 | else 56 | scattering_pdf = fabs(cosine / M_PI); 57 | return (scattering_pdf / mixed_sampling_pdf_value); 58 | } 59 | 60 | static double mixed_sampling_pdf(t_scene *scene, t_ray *ray, t_hit *hit) 61 | { 62 | t_list *iter; 63 | double cosine; 64 | double cosine_pdf; 65 | double import_pdf; 66 | 67 | cosine = vec3_dot(hit->normal, ray->dir); 68 | if ((cosine < 0.0 && hit->side == OUTSIDE) 69 | || (cosine > 0.0 && hit->side == INSIDE)) 70 | return (0.0); 71 | else 72 | cosine_pdf = fabs(cosine / M_PI); 73 | if (scene->l_light == NULL) 74 | return (cosine_pdf); 75 | import_pdf = 0.0; 76 | iter = scene->l_light; 77 | while (iter) 78 | { 79 | import_pdf += light_cont(iter)->weight 80 | * light_cont(iter)->pdf_value(iter, hit); 81 | iter = iter->next; 82 | } 83 | return (scene->sampling.cosine * cosine_pdf 84 | + scene->sampling.light * import_pdf); 85 | } 86 | -------------------------------------------------------------------------------- /src/trace/mrt_trace_obj_intersect_2_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_trace_obj_intersect_2_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:55:30 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:55:31 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_trace.h" 14 | 15 | static void rotate_ray_x(t_ray *ray, double rot_x); 16 | static void rotate_ray_y(t_ray *ray, double rot_y); 17 | static void rotate_ray_z(t_ray *ray, double rot_z); 18 | 19 | void intersect_rectangle_move_rotate_ray(t_ray *ray, t_list *obj) 20 | { 21 | ray->pos = vec3_sub(ray->pos, obj_cont(obj)->rt.pos); 22 | if (obj_cont(obj)->rt.rot.z != 0.0) 23 | rotate_ray_z(ray, obj_cont(obj)->rt.rot.z); 24 | if (obj_cont(obj)->rt.rot.y != 0.0) 25 | rotate_ray_y(ray, obj_cont(obj)->rt.rot.y); 26 | if (obj_cont(obj)->rt.rot.x != 0.0) 27 | rotate_ray_x(ray, obj_cont(obj)->rt.rot.x); 28 | ray->pos = vec3_sub(ray->pos, obj_cont(obj)->rt.rel_pos); 29 | } 30 | 31 | static void rotate_ray_x(t_ray *ray, double rot_x) 32 | { 33 | double sine; 34 | double cosine; 35 | double tmp; 36 | 37 | sine = sin(rot_x * (M_PI / 180.0)); 38 | cosine = cos(rot_x * (M_PI / 180.0)); 39 | tmp = cosine * ray->pos.y - sine * ray->pos.z; 40 | ray->pos.z = sine * ray->pos.y + cosine * ray->pos.z; 41 | ray->pos.y = tmp; 42 | tmp = cosine * ray->dir.y - sine * ray->dir.z; 43 | ray->dir.z = sine * ray->dir.y + cosine * ray->dir.z; 44 | ray->dir.y = tmp; 45 | } 46 | 47 | static void rotate_ray_y(t_ray *ray, double rot_y) 48 | { 49 | double sine; 50 | double cosine; 51 | double tmp; 52 | 53 | sine = sin(rot_y * (M_PI / 180.0)); 54 | cosine = cos(rot_y * (M_PI / 180.0)); 55 | tmp = cosine * ray->pos.x + sine * ray->pos.z; 56 | ray->pos.z = -sine * ray->pos.x + cosine * ray->pos.z; 57 | ray->pos.x = tmp; 58 | tmp = cosine * ray->dir.x + sine * ray->dir.z; 59 | ray->dir.z = -sine * ray->dir.x + cosine * ray->dir.z; 60 | ray->dir.x = tmp; 61 | } 62 | 63 | static void rotate_ray_z(t_ray *ray, double rot_z) 64 | { 65 | double sine; 66 | double cosine; 67 | double tmp; 68 | 69 | sine = sin(rot_z * (M_PI / 180.0)); 70 | cosine = cos(rot_z * (M_PI / 180.0)); 71 | tmp = cosine * ray->pos.x - sine * ray->pos.y; 72 | ray->pos.y = sine * ray->pos.x + cosine * ray->pos.y; 73 | ray->pos.x = tmp; 74 | tmp = cosine * ray->dir.x - sine * ray->dir.y; 75 | ray->dir.y = sine * ray->dir.x + cosine * ray->dir.y; 76 | ray->dir.x = tmp; 77 | } 78 | -------------------------------------------------------------------------------- /lib/libmlx/src/textures/mlx_texture.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* :::::::: */ 4 | /* mlx_texture.c :+: :+: */ 5 | /* +:+ */ 6 | /* By: W2Wizard +#+ */ 7 | /* +#+ */ 8 | /* Created: 2022/02/17 01:02:24 by W2Wizard #+# #+# */ 9 | /* Updated: 2022/04/13 00:37:53 by w2wizard ######## odam.nl */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "MLX42/MLX42_Int.h" 14 | 15 | //= Public =// 16 | 17 | mlx_image_t* mlx_texture_area_to_image(mlx_t* mlx, mlx_texture_t* texture, uint32_t xy[2], uint32_t wh[2]) 18 | { 19 | MLX_ASSERT(!mlx); 20 | MLX_ASSERT(!texture); 21 | MLX_ASSERT(!xy); 22 | MLX_ASSERT(!wh); 23 | 24 | if (xy[0] > texture->width || xy[1] > texture->height || \ 25 | wh[0] > texture->width || wh[1] > texture->height) 26 | return ((void*)mlx_error(MLX_INVAREA)); 27 | 28 | mlx_image_t* image; 29 | if (!(image = mlx_new_image(mlx, wh[0], wh[1]))) 30 | return ((void*)mlx_error(MLX_MEMFAIL)); 31 | 32 | uint8_t* pixelx; 33 | uint8_t* pixeli; 34 | for (uint32_t y = 0; y < wh[1]; y++) 35 | { 36 | pixelx = &texture->pixels[((xy[1] + y) * texture->width + xy[0]) * BPP]; 37 | pixeli = &image->pixels[y * wh[0] * BPP]; 38 | memmove(pixeli, pixelx, wh[0] * BPP); 39 | } 40 | return (image); 41 | } 42 | 43 | mlx_image_t* mlx_texture_to_image(mlx_t* mlx, mlx_texture_t* texture) 44 | { 45 | MLX_ASSERT(!mlx); 46 | MLX_ASSERT(!texture); 47 | 48 | mlx_image_t* img; 49 | const int32_t xy[] = {0, 0}; 50 | const uint32_t wh[] = {texture->width, texture->height}; 51 | 52 | if (!(img = mlx_texture_area_to_image(mlx, texture, (uint32_t*)xy, (uint32_t*)wh))) 53 | return ((void*)mlx_error(MLX_MEMFAIL)); 54 | return (img); 55 | } 56 | 57 | bool mlx_draw_texture(mlx_image_t* image, mlx_texture_t* texture, int32_t x, int32_t y) 58 | { 59 | MLX_ASSERT(!image); 60 | MLX_ASSERT(!texture); 61 | MLX_ASSERT(x < 0); 62 | MLX_ASSERT(y < 0); 63 | 64 | if (texture->width > image->width || texture->height > image->height) 65 | return (mlx_error(MLX_TEXTOBIG)); 66 | 67 | uint8_t* pixelx; 68 | uint8_t* pixeli; 69 | for (uint32_t i = 0; i < texture->height; i++) 70 | { 71 | pixelx = &texture->pixels[(i * texture->width) * texture->bytes_per_pixel]; 72 | pixeli = &image->pixels[((i + y) * image->width + x) * texture->bytes_per_pixel]; 73 | memmove(pixeli, pixelx, texture->width * texture->bytes_per_pixel); 74 | } 75 | return (true); 76 | } 77 | 78 | void mlx_delete_texture(mlx_texture_t* texture) 79 | { 80 | MLX_ASSERT(!texture); 81 | 82 | mlx_freen(2, texture->pixels, texture); 83 | } 84 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_material.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_material.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:44 by tjensen #+# #+# */ 9 | /* Updated: 2022/06/09 14:56:44 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | 15 | static int str_to_surfaces(const char *str, double surf[4], int line_num); 16 | 17 | int parse_material(t_material *material, char **split, int line_num) 18 | { 19 | double surf_sum; 20 | 21 | if (parse_color(split[0], &(material->color))) 22 | return (print_error_scene(line_num, ERR_PARSE, ERR_COLOR, NULL)); 23 | if (str_to_surfaces(split[1], material->surface, line_num)) 24 | return (-1); 25 | surf_sum = material->surface[DIFFUSE] 26 | + material->surface[SPECULAR] 27 | + material->surface[DIELECTRIC] 28 | + material->surface[EMISSION]; 29 | if (surf_sum != 1.0) 30 | return (print_error_scene(line_num, ERR_PARSE, ERR_SURF_SUM, NULL)); 31 | if (double_from_str(split[2], 1, 2, &(material->fuzz)) 32 | || material->fuzz < 0.0 || material->fuzz > 9.99) 33 | return (print_error_scene(line_num, ERR_PARSE, ERR_FUZZ, NULL)); 34 | if (double_from_str(split[3], 1, 2, &(material->refraction_index)) 35 | || (material->surface[DIELECTRIC] > 0.0 36 | && (material->refraction_index < 1.0 37 | || material->refraction_index > 2.0))) 38 | return (print_error_scene(line_num, ERR_PARSE, ERR_REFRACTION, NULL)); 39 | material->brightness = 1.0; 40 | material->get_color = &obj_color; 41 | return (0); 42 | } 43 | 44 | static int str_to_surfaces(const char *str, double surf[4], int line_num) 45 | { 46 | int error; 47 | char **split_surf; 48 | 49 | error = false; 50 | split_surf = ft_split(str, ','); 51 | if (split_surf == NULL) 52 | return (print_error_scene(-1, ERR_PARSE, strerror(errno), NULL)); 53 | if (ft_split_count_str(split_surf) != 3) 54 | error = -1; 55 | if (!error) 56 | error = double_from_str(split_surf[0], 1, 2, &(surf[DIFFUSE])); 57 | if (!error) 58 | error = double_from_str(split_surf[1], 1, 2, &(surf[SPECULAR])); 59 | if (!error) 60 | error = double_from_str(split_surf[2], 1, 2, &(surf[DIELECTRIC])); 61 | if (error 62 | || surf[DIFFUSE] > 1.0 || surf[DIFFUSE] < 0.0 63 | || surf[SPECULAR] > 1.0 || surf[SPECULAR] < 0.0 64 | || surf[DIELECTRIC] > 1.0 || surf[DIELECTRIC] < 0.0) 65 | error = print_error_scene(line_num, ERR_PARSE, ERR_SURF, NULL); 66 | ft_free_split(&split_surf); 67 | return (error); 68 | } 69 | -------------------------------------------------------------------------------- /src/parse/mrt_parse_texture.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mrt_parse_texture.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tjensen +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/06/09 14:56:22 by tjensen #+# #+# */ 9 | /* Updated: 2022/12/19 12:43:34 by tjensen ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "mrt_parse.h" 14 | #include "print/mrt_print.h" 15 | #include "trace/mrt_trace.h" 16 | 17 | #include "libmlx/include/MLX42/MLX42.h" 18 | 19 | int mlx_texture_to_color(mlx_texture_t *mlx_texture, t_texture *c_texture); 20 | 21 | int parse_texture(t_scene *scene, char **split, int line_num) 22 | { 23 | t_list *texture; 24 | mlx_texture_t *mlx_texture; 25 | 26 | if (ft_split_count_str(split) != 3) 27 | return (print_error_scene(line_num, ERR_PARSE, ERR_NUM_PARA, NULL)); 28 | texture = texture_new(); 29 | if (texture == NULL) 30 | return (print_error_scene(line_num, ERR_PARSE, strerror(errno), NULL)); 31 | ft_lstadd_back(&(scene->l_textures), texture); 32 | texture_cont(texture)->name = ft_strdup(split[1]); 33 | mlx_texture = mlx_load_png(split[2]); 34 | if (mlx_texture == NULL) 35 | return (print_error_scene(line_num, ERR_PARSE, 36 | ERR_LOAD_TEXTURE, split[2])); 37 | texture_cont(texture)->width = mlx_texture->width; 38 | texture_cont(texture)->height = mlx_texture->height; 39 | if (mlx_texture_to_color(mlx_texture, texture_cont(texture))) 40 | { 41 | mlx_delete_texture(mlx_texture); 42 | return (print_error_scene(line_num, ERR_PARSE, strerror(errno), NULL)); 43 | } 44 | mlx_delete_texture(mlx_texture); 45 | return (0); 46 | } 47 | 48 | int mlx_texture_to_color(mlx_texture_t *mlx_texture, t_texture *c_texture) 49 | { 50 | size_t i; 51 | size_t pixel; 52 | 53 | c_texture->color = malloc(mlx_texture->width 54 | * mlx_texture->height * sizeof(t_color)); 55 | if (c_texture->color == NULL) 56 | return (-1); 57 | i = 0; 58 | while (i < mlx_texture->width * mlx_texture->height) 59 | { 60 | pixel = i * mlx_texture->bytes_per_pixel; 61 | c_texture->color[i].r = mlx_texture->pixels[pixel] / 255.0; 62 | c_texture->color[i].g = mlx_texture->pixels[pixel + 1] / 255.0; 63 | c_texture->color[i].b = mlx_texture->pixels[pixel + 2] / 255.0; 64 | i++; 65 | } 66 | return (0); 67 | } 68 | 69 | t_texture *parse_c_texture_find(t_list *l_texture, const char *name) 70 | { 71 | while (l_texture) 72 | { 73 | if (ft_strcmp(texture_cont(l_texture)->name, name) == 0) 74 | return (texture_cont(l_texture)); 75 | l_texture = l_texture->next; 76 | } 77 | return (NULL); 78 | } 79 | --------------------------------------------------------------------------------