raytracer.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* raytracer.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: salee2 <salee2@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/05/24 20:23:15 by salee2 #+# #+# */ 9 /* Updated: 2023/05/24 20:23:16 by salee2 ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "include/minirt.h" 14 15 t_raytracer raytracer_(const int width, const int height) 16 { 17 t_raytracer this; 18 19 this.width = width; 20 this.height = height; 21 return this; 22 } 23 24 t_vec3 screen_to_world(t_vec2 pos_screen, t_raytracer* raytracer) 25 { 26 double x_scale = 2.0 / (raytracer->width - 1); 27 double y_scale = 2.0 / (raytracer->height - 1); 28 double aspect = (double) raytracer->width / raytracer->height; 29 30 return vec3_((pos_screen.x * x_scale - 1.0) * aspect, 31 -pos_screen.y * y_scale + 1.0, 0.0); 32 } 33 34 /* 35 t_vec3 trace_ray(t_ray* ray, t_raytracer* raytracer) 36 { 37 t_hit hit = intersect_ray_collision(ray, &raytracer->sphere); 38 39 if(hit.t < 0.0) 40 { 41 return vec3_(0.0, 0.0, 0.0); 42 } 43 else 44 { 45 // return scl_mul(raytracer->sphere.color, hit.t); 46 return vec3_(1.0, 0.0, 0.0); 47 } 48 } 49 */ 50 /* 51 52 void render() 53 { 54 const int image_width = 1280; 55 const int image_height = 720; 56 57 printf("P3\n%d %d\n255\n", image_width, image_height); 58 59 t_raytracer raytracer = raytracer_(image_width, image_height); 60 61 t_vec3* pixels = malloc(sizeof(t_vec3) * image_width * image_height); 62 for (int x = 0; x < image_width * image_height; ++x) 63 pixels[x] = color(0.0, 0.0, 0.25); 64 65 for (int y = 0; y < image_height; ++y) { 66 for (int x = 0; x < image_width; ++x) { 67 68 t_vec3 pixel_pos_world = screen_to_world(vec2_(x, y), &raytracer); 69 70 t_vec3 ray_dir = vec3_(0.0, 0.0, -1.0); 71 72 t_ray pixel_ray = ray_(pixel_pos_world, ray_dir); 73 pixels[x + y * image_width] = trace_ray(&pixel_ray, &raytracer); 74 // t_vec3 * cur = &pixels[x+y*image_width]; 75 // printf("%f %f %f\n", cur->r, cur->g, cur->b); 76 } 77 } 78 79 for (int y = 0; y < image_height; ++y) { 80 for (int x = 0; x < image_width; ++x) { 81 int ir = 255.999 * pixels->r; 82 int ig = 255.999 * pixels->g; 83 int ib = 255.999 * pixels->b; 84 printf("%d %d %d\n", ir, ig, ib); 85 } 86 } 87 88 } 89 */