-1

I wrote the light diffusing code and it's just not working I dont know why

this is the code I wrote for diffusing

            t_vec pi = vec_add(ray.org,vec_times_double(ray.dir, t));
            t_vec hp = vec_diff(light.pos, pi);
            t_vec normal = get_sphers_normal(sp, pi);
            double dot = vec_dot(normalize_vect(normal),normalize_vect(hp));
            printf("hitpoint : %lf\n", dot);
            put_pixel(mlx.img, x, y, rgb_to_int(sp.color)*double_abs(dot), resolution.width);

this is the result I got

  • Hi, even though I believe I spotted your problem (see my answer below) I strongly recommend to, at least, include the declarations of the relevant functions. Even better, post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Yakov Galka Dec 27 '19 at 19:06

1 Answers1

1

In this line:

 put_pixel(mlx.img, x, y, rgb_to_int(sp.color)*double_abs(dot), resolution.width);
                          ^-----------------------------------

You seem to be multiplying an integer by a double. I presume that put_pixel expects a 32-bit integer encoding the RGB color, in which case your double gets converted back to int, but in an un-meaningful way, giving those color bands. For example, if sp.color = { 255., 0., 0. } is a red surface, rgb_to_int converts it to 0xff0000, multiplying it by 0.00390625 (dimly lit surface) and converting back to int gives 0x00ff00, which is a bright green rather than a dark red.

You should rather use your vector times scalar function on the argument to rgb_to_int:

rgb_to_int(vec_times_double(sp.color, double_abs(dot)))

I assume here that sp.color is of type t_vec. If it's not then adjust your code accordingly.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220