This repository was archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_render.cpp
More file actions
165 lines (154 loc) · 4.57 KB
/
Copy pathft_render.cpp
File metadata and controls
165 lines (154 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rserban <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/01/19 15:50:49 by rserban #+# #+# */
/* Updated: 2015/03/11 16:05:59 by rserban ### ########.fr */
/* */
/* ************************************************************************** */
#include "raytracer.h"
static float get_shade(t_env *e, t_vec3 *l_norm, t_vec3 *pi)
{
t_vec3 vec;
t_vec3 vec2;
t_ray *ray;
double tdist;
int i;
tdist = vector_length(l_norm);
multiply_vector_value(&vec, l_norm, 1.0f / tdist);
ray = new_ray(add_vector(&vec2, pi, multiply_vector_value(&vec2, &vec,
EPSILON)), &vec);
i = 0;
while (e->objs[i])
{
if (intersect_primitive(e->objs[i], ray, &tdist))
return (0.0f);
i++;
}
if (ray)
{
ray->dir = NULL;
ray->ori = NULL;
free(ray);
}
return (1.0f);
}
static void check_objects(t_env *e, t_ray *ray, t_color *color, t_light *light, t_obj *obj, t_vec3 *pi)
{
t_vec3 l_norm;
t_vec3 norm;
float dot;
float value;
float shade;
substract_vector(&l_norm, light->pos, pi);
shade = get_shade(e, &l_norm, pi);
norm_vector(&l_norm);
get_normal(&norm, obj, pi);
if (shade > 0.0f)
{
if (obj->mat->diff > 0.0f)
{
dot = vector_dot(&l_norm, &norm);
if (dot > 0.0f)
{
value = dot * obj->mat->diff * shade;
color->r += value * obj->mat->color.r * light->color.r / 255;
color->g += value * obj->mat->color.g * light->color.g / 255;
color->b += value * obj->mat->color.b * light->color.b / 255;
}
}
if (obj->mat->spec > 0.0f)
{
dot = vector_dot(ray->dir, substract_vector(&l_norm, &l_norm, multiply_vector_value(&norm, &norm, 2.0f * vector_dot(&l_norm, &norm))));
if (dot > 0)
{
value = powf(dot, 20) * obj->mat->spec * shade;
color->r += value * obj->mat->color.r * light->color.r / 255;
color->g += value * obj->mat->color.g * light->color.g / 255;
color->b += value * obj->mat->color.b * light->color.b / 255;
}
}
}
}
static void determine_color(t_env *e, t_ray *ray, t_color *color, t_vec3 *pi, t_obj *obj)
{
int i;
i = 0;
while (e->lights && e->lights[i])
{
check_objects(e, ray, color, e->lights[i], obj, pi);
i++;
}
if (i == 0)
set_color(color, obj->mat->color.r * AMB_LIGHT,
obj->mat->color.g * AMB_LIGHT, obj->mat->color.b * AMB_LIGHT);
}
t_obj *ray_trace(t_env *e, t_ray *ray, t_color *color, int depth, float refrind, double *dist)
{
t_obj *temp;
int i;
t_vec3 pi;
int tres;
int result;
if (depth > TRACE_DEPTH)
return (NULL);
temp = NULL;
*dist = 1000000.0f;
i = 0;
while (e->objs[i])
{
if ((tres = intersect_primitive(e->objs[i], ray, dist)))
{
temp = e->objs[i];
result = tres;
}
i++;
}
if (temp)
{
add_vector(&pi, ray->ori, multiply_vector_value(&pi, ray->dir,
*dist));
determine_color(e, ray, color, &pi, temp);
calculate_reflection(e, ray, color, &pi, temp, refrind, depth);
calculate_refraction(e, ray, color, &pi, temp, refrind, depth, result);
return (temp);
}
return (NULL);
}
void draw_scene(t_env *e, int yy, int endy)
{
double dist;
float sx;
float sy;
t_obj *last_prim;
t_obj *prim;
float progres;
t_ray *ray;
t_color color;
last_prim = NULL;
for (int x = 0; x < WIN_WIDTH; x++)
{
for (int y = yy; y < endy; y++)
{
set_color(&color, 0, 0, 0);
get_sx_sy_aliasing(&sx, &sy, x, y, 0, 0);
ray = make_ray(e, sx, sy);
prim = ray_trace(e, ray, &color, 1, 1.0f, &dist);
if (prim != last_prim || SUPERSAMPLING)
{
last_prim = prim;
set_color(&color, 0, 0, 0);
prim = apply_antialiasing(e, &color, x, y, &dist);
}
put_pixel_to_img(e, &color, x, y);
if (ray)
free_ray(&ray);
}
progres = ((float)x / (float)WIN_WIDTH) * 100;
if ((int)progres == progres)
cout<<setprecision(2)<<fixed<<progres<<"%...\n";
}
}