render.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   render.c                                           :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/05/24 18:16:48 by gychoi            #+#    #+#             */
 9  /*   Updated: 2023/05/28 22:59:13 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "render.h"
14  #include <stdio.h>
15  
16  // something_wrond
17  
18  t_scene	*scene_init(void)
19  {
20  	t_scene		*scene;
21  	t_object	*world;
22  	t_object	*lights;
23  	double		ka;
24  
25  	scene = malloc(sizeof(t_scene));
26  	if (!scene)
27  		return (NULL);
28  	scene->camera = camera(point3(0, 0, 0));
29  	world = object(SP, sphere(point3(-2, 0, -5), 2), color3(0.5, 0, 0));
30  	oadd(&world, object(SP, sphere(point3(2, 0, -5), 2), color3(0, 0.5, 0)));
31  	oadd(&world, object(SP, sphere(point3(0, -1000, 0), 990), color3(1, 1, 1)));
32  	scene->world = world;
33  	lights = object(LIGHT_POINT, light_point(point3(0, 5, 0), color3(1, 1, 1), 0.5), color3(0, 0, 0));
34  	scene->light = lights;
35  	ka = 0.1;
36  	scene->ambient = vmults(color3(1, 1, 1), ka);
37  	return (scene);
38  }
39  
40  void	render(t_mlx mlx, t_img img)
41  {
42  	t_scene		*scene;
43  	int			i;
44  	int			j;
45  	double		u;
46  	double		v;
47  
48  	scene = scene_init();
49  	j = IMAGE_HEIGHT - 1;
50  	while (j >= 0)
51  	{
52  		printf("rendering... %.2f%%", \
53  				(float)(IMAGE_HEIGHT - j) / IMAGE_HEIGHT * 100);
54  		i = 0;
55  		while (i < IMAGE_WIDTH)
56  		{
57  			u = (double)i / (IMAGE_WIDTH - 1);
58  			v =  1 - (double)j / (IMAGE_HEIGHT - 1);
59  			scene->ray = ray_primary(scene->camera, u, v);
60  			my_mlx_pixel_put(&img, i, j, ray_color(scene->ray, scene->world));
61  			i++;
62  		}
63  		j--;
64  		printf("\033[1A\n");
65  	}
66  	printf("\n");
67  	mlx_put_image_to_window(mlx.mlx, mlx.win, img.img, 0, 0);
68  }