Skip to content

Commit

Permalink
add soft shadows
Browse files Browse the repository at this point in the history
  • Loading branch information
MNasybullin committed Sep 21, 2020
1 parent 0471ace commit 833a43a
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: sdiego <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/06 10:40:08 by aannara #+# #+# #
# Updated: 2020/09/20 17:07:30 by sdiego ### ########.fr #
# Updated: 2020/09/21 20:14:43 by sdiego ### ########.fr #
# #
# **************************************************************************** #

Expand Down
23 changes: 20 additions & 3 deletions include/rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sdiego <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 14:12:41 by sdiego #+# #+# */
/* Updated: 2020/09/20 18:57:45 by sdiego ### ########.fr */
/* Updated: 2020/09/21 20:51:43 by sdiego ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -192,7 +192,7 @@ typedef struct s_comps
t_vec normalv;
t_vec reflectv;
int inside;
int shadow;
double shadow;
t_vec over_point;
t_vec under_point;
double n1;
Expand All @@ -209,6 +209,15 @@ typedef struct s_light
{
t_color intensity;
t_vec pos;
t_vec corner;
t_vec uvec;
int usteps;
t_vec vvec;
int vsteps;
int samples;

double jetter[10];
int jetter_count;
} t_light;

typedef struct s_ray
Expand Down Expand Up @@ -276,6 +285,7 @@ t_color color(double r, double g, double b);
t_color add_col(t_color a1, t_color a2);
t_color sub_col(t_color a1, t_color a2);
t_color mult_col(t_color a, double b);
t_color divide_col(t_color a, int b); //
t_color hadamard_prod(t_color a1, t_color a2);
int identic_m_4(t_matrix a, t_matrix b);
t_matrix matrix_mult(t_matrix a, t_matrix b);
Expand Down Expand Up @@ -347,7 +357,7 @@ t_ray ray_for_pixel(t_camera *camera, int px, int py);
void render(t_sdl *sdl, t_camera camera, t_world world);

//shadow
int is_shadow(t_world w, t_vec p);
int is_shadow(t_world w, t_vec light_pos, t_vec p);
//shape
t_vec sp_normal_at(t_shape s, t_vec local_point);
void push_obj(void *obj, int (*loc_norm)(void *, t_vec, t_vec*),
Expand Down Expand Up @@ -408,4 +418,11 @@ t_trian set_trian(t_vec p1, t_vec p2, t_vec p3);
int normal_at_trian(void *v_s, t_vec world_point, t_vec *n);
t_x_t intersect_trian(void *v_s, t_ray r, t_x_t x, int obj_n);



double intensity_at(t_world w, t_vec p);
t_light area_light(t_vec corner, t_vec full_uvec, int usteps, t_vec full_vvec, int vsteps, t_color color);
t_vec point_on_light(t_light *l, int u, int v);


#endif
5 changes: 2 additions & 3 deletions src/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sdiego <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/08 18:08:48 by sdiego #+# #+# */
/* Updated: 2020/09/20 18:55:19 by sdiego ### ########.fr */
/* Updated: 2020/09/21 13:55:45 by sdiego ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -82,7 +82,7 @@ void draw(t_treads *treads)
int y;
t_ray r;
t_color col;
int remaining = 5;
int remaining = 5; // повторений рекурсии в отражении

y = treads->start;
while (y < treads->finish)
Expand All @@ -107,7 +107,6 @@ void draw(t_treads *treads)

void render(t_sdl *sdl, t_camera camera, t_world world)
{
int remaining = 5; // повторений рекурсии в отражении
pthread_t threads[THREADS];
t_treads htreads[THREADS];
int i = 0;
Expand Down
10 changes: 9 additions & 1 deletion src/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sdiego <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/01 15:32:12 by sdiego #+# #+# */
/* Updated: 2020/09/20 19:13:04 by sdiego ### ########.fr */
/* Updated: 2020/09/21 19:55:35 by sdiego ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,6 +50,14 @@ t_color mult_col(t_color a, double b)
return (a);
}

t_color divide_col(t_color a, int b)
{
a.r = a.r / b;
a.g = a.g / b;
a.b = a.b / b;
return (a);
}

t_color hadamard_prod(t_color a1, t_color a2)
{
t_color b;
Expand Down
133 changes: 125 additions & 8 deletions src/light.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,54 @@
/* By: sdiego <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/05 16:31:01 by sdiego #+# #+# */
/* Updated: 2020/09/20 18:52:29 by sdiego ### ########.fr */
/* Updated: 2020/09/21 20:37:39 by sdiego ### ########.fr */
/* */
/* ************************************************************************** */

#include "../include/rt.h"

double next(t_light *l)
{
double jetter;

if (l->jetter_count > 9)
l->jetter_count = 0;
jetter = l->jetter[l->jetter_count];
l->jetter_count++;
return (jetter);
}

t_vec point_on_light(t_light *l, int u, int v)
{
/*
double a = u + next(l);
double b = v + next(l);
*/
double a = u + 0.5;
double b = v + 0.5;

t_vec umult = mult(l->uvec, a);
t_vec vmult = mult(l->vvec, b);
t_vec uv = add(umult, vmult);
return (add(l->corner, uv));
}

t_light area_light(t_vec corner, t_vec full_uvec, int usteps, t_vec full_vvec, int vsteps, t_color color)
{
t_light l;

l.intensity = color;
l.corner = corner;
l.uvec = divi(full_uvec, usteps);
l.usteps = usteps;
l.vvec = divi(full_vvec, vsteps);
l.vsteps = vsteps;
l.samples = usteps * vsteps;
l.pos = add(divi(full_uvec, 2), divi(full_vvec, 2));
l.pos.c[3] = 1;
return (l);
}

t_light point_light(t_color color, t_vec pos)
{
t_light l;
Expand Down Expand Up @@ -85,7 +127,39 @@ t_material default_material(void)
return(ambient);
}*/

int is_shadow(t_world w, t_vec p)
double intensity_at(t_world w, t_vec p)
{
double total = 0.0;
int usteps = w.light[w.light_count].usteps;
int vsteps = w.light[w.light_count].vsteps;
int u = 0;
int v = 0;
while (v < vsteps)
{
u = 0;
while (u < usteps)
{
t_vec light_position = point_on_light(&w.light[w.light_count], u, v);
if (is_shadow(w, light_position ,p) == 0)
total = total + 1.0;
u++;
}
v++;
}
return (total / w.light[w.light_count].samples);
}

/*
double intensity_at(t_world w, t_vec p)
{
int shadow;
shadow = is_shadow(w, w.light[w.light_count].pos, p);
return (shadow == 1 ? 0.0 : 1.0);
}
*/

int is_shadow(t_world w, t_vec light_pos, t_vec p)
{
t_vec v;
t_vec direction;
Expand All @@ -94,7 +168,7 @@ int is_shadow(t_world w, t_vec p)
t_x_t x;
int hit_obj;

v = sub(w.light[w.light_count].pos, p);
v = sub(light_pos, p);
distance = magnitude(v);
direction = normalize(v);
r = set_ray(p, direction);
Expand Down Expand Up @@ -129,19 +203,61 @@ t_color lighting(t_material *m, t_world w, t_comps c)
m->color = (*m->pattern_at)(m->p, *w.obj_ar[c.obj].transform, c.over_point);
}
effective_color = hadamard_prod(m->color, w.light[w.light_count].intensity);
light_v = normalize(sub(w.light[w.light_count].pos, c.over_point));
ambient = mult_col(effective_color, m->ambient);
light_dot_normal = dot(light_v, c.normalv);
if (c.shadow == 0)
//light_v = normalize(sub(w.light[w.light_count].pos, c.over_point));
//light_dot_normal = dot(light_v, c.normalv);
if (c.shadow > 0.0) //
{
t_color sum = color(0,0,0);
int usteps = w.light[w.light_count].usteps;
int vsteps = w.light[w.light_count].vsteps;
int u = 0;
int v = 0;
while (v < vsteps)
{
u = 0;
while (u < usteps)
{
t_vec light_position = point_on_light(&w.light[w.light_count], u, v);

light_v = normalize(sub(light_position, c.over_point));
light_dot_normal = dot(light_v, c.normalv);
if (light_dot_normal < 0)
{
diffuse = color(0,0,0);
specular = color(0,0,0);
}
else
{
diffuse = mult_col(mult_col(mult_col(effective_color, m->diffuse), light_dot_normal), c.shadow);

reflect_v = reflect(neg(light_v), c.normalv);
reflect_dot_eye = dot(reflect_v, c.eyev);
if (reflect_dot_eye <= 0)
specular = color(0,0,0);
else
{
factor = powf(reflect_dot_eye, m->shininess);
specular = mult_col(mult_col(mult_col(w.light[w.light_count].intensity, m->specular), factor), c.shadow);
}
}
sum = add_col(add_col(sum, diffuse), specular);
u++;
}
v++;
}
return (add_col(mult_col(divide_col(sum, w.light[w.light_count].samples),c.shadow), ambient));
/*
light_v = normalize(sub(w.light[w.light_count].pos, c.over_point));
light_dot_normal = dot(light_v, c.normalv);
if (light_dot_normal < 0)
{
diffuse = color(0,0,0);
specular = color(0,0,0);
}
else
{
diffuse = mult_col(mult_col(effective_color, m->diffuse), light_dot_normal);
diffuse = mult_col(mult_col(mult_col(effective_color, m->diffuse), light_dot_normal), c.shadow);
reflect_v = reflect(neg(light_v), c.normalv);
reflect_dot_eye = dot(reflect_v, c.eyev);
Expand All @@ -150,10 +266,11 @@ t_color lighting(t_material *m, t_world w, t_comps c)
else
{
factor = powf(reflect_dot_eye, m->shininess);
specular = mult_col(mult_col(w.light[w.light_count].intensity, m->specular), factor);
specular = mult_col(mult_col(mult_col(w.light[w.light_count].intensity, m->specular), factor), c.shadow);
}
}
return (add_col(add_col(ambient, diffuse), specular));
*/
}
else
return(ambient);
Expand Down
Loading

0 comments on commit 833a43a

Please sign in to comment.