├── .gitignore ├── Makefile ├── README.md ├── chapter ├── ch11.c ├── ch13.c ├── ch2.c ├── ch4.c ├── ch5.c ├── ch6.c ├── ch7.c ├── ch8.c └── ch9.c ├── includes ├── chapter.h ├── libft.h ├── minirt.h ├── minirt_bonus.h ├── minirt_const.h ├── minirt_material.h ├── minirt_struct.h └── mlx.h ├── libft ├── Makefile ├── ft_atoi.c ├── ft_bzero.c ├── ft_calloc.c ├── ft_isalnum.c ├── ft_isalpha.c ├── ft_isascii.c ├── ft_isblank_bonus.c ├── ft_isdigit.c ├── ft_islower_bonus.c ├── ft_isnumber_bonus.c ├── ft_isprint.c ├── ft_isupper_bonus.c ├── ft_itoa.c ├── ft_lstadd_back.c ├── ft_lstadd_front.c ├── ft_lstclear.c ├── ft_lstdelone.c ├── ft_lstiter.c ├── ft_lstlast.c ├── ft_lstmap.c ├── ft_lstnew.c ├── ft_lstsize.c ├── ft_memccpy.c ├── ft_memchr.c ├── ft_memcmp.c ├── ft_memcpy.c ├── ft_memmove.c ├── ft_memset.c ├── ft_printf │ ├── ft_printf_bonus.c │ ├── ft_printf_bonus.h │ ├── percent_bonus.c │ ├── print_ascii_bonus.c │ ├── print_element_bonus.c │ ├── print_hex_bonus.c │ ├── print_num_bonus.c │ ├── print_pointer_bonus.c │ └── take_percentage_bonus.c ├── ft_putchar_fd.c ├── ft_putendl_fd.c ├── ft_putnbr_fd.c ├── ft_putstr_fd.c ├── ft_split.c ├── ft_strchr.c ├── ft_strdup.c ├── ft_strjoin.c ├── ft_strlcat.c ├── ft_strlcpy.c ├── ft_strlen.c ├── ft_strmapi.c ├── ft_strncmp.c ├── ft_strndup_bonus.c ├── ft_strnstr.c ├── ft_strnul_bonus.c ├── ft_strpush_bonus.c ├── ft_strrchr.c ├── ft_strtrim.c ├── ft_substr.c ├── ft_tolower.c ├── ft_toupper.c ├── get_next_line_bonus.c └── libft.h ├── mlx ├── Makefile ├── font.c ├── interface.swift ├── mlx.h ├── mlx_image.swift ├── mlx_init.swift ├── mlx_png.c ├── mlx_rgb.c ├── mlx_string_put.c ├── mlx_window.swift └── mlx_xpm.c ├── sample ├── ch11.bmp ├── ch13.bmp ├── ch2.bmp ├── ch4.bmp ├── ch5.bmp ├── ch6.bmp ├── ch7.bmp ├── ch8.bmp └── ch9.bmp └── srcs ├── cal_utils.c ├── color.c ├── draw_basic.c ├── draw_hittable.c ├── draw_hittable_bonus.c ├── draw_hittable_diffuse_bonus.c ├── draw_hittable_material_bonus.c ├── draw_hittable_pthread_bonus.c ├── img_data_utils.c ├── main.c ├── material └── simple_material.c ├── mlx_utils.c ├── object ├── simple_sphere.c ├── sky.c ├── sky_ray.c ├── sphere.c └── sphere_hit.c ├── random_bonus.c ├── save_bmp.c └── struct ├── camera_utils.c ├── hit_record.c ├── hitlst_info_utils.c ├── hitlst_utils.c ├── hittable_utils.c ├── material_utils.c ├── ray_utils.c ├── thread_info_utils_bonus.c ├── vec_calc_extra.c ├── vector_apply.c ├── vector_calc.c ├── vector_create.c ├── vector_random_bonus.c └── vector_utils.c /.gitignore: -------------------------------------------------------------------------------- 1 | weekend 2 | *.swiftdoc 3 | *.swiftmodule 4 | *.swiftsourceinfo 5 | 6 | # Created by https://www.toptal.com/developers/gitignore/api/macos,visualstudiocode,c,swift 7 | # Edit at https://www.toptal.com/developers/gitignore?templates=macos,visualstudiocode,c,swift 8 | 9 | ### C ### 10 | # Prerequisites 11 | *.d 12 | 13 | # Object files 14 | *.o 15 | *.ko 16 | *.obj 17 | *.elf 18 | 19 | # Linker output 20 | *.ilk 21 | *.map 22 | *.exp 23 | 24 | # Precompiled Headers 25 | *.gch 26 | *.pch 27 | 28 | # Libraries 29 | *.lib 30 | *.a 31 | *.la 32 | *.lo 33 | 34 | # Shared objects (inc. Windows DLLs) 35 | *.dll 36 | *.so 37 | *.so.* 38 | *.dylib 39 | 40 | # Executables 41 | *.exe 42 | *.out 43 | *.app 44 | *.i*86 45 | *.x86_64 46 | *.hex 47 | 48 | # Debug files 49 | *.dSYM/ 50 | *.su 51 | *.idb 52 | *.pdb 53 | 54 | # Kernel Module Compile Results 55 | *.mod* 56 | *.cmd 57 | .tmp_versions/ 58 | modules.order 59 | Module.symvers 60 | Mkfile.old 61 | dkms.conf 62 | 63 | ### macOS ### 64 | # General 65 | .DS_Store 66 | .AppleDouble 67 | .LSOverride 68 | 69 | # Icon must end with two \r 70 | Icon 71 | 72 | # Thumbnails 73 | ._* 74 | 75 | # Files that might appear in the root of a volume 76 | .DocumentRevisions-V100 77 | .fseventsd 78 | .Spotlight-V100 79 | .TemporaryItems 80 | .Trashes 81 | .VolumeIcon.icns 82 | .com.apple.timemachine.donotpresent 83 | 84 | # Directories potentially created on remote AFP share 85 | .AppleDB 86 | .AppleDesktop 87 | Network Trash Folder 88 | Temporary Items 89 | .apdisk 90 | 91 | ### Swift ### 92 | # Xcode 93 | # 94 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 95 | 96 | ## User settings 97 | xcuserdata/ 98 | 99 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 100 | *.xcscmblueprint 101 | *.xccheckout 102 | 103 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 104 | build/ 105 | DerivedData/ 106 | *.moved-aside 107 | *.pbxuser 108 | !default.pbxuser 109 | *.mode1v3 110 | !default.mode1v3 111 | *.mode2v3 112 | !default.mode2v3 113 | *.perspectivev3 114 | !default.perspectivev3 115 | 116 | ## Obj-C/Swift specific 117 | *.hmap 118 | 119 | ## App packaging 120 | *.ipa 121 | *.dSYM.zip 122 | *.dSYM 123 | 124 | ## Playgrounds 125 | timeline.xctimeline 126 | playground.xcworkspace 127 | 128 | # Swift Package Manager 129 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 130 | # Packages/ 131 | # Package.pins 132 | # Package.resolved 133 | # *.xcodeproj 134 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 135 | # hence it is not needed unless you have added a package configuration file to your project 136 | # .swiftpm 137 | 138 | .build/ 139 | 140 | # CocoaPods 141 | # We recommend against adding the Pods directory to your .gitignore. However 142 | # you should judge for yourself, the pros and cons are mentioned at: 143 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 144 | # Pods/ 145 | # Add this line if you want to avoid checking in source code from the Xcode workspace 146 | # *.xcworkspace 147 | 148 | # Carthage 149 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 150 | # Carthage/Checkouts 151 | 152 | Carthage/Build/ 153 | 154 | # Accio dependency management 155 | Dependencies/ 156 | .accio/ 157 | 158 | # fastlane 159 | # It is recommended to not store the screenshots in the git repo. 160 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 161 | # For more information about the recommended setup visit: 162 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 163 | 164 | fastlane/report.xml 165 | fastlane/Preview.html 166 | fastlane/screenshots/**/*.png 167 | fastlane/test_output 168 | 169 | # Code Injection 170 | # After new code Injection tools there's a generated folder /iOSInjectionProject 171 | # https://github.com/johnno1962/injectionforxcode 172 | 173 | iOSInjectionProject/ 174 | 175 | ### VisualStudioCode ### 176 | .vscode/* 177 | !.vscode/settings.json 178 | !.vscode/tasks.json 179 | !.vscode/launch.json 180 | !.vscode/extensions.json 181 | *.code-workspace 182 | 183 | ### VisualStudioCode Patch ### 184 | # Ignore all local history of files 185 | .history 186 | 187 | # End of https://www.toptal.com/developers/gitignore/api/macos,visualstudiocode,c,swift 188 | 189 | *.bmp 190 | !sample/*.bmp 191 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: sunpark +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2020/07/04 13:34:20 by sunpark #+# #+# # 9 | # Updated: 2020/10/23 13:44:08 by sunpark ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | SRC = main.c color.c draw_basic.c mlx_utils.c img_data_utils.c save_bmp.c \ 14 | draw_hittable.c cal_utils.c draw_hittable_material_bonus.c \ 15 | draw_hittable_bonus.c random_bonus.c draw_hittable_diffuse_bonus.c \ 16 | draw_hittable_pthread_bonus.c \ 17 | object/sky.c object/sky_ray.c object/sphere.c object/simple_sphere.c \ 18 | object/sphere_hit.c \ 19 | struct/vector_calc.c struct/vector_utils.c struct/ray_utils.c \ 20 | struct/vector_apply.c struct/hit_record.c struct/hittable_utils.c \ 21 | struct/hitlst_utils.c struct/hitlst_info_utils.c struct/camera_utils.c \ 22 | struct/vector_create.c struct/vector_random_bonus.c \ 23 | struct/vec_calc_extra.c struct/material_utils.c \ 24 | struct/thread_info_utils_bonus.c \ 25 | material/simple_material.c 26 | 27 | CSRC = ch2.c ch4.c ch5.c ch6.c ch7.c ch8.c ch9.c ch11.c ch13.c 28 | SRCDIR = ./srcs/ 29 | CSRCDIR = ./chapter/ 30 | SRCS = $(addprefix $(SRCDIR), $(SRC)) 31 | CSRCS = $(addprefix $(CSRCDIR), $(CSRC)) 32 | OBJS = $(SRCS:.c=.o) 33 | COBJS = $(CSRCS:.c=.o) 34 | 35 | INCDIR = ./includes/ 36 | LIBDIR = ./libft/ 37 | LIBNAME = libft.a 38 | MLXDIR = ./mlx/ 39 | MLXNAME = libmlx.dylib 40 | 41 | NAME = weekend 42 | 43 | GCC = gcc 44 | GCCFLAG = -Wall -Wextra -Werror -g 45 | RM = rm -f 46 | 47 | %.o: %.c 48 | $(GCC) $(GCCFLAG) -I$(INCDIR) -c $< -o $@ 49 | 50 | $(NAME): $(LIBNAME) $(MLXNAME) $(OBJS) $(COBJS) 51 | $(GCC) $(GCCFLAG) -I$(INCDIR) -L$(MLXDIR) -lmlx -framework OpenGL -framework AppKit -o $(NAME) $(OBJS) $(COBJS) $(LIBNAME) $(MLXNAME) 52 | 53 | $(LIBNAME): 54 | @$(MAKE) -C $(LIBDIR) bonus 55 | @cp $(addprefix $(LIBDIR), $(LIBNAME)) $(LIBNAME) 56 | 57 | $(MLXNAME): 58 | @$(MAKE) -C $(MLXDIR) 59 | @cp $(addprefix $(MLXDIR), $(MLXNAME)) $(MLXNAME) 60 | 61 | all: $(NAME) 62 | 63 | clean: 64 | $(RM) $(OBJS) $(COBJS) *.bmp 65 | 66 | fclean: clean 67 | $(RM) $(NAME) $(LIBNAME) $(MLXNAME) 68 | @$(MAKE) -C $(LIBDIR) fclean 69 | @$(MAKE) -C $(MLXDIR) clean 70 | 71 | re: fclean all 72 | 73 | .PHONY: all clean fclean re 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

☀ Ray Tracing in One Weekend

2 |

Implement in Minilibx, C library in 42

3 | 4 |
5 | 6 |

7 | 8 | Practice version of [miniRT](https://github.com/cos18/miniRT) that implement [Ray Tracing in One Weekend](https://raytracing.github.io/books/RayTracingInOneWeekend.html) by Peter Shirley using Minilibx, simple C library that manage GUI in macOS. 9 | 10 |
11 | 12 | ## 📂 Related Repository 13 | - [ray-tracing-the-next-week-mlx](https://github.com/cos18/ray-tracing-the-next-week-mlx) 14 | 15 |
16 | 17 | ## 🛠 Development Environment 18 | 19 | - macOS 10.14+ 20 | - clang 11.0+ 21 | - Minilibx 20200219_beta (Already in project src!) 22 | - [Norm 2.0.2](https://cdn.intra.42.fr/pdf/pdf/1608/norme.en.pdf) - C code standard in 42 23 | 24 | > Minilibx is available on macOS and Linux, but there are only macOS ver. mlx in this project. So this project ONLY runs on macOS 😢 25 | 26 |
27 | 28 | ## 💻 How to Run 29 | 30 | ```bash 31 | $ make 32 | $ ./weekend 2 33 | ``` 34 | 35 | ### Usage 36 | 37 | `./weekend (chapter) (--save)` 38 | 39 | - chapter : **Required.** Chapter number that execute (2 ~ 13) 40 | - --save : Optional. Save Image in bmp format. When this flag is off, it's running through Minilibx. 41 | 42 | > 🎉 --save option work well! Sample bmp files are in [sample folder](./sample). 43 | 44 | ### Progress 45 | 46 | - [x] 2. Output an Image 47 | - [x] 3. The vec3 Class (Same result of ch2) 48 | - [x] 4. Rays, a Simple Camera, and Background 49 | - [x] 5. Adding a Sphere 50 | - [x] 6. Surface Normals and Multiple Objects 51 | - [x] 7. Antialiasing 52 | - [x] 8. Diffuse Materials 53 | - [x] 9. Metal 54 | - [ ] 10. Dielectrics 55 | - [x] 11. Positionable Camera 56 | - [ ] 12. Defocus Blur 57 | - [x] 13. Where Next? (Without Dielectrics and Defocus Blur) 58 | 59 | > 🚨 Last Example (Chapter 13) renders 125 spheres, so it will take few hours to render. 60 | 61 | > 🚀 If you want fast rendering, fix `PTHREAD_CNT` (Numbers of threads to render), `ANTI_SAMPLES` (Samples of antializing) and `REFLECT_DEPTH` (calculate times of ray reflecting) in [`minirt_const.h`](./includes/minirt_const.h) to optimize rendering time. 62 | -------------------------------------------------------------------------------- /chapter/ch11.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ch11.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 15:23:30 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/21 21:00:13 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | void show_sphere_camera(int is_save) 16 | { 17 | double aspect_ratio; 18 | t_img_data *img; 19 | t_camera *cam; 20 | t_sphere *s; 21 | t_list *hitlst; 22 | t_material *mat; 23 | 24 | aspect_ratio = 16.0 / 9.0; 25 | img = create_img_data(400, (int)(400 / aspect_ratio)); 26 | cam = camera_locate_new(vec_create(-2, 2, 1), vec_create(1, -1, -1), aspect_ratio, get_radian(90)); 27 | cam->data = img; 28 | hitlst = hitlst_new(); 29 | s = init_sphere(vec_create(0, 0, -1), 0.5); 30 | mat = mat_lambertian_new(vec_create(0.7, 0.3, 0.3)); 31 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 32 | free_material(mat); 33 | s = init_sphere(vec_create(0, -100.5, -1), 100); 34 | mat = mat_lambertian_new(vec_create(0.8, 0.8, 0)); 35 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 36 | free_material(mat); 37 | s = init_sphere(vec_create(-1, 0, -1), 0.5); 38 | mat = mat_metal_new(vec_create(0.8, 0.8, 0.8), 0.3); 39 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 40 | free_material(mat); 41 | s = init_sphere(vec_create(1, 0, -1), 0.5); 42 | mat = mat_metal_new(vec_create(0.8, 0.6, 0.2), 1); 43 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 44 | free_material(mat); 45 | ft_printf("Drawing start\n"); 46 | draw_hittable_material(cam, hitlst); 47 | ft_printf("Finish Drawing\n"); 48 | if (is_save) 49 | save_bmp(img, "ch11.rt"); 50 | else 51 | mlx_show(img, "Camera"); 52 | free_hitlst(hitlst); 53 | free_camera(cam); 54 | free_img_data(img); 55 | } 56 | -------------------------------------------------------------------------------- /chapter/ch13.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ch13.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 15:23:30 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/23 13:42:40 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | t_vec *get_random_center(int a, int b) 16 | { 17 | t_vec *result; 18 | t_vec *tmp; 19 | double length; 20 | 21 | result = vec_create(a + 0.9 * random_double(), 0.2, b + 0.9 * random_double()); 22 | tmp = vec_sub_apply(vec_create(4, 0.2, 0), result); 23 | length = vec_length(tmp); 24 | free(tmp); 25 | if (length <= 0.9) 26 | { 27 | free(result); 28 | return get_random_center(a, b); 29 | } 30 | return (result); 31 | } 32 | 33 | t_list *random_scene() 34 | { 35 | t_sphere *s; 36 | t_list *hitlst; 37 | t_material *mat; 38 | t_vec *center; 39 | double choose_mat; 40 | 41 | hitlst = hitlst_new(); 42 | s = init_sphere(vec_create(0, -1000, 0), 1000); 43 | mat = mat_lambertian_new(vec_create(0.5, 0.5, 0.5)); 44 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 45 | free_material(mat); 46 | for (int a = -11; a < 11; a++) 47 | { 48 | for (int b = -11; b < 11; b++) 49 | { 50 | choose_mat = random_double(); 51 | center = get_random_center(a, b); 52 | if (choose_mat < 0.8) 53 | mat = mat_lambertian_new(vec_random_range(0, 1)); 54 | else 55 | mat = mat_metal_new(vec_random_range(0.5, 1), random_double_range(0, 0.5)); 56 | s = init_sphere(center, 0.2); 57 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 58 | free_material(mat); 59 | } 60 | } 61 | s = init_sphere(vec_create(0, 1, 0), 1.0); 62 | mat = mat_lambertian_new(vec_create(0.4, 0.2, 0.1)); 63 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 64 | free_material(mat); 65 | s = init_sphere(vec_create(-4, 1, 0), 1.0); 66 | mat = mat_metal_new(vec_create(0.7, 0.6, 0.5), 0); 67 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 68 | free_material(mat); 69 | s = init_sphere(vec_create(4, 1, 0), 1.0); 70 | mat = mat_metal_new(vec_create(0.7, 0.6, 0.5), 0); 71 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 72 | free_material(mat); 73 | return (hitlst); 74 | } 75 | 76 | void show_final(int is_save) 77 | { 78 | double aspect_ratio; 79 | t_img_data *img; 80 | t_camera *cam; 81 | t_list *hitlst; 82 | 83 | ft_printf("WARNING! This example takes about 2~3 hours to render.\n"); 84 | aspect_ratio = 3.0 / 2.0; 85 | img = create_img_data(1200, (int)(1200 / aspect_ratio)); 86 | cam = camera_locate_new(vec_create(13, 2, 3), vec_create(-13, -2, -3), aspect_ratio, get_radian(20)); 87 | cam->data = img; 88 | hitlst = random_scene(); 89 | ft_printf("Drawing start\n"); 90 | draw_hittable_pthread(cam, hitlst); 91 | ft_printf("Finish Drawing\n"); 92 | if (is_save) 93 | save_bmp(img, "ch13.rt"); 94 | else 95 | mlx_show(img, "Final"); 96 | free_hitlst(hitlst); 97 | free_camera(cam); 98 | free_img_data(img); 99 | } 100 | -------------------------------------------------------------------------------- /chapter/ch2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ch2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/28 14:24:03 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/14 04:52:39 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | void show_gradation(int is_save) 16 | { 17 | t_img_data *img; 18 | 19 | img = create_img_data(256, 256); 20 | draw_gradaition(img); 21 | if (is_save) 22 | save_bmp(img, "ch2.rt"); 23 | else 24 | mlx_show(img, "Gradation"); 25 | free_img_data(img); 26 | } 27 | -------------------------------------------------------------------------------- /chapter/ch4.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ch4.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/04 22:29:56 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/15 13:07:47 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | void show_sky(int is_save) 16 | { 17 | double aspect_ratio; 18 | t_img_data *img; 19 | t_sky_info *info; 20 | 21 | aspect_ratio = 16.0 / 9.0; 22 | img = create_img_data(400, (int)(400 / aspect_ratio)); 23 | info = init_sky_info(2.0 * aspect_ratio, 2.0, 1.0); 24 | draw_sky(img, info); 25 | if (is_save) 26 | save_bmp(img, "ch4.rt"); 27 | else 28 | mlx_show(img, "Sky"); 29 | free(info); 30 | free_img_data(img); 31 | } 32 | -------------------------------------------------------------------------------- /chapter/ch5.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ch5.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 15:23:30 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/18 21:19:44 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | void show_s_sphere(int is_save) 16 | { 17 | double aspect_ratio; 18 | t_img_data *img; 19 | t_sky_info *info; 20 | t_sphere *s; 21 | 22 | aspect_ratio = 16.0 / 9.0; 23 | img = create_img_data(400, (int)(400 / aspect_ratio)); 24 | info = init_sky_info(2.0 * aspect_ratio, 2.0, 1.0); 25 | draw_sky(img, info); 26 | s = init_sphere(vec_create(0, 0, -1), 0.5); 27 | draw_s_sphere(img, info, s); 28 | if (is_save) 29 | save_bmp(img, "ch5.rt"); 30 | else 31 | mlx_show(img, "Simple Sphere at Sky"); 32 | free_sphere(s); 33 | free(info); 34 | free_img_data(img); 35 | } 36 | -------------------------------------------------------------------------------- /chapter/ch6.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ch6.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 15:23:30 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/21 18:56:27 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | void show_normal_sphere(int is_save) 16 | { 17 | double aspect_ratio; 18 | t_img_data *img; 19 | t_camera *cam; 20 | t_sphere *s; 21 | t_list *hitlst; 22 | 23 | aspect_ratio = 16.0 / 9.0; 24 | img = create_img_data(400, (int)(400 / aspect_ratio)); 25 | cam = camera_new(aspect_ratio); 26 | cam->data = img; 27 | hitlst = hitlst_new(); 28 | s = init_sphere(vec_create(0, 0, -1), 0.5); 29 | hitlst_add(hitlst, (void *)s, OBJ_SPHERE); 30 | s = init_sphere(vec_create(0, -100.5, -1), 100); 31 | hitlst_add(hitlst, (void *)s, OBJ_SPHERE); 32 | draw_hittable(cam, hitlst); 33 | if (is_save) 34 | save_bmp(img, "ch6.rt"); 35 | else 36 | mlx_show(img, "Normals-colored Sphere with Ground"); 37 | free_hitlst(hitlst); 38 | free_camera(cam); 39 | free_img_data(img); 40 | } 41 | -------------------------------------------------------------------------------- /chapter/ch7.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ch7.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 15:23:30 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/21 18:56:30 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | void show_normal_sphere_anti(int is_save) 16 | { 17 | double aspect_ratio; 18 | t_img_data *img; 19 | t_camera *cam; 20 | t_sphere *s; 21 | t_list *hitlst; 22 | 23 | aspect_ratio = 16.0 / 9.0; 24 | img = create_img_data(400, (int)(400 / aspect_ratio)); 25 | cam = camera_new(aspect_ratio); 26 | cam->data = img; 27 | hitlst = hitlst_new(); 28 | s = init_sphere(vec_create(0, 0, -1), 0.5); 29 | hitlst_add(hitlst, (void *)s, OBJ_SPHERE); 30 | s = init_sphere(vec_create(0, -100.5, -1), 100); 31 | hitlst_add(hitlst, (void *)s, OBJ_SPHERE); 32 | ft_printf("Drawing start\n"); 33 | draw_hittable_anti(cam, hitlst); 34 | ft_printf("Finish Drawing\n"); 35 | if (is_save) 36 | save_bmp(img, "ch7.rt"); 37 | else 38 | mlx_show(img, "Antialiasing"); 39 | free_hitlst(hitlst); 40 | free_camera(cam); 41 | free_img_data(img); 42 | } 43 | -------------------------------------------------------------------------------- /chapter/ch8.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ch8.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 15:23:30 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/21 18:56:34 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | void show_normal_sphere_anti_dif(int is_save) 16 | { 17 | double aspect_ratio; 18 | t_img_data *img; 19 | t_camera *cam; 20 | t_sphere *s; 21 | t_list *hitlst; 22 | 23 | aspect_ratio = 16.0 / 9.0; 24 | img = create_img_data(400, (int)(400 / aspect_ratio)); 25 | cam = camera_new(aspect_ratio); 26 | cam->data = img; 27 | hitlst = hitlst_new(); 28 | s = init_sphere(vec_create(0, 0, -1), 0.5); 29 | hitlst_add(hitlst, (void *)s, OBJ_SPHERE); 30 | s = init_sphere(vec_create(0, -100.5, -1), 100); 31 | hitlst_add(hitlst, (void *)s, OBJ_SPHERE); 32 | ft_printf("Drawing start\n"); 33 | draw_hittable_diffuse_anti(cam, hitlst); 34 | ft_printf("Finish Drawing\n"); 35 | if (is_save) 36 | save_bmp(img, "ch8.rt"); 37 | else 38 | mlx_show(img, "Diffuse"); 39 | free_hitlst(hitlst); 40 | free_camera(cam); 41 | free_img_data(img); 42 | } 43 | -------------------------------------------------------------------------------- /chapter/ch9.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ch9.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 15:23:30 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/21 18:57:02 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | void show_sphere_material(int is_save) 16 | { 17 | double aspect_ratio; 18 | t_img_data *img; 19 | t_camera *cam; 20 | t_sphere *s; 21 | t_list *hitlst; 22 | t_material *mat; 23 | 24 | aspect_ratio = 16.0 / 9.0; 25 | img = create_img_data(400, (int)(400 / aspect_ratio)); 26 | cam = camera_new(aspect_ratio); 27 | cam->data = img; 28 | hitlst = hitlst_new(); 29 | s = init_sphere(vec_create(0, 0, -1), 0.5); 30 | mat = mat_lambertian_new(vec_create(0.7, 0.3, 0.3)); 31 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 32 | free_material(mat); 33 | s = init_sphere(vec_create(0, -100.5, -1), 100); 34 | mat = mat_lambertian_new(vec_create(0.8, 0.8, 0)); 35 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 36 | free_material(mat); 37 | s = init_sphere(vec_create(-1, 0, -1), 0.5); 38 | mat = mat_metal_new(vec_create(0.8, 0.8, 0.8), 0.3); 39 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 40 | free_material(mat); 41 | s = init_sphere(vec_create(1, 0, -1), 0.5); 42 | mat = mat_metal_new(vec_create(0.8, 0.6, 0.2), 1); 43 | hitlst_mat_add(hitlst, (void *)s, OBJ_SPHERE, mat); 44 | free_material(mat); 45 | ft_printf("Drawing start\n"); 46 | draw_hittable_material(cam, hitlst); 47 | ft_printf("Finish Drawing\n"); 48 | if (is_save) 49 | save_bmp(img, "ch9.rt"); 50 | else 51 | mlx_show(img, "Material"); 52 | free_hitlst(hitlst); 53 | free_camera(cam); 54 | free_img_data(img); 55 | } 56 | -------------------------------------------------------------------------------- /includes/chapter.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* chapter.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/28 14:24:52 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/22 22:27:57 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef CHAPTER_H 14 | # define CHAPTER_H 15 | 16 | void show_gradation(int is_save); 17 | void show_sky(int is_save); 18 | void show_s_sphere(int is_save); 19 | void show_normal_sphere(int is_save); 20 | void show_normal_sphere_anti(int is_save); 21 | void show_normal_sphere_anti_dif(int is_save); 22 | void show_sphere_material(int is_save); 23 | void show_sphere_camera(int is_save); 24 | void show_final(int is_save); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /includes/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/25 11:39:22 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/04 14:30:37 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | 21 | # define BUFFER_SIZE 1024 22 | # define FD_SIZE 1024 23 | # define READ 1 24 | # define READ_EOF 0 25 | # define ERROR -1 26 | 27 | void *ft_memset(void *b, int c, size_t len); 28 | void ft_bzero(void *s, size_t n); 29 | void *ft_memcpy(void *dst, const void *src, size_t len); 30 | void *ft_memccpy(void *dst, const void *src, int c, size_t n); 31 | void *ft_memmove(void *dst, const void *src, size_t len); 32 | void *ft_memchr(const void *s, int c, size_t n); 33 | int ft_memcmp(const void *s1, const void *s2, size_t n); 34 | size_t ft_strlen(const char *s); 35 | int ft_isalpha(int c); 36 | int ft_isdigit(int c); 37 | int ft_isalnum(int c); 38 | int ft_isascii(int c); 39 | int ft_isprint(int c); 40 | int ft_toupper(int c); 41 | int ft_tolower(int c); 42 | char *ft_strchr(const char *s, int c); 43 | char *ft_strrchr(const char *s, int c); 44 | int ft_strncmp(const char *s1, const char *s2, size_t n); 45 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); 46 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize); 47 | char *ft_strnstr(const char *heystack, const char *needle, 48 | size_t len); 49 | int ft_atoi(const char *str); 50 | void *ft_calloc(size_t count, size_t size); 51 | char *ft_strdup(const char *s1); 52 | char *ft_substr(char const *s, unsigned int start, size_t len); 53 | char *ft_strjoin(char const *s1, char const *s2); 54 | char *ft_strtrim(char const *s1, char const *set); 55 | char **ft_split(char const *s, char c); 56 | char *ft_itoa(int n); 57 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 58 | void ft_putchar_fd(char c, int fd); 59 | void ft_putstr_fd(char *s, int fd); 60 | void ft_putendl_fd(char *s, int fd); 61 | void ft_putnbr_fd(int n, int fd); 62 | 63 | typedef struct s_list 64 | { 65 | void *content; 66 | struct s_list *next; 67 | } t_list; 68 | 69 | t_list *ft_lstnew(void *content); 70 | void ft_lstadd_front(t_list **lst, t_list *new); 71 | int ft_lstsize(t_list *lst); 72 | t_list *ft_lstlast(t_list *lst); 73 | void ft_lstadd_back(t_list **lst, t_list *new); 74 | void ft_lstdelone(t_list *lst, void (*del)(void *)); 75 | void ft_lstclear(t_list **lst, void (*del)(void *)); 76 | void ft_lstiter(t_list *lst, void (*f)(void *)); 77 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), 78 | void (*del)(void *)); 79 | 80 | int ft_isupper(int c); 81 | int ft_islower(int c); 82 | int ft_isnumber(int c); 83 | int ft_isblank(int c); 84 | char *ft_strpush(char *s, int push); 85 | 86 | char *ft_strndup(const char *s1, size_t n); 87 | char *ft_strnul(void); 88 | int get_next_line(int fd, char **line); 89 | 90 | int ft_printf(const char *format, ...); 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /includes/minirt.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minirt.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/07/11 21:12:59 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/19 20:15:05 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MINIRT_H 14 | # define MINIRT_H 15 | 16 | # include "mlx.h" 17 | # include "minirt_struct.h" 18 | # include "minirt_material.h" 19 | # include "minirt_const.h" 20 | # include 21 | # include 22 | 23 | int get_color_val(t_vec *color); 24 | int get_t(int trgb); 25 | t_vec *get_color(int trgb); 26 | 27 | t_img_data *create_img_data(int width, int height); 28 | void free_img_data(t_img_data *data); 29 | 30 | void mlx_draw_by_img_data(t_mlx_data *mlx_data, t_img_data *img_data); 31 | int mlx_key_handle(int keycode); 32 | void mlx_show(t_img_data *data, char *title); 33 | 34 | # define BMP_HEADER_SIZE 122 35 | void save_bmp(t_img_data *data, char *filename); 36 | 37 | double clamp(double x, double min, double max); 38 | double get_radian(double degree); 39 | 40 | void draw_gradaition(t_img_data *data); 41 | void draw_vertical_line(t_img_data *data, int x); 42 | void draw_horizontal_line(t_img_data *data, int y); 43 | 44 | typedef struct s_sky_info 45 | { 46 | double viewport_width; 47 | double viewport_height; 48 | double focal_length; 49 | } t_sky_info; 50 | 51 | 52 | typedef struct s_sky 53 | { 54 | t_img_data *data; 55 | t_vec *horizontal; 56 | t_vec *vertical; 57 | t_vec *lower_left_corner; 58 | t_vec *origin; 59 | } t_sky; 60 | 61 | t_sky_info *init_sky_info(double viewport_width, double viewport_height, 62 | double focal_length); 63 | t_sky *init_sky(t_sky_info *info, t_vec *origin); 64 | int cal_sky_color(t_ray *r); 65 | void free_sky(t_sky *my_sky, int is_origin_free); 66 | void draw_sky(t_img_data *data, t_sky_info *info); 67 | 68 | t_ray *cal_sky_ray(int x, int y, t_sky *my_sky); 69 | 70 | typedef struct s_sphere 71 | { 72 | t_vec *center; 73 | double radius; 74 | } t_sphere; 75 | 76 | t_sphere *init_sphere(t_vec *center, double radius); 77 | void free_sphere(t_sphere *s); 78 | void draw_sphere(t_img_data *data, t_sky_info *info, t_sphere *s); 79 | 80 | void draw_s_sphere(t_img_data *data, t_sky_info *info, t_sphere *s); 81 | 82 | int sphere_hit(void *s, t_ray *r, t_hitlst_info *info, 83 | t_hit_record *rec); 84 | 85 | void draw_hittable(t_camera *cam, t_list *lst); 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /includes/minirt_bonus.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minirt_bonus.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/15 12:42:18 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/25 12:03:25 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MINIRT_BONUS_H 14 | # define MINIRT_BONUS_H 15 | 16 | # include "minirt.h" 17 | # include 18 | 19 | double random_double(void); 20 | double random_double_range(double min, double max); 21 | 22 | t_vec *vec_random_range(double min, double max); 23 | t_vec *vec_random_in_unit_sphere(void); 24 | t_vec *vec_random_unit(void); 25 | t_vec *vec_random_in_hemisphere(t_vec *normal); 26 | 27 | int get_color_sample_gamma(t_vec *color); 28 | 29 | t_hitlst_info *get_hitlst_by_locate(int x, int y, t_camera *cam); 30 | void draw_hittable_anti(t_camera *cam, t_list *lst); 31 | 32 | void draw_hittable_diffuse_anti(t_camera *cam, t_list *lst); 33 | 34 | t_vec *get_sky_color_t(double t); 35 | t_vec *get_recur_mat_color(t_list *lst, t_hitlst_info **info, 36 | int depth, int *is_free); 37 | void get_hittable_material_color(t_list *lst, 38 | t_hitlst_info *info, 39 | t_vec *color); 40 | void draw_hittable_material(t_camera *cam, t_list *lst); 41 | 42 | void draw_hittable_pthread(t_camera *cam, t_list *lst); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /includes/minirt_const.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minirt_const.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/08 16:11:02 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/23 14:10:42 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MINIRT_CONST_H 14 | # define MINIRT_CONST_H 15 | 16 | # define TRUE 1 17 | # define FALSE 0 18 | 19 | # define MINI_PI 3.1415926535897932385 20 | 21 | # define OBJ_SPHERE 1 22 | 23 | # define MAT_LAMBERTIAN 1 24 | # define MAT_METAL 2 25 | 26 | # define HIT_T_MIN 0.001 27 | # define ANTI_SAMPLES 50 28 | # define REFLECT_DEPTH 5 29 | # define PTHREAD_CNT 16 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /includes/minirt_material.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minirt_material.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/18 21:08:27 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/19 15:46:29 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MINIRT_MATERIAL_H 14 | # define MINIRT_MATERIAL_H 15 | 16 | #include "minirt_struct.h" 17 | 18 | t_material *mat_lambertian_new(t_vec *color); 19 | int lambertian_scatter(t_material *mat, t_ray *ray_in, 20 | t_hit_record *rec, t_material_info *info); 21 | t_material *mat_metal_new(t_vec *color, double fuzz); 22 | int metal_scatter(t_material *mat, t_ray *ray_in, t_hit_record *rec, 23 | t_material_info *info); 24 | 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: sunpark +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2020/02/25 11:37:14 by sunpark #+# #+# # 9 | # Updated: 2020/07/04 14:52:12 by sunpark ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | SRCS = ft_memset.c ft_bzero.c ft_memcpy.c ft_memccpy.c ft_memmove.c \ 14 | ft_memchr.c ft_memcmp.c ft_strlen.c ft_isalpha.c ft_isdigit.c \ 15 | ft_isalnum.c ft_isascii.c ft_isprint.c ft_toupper.c ft_tolower.c \ 16 | ft_strchr.c ft_strrchr.c ft_strncmp.c ft_strlcpy.c ft_strlcat.c \ 17 | ft_strnstr.c ft_atoi.c ft_calloc.c ft_strdup.c ft_substr.c \ 18 | ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c ft_strmapi.c \ 19 | ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c 20 | OBJS = $(SRCS:.c=.o) 21 | 22 | BSRCS = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c \ 23 | ft_lstadd_back.c ft_lstdelone.c ft_lstclear.c ft_lstiter.c \ 24 | ft_lstmap.c \ 25 | ft_isupper_bonus.c ft_islower_bonus.c ft_isnumber_bonus.c \ 26 | ft_isblank_bonus.c ft_strndup_bonus.c ft_strnul_bonus.c \ 27 | ft_strpush_bonus.c get_next_line_bonus.c 28 | BOBJS = $(BSRCS:.c=.o) 29 | 30 | PSRCDIR = ./ft_printf/ 31 | PSRC = ft_printf percent print_ascii print_element print_hex \ 32 | print_num print_pointer take_percentage 33 | PSUF = _bonus.c 34 | PSRCS = $(addprefix $(PSRCDIR), $(addsuffix $(PSUF), $(PSRC))) 35 | POBJS = $(PSRCS:.c=.o) 36 | 37 | NAME = libft.a 38 | 39 | GCC = gcc 40 | GCCFLAG = -Wall -Wextra -Werror -std=c99 41 | RM = rm -f 42 | 43 | .c.o: 44 | $(GCC) $(GCCFLAG) -c $< -o $(<:.c=.o) 45 | 46 | $(NAME): $(OBJS) 47 | ar rc $(NAME) $(OBJS) 48 | 49 | all: $(NAME) 50 | 51 | bonus: $(OBJS) $(BOBJS) $(POBJS) 52 | ar rc $(NAME) $(BOBJS) $(OBJS) $(POBJS) 53 | 54 | clean: 55 | $(RM) $(OBJS) $(BOBJS) $(POBJS) 56 | 57 | fclean: clean 58 | $(RM) $(NAME) $(TSTNAME) 59 | 60 | re: fclean all 61 | 62 | reclean: fclean all clean 63 | 64 | .PHONY: all clean fclean re reclean .c.o 65 | -------------------------------------------------------------------------------- /libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 19:14:43 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:12:29 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int isblank(char a) 16 | { 17 | return (a == '\t' || a == '\v' || a == '\f' || \ 18 | a == '\r' || a == '\n' || a == ' '); 19 | } 20 | 21 | int ft_atoi(const char *str) 22 | { 23 | unsigned long long ans; 24 | int minus; 25 | 26 | minus = 1; 27 | ans = 0; 28 | while (isblank(*str)) 29 | str++; 30 | if (*str == '-' || *str == '+') 31 | { 32 | minus = ((*str == '-') ? -1 : 1); 33 | str++; 34 | } 35 | while (*str) 36 | { 37 | if (ft_isdigit(*str) == 0) 38 | break ; 39 | ans *= 10; 40 | ans += (*str - '0'); 41 | str++; 42 | } 43 | return ((int)(ans * minus)); 44 | } 45 | -------------------------------------------------------------------------------- /libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/25 13:56:08 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/25 13:59:01 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | ft_memset(s, 0, n); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 20:15:29 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/27 20:22:58 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | void *ft_calloc(size_t count, size_t size) 17 | { 18 | void *re; 19 | unsigned char *put; 20 | size_t locate; 21 | 22 | re = malloc(count * size); 23 | if (re) 24 | { 25 | locate = -1; 26 | put = (unsigned char *)re; 27 | while (++locate < count * size) 28 | *(put++) = 0; 29 | } 30 | return (re); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 22:34:09 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/26 22:35:33 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | return (ft_isalpha(c) || ft_isdigit(c)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 21:59:26 by sunpark #+# #+# */ 9 | /* Updated: 2020/03/01 18:15:34 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | return (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 22:37:52 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/26 22:39:38 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | return (0 <= c && c <= 127); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isblank_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isblank_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 17:48:03 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/09 10:37:17 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isblank(int c) 16 | { 17 | return (c == ' ' || c == '\t'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 22:33:03 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/26 22:33:51 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | return ('0' <= c && c <= '9'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_islower_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_islower_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 21:55:12 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/09 10:37:23 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_islower(int c) 16 | { 17 | return ('a' <= c && c <= 'z'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isnumber_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isnumber_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 17:46:02 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/09 10:37:26 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isnumber(int c) 16 | { 17 | return (ft_isdigit(c)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 22:39:49 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/26 22:41:07 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | return (' ' <= c && c <= '~'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isupper_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isupper_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 20:58:42 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/09 10:37:31 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isupper(int c) 16 | { 17 | return ('A' <= c && c <= 'Z'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/28 22:28:15 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/28 22:46:49 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | static int get_len(long long n) 17 | { 18 | int result; 19 | 20 | if (!n) 21 | return (1); 22 | result = ((n < 0) ? 1 : 0); 23 | if (n < 0) 24 | n *= -1; 25 | while (n) 26 | { 27 | result++; 28 | n /= 10; 29 | } 30 | return (result); 31 | } 32 | 33 | char *ft_itoa(int n) 34 | { 35 | char *result; 36 | long long nn; 37 | char *put; 38 | int locate; 39 | 40 | result = (char *)malloc(get_len(n) + 1); 41 | if (result) 42 | { 43 | put = result; 44 | nn = n; 45 | if (nn < 0) 46 | { 47 | *(put++) = '-'; 48 | nn *= -1; 49 | } 50 | locate = 0; 51 | n = get_len(nn); 52 | while (!locate || nn) 53 | { 54 | put[n - 1 - (locate++)] = (nn % 10 + '0'); 55 | nn /= 10; 56 | } 57 | put[n] = '\0'; 58 | } 59 | return (result); 60 | } 61 | -------------------------------------------------------------------------------- /libft/ft_lstadd_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 15:57:38 by sunpark #+# #+# */ 9 | /* Updated: 2020/03/01 16:10:01 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_back(t_list **lst, t_list *new) 16 | { 17 | t_list *now; 18 | 19 | if (*lst == NULL) 20 | { 21 | *lst = new; 22 | return ; 23 | } 24 | now = *lst; 25 | while (now->next) 26 | now = now->next; 27 | now->next = new; 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_lstadd_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 15:24:50 by sunpark #+# #+# */ 9 | /* Updated: 2020/03/01 15:41:51 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **lst, t_list *new) 16 | { 17 | if (!new || !lst) 18 | return ; 19 | if (*lst) 20 | new->next = *lst; 21 | else 22 | new->next = NULL; 23 | *lst = new; 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_lstclear.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 16:13:19 by sunpark #+# #+# */ 9 | /* Updated: 2020/03/01 16:23:00 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | void ft_lstclear(t_list **lst, void (*del)(void *)) 17 | { 18 | t_list *now; 19 | t_list *next; 20 | 21 | now = *lst; 22 | while (now) 23 | { 24 | next = now->next; 25 | ft_lstdelone(now, del); 26 | now = next; 27 | } 28 | *lst = NULL; 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 16:10:31 by sunpark #+# #+# */ 9 | /* Updated: 2020/03/01 16:12:44 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 17 | { 18 | if (!lst) 19 | return ; 20 | (*del)(lst->content); 21 | free(lst); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 16:23:43 by sunpark #+# #+# */ 9 | /* Updated: 2020/03/01 16:25:28 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(void *)) 16 | { 17 | while (lst) 18 | { 19 | (*f)(lst->content); 20 | lst = lst->next; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_lstlast.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 15:48:40 by sunpark #+# #+# */ 9 | /* Updated: 2020/03/01 15:56:15 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list *lst) 16 | { 17 | if (!lst) 18 | return (NULL); 19 | if (!(lst->next)) 20 | return (lst); 21 | return (ft_lstlast(lst->next)); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 16:26:05 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:13:24 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 17 | { 18 | t_list *result; 19 | t_list *after; 20 | t_list *now; 21 | 22 | result = NULL; 23 | now = lst; 24 | while (now) 25 | { 26 | after = ft_lstnew((*f)(now->content)); 27 | if (!after) 28 | break ; 29 | ft_lstadd_back(&result, after); 30 | now = now->next; 31 | } 32 | if (ft_lstsize(lst) != ft_lstsize(result)) 33 | ft_lstclear(&result, del); 34 | return (result); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 15:20:37 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:15:08 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | t_list *ft_lstnew(void *content) 17 | { 18 | t_list *result; 19 | 20 | result = (t_list *)malloc(sizeof(t_list)); 21 | if (result) 22 | { 23 | result->content = content; 24 | result->next = NULL; 25 | } 26 | return (result); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_lstsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/01 15:46:09 by sunpark #+# #+# */ 9 | /* Updated: 2020/03/01 15:47:48 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstsize(t_list *lst) 16 | { 17 | return ((lst) ? 1 + ft_lstsize(lst->next) : 0); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 13:31:19 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:13:58 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 16 | { 17 | size_t locate; 18 | 19 | if (!dst && !src) 20 | return (dst); 21 | locate = 0; 22 | while (locate < n) 23 | { 24 | ((unsigned char *)dst)[locate] = ((unsigned char *)src)[locate]; 25 | if (((unsigned char *)dst)[locate] == (unsigned char)c) 26 | break ; 27 | locate++; 28 | } 29 | if (locate == n) 30 | return ((void *)0); 31 | return (dst + locate + 1); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 19:56:06 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:14:17 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | size_t locate; 18 | void *find; 19 | 20 | locate = -1; 21 | find = (void *)s; 22 | while (++locate < n) 23 | { 24 | if (*((unsigned char *)find) == (unsigned char)c) 25 | break ; 26 | find++; 27 | } 28 | if (locate == n) 29 | return (NULL); 30 | return (find); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 20:39:22 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:15:39 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | size_t locate; 18 | 19 | if (!n) 20 | return (0); 21 | locate = -1; 22 | while (++locate < n) 23 | { 24 | if (((unsigned char *)s1)[locate] != ((unsigned char *)s2)[locate]) 25 | break ; 26 | } 27 | if (locate == n) 28 | return (0); 29 | return (((unsigned char *)s1)[locate] - ((unsigned char *)s2)[locate]); 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/25 15:24:18 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/26 14:47:42 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t len) 16 | { 17 | unsigned char *d; 18 | 19 | if (!dst && !src) 20 | return (dst); 21 | d = (unsigned char *)dst; 22 | while (len--) 23 | { 24 | *d = *((unsigned char *)src); 25 | d++; 26 | src++; 27 | } 28 | return (dst); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 16:48:48 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/26 19:25:59 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | size_t locate; 18 | 19 | if (!dst && !src) 20 | return (dst); 21 | if (dst > src) 22 | { 23 | locate = len; 24 | while (--locate < len) 25 | ((unsigned char *)dst)[locate] = ((unsigned char *)src)[locate]; 26 | } 27 | else 28 | ft_memcpy(dst, src, len); 29 | (void)locate; 30 | return (dst); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/25 12:22:06 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/25 13:46:47 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | unsigned char *now; 18 | 19 | now = (unsigned char *)b; 20 | while (len-- > 0) 21 | *(now++) = (unsigned char)c; 22 | return (b); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_printf/ft_printf_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/04 15:48:45 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/03 21:29:51 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf_bonus.h" 14 | 15 | void put_printed_num(va_list args_list, int printed) 16 | { 17 | int *save; 18 | 19 | save = (int *)va_arg(args_list, int *); 20 | *save = printed; 21 | } 22 | 23 | int ft_printf(const char *format, ...) 24 | { 25 | va_list args_list; 26 | int printed; 27 | 28 | va_start(args_list, format); 29 | printed = 0; 30 | while (*format) 31 | { 32 | if (*format == '%') 33 | { 34 | if (*(format + 1) == 'n') 35 | { 36 | put_printed_num(args_list, printed); 37 | format += 2; 38 | } 39 | else 40 | printed += print_percent(&format, args_list); 41 | } 42 | else 43 | { 44 | ft_putchar_fd(*(format++), 1); 45 | printed += 1; 46 | } 47 | } 48 | va_end(args_list); 49 | return (printed); 50 | } 51 | -------------------------------------------------------------------------------- /libft/ft_printf/ft_printf_bonus.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf_bonus.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/04 15:42:45 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/04 13:33:17 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_PRINTF_H 14 | # define FT_PRINTF_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include "../libft.h" 20 | 21 | # define TRUE 1 22 | # define FALSE 0 23 | 24 | typedef struct s_percent 25 | { 26 | int sort; 27 | int sign; 28 | int width; 29 | int precision; 30 | } t_percent; 31 | 32 | int print_percent(const char **format, va_list args_list); 33 | 34 | t_percent *create_percent(void); 35 | void set_sort(t_percent *p, char c); 36 | void set_sign(t_percent *p, char c); 37 | void clear_percent(t_percent *p); 38 | 39 | int print_sort(t_percent *p, int len); 40 | int print_char(t_percent *p, va_list ap); 41 | int print_string(t_percent *p, va_list ap); 42 | int print_real_percent(t_percent *p); 43 | int print_int(t_percent *p, va_list ap); 44 | int print_unint(t_percent *p, va_list ap); 45 | int print_pointer(t_percent *p, va_list ap); 46 | int print_hex(t_percent *p, va_list ap, int is_upper); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /libft/ft_printf/percent_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* percent_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/13 18:54:02 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/03 21:30:02 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf_bonus.h" 14 | 15 | t_percent *create_percent(void) 16 | { 17 | t_percent *result; 18 | 19 | result = (t_percent *)malloc(sizeof(t_percent)); 20 | if (result) 21 | clear_percent(result); 22 | return (result); 23 | } 24 | 25 | void set_sort(t_percent *p, char c) 26 | { 27 | if (c == '-') 28 | p->sort = 2; 29 | else if (c == '0' && p->sort != 2) 30 | p->sort = 1; 31 | } 32 | 33 | void set_sign(t_percent *p, char c) 34 | { 35 | if (c == '+') 36 | p->sign = 2; 37 | else if (c == ' ' && p->sign != 2) 38 | p->sign = 1; 39 | } 40 | 41 | void clear_percent(t_percent *p) 42 | { 43 | p->sort = 0; 44 | p->sign = 0; 45 | p->width = 0; 46 | p->precision = -1; 47 | } 48 | -------------------------------------------------------------------------------- /libft/ft_printf/print_ascii_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_ascii_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/15 00:39:26 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/03 21:30:07 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf_bonus.h" 14 | 15 | int print_real_percent(t_percent *p) 16 | { 17 | int result; 18 | 19 | result = 0; 20 | if (p->sort != 2) 21 | result += print_sort(p, 1); 22 | ft_putchar_fd('%', 1); 23 | result += 1; 24 | if (p->sort == 2) 25 | result += print_sort(p, 1); 26 | return (result); 27 | } 28 | 29 | int print_char(t_percent *p, va_list ap) 30 | { 31 | int result; 32 | char print; 33 | 34 | result = 0; 35 | if (p->sort != 2) 36 | result += print_sort(p, 1); 37 | print = (char)va_arg(ap, int); 38 | ft_putchar_fd(print, 1); 39 | result += 1; 40 | if (p->sort == 2) 41 | result += print_sort(p, 1); 42 | return (result); 43 | } 44 | 45 | int print_string(t_percent *p, va_list ap) 46 | { 47 | int result; 48 | char *print; 49 | int p_len; 50 | int cnt; 51 | 52 | result = 0; 53 | print = (char *)va_arg(ap, char *); 54 | if (print == NULL) 55 | print = "(null)"; 56 | p_len = ft_strlen(print); 57 | if (p->precision != -1) 58 | p_len = (p_len > p->precision) ? p->precision : p_len; 59 | if (p->sort != 2) 60 | result += print_sort(p, p_len); 61 | cnt = -1; 62 | while (++cnt < p_len) 63 | ft_putchar_fd(print[cnt], 1); 64 | result += p_len; 65 | if (p->sort == 2) 66 | result += print_sort(p, p_len); 67 | return (result); 68 | } 69 | -------------------------------------------------------------------------------- /libft/ft_printf/print_element_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_element_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/17 22:47:45 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/03 21:30:20 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf_bonus.h" 14 | 15 | int print_sort(t_percent *p, int len) 16 | { 17 | int print_times; 18 | 19 | if (p->width <= len) 20 | return (0); 21 | print_times = p->width - len; 22 | while (print_times--) 23 | ft_putchar_fd((p->sort == 1) ? '0' : ' ', 1); 24 | return (p->width - len); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_printf/print_hex_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_hex_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/21 20:26:48 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/04 14:52:40 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../libft.h" 14 | #include "ft_printf_bonus.h" 15 | 16 | int get_hex_len(unsigned int n) 17 | { 18 | return ((n < 16) ? 1 : 1 + get_hex_len(n / 16)); 19 | } 20 | 21 | unsigned int hex_pow(int n) 22 | { 23 | return ((n == 0) ? 1 : hex_pow(n - 1) * 16); 24 | } 25 | 26 | void print_hex_num(unsigned int num, t_percent *p, int is_upper) 27 | { 28 | int locate; 29 | int minnum; 30 | 31 | if (num == 0 && p->precision == 0) 32 | { 33 | if (p->width) 34 | ft_putchar_fd(' ', 1); 35 | return ; 36 | } 37 | locate = get_hex_len(num); 38 | while (locate--) 39 | { 40 | minnum = num / hex_pow(locate) % 16; 41 | if (minnum < 10) 42 | ft_putchar_fd('0' + minnum, 1); 43 | else 44 | ft_putchar_fd((is_upper ? 'A' : 'a') + minnum - 10, 1); 45 | } 46 | } 47 | 48 | int print_hex(t_percent *p, va_list ap, int is_upper) 49 | { 50 | unsigned int print; 51 | int result; 52 | int len; 53 | int dlen; 54 | 55 | print = va_arg(ap, unsigned int); 56 | dlen = get_hex_len(print); 57 | len = ((p->precision > dlen) ? p->precision : dlen); 58 | result = len; 59 | if (p->sort != 2) 60 | { 61 | if (p->precision != -1) 62 | p->sort = 0; 63 | result += print_sort(p, len); 64 | } 65 | while (len > dlen && (len--)) 66 | ft_putchar_fd('0', 1); 67 | print_hex_num(print, p, is_upper); 68 | len = ((p->precision > len) ? p->precision : len); 69 | if (p->sort == 2) 70 | result += print_sort(p, len); 71 | return (result - ((print || p->precision || p->width) ? 0 : 1)); 72 | } 73 | -------------------------------------------------------------------------------- /libft/ft_printf/print_num_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_num_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/18 19:40:37 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/03 21:30:46 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf_bonus.h" 14 | 15 | int get_digit_len(int n) 16 | { 17 | return ((-10 < n && n < 10) ? 1 : 1 + get_digit_len(n / 10)); 18 | } 19 | 20 | void put_unint(unsigned int n, t_percent *p) 21 | { 22 | if (n == 0 && p->precision == 0) 23 | { 24 | if (p->width) 25 | ft_putchar_fd(' ', 1); 26 | return ; 27 | } 28 | if (n >= 100000) 29 | put_unint(n / 100000, p); 30 | ft_putnbr_fd(n % 100000, 1); 31 | } 32 | 33 | int sort_front(t_percent *p, int print, int len, char plus) 34 | { 35 | if (p->sort && (print < 0 || p->sign)) 36 | ft_putchar_fd(((p->sign) ? plus : '-'), 1); 37 | if (p->precision != -1) 38 | p->sort = 0; 39 | return (print_sort(p, len + ((print < 0) ? 1 : 0))); 40 | } 41 | 42 | int print_int(t_percent *p, va_list ap) 43 | { 44 | int len; 45 | int print; 46 | int result; 47 | char plus; 48 | 49 | print = va_arg(ap, int); 50 | plus = ((p->sign == 1) ? ' ' : '+'); 51 | p->sort = (p->sort == 1 && print < 0 && p->precision >= 0) ? 0 : p->sort; 52 | result = ((print < 0) || (p->sign)) ? 1 : 0; 53 | len = get_digit_len(print); 54 | len = ((p->precision > len) ? p->precision : len); 55 | result += len; 56 | if (p->sort != 2) 57 | result += sort_front(p, print, len, plus); 58 | if (p->sort != 1 && (print < 0 || p->sign)) 59 | ft_putchar_fd(((p->sign) ? plus : '-'), 1); 60 | while (len > get_digit_len(print) && (len--)) 61 | ft_putchar_fd('0', 1); 62 | put_unint((print < 0) ? -1 * print : print, p); 63 | len = ((p->precision > len) ? p->precision : len); 64 | if (p->sort == 2) 65 | result += print_sort(p, len + ((print < 0) ? 1 : 0)); 66 | return (result - ((print || p->precision || p->width) ? 0 : 1)); 67 | } 68 | 69 | int print_unint(t_percent *p, va_list ap) 70 | { 71 | unsigned int print; 72 | int result; 73 | int dlen; 74 | int len; 75 | 76 | print = va_arg(ap, unsigned int); 77 | dlen = (print >= 1000000000) ? 10 : get_digit_len(print); 78 | len = ((p->precision > dlen) ? p->precision : dlen); 79 | result = len; 80 | if (p->sort != 2) 81 | { 82 | if (p->precision != -1) 83 | p->sort = 0; 84 | result += print_sort(p, len); 85 | } 86 | while (len > dlen && (len--)) 87 | ft_putchar_fd('0', 1); 88 | put_unint(print, p); 89 | len = ((p->precision > len) ? p->precision : len); 90 | if (p->sort == 2) 91 | result += print_sort(p, len); 92 | return (result - ((print || p->precision || p->width) ? 0 : 1)); 93 | } 94 | -------------------------------------------------------------------------------- /libft/ft_printf/print_pointer_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_pointer_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/21 22:35:33 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/03 21:31:01 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf_bonus.h" 14 | 15 | int get_long_hex_len(unsigned long long print) 16 | { 17 | return ((print < 16) ? 1 : 1 + get_long_hex_len(print / 16)); 18 | } 19 | 20 | unsigned long long long_hex_pow(int n) 21 | { 22 | return ((n == 0) ? 1 : long_hex_pow(n - 1) * 16); 23 | } 24 | 25 | void print_long_hex_num(unsigned long long print, t_percent *p) 26 | { 27 | int locate; 28 | int minnum; 29 | 30 | if (print == 0 && p->precision == 0) 31 | return ; 32 | locate = get_long_hex_len(print); 33 | while (locate--) 34 | { 35 | minnum = print / long_hex_pow(locate) % 16; 36 | if (minnum < 10) 37 | ft_putchar_fd('0' + minnum, 1); 38 | else 39 | ft_putchar_fd('a' + minnum - 10, 1); 40 | } 41 | } 42 | 43 | void set_sort_two(t_percent *p, int len, int *result) 44 | { 45 | p->sort = 0; 46 | *result += print_sort(p, len + 2); 47 | } 48 | 49 | int print_pointer(t_percent *p, va_list ap) 50 | { 51 | unsigned long long print; 52 | int result; 53 | int len; 54 | int dlen; 55 | 56 | print = va_arg(ap, unsigned long long); 57 | dlen = get_long_hex_len(print); 58 | len = ((p->precision > dlen) ? p->precision : dlen); 59 | if (!print && p->precision == 0) 60 | { 61 | len = 0; 62 | dlen = 0; 63 | } 64 | result = len + 2; 65 | if (p->sort != 2) 66 | set_sort_two(p, len, &result); 67 | ft_putstr_fd("0x", 1); 68 | while (len > dlen && (len--)) 69 | ft_putchar_fd('0', 1); 70 | print_long_hex_num(print, p); 71 | len = ((p->precision > len) ? p->precision : len); 72 | if (p->sort == 2) 73 | result += print_sort(p, len + 2); 74 | return (result); 75 | } 76 | -------------------------------------------------------------------------------- /libft/ft_printf/take_percentage_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* take_percentage_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/04 16:43:20 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/03 21:31:13 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf_bonus.h" 14 | 15 | int get_char_num(char c) 16 | { 17 | if (c == '-' || c == '0') 18 | return (1); 19 | if (c == ' ' || c == '+') 20 | return (2); 21 | if (c == '*' || ft_isdigit(c)) 22 | return (3); 23 | if (c == '.') 24 | return (4); 25 | if (c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i' || \ 26 | c == 'u' || c == 'x' || c == 'X' || c == '%') 27 | return (5); 28 | return (0); 29 | } 30 | 31 | int get_p_num(const char **format, va_list args_list) 32 | { 33 | char *numstr; 34 | const char *first; 35 | size_t len; 36 | int result; 37 | 38 | if (**format == '*') 39 | { 40 | result = va_arg(args_list, int); 41 | ++(*format); 42 | return (result); 43 | } 44 | len = 0; 45 | while (**format == '0') 46 | ++(*format); 47 | first = *format; 48 | while (ft_isdigit(**format)) 49 | { 50 | len++; 51 | ++(*format); 52 | } 53 | numstr = ft_strndup(first, len); 54 | result = ft_atoi(numstr); 55 | free(numstr); 56 | return (result); 57 | } 58 | 59 | void assign_p(t_percent *now, const char **format, va_list args_list) 60 | { 61 | if (get_char_num(**format) == 1 || get_char_num(**format) == 2) 62 | { 63 | (get_char_num(**format) == 1 ? \ 64 | set_sort(now, **format) : set_sign(now, **format)); 65 | (*format)++; 66 | } 67 | else if (get_char_num(**format) == 3) 68 | { 69 | now->width = get_p_num(format, args_list); 70 | if (now->width < 0) 71 | { 72 | now->width *= -1; 73 | set_sort(now, '-'); 74 | } 75 | } 76 | else 77 | { 78 | ++(*format); 79 | if (get_char_num(**format) == 3 || **format == '0') 80 | now->precision = get_p_num(format, args_list); 81 | else 82 | now->precision = 0; 83 | if (now->precision < 0) 84 | now->precision = -1; 85 | } 86 | } 87 | 88 | int print_p(t_percent *now, const char **format, va_list args_list) 89 | { 90 | int result; 91 | 92 | result = 0; 93 | if (**format == 'c') 94 | result = print_char(now, args_list); 95 | else if (**format == 's') 96 | result = print_string(now, args_list); 97 | else if (**format == '%') 98 | result = print_real_percent(now); 99 | else if (**format == 'd' || **format == 'i') 100 | result = print_int(now, args_list); 101 | else if (**format == 'u') 102 | result = print_unint(now, args_list); 103 | else if (**format == 'x' || **format == 'X') 104 | result = print_hex(now, args_list, (**format == 'X' ? TRUE : FALSE)); 105 | else 106 | result = print_pointer(now, args_list); 107 | (*format)++; 108 | return (result); 109 | } 110 | 111 | int print_percent(const char **format, va_list args_list) 112 | { 113 | int status; 114 | int printed; 115 | t_percent *now; 116 | 117 | printed = 0; 118 | status = 1; 119 | now = create_percent(); 120 | (*format)++; 121 | while (**format) 122 | { 123 | if (!get_char_num(**format)) 124 | break ; 125 | if (status > 2 && status > get_char_num(**format)) 126 | clear_percent(now); 127 | status = get_char_num(**format); 128 | if (status == 5) 129 | { 130 | printed = print_p(now, format, args_list); 131 | break ; 132 | } 133 | else 134 | assign_p(now, format, args_list); 135 | } 136 | free(now); 137 | return (printed); 138 | } 139 | -------------------------------------------------------------------------------- /libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/28 23:01:21 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/28 23:02:37 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | void ft_putchar_fd(char c, int fd) 17 | { 18 | write(fd, &c, 1); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/28 23:06:57 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/28 23:08:25 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | void ft_putendl_fd(char *s, int fd) 17 | { 18 | if (s) 19 | { 20 | ft_putstr_fd(s, fd); 21 | write(fd, "\n", 1); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/28 23:11:01 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/02 22:28:36 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | long long nn; 18 | int locate; 19 | char number[15]; 20 | 21 | if (n == 0) 22 | { 23 | write(fd, "0", 1); 24 | return ; 25 | } 26 | if (n < 0) 27 | write(fd, "-", 1); 28 | nn = n; 29 | if (nn < 0) 30 | nn *= -1; 31 | locate = 0; 32 | while (nn) 33 | { 34 | number[locate++] = nn % 10 + '0'; 35 | nn /= 10; 36 | } 37 | while (--locate >= 0) 38 | write(fd, number + locate, 1); 39 | } 40 | -------------------------------------------------------------------------------- /libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/28 23:06:01 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/28 23:08:35 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | void ft_putstr_fd(char *s, int fd) 17 | { 18 | if (s) 19 | write(fd, s, ft_strlen(s)); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/28 21:57:59 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/13 13:29:48 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | static int count_words(char const *s, char c) 17 | { 18 | int result; 19 | int is_word; 20 | 21 | result = 0; 22 | is_word = 0; 23 | while (*s) 24 | { 25 | if (is_word == 0 && *s != c) 26 | is_word = 1; 27 | if (is_word && *s == c) 28 | { 29 | result++; 30 | is_word = 0; 31 | } 32 | s++; 33 | } 34 | if (is_word) 35 | result++; 36 | return (result); 37 | } 38 | 39 | static int free_strs(char **sep, int word_locate) 40 | { 41 | int locate; 42 | 43 | locate = -1; 44 | while (++locate < word_locate - 1) 45 | free(sep[locate]); 46 | return (1); 47 | } 48 | 49 | static int get_sep_word(char **sep, char const *s, char c) 50 | { 51 | int word_size; 52 | int word_locate; 53 | char *start; 54 | 55 | word_size = 0; 56 | word_locate = 0; 57 | while (*s) 58 | { 59 | if (word_size == 0 && *s != c) 60 | start = (char *)s; 61 | if (word_size && *s == c) 62 | { 63 | sep[word_locate] = ft_substr(start, 0, word_size); 64 | if (sep[word_locate++] == NULL) 65 | return (free_strs(sep, word_locate)); 66 | word_size = 0; 67 | } 68 | if (*s != c) 69 | word_size++; 70 | s++; 71 | } 72 | if (word_size) 73 | sep[word_locate++] = ft_substr(start, 0, word_size); 74 | sep[word_locate] = NULL; 75 | return (0); 76 | } 77 | 78 | char **ft_split(char const *s, char c) 79 | { 80 | int words; 81 | char **result; 82 | 83 | if (!s) 84 | return (NULL); 85 | words = count_words(s, c); 86 | result = (char **)malloc(sizeof(char *) * (words + 1)); 87 | if (result && get_sep_word(result, s, c)) 88 | { 89 | free(result); 90 | result = NULL; 91 | } 92 | return (result); 93 | } 94 | -------------------------------------------------------------------------------- /libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 16:52:18 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:16:11 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | int is_end; 18 | char *now; 19 | 20 | is_end = 0; 21 | now = (char *)s; 22 | while (1) 23 | { 24 | if (*now == c) 25 | break ; 26 | if (*now == '\0') 27 | { 28 | is_end = 1; 29 | break ; 30 | } 31 | now++; 32 | } 33 | return ((is_end) ? NULL : now); 34 | } 35 | -------------------------------------------------------------------------------- /libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 20:24:33 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/27 20:29:24 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | char *ft_strdup(const char *s1) 17 | { 18 | char *re; 19 | size_t locate; 20 | 21 | re = (char *)malloc(ft_strlen(s1) + 1); 22 | if (re) 23 | { 24 | locate = -1; 25 | while (++locate < ft_strlen(s1)) 26 | re[locate] = s1[locate]; 27 | re[locate] = '\0'; 28 | } 29 | return (re); 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 21:36:12 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/27 21:45:36 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | char *ft_strjoin(char const *s1, char const *s2) 17 | { 18 | char *result; 19 | int locate; 20 | 21 | if (!s1 || !s2) 22 | return (NULL); 23 | result = (char *)malloc(ft_strlen(s1) + ft_strlen(s2) + 1); 24 | if (result) 25 | { 26 | locate = 0; 27 | while (*s1) 28 | { 29 | result[locate++] = *s1; 30 | s1++; 31 | } 32 | while (*s2) 33 | { 34 | result[locate++] = *s2; 35 | s2++; 36 | } 37 | result[locate] = '\0'; 38 | } 39 | return (result); 40 | } 41 | -------------------------------------------------------------------------------- /libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 18:55:54 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:17:35 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t strsize[2]; 18 | size_t cat_lo; 19 | 20 | strsize[0] = ft_strlen(dst); 21 | strsize[1] = ft_strlen(src); 22 | if (strsize[0] + 1 < dstsize) 23 | { 24 | while (*dst) 25 | dst++; 26 | cat_lo = 0; 27 | while ((cat_lo < dstsize - strsize[0] - 1) && src[cat_lo]) 28 | { 29 | *(dst) = src[cat_lo]; 30 | dst++; 31 | cat_lo++; 32 | } 33 | *dst = '\0'; 34 | } 35 | if (strsize[0] > dstsize) 36 | return (strsize[1] + dstsize); 37 | return (strsize[0] + strsize[1]); 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 17:49:32 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:16:55 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t locate; 18 | 19 | if (!dst || !src) 20 | return (0); 21 | if (dstsize != 0) 22 | { 23 | locate = 0; 24 | while ((locate + 1) < dstsize && src[locate] != '\0') 25 | { 26 | dst[locate] = src[locate]; 27 | locate++; 28 | } 29 | dst[locate] = '\0'; 30 | } 31 | return (ft_strlen(src)); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 20:48:37 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/26 20:50:40 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | return ((*s) ? 1 + ft_strlen(s + 1) : 0); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/28 22:48:32 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/28 22:59:14 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 17 | { 18 | char *result; 19 | int locate; 20 | 21 | if (!s) 22 | return (NULL); 23 | result = (char *)malloc(ft_strlen(s) + 1); 24 | if (result) 25 | { 26 | locate = -1; 27 | while (++locate < (int)ft_strlen(s)) 28 | result[locate] = (*f)(locate, s[locate]); 29 | result[locate] = '\0'; 30 | } 31 | return (result); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 17:21:40 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:20:24 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | size_t locate; 18 | 19 | locate = -1; 20 | while (++locate < n && *s1 && *s2) 21 | { 22 | if (*s1 != *s2) 23 | break ; 24 | s1++; 25 | s2++; 26 | } 27 | if (locate == n) 28 | return (0); 29 | return ((unsigned char)(*s1) - (unsigned char)(*s2)); 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_strndup_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strndup_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/14 23:23:49 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/09 10:37:37 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strndup(const char *s1, size_t n) 16 | { 17 | size_t newsize; 18 | char *result; 19 | char *now; 20 | 21 | newsize = ft_strlen(s1); 22 | newsize = (newsize > n) ? n : newsize; 23 | result = (char *)malloc(newsize + 1); 24 | if (result) 25 | { 26 | now = result; 27 | while (newsize--) 28 | *(now++) = *(s1++); 29 | *now = '\0'; 30 | } 31 | return (result); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 19:01:28 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:18:33 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 16 | { 17 | size_t locate; 18 | char *find; 19 | size_t now; 20 | 21 | find = (char *)haystack; 22 | if (*needle == '\0') 23 | return (find); 24 | now = -1; 25 | while (*find && (++now < len)) 26 | { 27 | locate = 0; 28 | while (locate < ft_strlen(needle)) 29 | { 30 | if (find[locate] != needle[locate] || (locate + now) >= len) 31 | break ; 32 | locate++; 33 | } 34 | if (locate == ft_strlen(needle)) 35 | return (find); 36 | find++; 37 | } 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /libft/ft_strnul_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnul_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/28 23:30:47 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/09 10:37:41 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnul(void) 16 | { 17 | char *result; 18 | 19 | result = (char *)malloc(1); 20 | if (result) 21 | *result = '\0'; 22 | return (result); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strpush_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strpush_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/28 23:33:54 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/09 10:38:42 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | ** desc : push string by given push integer 17 | ** params : char pointer and push integer 18 | ** returns : origin char pointer 19 | ** ex) ft_strpush("hello", 2) => return "llo" 20 | */ 21 | 22 | char *ft_strpush(char *s, int push) 23 | { 24 | int locate; 25 | 26 | if (s && ((int)ft_strlen(s) > push)) 27 | { 28 | locate = 0; 29 | while (s[locate + push]) 30 | { 31 | s[locate] = s[locate + push]; 32 | locate++; 33 | } 34 | while (s[locate]) 35 | s[locate++] = '\0'; 36 | } 37 | return (s); 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 17:05:38 by sunpark #+# #+# */ 9 | /* Updated: 2020/02/27 17:16:24 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *recur(char *now, int c) 16 | { 17 | char *next; 18 | 19 | next = ft_strchr(now + 1, c); 20 | if (next == NULL) 21 | return (now); 22 | return (recur(next, c)); 23 | } 24 | 25 | char *ft_strrchr(const char *s, int c) 26 | { 27 | char *first; 28 | 29 | if (c == '\0') 30 | return (ft_strchr(s, c)); 31 | first = ft_strchr(s, c); 32 | if (first == NULL) 33 | return (NULL); 34 | return (recur(first, c)); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/28 21:40:40 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:21:16 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | static int is_set(char c, char const *set) 17 | { 18 | while (*set) 19 | { 20 | if (c == *set) 21 | return (1); 22 | set++; 23 | } 24 | return (0); 25 | } 26 | 27 | char *ft_strtrim(char const *s1, char const *set) 28 | { 29 | int start; 30 | int end; 31 | int locate; 32 | char *result; 33 | 34 | if (!s1 || !set) 35 | return (NULL); 36 | start = 0; 37 | while (start < (int)ft_strlen(s1) && is_set(s1[start], set)) 38 | start++; 39 | end = ft_strlen(s1) - 1; 40 | while (end >= 0 && is_set(s1[end], set)) 41 | end--; 42 | result = (char *)malloc(((start > end) ? 0 : end - start + 1) + 1); 43 | if (result) 44 | { 45 | locate = -1; 46 | if (start <= end) 47 | { 48 | while (++locate <= end - start) 49 | result[locate] = s1[start + locate]; 50 | } 51 | result[((locate == -1) ? 0 : locate)] = '\0'; 52 | } 53 | return (result); 54 | } 55 | -------------------------------------------------------------------------------- /libft/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/27 20:53:26 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:21:00 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | char *ft_substr(char const *s, unsigned int start, size_t len) 17 | { 18 | char *result; 19 | size_t locate; 20 | size_t totsize; 21 | 22 | if (!s) 23 | return (NULL); 24 | locate = ft_strlen(s); 25 | if (locate <= start) 26 | { 27 | result = (char *)malloc(1); 28 | *result = '\0'; 29 | return (result); 30 | } 31 | totsize = ft_strlen(s + start); 32 | totsize = (totsize > len) ? len : totsize; 33 | result = (char *)malloc(totsize + 1); 34 | if (result) 35 | { 36 | locate = -1; 37 | while (++locate < totsize) 38 | result[locate] = s[start + locate]; 39 | result[locate] = '\0'; 40 | } 41 | return (result); 42 | } 43 | -------------------------------------------------------------------------------- /libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 22:52:57 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/03 23:20:41 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if ('A' <= c && c <= 'Z') 18 | c += ('a' - 'A'); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/26 22:51:28 by sunpark #+# #+# */ 9 | /* Updated: 2020/03/01 18:16:14 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if ('a' <= c && c <= 'z') 18 | c += ('A' - 'a'); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/get_next_line_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/22 16:22:58 by sunpark #+# #+# */ 9 | /* Updated: 2020/04/17 23:05:55 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int set_new_str(char **line, char **save) 16 | { 17 | int size; 18 | int locate; 19 | int result; 20 | char *tmp_str; 21 | 22 | size = 0; 23 | while ((*save)[size] && (*save)[size] != '\n') 24 | size++; 25 | *line = (char *)malloc(size + 1); 26 | locate = 0; 27 | while (locate < size) 28 | { 29 | (*line)[locate] = (*save)[locate]; 30 | locate++; 31 | } 32 | (*line)[locate] = '\0'; 33 | result = (((*save)[size] == '\n') ? READ : READ_EOF); 34 | tmp_str = (result ? ft_strpush(*save, size + 1) : NULL); 35 | if (result == READ_EOF && *save) 36 | free(*save); 37 | *save = tmp_str; 38 | return (result); 39 | } 40 | 41 | static int error_handling(char **save) 42 | { 43 | if (*save) 44 | free(*save); 45 | *save = NULL; 46 | return (ERROR); 47 | } 48 | 49 | static int final_reset(char **line, char **save) 50 | { 51 | *line = ft_strnul(); 52 | if (*save) 53 | free(*save); 54 | *save = NULL; 55 | return (READ_EOF); 56 | } 57 | 58 | int get_next_line(int fd, char **line) 59 | { 60 | long long len; 61 | char buff[BUFFER_SIZE + 1]; 62 | static char *str[FD_SIZE]; 63 | char *tmp_str; 64 | 65 | if (fd < 0 || !line || fd >= FD_SIZE || \ 66 | BUFFER_SIZE <= 0 || read(fd, buff, 0)) 67 | return (ERROR); 68 | if (!str[fd]) 69 | str[fd] = ft_strnul(); 70 | len = 0; 71 | while (!(ft_strchr(str[fd], '\n')) && \ 72 | ((len = read(fd, buff, BUFFER_SIZE)) > 0)) 73 | { 74 | buff[len] = '\0'; 75 | tmp_str = ft_strjoin(str[fd], buff); 76 | if (str[fd]) 77 | free(str[fd]); 78 | str[fd] = tmp_str; 79 | } 80 | if (len < 0) 81 | return (error_handling(&(str[fd]))); 82 | if (len == 0 && str[fd][0] == '\0') 83 | return (final_reset(line, &(str[fd]))); 84 | return (set_new_str(line, &(str[fd]))); 85 | } 86 | -------------------------------------------------------------------------------- /libft/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/25 11:39:22 by sunpark #+# #+# */ 9 | /* Updated: 2020/07/03 21:39:09 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | 21 | # define BUFFER_SIZE 1024 22 | # define FD_SIZE 1024 23 | # define READ 1 24 | # define READ_EOF 0 25 | # define ERROR -1 26 | 27 | void *ft_memset(void *b, int c, size_t len); 28 | void ft_bzero(void *s, size_t n); 29 | void *ft_memcpy(void *dst, const void *src, size_t len); 30 | void *ft_memccpy(void *dst, const void *src, int c, size_t n); 31 | void *ft_memmove(void *dst, const void *src, size_t len); 32 | void *ft_memchr(const void *s, int c, size_t n); 33 | int ft_memcmp(const void *s1, const void *s2, size_t n); 34 | size_t ft_strlen(const char *s); 35 | int ft_isalpha(int c); 36 | int ft_isdigit(int c); 37 | int ft_isalnum(int c); 38 | int ft_isascii(int c); 39 | int ft_isprint(int c); 40 | int ft_toupper(int c); 41 | int ft_tolower(int c); 42 | char *ft_strchr(const char *s, int c); 43 | char *ft_strrchr(const char *s, int c); 44 | int ft_strncmp(const char *s1, const char *s2, size_t n); 45 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); 46 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize); 47 | char *ft_strnstr(const char *heystack, const char *needle, 48 | size_t len); 49 | int ft_atoi(const char *str); 50 | void *ft_calloc(size_t count, size_t size); 51 | char *ft_strdup(const char *s1); 52 | char *ft_substr(char const *s, unsigned int start, size_t len); 53 | char *ft_strjoin(char const *s1, char const *s2); 54 | char *ft_strtrim(char const *s1, char const *set); 55 | char **ft_split(char const *s, char c); 56 | char *ft_itoa(int n); 57 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 58 | void ft_putchar_fd(char c, int fd); 59 | void ft_putstr_fd(char *s, int fd); 60 | void ft_putendl_fd(char *s, int fd); 61 | void ft_putnbr_fd(int n, int fd); 62 | 63 | typedef struct s_list 64 | { 65 | void *content; 66 | struct s_list *next; 67 | } t_list; 68 | 69 | t_list *ft_lstnew(void *content); 70 | void ft_lstadd_front(t_list **lst, t_list *new); 71 | int ft_lstsize(t_list *lst); 72 | t_list *ft_lstlast(t_list *lst); 73 | void ft_lstadd_back(t_list **lst, t_list *new); 74 | void ft_lstdelone(t_list *lst, void (*del)(void *)); 75 | void ft_lstclear(t_list **lst, void (*del)(void *)); 76 | void ft_lstiter(t_list *lst, void (*f)(void *)); 77 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), 78 | void (*del)(void *)); 79 | 80 | int ft_isupper(int c); 81 | int ft_islower(int c); 82 | int ft_isnumber(int c); 83 | int ft_isblank(int c); 84 | char *ft_strpush(char *s, int push); 85 | 86 | char *ft_strndup(const char *s1, size_t n); 87 | char *ft_strnul(void); 88 | int get_next_line(int fd, char **line); 89 | 90 | int ft_printf(const char *format, ...); 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /mlx/Makefile: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MODULE_SRC= mlx_image.swift mlx_window.swift mlx_init.swift 5 | MODULE_OBJ=$(MODULE_SRC:.swift=.swiftmodule) 6 | 7 | SRC= interface.swift $(MODULE_SRC) 8 | OBJ=$(SRC:.swift=.o) 9 | 10 | C_SRC= mlx_xpm.c mlx_png.c mlx_string_put.c 11 | C_OBJ=$(C_SRC:.c=.o) 12 | CFLAGS=-O3 13 | 14 | NAME=libmlx.dylib 15 | 16 | INC=-I. 17 | #OPTI=-Ounchecked 18 | 19 | all: $(NAME) 20 | 21 | $(NAME): $(MODULE_OBJ) $(OBJ) $(C_OBJ) 22 | swiftc $(OPTI) $(INC) -o $(NAME) -emit-library $(OBJ) $(C_OBJ) -lz 23 | 24 | 25 | 26 | %.o: %.swift 27 | swiftc $(OPTI) $(INC) -parse-as-library -c $< -o $@ 28 | 29 | %.swiftmodule: %.swift 30 | swiftc $(OPTI) $(INC) -parse-as-library -c $< -o $@ -emit-module -module-name $(patsubst %.swift,%,$<) -module-link-name $(patsubst %.swift,%,$<) 31 | 32 | clean: 33 | rm -f $(NAME) $(OBJ) $(MODULE_OBJ) $(C_OBJ) *.swiftdoc *~ 34 | 35 | -------------------------------------------------------------------------------- /mlx/mlx_image.swift: -------------------------------------------------------------------------------- 1 | 2 | 3 | import Metal 4 | 5 | 6 | public class MlxImg 7 | { 8 | public var texture: MTLTexture 9 | /// var texture_buff: MTLBuffer 10 | 11 | public var texture_sizeline: Int 12 | public var texture_data: UnsafeMutablePointer 13 | public var texture_width: Int 14 | public var texture_height: Int 15 | 16 | public var onGPU = 0 17 | 18 | convenience public init(d device:MTLDevice, w width:Int, h height:Int) 19 | { 20 | self.init(d:device, w:width, h:height, t:0) 21 | } 22 | 23 | public init(d device:MTLDevice, w width:Int, h height:Int, t target:Int) 24 | { 25 | texture_width = width 26 | texture_height = height 27 | texture_sizeline = width * 4 28 | texture_sizeline = 256 * (texture_sizeline / 256 + (texture_sizeline%256 >= 1 ? 1 : 0) ) 29 | 30 | let textureDesc = MTLTextureDescriptor() 31 | textureDesc.width = texture_width 32 | textureDesc.height = texture_height 33 | textureDesc.usage = .shaderRead 34 | if (target == 1) 35 | { 36 | textureDesc.usage = .renderTarget 37 | textureDesc.storageMode = .private 38 | } 39 | textureDesc.pixelFormat = MTLPixelFormat.bgra8Unorm 40 | let texture_buff = device.makeBuffer(length: texture_sizeline * height)! 41 | texture = texture_buff.makeTexture(descriptor:textureDesc, offset:0, bytesPerRow:texture_sizeline)! 42 | 43 | let tmpptr = texture_buff.contents() 44 | texture_data = tmpptr.assumingMemoryBound(to:UInt32.self) 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /mlx/mlx_init.swift: -------------------------------------------------------------------------------- 1 | 2 | import Cocoa 3 | import Metal 4 | import mlx_window 5 | import mlx_image 6 | 7 | 8 | func _mlx_bridge(obj : T) -> UnsafeMutableRawPointer? { 9 | return UnsafeMutableRawPointer(Unmanaged.passUnretained(obj).toOpaque()) 10 | } 11 | 12 | func _mlx_bridge(ptr : UnsafeRawPointer) -> T { 13 | return Unmanaged.fromOpaque(ptr).takeUnretainedValue() 14 | } 15 | 16 | 17 | 18 | public class MlxMain { 19 | 20 | public var winList = [MlxWin]() 21 | public var imgList = [MlxImg]() 22 | var myMlxApp:NSApplication? 23 | public var device:MTLDevice! 24 | var loopHook:UnsafeMutableRawPointer? 25 | var loopParam:UnsafeMutableRawPointer 26 | var loopHookTimer:CFRunLoopTimer? 27 | public var inLoop = false 28 | 29 | public init(_ flag:Int = 0) 30 | { 31 | /// make app with top menubar 32 | myMlxApp = NSApplication.shared 33 | if (flag == 1) 34 | { 35 | NSApp.setActivationPolicy(NSApplication.ActivationPolicy.prohibited) /// for non clickable win, no top menu 36 | } 37 | else 38 | { 39 | NSApp.setActivationPolicy(NSApplication.ActivationPolicy.regular) 40 | } 41 | 42 | device = MTLCreateSystemDefaultDevice()! 43 | loopParam = UnsafeMutableRawPointer(&inLoop) /// dummy addr init 44 | 45 | /// Add observer anyway to flush pixels every loop. If loop_hook exists, call it. 46 | var ocontext = CFRunLoopObserverContext(version:0, info:_mlx_bridge(obj:self), retain:nil, release:nil, copyDescription:nil) 47 | let observer = CFRunLoopObserverCreate(kCFAllocatorDefault, CFRunLoopActivity.beforeWaiting.rawValue, true, 0, createOCallback(), &ocontext) 48 | CFRunLoopAddObserver(CFRunLoopGetMain(), observer, CFRunLoopMode.commonModes) 49 | 50 | } 51 | 52 | public func addWinToList(_ win:MlxWin) 53 | { winList.append(win) } 54 | public func addImgToList(_ img:MlxImg) 55 | { imgList.append(img) } 56 | 57 | 58 | func doCallLoopHook() 59 | { 60 | /// if (loopHook != nil) 61 | /// { 62 | _ = (unsafeBitCast(loopHook!,to:(@convention(c)(UnsafeRawPointer)->Void).self))(loopParam) 63 | /// } 64 | } 65 | 66 | func createOCallback() -> CFRunLoopObserverCallBack 67 | { 68 | return { (cfRunloopObserver, cfrunloopactivity, info) -> Void in 69 | let mlx:MlxMain = _mlx_bridge(ptr:info!) 70 | mlx.winList.forEach { $0.flushImages() } 71 | /// mlx.doCallLoopHook() 72 | } 73 | } 74 | 75 | func createTCallback() -> CFRunLoopTimerCallBack 76 | { 77 | return { (cfRunloopTimer, info) -> Void in 78 | let mlx:MlxMain = _mlx_bridge(ptr:info!) 79 | mlx.doCallLoopHook() 80 | } 81 | } 82 | 83 | public func addLoopHook(_ f:UnsafeMutableRawPointer?, _ p:UnsafeMutableRawPointer) 84 | { 85 | var tcontext = CFRunLoopTimerContext(version:0, info:_mlx_bridge(obj:self), retain:nil, release:nil, copyDescription:nil) 86 | if (loopHook != nil) 87 | { 88 | CFRunLoopTimerInvalidate(loopHookTimer) 89 | } 90 | 91 | loopHook = f 92 | loopParam = p 93 | if (loopHook != nil) 94 | { 95 | loopHookTimer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0.0, 0.0001, 0, 0, createTCallback(), &tcontext) 96 | CFRunLoopAddTimer(CFRunLoopGetMain(), loopHookTimer, CFRunLoopMode.commonModes) 97 | } 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /mlx/mlx_string_put.c: -------------------------------------------------------------------------------- 1 | 2 | #include "mlx.h" 3 | 4 | #include "font.c" 5 | 6 | #define ATLAS_NB_CHAR 95 7 | 8 | #define FONT_WIDTH ((font_atlas.width/(ATLAS_NB_CHAR))-2) 9 | 10 | 11 | int mlx_put_image_to_window_scale(void *mlx_ptr, void *win_ptr, void *img_ptr, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, unsigned int color); 12 | 13 | void mlx_int_fill(unsigned char *data, int sl) 14 | { 15 | int i, j; 16 | j = 0; 17 | while (j < font_atlas.height) 18 | { 19 | i = 0; 20 | while (i < font_atlas.width) 21 | { 22 | data[j*sl+i*4] = font_atlas.pixel_data[j*font_atlas.width*font_atlas.bytes_per_pixel+i*4+2]; 23 | data[j*sl+i*4+1] = font_atlas.pixel_data[j*font_atlas.width*font_atlas.bytes_per_pixel+i*4+1]; 24 | data[j*sl+i*4+2] = font_atlas.pixel_data[j*font_atlas.width*font_atlas.bytes_per_pixel+i*4]; 25 | data[j*sl+i*4+3] = 0xFF - font_atlas.pixel_data[j*font_atlas.width*font_atlas.bytes_per_pixel+i*4+3]; 26 | i ++; 27 | } 28 | j ++; 29 | } 30 | 31 | } 32 | 33 | int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color, char *string) 34 | { 35 | static void *font = (void *)0; 36 | static unsigned char *data = (void *)0; 37 | static int size_line = 0; 38 | int bpp; 39 | int endian; 40 | int pos; 41 | int val; 42 | int dest_w; 43 | int dest_h; 44 | 45 | if (font == (void *)0) 46 | { 47 | font = mlx_new_image(mlx_ptr, font_atlas.width, font_atlas.height); 48 | data = (unsigned char *)mlx_get_data_addr(font, &bpp, &size_line, &endian); 49 | mlx_int_fill(data, size_line); 50 | } 51 | 52 | color = (color&0xFFFFFF)|0xFF000000; 53 | 54 | // dest_w = (FONT_WIDTH*5)/7; /// ratio with X11 standard mlx_string_put 55 | // dest_h = (font_atlas.height*5)/7; 56 | dest_w = FONT_WIDTH; 57 | dest_h = font_atlas.height; 58 | y = y - (dest_h*3)/4; 59 | 60 | pos = 0; 61 | while (*string) 62 | { 63 | if (*string >= 32 && *string <= 127) 64 | val = *string - 32; 65 | else 66 | val = 31; 67 | mlx_put_image_to_window_scale(mlx_ptr, win_ptr, font, val*(FONT_WIDTH+2), 0, FONT_WIDTH, font_atlas.height, x+pos*dest_w, y, dest_w, dest_h, color); 68 | pos ++; 69 | string ++; 70 | } 71 | return (0); 72 | } 73 | -------------------------------------------------------------------------------- /sample/ch11.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cos18/ray-tracing-in-one-weekend-mlx/8d2c1bcc8f54fc7309a25086ee981e475875e397/sample/ch11.bmp -------------------------------------------------------------------------------- /sample/ch13.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cos18/ray-tracing-in-one-weekend-mlx/8d2c1bcc8f54fc7309a25086ee981e475875e397/sample/ch13.bmp -------------------------------------------------------------------------------- /sample/ch2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cos18/ray-tracing-in-one-weekend-mlx/8d2c1bcc8f54fc7309a25086ee981e475875e397/sample/ch2.bmp -------------------------------------------------------------------------------- /sample/ch4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cos18/ray-tracing-in-one-weekend-mlx/8d2c1bcc8f54fc7309a25086ee981e475875e397/sample/ch4.bmp -------------------------------------------------------------------------------- /sample/ch5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cos18/ray-tracing-in-one-weekend-mlx/8d2c1bcc8f54fc7309a25086ee981e475875e397/sample/ch5.bmp -------------------------------------------------------------------------------- /sample/ch6.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cos18/ray-tracing-in-one-weekend-mlx/8d2c1bcc8f54fc7309a25086ee981e475875e397/sample/ch6.bmp -------------------------------------------------------------------------------- /sample/ch7.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cos18/ray-tracing-in-one-weekend-mlx/8d2c1bcc8f54fc7309a25086ee981e475875e397/sample/ch7.bmp -------------------------------------------------------------------------------- /sample/ch8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cos18/ray-tracing-in-one-weekend-mlx/8d2c1bcc8f54fc7309a25086ee981e475875e397/sample/ch8.bmp -------------------------------------------------------------------------------- /sample/ch9.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cos18/ray-tracing-in-one-weekend-mlx/8d2c1bcc8f54fc7309a25086ee981e475875e397/sample/ch9.bmp -------------------------------------------------------------------------------- /srcs/cal_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cal_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/15 12:57:01 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/19 20:14:46 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | double clamp(double x, double min, double max) 16 | { 17 | if (x < min) 18 | return (min); 19 | if (x > max) 20 | return (max); 21 | return (x); 22 | } 23 | 24 | double get_radian(double degree) 25 | { 26 | return (degree * MINI_PI / 180.0); 27 | } 28 | -------------------------------------------------------------------------------- /srcs/color.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* color.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/28 14:06:38 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/15 20:45:16 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | int get_color_val(t_vec *color) 16 | { 17 | int x; 18 | int y; 19 | int z; 20 | 21 | x = clamp(color->x, 0.0, 0.999) * 256.0; 22 | y = clamp(color->y, 0.0, 0.999) * 256.0; 23 | z = clamp(color->z, 0.0, 0.999) * 256.0; 24 | return (x << 16 | y << 8 | z); 25 | } 26 | 27 | int get_color_sample_gamma(t_vec *color) 28 | { 29 | double scale; 30 | double x; 31 | double y; 32 | double z; 33 | 34 | scale = 1.0 / ANTI_SAMPLES; 35 | x = sqrt(color->x * scale); 36 | y = sqrt(color->y * scale); 37 | z = sqrt(color->z * scale); 38 | x = clamp(x, 0.0, 0.999) * 256.0; 39 | y = clamp(y, 0.0, 0.999) * 256.0; 40 | z = clamp(z, 0.0, 0.999) * 256.0; 41 | return ((int)x << 16 | (int)y << 8 | (int)z); 42 | } 43 | 44 | int get_t(int trgb) 45 | { 46 | return (trgb & (0xFF << 24)); 47 | } 48 | 49 | t_vec *get_color(int rgb) 50 | { 51 | t_vec *result; 52 | 53 | result = (t_vec*)malloc(sizeof(t_vec)); 54 | result->x = (rgb & (0xFF << 16)) >> 16; 55 | result->y = (rgb & (0xFF << 8)) >> 8; 56 | result->z = rgb & 0xFF; 57 | return (result); 58 | } 59 | -------------------------------------------------------------------------------- /srcs/draw_basic.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* draw_basic.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/28 14:09:02 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/15 13:06:35 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | void draw_gradaition(t_img_data *data) 16 | { 17 | int rgb; 18 | t_vec *color; 19 | double x; 20 | double y; 21 | 22 | y = -1; 23 | while ((++y) < data->height) 24 | { 25 | x = -1; 26 | while ((++x) < data->width) 27 | { 28 | color = vec_create(x / data->width, y / data->height, 1.0 / 4.0); 29 | rgb = get_color_val(color); 30 | data->img[(int)x][(int)y] = rgb; 31 | free(color); 32 | } 33 | } 34 | } 35 | 36 | void draw_vertical_line(t_img_data *data, int x) 37 | { 38 | int rgb; 39 | t_vec *color; 40 | int y; 41 | 42 | y = -1; 43 | while ((++y) < data->height) 44 | { 45 | color = vec_create(1, 1, 1); 46 | rgb = get_color_val(color); 47 | data->img[x][y] = rgb; 48 | free(color); 49 | } 50 | } 51 | 52 | void draw_horizontal_line(t_img_data *data, int y) 53 | { 54 | int rgb; 55 | t_vec *color; 56 | int x; 57 | 58 | x = -1; 59 | while ((++x) < data->height) 60 | { 61 | color = vec_create(1, 1, 1); 62 | rgb = get_color_val(color); 63 | data->img[x][y] = rgb; 64 | free(color); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /srcs/draw_hittable.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* draw_hittable.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/14 04:55:05 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/18 20:57:42 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | int cal_hittable_color(t_list *lst, t_hitlst_info *info) 16 | { 17 | t_vec *color; 18 | t_vec *tmp; 19 | int result; 20 | double t; 21 | 22 | if (hitlst_hit(lst, info)) 23 | { 24 | color = vec_add_apply(vec_create(1, 1, 1), info->rec->normal); 25 | vec_mul_const_apply(color, 0.5); 26 | } 27 | else 28 | { 29 | tmp = vec_unit(info->ray->dir); 30 | t = 0.5 * (tmp->y + 1.0); 31 | free(tmp); 32 | color = vec_mul_const_apply(vec_create(1, 1, 1), 1.0 - t); 33 | tmp = vec_mul_const_apply(vec_create(0.5, 0.7, 1), t); 34 | vec_add_apply(color, tmp); 35 | free(tmp); 36 | } 37 | result = get_color_val(color); 38 | free(color); 39 | return (result); 40 | } 41 | 42 | void draw_hittable(t_camera *cam, t_list *lst) 43 | { 44 | int x; 45 | int y; 46 | double u; 47 | double v; 48 | t_hitlst_info *lst_info; 49 | 50 | y = cam->data->height; 51 | while ((--y) >= 0) 52 | { 53 | x = -1; 54 | while ((++x) < cam->data->width) 55 | { 56 | u = (double)x / (cam->data->width - 1); 57 | v = (double)y / (cam->data->height - 1); 58 | lst_info = hitlst_info_new(camera_get_ray(cam, u, v)); 59 | cam->data->img[x][y] = cal_hittable_color(lst, lst_info); 60 | free_hitlst_info(lst_info, FALSE); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /srcs/draw_hittable_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* draw_hittable_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/14 04:55:05 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/23 13:37:00 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | t_hitlst_info *get_hitlst_by_locate(int x, int y, t_camera *cam) 16 | { 17 | double u; 18 | double v; 19 | t_ray *ray; 20 | 21 | u = ((double)x + random_double()) / (cam->data->width - 1); 22 | v = ((double)y + random_double()) / (cam->data->height - 1); 23 | ray = camera_get_ray(cam, u, v); 24 | return (hitlst_info_new(ray)); 25 | } 26 | 27 | void get_hittable_color(t_list *lst, t_hitlst_info *info, 28 | t_vec *color) 29 | { 30 | t_vec *cal_color; 31 | t_vec *tmp; 32 | double t; 33 | 34 | if (hitlst_hit(lst, info)) 35 | { 36 | cal_color = vec_add_apply(vec_create(1, 1, 1), info->rec->normal); 37 | vec_mul_const_apply(cal_color, 0.5); 38 | } 39 | else 40 | { 41 | tmp = vec_unit(info->ray->dir); 42 | t = 0.5 * (tmp->y + 1.0); 43 | free(tmp); 44 | cal_color = vec_mul_const_apply(vec_create(1, 1, 1), 1.0 - t); 45 | tmp = vec_mul_const_apply(vec_create(0.5, 0.7, 1), t); 46 | vec_add_apply(cal_color, tmp); 47 | free(tmp); 48 | } 49 | vec_div_const_apply(cal_color, ANTI_SAMPLES); 50 | vec_add_apply(color, cal_color); 51 | free(cal_color); 52 | } 53 | 54 | void draw_hittable_anti(t_camera *cam, t_list *lst) 55 | { 56 | int x; 57 | int y; 58 | int locate; 59 | t_vec *color; 60 | t_hitlst_info *lst_info; 61 | 62 | y = cam->data->height; 63 | while ((--y) >= 0) 64 | { 65 | x = -1; 66 | while ((++x) < cam->data->width) 67 | { 68 | color = vec_create(0, 0, 0); 69 | locate = -1; 70 | while ((++locate) < ANTI_SAMPLES) 71 | { 72 | lst_info = get_hitlst_by_locate(x, y, cam); 73 | get_hittable_color(lst, lst_info, color); 74 | free_hitlst_info(lst_info, FALSE); 75 | } 76 | cam->data->img[x][y] = get_color_val(color); 77 | free(color); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /srcs/draw_hittable_diffuse_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* draw_hittable_diffuse_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/14 04:55:05 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/23 13:31:02 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | t_vec *get_recursive_color(t_list *lst, t_hitlst_info **info, 16 | int depth, int *is_free) 17 | { 18 | t_vec *target; 19 | t_ray *ray; 20 | t_vec *tmp; 21 | double t; 22 | 23 | if (depth <= 0) 24 | return (vec_create(0, 0, 0)); 25 | if (hitlst_hit(lst, *info)) 26 | { 27 | target = vec_add_apply(vec_random_in_hemisphere((*info)->rec->normal), 28 | (*info)->rec->p); 29 | tmp = vec_dup((*info)->rec->p); 30 | ray = ray_create(tmp, vec_sub_apply(target, (*info)->rec->p)); 31 | free_hitlst_info(*info, *is_free); 32 | *is_free = TRUE; 33 | *info = hitlst_info_new(ray); 34 | return (vec_mul_const_apply(get_recursive_color(lst, info, depth - 1, 35 | is_free), 0.5)); 36 | } 37 | t = 0.5 * ((vec_unit_apply((*info)->ray->dir))->y + 1.0); 38 | target = vec_mul_const_apply(vec_create(1, 1, 1), 1.0 - t); 39 | tmp = vec_mul_const_apply(vec_create(0.5, 0.7, 1), t); 40 | vec_add_apply(target, tmp); 41 | free(tmp); 42 | return (target); 43 | } 44 | 45 | void get_hittable_diffuse_color(t_list *lst, 46 | t_hitlst_info **info, 47 | t_vec *color) 48 | { 49 | t_vec *target; 50 | int is_free; 51 | 52 | is_free = FALSE; 53 | target = get_recursive_color(lst, info, REFLECT_DEPTH, &is_free); 54 | vec_add_apply(color, target); 55 | free(target); 56 | free_hitlst_info(*info, is_free); 57 | } 58 | 59 | void draw_hittable_diffuse_anti(t_camera *cam, t_list *lst) 60 | { 61 | int x; 62 | int y; 63 | int locate; 64 | t_vec *color; 65 | t_hitlst_info *lst_info; 66 | 67 | y = cam->data->height; 68 | while ((--y) >= 0) 69 | { 70 | x = -1; 71 | while ((++x) < cam->data->width) 72 | { 73 | color = vec_create(0, 0, 0); 74 | locate = -1; 75 | while ((++locate) < ANTI_SAMPLES) 76 | { 77 | lst_info = get_hitlst_by_locate(x, y, cam); 78 | get_hittable_diffuse_color(lst, &lst_info, color); 79 | } 80 | cam->data->img[x][y] = get_color_sample_gamma(color); 81 | free(color); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /srcs/draw_hittable_material_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* draw_hittable_material_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/14 04:55:05 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/25 12:05:19 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | t_vec *get_sky_color_t(double t) 16 | { 17 | t_vec *target; 18 | t_vec *tmp; 19 | 20 | tmp = vec_mul_const_apply(vec_create(0.5, 0.7, 1), t); 21 | target = vec_mul_const_apply(vec_create(1, 1, 1), 1.0 - t); 22 | vec_add_apply(target, tmp); 23 | free(tmp); 24 | return (target); 25 | } 26 | 27 | t_vec *get_recur_mat_color(t_list *lst, t_hitlst_info **info, 28 | int depth, int *is_free) 29 | { 30 | t_material *mat; 31 | t_material_info mat_info; 32 | t_vec *target; 33 | double t; 34 | 35 | if (depth <= 0) 36 | return (vec_create(0, 0, 0)); 37 | if (hitlst_hit(lst, *info)) 38 | { 39 | mat = (*info)->rec->mat; 40 | if ((t = (*(mat->scatter))(mat, (*info)->ray, (*info)->rec, &mat_info))) 41 | { 42 | free_hitlst_info(*info, (*is_free)++); 43 | *info = hitlst_info_new(mat_info.scattered); 44 | target = vec_mul_each_apply( 45 | get_recur_mat_color(lst, info, depth - 1, is_free), mat_info.attenuation); 46 | } 47 | else 48 | target = vec_create(0, 0, 0); 49 | free_material_info(&mat_info, FALSE, !t); 50 | return (target); 51 | } 52 | t = 0.5 * ((vec_unit_apply((*info)->ray->dir))->y + 1.0); 53 | return (get_sky_color_t(t)); 54 | } 55 | 56 | void get_hittable_material_color(t_list *lst, 57 | t_hitlst_info *info, 58 | t_vec *color) 59 | { 60 | t_vec *target; 61 | int is_free; 62 | 63 | is_free = FALSE; 64 | target = get_recur_mat_color(lst, &info, REFLECT_DEPTH, &is_free); 65 | vec_add_apply(color, target); 66 | free(target); 67 | free_hitlst_info(info, is_free); 68 | } 69 | 70 | void draw_hittable_material(t_camera *cam, t_list *lst) 71 | { 72 | int x; 73 | int y; 74 | int locate; 75 | t_vec *color; 76 | 77 | y = cam->data->height; 78 | while ((--y) >= 0) 79 | { 80 | x = -1; 81 | while ((++x) < cam->data->width) 82 | { 83 | color = vec_create(0, 0, 0); 84 | locate = -1; 85 | while ((++locate) < ANTI_SAMPLES) 86 | get_hittable_material_color(lst, 87 | get_hitlst_by_locate(x, y, cam), color); 88 | cam->data->img[x][y] = get_color_sample_gamma(color); 89 | free(color); 90 | } 91 | ft_printf("%d / %d\n", cam->data->height - y, cam->data->height); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /srcs/draw_hittable_pthread_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* draw_hittable_pthread_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/14 04:55:05 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/25 12:04:56 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | void *render(void *arg) 16 | { 17 | int x; 18 | int y; 19 | int locate; 20 | t_vec *color; 21 | t_thread_info *tinfo; 22 | 23 | tinfo = (t_thread_info *)arg; 24 | y = tinfo_get_y_init_value(tinfo); 25 | while ((--y) >= (tinfo_get_step(tinfo) * (tinfo->tnum - 1))) 26 | { 27 | x = -1; 28 | while ((++x) < tinfo->cam->data->width) 29 | { 30 | color = vec_create(0, 0, 0); 31 | locate = -1; 32 | while ((++locate) < ANTI_SAMPLES) 33 | get_hittable_material_color(tinfo->lst, 34 | get_hitlst_by_locate(x, y, tinfo->cam), color); 35 | tinfo->cam->data->img[x][y] = get_color_sample_gamma(color); 36 | free(color); 37 | } 38 | ft_printf("Finish render line %d\n", y); 39 | } 40 | free(tinfo); 41 | return (NULL); 42 | } 43 | 44 | void draw_hittable_pthread(t_camera *cam, t_list *lst) 45 | { 46 | pthread_t *threads; 47 | int pnum; 48 | t_thread_info *info; 49 | 50 | threads = (pthread_t *)malloc(sizeof(pthread_t) * PTHREAD_CNT); 51 | pnum = 0; 52 | while (++pnum <= PTHREAD_CNT) 53 | { 54 | info = tinfo_new(cam, lst, pnum); 55 | pthread_create(&(threads[pnum - 1]), NULL, render, (void *)info); 56 | } 57 | pnum = 0; 58 | while (++pnum <= PTHREAD_CNT) 59 | pthread_join(threads[pnum - 1], NULL); 60 | free(threads); 61 | } 62 | -------------------------------------------------------------------------------- /srcs/img_data_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* img_data_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/03 11:50:24 by cos #+# #+# */ 9 | /* Updated: 2020/10/08 14:43:24 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_img_data *create_img_data(int width, int height) 16 | { 17 | t_img_data *result; 18 | int h; 19 | int w; 20 | 21 | result = (t_img_data *)malloc(sizeof(t_img_data)); 22 | result->width = width; 23 | result->height = height; 24 | result->img = (int **)malloc(sizeof(int *) * width); 25 | w = -1; 26 | while ((++w) < width) 27 | { 28 | result->img[w] = (int *)malloc(sizeof(int) * height); 29 | h = -1; 30 | while ((++h) < height) 31 | result->img[w][h] = 0; 32 | } 33 | return (result); 34 | } 35 | 36 | void free_img_data(t_img_data *data) 37 | { 38 | int w; 39 | 40 | w = -1; 41 | while ((++w) < data->width) 42 | free(data->img[w]); 43 | free(data->img); 44 | free(data); 45 | } 46 | -------------------------------------------------------------------------------- /srcs/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/07/11 21:12:06 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/22 22:29:09 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | #include "chapter.h" 15 | 16 | int main(int argv, char **argc) 17 | { 18 | int chapter; 19 | 20 | if (argv < 2 || argv > 3 || 21 | (argv == 3 && (ft_strncmp(argc[2], "--save", 6) != 0))) 22 | return (ft_printf("Wrong argc\n")); 23 | chapter = ft_atoi(argc[1]); 24 | if (chapter == 2 || chapter == 3) 25 | show_gradation(argv - 2); 26 | else if (chapter == 4) 27 | show_sky(argv - 2); 28 | else if (chapter == 5) 29 | show_s_sphere(argv - 2); 30 | else if (chapter == 6) 31 | show_normal_sphere(argv - 2); 32 | else if (chapter == 7) 33 | show_normal_sphere_anti(argv - 2); 34 | else if (chapter == 8) 35 | show_normal_sphere_anti_dif(argv - 2); 36 | else if (chapter == 9) 37 | show_sphere_material(argv - 2); 38 | else if (chapter == 11) 39 | show_sphere_camera(argv - 2); 40 | else if (chapter == 13) 41 | show_final(argv - 2); 42 | else 43 | ft_printf("Wrong argc\n"); 44 | } 45 | -------------------------------------------------------------------------------- /srcs/material/simple_material.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* simple_material.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/17 20:31:29 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/23 08:28:26 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | t_material *mat_lambertian_new(t_vec *color) 16 | { 17 | t_material *result; 18 | 19 | result = material_new(MAT_LAMBERTIAN, color); 20 | result->scatter = &lambertian_scatter; 21 | return (result); 22 | } 23 | 24 | int lambertian_scatter(t_material *mat, t_ray *ray_in, 25 | t_hit_record *rec, t_material_info *info) 26 | { 27 | t_vec *scatter_direction; 28 | 29 | ray_in->dir->x = ray_in->dir->x; 30 | scatter_direction = vec_add_apply(vec_random_unit(), rec->normal); 31 | info->scattered = ray_create(vec_dup(rec->p), scatter_direction); 32 | info->attenuation = vec_dup(mat->color); 33 | return (TRUE); 34 | } 35 | 36 | t_material *mat_metal_new(t_vec *color, double fuzz) 37 | { 38 | t_material *result; 39 | 40 | result = material_new(MAT_METAL, color); 41 | result->scatter = &metal_scatter; 42 | result->fuzz = ((fuzz < 1) ? fuzz : 1); 43 | return (result); 44 | } 45 | 46 | int metal_scatter(t_material *mat, t_ray *ray_in, t_hit_record *rec, 47 | t_material_info *info) 48 | { 49 | t_vec *reflected; 50 | t_vec *tmp; 51 | 52 | tmp = vec_unit(ray_in->dir); 53 | reflected = vec_reflect(tmp, rec->normal); 54 | free(tmp); 55 | tmp = vec_mul_const_apply(vec_random_in_unit_sphere(), mat->fuzz); 56 | info->scattered = ray_create(vec_dup(rec->p), 57 | vec_add_apply(reflected, tmp)); 58 | free(tmp); 59 | info->attenuation = vec_dup(mat->color); 60 | return ((vec_dot(info->scattered->dir, rec->normal) > 0) ? TRUE : FALSE); 61 | } 62 | -------------------------------------------------------------------------------- /srcs/mlx_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mlx_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/28 14:13:02 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/08 20:15:08 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | void mlx_draw_by_img_data(t_mlx_data *mlx_data, t_img_data *img_data) 16 | { 17 | int width; 18 | int height; 19 | char *dst; 20 | int next; 21 | 22 | width = -1; 23 | while ((++width) < img_data->width) 24 | { 25 | height = -1; 26 | while ((++height) < img_data->height) 27 | { 28 | next = (img_data->height - height - 1) * mlx_data->line_length; 29 | next += width * (mlx_data->bits_per_pixel / 8); 30 | dst = mlx_data->addr + next; 31 | *(unsigned int*)dst = img_data->img[width][height]; 32 | } 33 | } 34 | } 35 | 36 | int exit_program(void) 37 | { 38 | exit(0); 39 | return (0); 40 | } 41 | 42 | int mlx_key_handle(int keycode) 43 | { 44 | // ft_printf("keycode : %d\n", keycode); 45 | if (keycode == 53) 46 | return (exit_program()); 47 | return (0); 48 | } 49 | 50 | void mlx_show(t_img_data *data, char *title) 51 | { 52 | t_vars vars; 53 | t_mlx_data *img; 54 | 55 | vars.mlx = mlx_init(); 56 | vars.win = mlx_new_window(vars.mlx, data->width, data->height, title); 57 | img = (t_mlx_data *)malloc(sizeof(t_mlx_data)); 58 | img->img = mlx_new_image(vars.mlx, data->width, data->height); 59 | img->addr = mlx_get_data_addr(img->img, &(img->bits_per_pixel), \ 60 | &(img->line_length), &(img->endian)); 61 | mlx_draw_by_img_data(img, data); 62 | mlx_put_image_to_window(vars.mlx, vars.win, img->img, 0, 0); 63 | mlx_destroy_image(vars.mlx, img->img); 64 | mlx_hook(vars.win, 2, 1L << 0, mlx_key_handle, 0); 65 | mlx_hook(vars.win, 17, 1L << 17, exit_program, 0); 66 | mlx_loop(vars.mlx); 67 | free(img); 68 | ft_printf("test\n"); 69 | } 70 | -------------------------------------------------------------------------------- /srcs/object/simple_sphere.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* simple_sphere.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 15:33:55 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/18 21:44:34 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | int hit_s_sphere(t_sphere *s, t_ray *r) 16 | { 17 | t_vec *oc; 18 | double a; 19 | double b; 20 | double c; 21 | double discriminant; 22 | 23 | oc = vec_sub(r->orig, s->center); 24 | a = vec_dot(r->dir, r->dir); 25 | b = vec_dot(oc, r->dir) * 2.0; 26 | c = vec_dot(oc, oc) - s->radius * s->radius; 27 | free(oc); 28 | discriminant = b * b - 4 * a * c; 29 | return ((discriminant >= 0) ? TRUE : FALSE); 30 | } 31 | 32 | void draw_s_sphere(t_img_data *data, t_sky_info *info, t_sphere *s) 33 | { 34 | t_vec *color; 35 | t_sky *my_sky; 36 | t_ray *r; 37 | int x; 38 | int y; 39 | 40 | my_sky = init_sky(info, vec_create(0, 0, 0)); 41 | my_sky->data = data; 42 | color = vec_create(1, 1, 0); 43 | y = data->height; 44 | while (--y >= 0) 45 | { 46 | x = -1; 47 | while (++x < data->width) 48 | { 49 | r = cal_sky_ray(x, y, my_sky); 50 | if (hit_s_sphere(s, r)) 51 | data->img[x][y] = get_color_val(color); 52 | ray_free(r, FALSE); 53 | } 54 | } 55 | free(color); 56 | free_sky(my_sky, TRUE); 57 | } 58 | -------------------------------------------------------------------------------- /srcs/object/sky.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sky.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/07 15:35:13 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/15 16:44:53 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_sky_info *init_sky_info(double viewport_width, double viewport_height, 16 | double focal_length) 17 | { 18 | t_sky_info *result; 19 | 20 | result = (t_sky_info *)malloc(sizeof(t_sky_info)); 21 | result->viewport_width = viewport_width; 22 | result->viewport_height = viewport_height; 23 | result->focal_length = focal_length; 24 | return (result); 25 | } 26 | 27 | t_sky *init_sky(t_sky_info *info, t_vec *origin) 28 | { 29 | t_sky *result; 30 | t_vec *tmp; 31 | t_vec *tmp2; 32 | t_vec *tmp3; 33 | 34 | result = (t_sky *)malloc(sizeof(t_sky)); 35 | result->origin = origin; 36 | result->horizontal = vec_create(info->viewport_width, 0, 0); 37 | result->vertical = vec_create(0, info->viewport_height, 0); 38 | vec_div_const_apply(result->horizontal, 2.0); 39 | vec_div_const_apply(result->vertical, 2.0); 40 | tmp = vec_add(result->horizontal, result->vertical); 41 | tmp2 = vec_create(0, 0, info->focal_length); 42 | tmp3 = vec_add(tmp, tmp2); 43 | result->lower_left_corner = vec_sub(origin, tmp3); 44 | free(tmp); 45 | free(tmp2); 46 | free(tmp3); 47 | vec_mul_const_apply(result->horizontal, 2.0); 48 | vec_mul_const_apply(result->vertical, 2.0); 49 | return (result); 50 | } 51 | 52 | int cal_sky_color(t_ray *r) 53 | { 54 | t_vec *tmp; 55 | t_vec *tmp2; 56 | double t; 57 | int result; 58 | 59 | tmp = vec_unit(r->dir); 60 | t = 0.5 * (tmp->y + 1.0); 61 | free(tmp); 62 | tmp = vec_mul_const_apply(vec_create(1, 1, 1), 1.0 - t); 63 | tmp2 = vec_mul_const_apply(vec_create(0.5, 0.7, 1), t); 64 | vec_add_apply(tmp, tmp2); 65 | free(tmp2); 66 | result = get_color_val(tmp); 67 | free(tmp); 68 | return (result); 69 | } 70 | 71 | void free_sky(t_sky *my_sky, int is_origin_free) 72 | { 73 | free(my_sky->horizontal); 74 | free(my_sky->vertical); 75 | free(my_sky->lower_left_corner); 76 | if (is_origin_free) 77 | free(my_sky->origin); 78 | free(my_sky); 79 | } 80 | 81 | void draw_sky(t_img_data *data, t_sky_info *info) 82 | { 83 | t_sky *my_sky; 84 | t_ray *r; 85 | int x; 86 | int y; 87 | 88 | my_sky = init_sky(info, vec_create(0, 0, 0)); 89 | my_sky->data = data; 90 | y = data->height; 91 | while (--y >= 0) 92 | { 93 | x = -1; 94 | while (++x < data->width) 95 | { 96 | r = cal_sky_ray(x, y, my_sky); 97 | data->img[x][y] = cal_sky_color(r); 98 | ray_free(r, FALSE); 99 | } 100 | } 101 | free_sky(my_sky, TRUE); 102 | } 103 | -------------------------------------------------------------------------------- /srcs/object/sky_ray.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sky_ray.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 16:01:03 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/09 20:00:56 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_ray *cal_sky_ray(int x, int y, t_sky *my_sky) 16 | { 17 | t_ray *r; 18 | t_vec *tmp; 19 | t_vec *tmp2; 20 | 21 | tmp = vec_mul_const(my_sky->horizontal, 22 | (double)x / (my_sky->data->width - 1)); 23 | tmp2 = vec_mul_const(my_sky->vertical, 24 | (double)y / (my_sky->data->height - 1)); 25 | vec_add_apply(tmp, tmp2); 26 | free(tmp2); 27 | vec_add_apply(tmp, my_sky->lower_left_corner); 28 | r = ray_create(my_sky->origin, vec_sub_apply(tmp, my_sky->origin)); 29 | return (r); 30 | } 31 | -------------------------------------------------------------------------------- /srcs/object/sphere.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sphere.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/09 15:33:55 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/18 21:43:23 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_sphere *init_sphere(t_vec *center, double radius) 16 | { 17 | t_sphere *result; 18 | 19 | result = (t_sphere *)malloc(sizeof(t_sphere)); 20 | result->center = center; 21 | result->radius = radius; 22 | return (result); 23 | } 24 | 25 | void free_sphere(t_sphere *s) 26 | { 27 | free(s->center); 28 | free(s); 29 | } 30 | 31 | double hit_sphere(t_sphere *s, t_ray *r) 32 | { 33 | t_vec *oc; 34 | double a; 35 | double half_b; 36 | double c; 37 | double discriminant; 38 | 39 | oc = vec_sub(r->orig, s->center); 40 | a = vec_length_squared(r->dir); 41 | half_b = vec_dot(oc, r->dir); 42 | c = vec_length_squared(oc) - s->radius * s->radius; 43 | free(oc); 44 | discriminant = half_b * half_b - a * c; 45 | if (discriminant < 0) 46 | return (-1.0); 47 | return ((-half_b - sqrt(discriminant)) / a); 48 | } 49 | 50 | int cal_ray_color(t_ray *r, t_sphere *s, double t) 51 | { 52 | t_vec *at; 53 | t_vec *n; 54 | t_vec *c; 55 | t_vec *color; 56 | int rgb; 57 | 58 | at = ray_at(r, t); 59 | n = vec_unit_apply(vec_sub(at, s->center)); 60 | c = vec_create(1, 1, 1); 61 | color = vec_mul_const(vec_add_apply(n, c), 0.5); 62 | rgb = get_color_val(color); 63 | free(at); 64 | free(n); 65 | free(c); 66 | free(color); 67 | return (rgb); 68 | } 69 | 70 | void draw_sphere(t_img_data *data, t_sky_info *info, t_sphere *s) 71 | { 72 | t_sky *my_sky; 73 | t_ray *r; 74 | int x; 75 | int y; 76 | double t; 77 | 78 | my_sky = init_sky(info, vec_create(0, 0, 0)); 79 | my_sky->data = data; 80 | y = data->height; 81 | while (--y >= 0) 82 | { 83 | x = -1; 84 | while (++x < data->width) 85 | { 86 | r = cal_sky_ray(x, y, my_sky); 87 | t = hit_sphere(s, r); 88 | if (t > 0.0) 89 | data->img[x][y] = cal_ray_color(r, s, t); 90 | ray_free(r, FALSE); 91 | } 92 | } 93 | free_sky(my_sky, TRUE); 94 | } 95 | -------------------------------------------------------------------------------- /srcs/object/sphere_hit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sphere_hit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/10 14:49:41 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/18 19:10:04 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | int check_sphere_hitrange(void *s, t_ray *r, 16 | t_hitlst_info *info, t_hit_record *rec) 17 | { 18 | int is_big_t; 19 | double t; 20 | 21 | is_big_t = -1; 22 | while ((++is_big_t) < 2) 23 | { 24 | t = (-info->half_b + info->root_d * (is_big_t ? (1) : (-1))) / info->a; 25 | if (info->t_min < t && t < info->t_max) 26 | { 27 | if (rec->p) 28 | reset_hit_record(rec); 29 | rec->t = t; 30 | rec->p = ray_at(r, t); 31 | rec->normal = vec_sub(rec->p, ((t_sphere *)s)->center); 32 | vec_div_const_apply(rec->normal, ((t_sphere *)s)->radius); 33 | hit_set_normal(rec, r); 34 | rec->mat = info->mat; 35 | return (TRUE); 36 | } 37 | } 38 | return (FALSE); 39 | } 40 | 41 | int sphere_hit(void *s, t_ray *r, t_hitlst_info *info, 42 | t_hit_record *rec) 43 | { 44 | t_vec *oc; 45 | double a; 46 | double half_b; 47 | double c; 48 | double discriminant; 49 | 50 | oc = vec_sub(r->orig, ((t_sphere *)s)->center); 51 | a = vec_length_squared(r->dir); 52 | half_b = vec_dot(oc, r->dir); 53 | c = vec_length_squared(oc); 54 | c -= pow(((t_sphere *)s)->radius, 2); 55 | free(oc); 56 | discriminant = half_b * half_b - a * c; 57 | if (discriminant <= 0) 58 | return (FALSE); 59 | info->a = a; 60 | info->half_b = half_b; 61 | info->root_d = sqrt(discriminant); 62 | return (check_sphere_hitrange(s, r, info, rec)); 63 | } 64 | -------------------------------------------------------------------------------- /srcs/random_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* random_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/15 10:05:46 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/17 16:52:29 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | double random_double(void) 16 | { 17 | return (rand() / (RAND_MAX + 1.0)); 18 | } 19 | 20 | double random_double_range(double min, double max) 21 | { 22 | return (min + (max - min) * random_double()); 23 | } 24 | -------------------------------------------------------------------------------- /srcs/save_bmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* save_bmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/03 14:18:55 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/09 21:21:15 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | void raw_fill_header(t_img_data *data, 16 | char **raw_bmp, unsigned int img_size) 17 | { 18 | **raw_bmp = 'B'; 19 | *(*raw_bmp + 1) = 'M'; 20 | *(unsigned int *)(*raw_bmp + 2) = (img_size + BMP_HEADER_SIZE); 21 | *(unsigned int *)(*raw_bmp + 6) = 0u; 22 | *(unsigned char *)(*raw_bmp + 10) = BMP_HEADER_SIZE; 23 | *(unsigned int *)(*raw_bmp + 14) = BMP_HEADER_SIZE - 14; 24 | *(unsigned int *)(*raw_bmp + 18) = data->width; 25 | *(unsigned int *)(*raw_bmp + 22) = data->height; 26 | *(unsigned short *)(*raw_bmp + 26) = 1; 27 | *(unsigned short *)(*raw_bmp + 28) = 24; 28 | *(unsigned int *)(*raw_bmp + 30) = 0; 29 | *(unsigned int *)(*raw_bmp + 34) = img_size; 30 | *(unsigned int *)(*raw_bmp + 38) = 3780; 31 | *(unsigned int *)(*raw_bmp + 42) = 3780; 32 | *(int *)(*raw_bmp + 46) = 0; 33 | *(int *)(*raw_bmp + 50) = 0; 34 | } 35 | 36 | void raw_fill_data(t_img_data *data, char *raw_bmp) 37 | { 38 | t_vec *color; 39 | int w; 40 | int h; 41 | int locate; 42 | 43 | locate = BMP_HEADER_SIZE + 1; 44 | h = -1; 45 | while ((++h) < data->height) 46 | { 47 | w = -1; 48 | while ((++w) < data->width) 49 | { 50 | color = get_color(data->img[w][h]); 51 | *(raw_bmp + locate++) = (unsigned char)(color->y); 52 | *(raw_bmp + locate++) = (unsigned char)(color->x); 53 | *(raw_bmp + locate++) = (unsigned char)(color->z); 54 | free(color); 55 | } 56 | } 57 | } 58 | 59 | char *get_bmp_filename(char *filename) 60 | { 61 | char *result; 62 | 63 | result = (char *)malloc(ft_strlen(filename) + 2); 64 | ft_strlcpy(result, filename, ft_strlen(filename) - 1); 65 | result[ft_strlen(filename) - 2] = 'b'; 66 | result[ft_strlen(filename) - 1] = 'm'; 67 | result[ft_strlen(filename)] = 'p'; 68 | result[ft_strlen(filename) + 1] = 0; 69 | return (result); 70 | } 71 | 72 | void save_bmp(t_img_data *data, char *filename) 73 | { 74 | char *bmp_filename; 75 | char *raw_bmp; 76 | int img_size; 77 | int locate; 78 | int fd; 79 | 80 | bmp_filename = get_bmp_filename(filename); 81 | img_size = data->width * data->height * 3; 82 | raw_bmp = (char *)malloc(img_size + BMP_HEADER_SIZE); 83 | locate = 0; 84 | while (locate < img_size + BMP_HEADER_SIZE) 85 | raw_bmp[locate++] = 0; 86 | raw_fill_header(data, &raw_bmp, img_size); 87 | raw_fill_data(data, raw_bmp); 88 | fd = open(bmp_filename, O_CREAT | O_TRUNC | O_RDWR, 0644); 89 | write(fd, raw_bmp, (img_size + BMP_HEADER_SIZE)); 90 | close(fd); 91 | free(bmp_filename); 92 | free(raw_bmp); 93 | } 94 | -------------------------------------------------------------------------------- /srcs/struct/camera_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* camera_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/15 10:33:13 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/25 11:58:36 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | void set_camera_llc(t_camera *cam, t_vec *lookat) 16 | { 17 | t_vec *tmp; 18 | 19 | vec_div_const_apply(cam->horizontal, 2.0); 20 | vec_div_const_apply(cam->vertical, 2.0); 21 | tmp = vec_add(cam->horizontal, cam->vertical); 22 | vec_add_apply(tmp, lookat); 23 | cam->lower_left_corner = vec_sub(cam->origin, tmp); 24 | free(tmp); 25 | vec_mul_const_apply(cam->horizontal, 2.0); 26 | vec_mul_const_apply(cam->vertical, 2.0); 27 | } 28 | 29 | t_camera *camera_new(double aspect_ratio) 30 | { 31 | t_camera *result; 32 | double viewport_height; 33 | double viewport_width; 34 | double focal_length; 35 | t_vec *tmp; 36 | 37 | result = (t_camera *)malloc(sizeof(t_camera)); 38 | result->aspect_ratio = aspect_ratio; 39 | viewport_height = 2.0; 40 | viewport_width = viewport_height * aspect_ratio; 41 | focal_length = 1.0; 42 | result->origin = vec_create(0, 0, 0); 43 | result->horizontal = vec_create(viewport_width, 0, 0); 44 | result->vertical = vec_create(0, viewport_height, 0); 45 | tmp = vec_create(0, 0, focal_length); 46 | set_camera_llc(result, tmp); 47 | free(tmp); 48 | return (result); 49 | } 50 | 51 | /* 52 | ** vfov -> radian 53 | ** lookat : camera view vector (normal vector) 54 | */ 55 | 56 | t_camera *camera_locate_new(t_vec *lookfrom, t_vec *lookat, 57 | double aspect_ratio, double vfov) 58 | { 59 | t_camera *result; 60 | double view_h; 61 | double view_w; 62 | t_vec *vup; 63 | 64 | vec_unit_apply(vec_mul_const_apply(lookat, -1.0)); 65 | vup = vec_create(0, 1, 0); 66 | if (vec_is_parallel(vup, lookat)) 67 | { 68 | free(vup); 69 | vup = vec_create(1, 0, 0); 70 | } 71 | result = (t_camera *)malloc(sizeof(t_camera)); 72 | view_h = 2.0 * tan(vfov / 2.0); 73 | view_w = view_h * aspect_ratio; 74 | result->horizontal = vec_unit_apply(vec_cross_apply(vup, lookat)); 75 | result->vertical = vec_mul_const_apply( 76 | vec_cross(lookat, result->horizontal), view_h); 77 | vec_mul_const_apply(result->horizontal, view_w); 78 | result->origin = lookfrom; 79 | set_camera_llc(result, lookat); 80 | free(lookat); 81 | return (result); 82 | } 83 | 84 | t_ray *camera_get_ray(t_camera *cam, double u, double v) 85 | { 86 | t_vec *tmp; 87 | t_vec *tmp2; 88 | 89 | tmp2 = vec_mul_const(cam->horizontal, u); 90 | tmp = vec_add(cam->lower_left_corner, tmp2); 91 | free(tmp2); 92 | tmp2 = vec_mul_const(cam->vertical, v); 93 | vec_add_apply(tmp, tmp2); 94 | free(tmp2); 95 | vec_sub_apply(tmp, cam->origin); 96 | return (ray_create(cam->origin, tmp)); 97 | } 98 | 99 | void free_camera(t_camera *cam) 100 | { 101 | free(cam->horizontal); 102 | free(cam->vertical); 103 | free(cam->lower_left_corner); 104 | free(cam->origin); 105 | free(cam); 106 | } 107 | -------------------------------------------------------------------------------- /srcs/struct/hit_record.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* hit_record.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/10 15:57:03 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/17 16:18:38 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_hit_record *hit_record_new(void) 16 | { 17 | t_hit_record *result; 18 | 19 | result = (t_hit_record *)malloc(sizeof(t_hit_record)); 20 | result->normal = NULL; 21 | result->p = NULL; 22 | return (result); 23 | } 24 | 25 | void reset_hit_record(t_hit_record *rec) 26 | { 27 | if (rec->normal) 28 | free(rec->normal); 29 | if (rec->p) 30 | free(rec->p); 31 | } 32 | 33 | void free_hit_record(t_hit_record *rec) 34 | { 35 | reset_hit_record(rec); 36 | free(rec); 37 | } 38 | 39 | void hit_set_normal(t_hit_record *record, t_ray *r) 40 | { 41 | record->is_front_face = (vec_dot(r->dir, record->normal) < 0) ? 42 | TRUE : FALSE; 43 | if (record->is_front_face == FALSE) 44 | vec_mul_const_apply(record->normal, -1); 45 | } 46 | -------------------------------------------------------------------------------- /srcs/struct/hitlst_info_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* hitlst_info_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/14 04:25:55 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/23 13:29:25 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_hitlst_info *hitlst_info_new(t_ray *r) 16 | { 17 | t_hitlst_info *result; 18 | 19 | result = (t_hitlst_info *)malloc(sizeof(t_hitlst_info)); 20 | result->ray = r; 21 | result->t_min = HIT_T_MIN; 22 | result->t_max = INFINITY; 23 | result->rec = hit_record_new(); 24 | return (result); 25 | } 26 | 27 | void free_hitlst_info(t_hitlst_info *info, int is_ray_ori_free) 28 | { 29 | ray_free(info->ray, is_ray_ori_free); 30 | free_hit_record(info->rec); 31 | free(info); 32 | } 33 | -------------------------------------------------------------------------------- /srcs/struct/hitlst_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* hitlst_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/10 16:29:29 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/18 21:18:22 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_list *hitlst_new(void) 16 | { 17 | t_list *result; 18 | 19 | result = (t_list *)malloc(sizeof(t_list)); 20 | result->content = NULL; 21 | result->next = NULL; 22 | return (result); 23 | } 24 | 25 | void hitlst_add(t_list *lst, void *obj, int obj_type) 26 | { 27 | if (lst->content) 28 | { 29 | while (lst->next) 30 | lst = lst->next; 31 | lst->next = hitlst_new(); 32 | lst = lst->next; 33 | } 34 | lst->content = hittable_create(obj, obj_type); 35 | } 36 | 37 | void hitlst_mat_add(t_list *lst, void *obj, int obj_type, 38 | t_material *mat) 39 | { 40 | if (lst->content) 41 | { 42 | while (lst->next) 43 | lst = lst->next; 44 | lst->next = hitlst_new(); 45 | lst = lst->next; 46 | } 47 | lst->content = hittable_mat_create(obj, obj_type, mat); 48 | } 49 | 50 | void free_hitlst(t_list *lst) 51 | { 52 | if (lst->next) 53 | free_hitlst(lst->next); 54 | free_hittable(lst->content); 55 | free(lst); 56 | } 57 | 58 | int hitlst_hit(t_list *lst, t_hitlst_info *info) 59 | { 60 | int hit_anything; 61 | t_hittable *hittable; 62 | 63 | hit_anything = FALSE; 64 | while (lst && lst->content) 65 | { 66 | hittable = (t_hittable *)(lst->content); 67 | info->mat = hittable->mat; 68 | if ((*(hittable->hit))(hittable->obj, info->ray, info, info->rec)) 69 | { 70 | hit_anything = TRUE; 71 | info->t_max = info->rec->t; 72 | } 73 | lst = lst->next; 74 | } 75 | return (hit_anything); 76 | } 77 | -------------------------------------------------------------------------------- /srcs/struct/hittable_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* hittable_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/10 16:35:38 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/18 21:45:13 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_hittable *hittable_create(void *obj, int obj_type) 16 | { 17 | t_hittable *result; 18 | 19 | result = (t_hittable *)malloc(sizeof(t_hittable)); 20 | result->obj = obj; 21 | result->obj_type = obj_type; 22 | result->mat = NULL; 23 | if (obj_type == OBJ_SPHERE) 24 | result->hit = &sphere_hit; 25 | return (result); 26 | } 27 | 28 | void free_hittable(t_hittable *h) 29 | { 30 | if (h->obj_type == OBJ_SPHERE) 31 | free_sphere(h->obj); 32 | if (h->mat) 33 | free_material(h->mat); 34 | free(h); 35 | } 36 | 37 | t_hittable *hittable_mat_create(void *obj, int obj_type, t_material *mat) 38 | { 39 | t_hittable *result; 40 | 41 | result = (t_hittable *)malloc(sizeof(t_hittable)); 42 | result->obj = obj; 43 | result->obj_type = obj_type; 44 | result->mat = material_dup(mat); 45 | if (obj_type == OBJ_SPHERE) 46 | result->hit = &sphere_hit; 47 | return (result); 48 | } 49 | -------------------------------------------------------------------------------- /srcs/struct/material_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* material_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/17 20:27:10 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/19 16:14:34 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_material_info *material_info_new(void) 16 | { 17 | t_material_info *result; 18 | 19 | result = (t_material_info *)malloc(sizeof(t_material_info)); 20 | result->attenuation = NULL; 21 | result->scattered = NULL; 22 | return (result); 23 | } 24 | 25 | void free_material_info(t_material_info *info, int is_ori_free, 26 | int is_scattered_free) 27 | { 28 | if (info->attenuation) 29 | free(info->attenuation); 30 | if (info->scattered && is_scattered_free) 31 | ray_free(info->scattered, TRUE); 32 | if (is_ori_free) 33 | free(info); 34 | } 35 | 36 | t_material *material_new(int mat_type, t_vec *color) 37 | { 38 | t_material *result; 39 | 40 | result = (t_material *)malloc(sizeof(t_material)); 41 | result->color = color; 42 | result->mat_type = mat_type; 43 | return (result); 44 | } 45 | 46 | t_material *material_dup(t_material *mat) 47 | { 48 | t_material *result; 49 | 50 | result = (t_material *)malloc(sizeof(t_material)); 51 | result->color = vec_dup(mat->color); 52 | result->mat_type = mat->mat_type; 53 | result->scatter = mat->scatter; 54 | if (mat->mat_type == MAT_METAL) 55 | result->fuzz = mat->fuzz; 56 | return (result); 57 | } 58 | 59 | void free_material(t_material *mat) 60 | { 61 | free(mat->color); 62 | free(mat); 63 | } 64 | -------------------------------------------------------------------------------- /srcs/struct/ray_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ray_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/04 22:07:33 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/17 16:34:52 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_ray *ray_create(t_vec *origin, t_vec *direction) 16 | { 17 | t_ray *result; 18 | 19 | result = (t_ray *)malloc(sizeof(t_ray)); 20 | result->orig = origin; 21 | result->dir = direction; 22 | return (result); 23 | } 24 | 25 | t_vec *ray_at(t_ray *ray, double t) 26 | { 27 | t_vec *result; 28 | t_vec *tmp_vec; 29 | 30 | tmp_vec = vec_mul_const(ray->dir, t); 31 | result = vec_add(tmp_vec, ray->orig); 32 | free(tmp_vec); 33 | return (result); 34 | } 35 | 36 | void ray_free(t_ray *ray, int is_orig_free) 37 | { 38 | if (ray == NULL) 39 | return ; 40 | if (is_orig_free && ray->orig != NULL) 41 | free(ray->orig); 42 | free(ray->dir); 43 | free(ray); 44 | } 45 | -------------------------------------------------------------------------------- /srcs/struct/thread_info_utils_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* thread_info_utils_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/23 11:23:18 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/23 14:34:24 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | t_thread_info *tinfo_new(t_camera *cam, t_list *lst, int tnum) 16 | { 17 | t_thread_info *result; 18 | 19 | result = (t_thread_info *)malloc(sizeof(t_thread_info)); 20 | result->cam = cam; 21 | result->lst = lst; 22 | result->tnum = tnum; 23 | return (result); 24 | } 25 | 26 | int tinfo_get_step(t_thread_info *info) 27 | { 28 | return (info->cam->data->height / PTHREAD_CNT); 29 | } 30 | 31 | int tinfo_get_y_init_value(t_thread_info *info) 32 | { 33 | if (info->tnum == PTHREAD_CNT) 34 | return (info->cam->data->height); 35 | return (tinfo_get_step(info) * info->tnum); 36 | } 37 | -------------------------------------------------------------------------------- /srcs/struct/vec_calc_extra.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* vec_calc_extra.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/18 20:29:30 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/23 08:28:52 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | #include 15 | 16 | // Need to delete this function before final submit 17 | 18 | void vec_print(t_vec *a) 19 | { 20 | printf("%lf %lf %lf\n", a->x, a->y, a->z); 21 | } 22 | 23 | int vec_is_parallel(t_vec *a, t_vec *b) 24 | { 25 | double dot; 26 | double length; 27 | 28 | dot = vec_dot(a, b); 29 | dot = ((dot < 0) ? (-1 * dot) : dot); 30 | length = vec_length(a) * vec_length(b); 31 | return ((dot == length) ? TRUE : FALSE); 32 | } 33 | 34 | t_vec *vec_cross_apply(t_vec *a, t_vec *b) 35 | { 36 | t_vec *result; 37 | 38 | result = vec_cross(a, b); 39 | a->x = result->x; 40 | a->y = result->y; 41 | a->z = result->z; 42 | free(result); 43 | return (a); 44 | } 45 | 46 | t_vec *vec_cross_apply_tob(t_vec *a, t_vec *b) 47 | { 48 | t_vec *result; 49 | 50 | result = vec_cross(a, b); 51 | b->x = result->x; 52 | b->y = result->y; 53 | b->z = result->z; 54 | free(result); 55 | return (b); 56 | } 57 | 58 | t_vec *vec_mul_each(t_vec *a, t_vec *b) 59 | { 60 | t_vec *result; 61 | 62 | result = (t_vec*)malloc(sizeof(t_vec)); 63 | result->x = a->x * b->x; 64 | result->y = a->y * b->y; 65 | result->z = a->z * b->z; 66 | return (result); 67 | } 68 | 69 | t_vec *vec_mul_each_apply(t_vec *a, t_vec *b) 70 | { 71 | a->x *= b->x; 72 | a->y *= b->y; 73 | a->z *= b->z; 74 | return (a); 75 | } 76 | -------------------------------------------------------------------------------- /srcs/struct/vector_apply.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* vector_apply.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/08 14:34:23 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/15 10:43:59 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_vec *vec_add_apply(t_vec *a, t_vec *b) 16 | { 17 | a->x += b->x; 18 | a->y += b->y; 19 | a->z += b->z; 20 | return (a); 21 | } 22 | 23 | t_vec *vec_sub_apply(t_vec *a, t_vec *b) 24 | { 25 | a->x -= b->x; 26 | a->y -= b->y; 27 | a->z -= b->z; 28 | return (a); 29 | } 30 | 31 | t_vec *vec_mul_const_apply(t_vec *a, double c) 32 | { 33 | a->x *= c; 34 | a->y *= c; 35 | a->z *= c; 36 | return (a); 37 | } 38 | 39 | t_vec *vec_div_const_apply(t_vec *a, double c) 40 | { 41 | a->x /= c; 42 | a->y /= c; 43 | a->z /= c; 44 | return (a); 45 | } 46 | 47 | t_vec *vec_unit_apply(t_vec *a) 48 | { 49 | return (vec_div_const_apply(a, vec_length(a))); 50 | } 51 | -------------------------------------------------------------------------------- /srcs/struct/vector_calc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* vector_calc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/28 15:28:05 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/18 19:00:48 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_vec *vec_add(t_vec *a, t_vec *b) 16 | { 17 | t_vec *result; 18 | 19 | result = (t_vec*)malloc(sizeof(t_vec)); 20 | result->x = a->x + b->x; 21 | result->y = a->y + b->y; 22 | result->z = a->z + b->z; 23 | return (result); 24 | } 25 | 26 | t_vec *vec_sub(t_vec *a, t_vec *b) 27 | { 28 | t_vec *result; 29 | 30 | result = (t_vec*)malloc(sizeof(t_vec)); 31 | result->x = a->x - b->x; 32 | result->y = a->y - b->y; 33 | result->z = a->z - b->z; 34 | return (result); 35 | } 36 | 37 | t_vec *vec_mul_const(t_vec *a, double c) 38 | { 39 | t_vec *result; 40 | 41 | result = (t_vec*)malloc(sizeof(t_vec)); 42 | result->x = a->x * c; 43 | result->y = a->y * c; 44 | result->z = a->z * c; 45 | return (result); 46 | } 47 | 48 | t_vec *vec_div_const(t_vec *a, double c) 49 | { 50 | t_vec *result; 51 | 52 | result = (t_vec*)malloc(sizeof(t_vec)); 53 | result->x = a->x / c; 54 | result->y = a->y / c; 55 | result->z = a->z / c; 56 | return (result); 57 | } 58 | 59 | t_vec *vec_reflect(t_vec *v, t_vec *n) 60 | { 61 | t_vec *result; 62 | t_vec *tmp; 63 | 64 | tmp = vec_mul_const(n, 2 * vec_dot(v, n)); 65 | result = vec_sub(v, tmp); 66 | free(tmp); 67 | return (result); 68 | } 69 | -------------------------------------------------------------------------------- /srcs/struct/vector_create.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* vector_create.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/17 15:40:57 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/17 15:42:36 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | t_vec *vec_create(double x, double y, double z) 16 | { 17 | t_vec *result; 18 | 19 | result = (t_vec*)malloc(sizeof(t_vec)); 20 | result->x = x; 21 | result->y = y; 22 | result->z = z; 23 | return (result); 24 | } 25 | 26 | t_vec *vec_dup(t_vec *ori) 27 | { 28 | return (vec_create(ori->x, ori->y, ori->z)); 29 | } 30 | -------------------------------------------------------------------------------- /srcs/struct/vector_random_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* vector_random_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/10/17 16:52:02 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/19 16:06:18 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt_bonus.h" 14 | 15 | t_vec *vec_random_range(double min, double max) 16 | { 17 | return (vec_create(random_double_range(min, max), 18 | random_double_range(min, max), 19 | random_double_range(min, max))); 20 | } 21 | 22 | t_vec *vec_random_in_unit_sphere(void) 23 | { 24 | t_vec *result; 25 | 26 | while (TRUE) 27 | { 28 | result = vec_random_range(-1, 1); 29 | if (vec_length_squared(result) >= 1) 30 | { 31 | free(result); 32 | continue ; 33 | } 34 | return (result); 35 | } 36 | } 37 | 38 | t_vec *vec_random_unit(void) 39 | { 40 | double a; 41 | double z; 42 | double r; 43 | 44 | a = random_double_range(0, 2 * MINI_PI); 45 | z = random_double_range(-1, 1); 46 | r = sqrt(1.0 - z * z); 47 | return (vec_create(r * cos(a), r * sin(a), z)); 48 | } 49 | 50 | t_vec *vec_random_in_hemisphere(t_vec *normal) 51 | { 52 | t_vec *in_unit; 53 | 54 | in_unit = vec_random_unit(); 55 | if (vec_dot(in_unit, normal) > 0.0) 56 | return (in_unit); 57 | return (vec_mul_const_apply(in_unit, -1.0)); 58 | } 59 | -------------------------------------------------------------------------------- /srcs/struct/vector_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* vector_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sunpark +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/09/28 15:15:13 by sunpark #+# #+# */ 9 | /* Updated: 2020/10/19 14:48:19 by sunpark ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minirt.h" 14 | 15 | double vec_length_squared(t_vec *a) 16 | { 17 | return (pow(a->x, 2) + pow(a->y, 2) + pow(a->z, 2)); 18 | } 19 | 20 | double vec_length(t_vec *a) 21 | { 22 | return (sqrt(vec_length_squared(a))); 23 | } 24 | 25 | double vec_dot(t_vec *a, t_vec *b) 26 | { 27 | return (a->x * b->x + a->y * b->y + a->z * b->z); 28 | } 29 | 30 | t_vec *vec_cross(t_vec *a, t_vec *b) 31 | { 32 | t_vec *result; 33 | 34 | result = (t_vec*)malloc(sizeof(t_vec)); 35 | result->x = a->y * b->z - a->z * b->y; 36 | result->y = a->z * b->x - a->x * b->z; 37 | result->z = a->x * b->y - a->y * b->x; 38 | return (result); 39 | } 40 | 41 | t_vec *vec_unit(t_vec *a) 42 | { 43 | return (vec_div_const(a, vec_length(a))); 44 | } 45 | --------------------------------------------------------------------------------