kporceil
The Ray Tracer Challenge: refraction issue: passed all the tests but at the render it doesnt work like expected
Hello,
first of all, thanks for the book, it helped me a lot.
I just encountered an issue with the refraction, i have implemented the tests and made them all pass, but when i want to render with refraction, the render is not really what i think it should be.
Here some picture :
the yellow sphere have a transparancy of 1 and a refractive_index of 1
here is my main code :
int main(void)
{
t_world world = world_create();
world.lights_count = 1;
world.objs_count = 6;
world.objs = malloc(sizeof(t_shape) * world.objs_count);
world.lights = malloc(sizeof(t_plight) * world.lights_count);
world.objs[0] = sphere(0);
world.objs[0].material.pat = pattern(GRADIENT, color(0.1, 0.5, 1), color(0.1, 1, 0.5));
pattern_set_transform(&world.objs[0].material.pat, matrix_mult(matrix_translation(-1, 0, 0), matrix_scaling(2, 2, 2)));
world.objs[0].material.color = color(0.1, 1, 0.5);
world.objs[0].material.diffuse = 0.7;
world.objs[0].material.specular = 0.3;
shape_set_matrix(world.objs, matrix_translation(-0.5, 1, 0.5));
world.objs[1] = sphere(1);
world.objs[1].material.color = color(0.5, 1, 0.1);
world.objs[1].material.diffuse = 0.7;
world.objs[1].material.specular = 0.3;
shape_set_matrix(world.objs + 1, matrix_mult(matrix_translation(1.2, 0.7, -0.5), matrix_scaling(0.7, 0.7, 0.7)));
world.objs[2] = sphere(2);
world.objs[2].material.transparency = 1;
world.objs[2].material.refractive_index = 1;
world.objs[2].material.color = color(1, 0.8, 0.1);
world.objs[2].material.diffuse = 0.7;
world.objs[2].material.specular = 0.3;
shape_set_matrix(world.objs + 2, matrix_mult(matrix_translation(-1.5, 0.33, -0.75), matrix_scaling(0.33, 0.33, 0.33)));
world.objs[3] = plane(3);
world.objs[3].material.pat = pattern(CHECKER, color(0, 0, 0), color(1, 1, 1));
world.objs[3].material.color = color(0, 0, 0);
world.objs[4] = plane(4);
world.objs[4].material.color = color(0.7, 0.4, 0.3);
world.objs[4].material.ambient = 0.6;
shape_set_matrix(world.objs + 4, matrix_mult(matrix_translation(0, 0, 12), matrix_mult(matrix_y_rotation(-(M_PI/12)), matrix_z_rotation(M_PI/2))));
world.objs[5] = plane(5);
world.objs[5].material.color = color(0.2, 0.6, 0.4);
world.objs[5].material.ambient = 0.6;
shape_set_matrix(world.objs + 5, matrix_mult(matrix_translation(-3, 0, 0), matrix_mult(matrix_y_rotation(M_PI/12), matrix_z_rotation(M_PI/2))));
world.lights[0] = point_light(point(-2, 10, -5), color(1, 1, 1));
t_camera cam = camera(1920, 1080, M_PI / 2);
camera_set_transform(&cam, view_transform(point(0, 1, -7), point(0, 1, 0), vector(0, 1, 0)));
t_canva image = render(cam, world);
if (!image.canva)
return (1);
(void)image;
char *ppm = canva_to_ppm(image);
write_file("render/test.ppm", ppm);
free(ppm);
free(image.canva);
free(world.objs);
free(world.lights);
}
Here my refracted_color function :
t_color refracted_color(t_world world, t_precomp comps, size_t remaining)
{
double n_ratio;
double cos_i;
double cos_t;
double sin2_t;
t_color c;
if (comps.obj->material.transparency == 0 || remaining == 0)
return (color(0, 0, 0));
n_ratio = comps.n1 / comps.n2;
cos_i = dot(comps.eyev, comps.normalv);
sin2_t = n_ratio * n_ratio * (1 - cos_i * cos_i);
if (sin2_t > 1)
return (color(0, 0, 0));
cos_t = sqrt(1.0 - sin2_t);
color_at(world, ray(comps.under_point, tuple_substract(tuple_scalar_mult
(comps.normalv, n_ratio * cos_i - cos_t), tuple_scalar_mult
(comps.eyev, n_ratio))), &c, remaining - 1);
return (color_scalar_mult(c, comps.obj->material.transparency));
}
I don’t really know how to get the refraction worked, if something can help please !
First Post!
kporceil
I solved it ! I just realized that I compute under_point before normal vector inversion.
Popular Pragmatic Bookshelf topics
Title: Web Development with Clojure, Third Edition, pg 116
Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
Hi everyone!
There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
Title: Hands-on Rust: question about get_component (page 295)
(feel free to respond. “You dug you’re own hole… good luck”)
I have somet...
New
Page 28: It implements io.ReaderAt on the store type.
Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt?
...
New
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
When installing Cards as an editable package, I get the following error:
ERROR: File “setup.py” not found. Directory cannot be installe...
New
Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New
Modern front-end development for Rails, second edition - Struggling to get the first chapter to work
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
Getting an error when installing the dependencies at the start of this chapter:
could not compile dependency :exla, "mix compile" failed...
New
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New
Other popular topics
Machine learning can be intimidating, with its reliance on math and algorithms that most programmers don't encounter in their regular wor...
New
Write Elixir tests that you can be proud of. Dive into Elixir’s test philosophy and gain mastery over the terminology and concepts that u...
New
poll
poll
Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
There’s a whole world of custom keycaps out there that I didn’t know existed!
Check out all of our Keycaps threads here:
https://forum....
New
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
Author Spotlight
Erin Dees
@undees
Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc.
However, I don’t...
New
This is cool!
DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON
We just witnessed something incredible: the largest open-s...
New
Background
Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New
Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /html
- /opensuse
- /deepseek
- /zig
- /centos
- /php
- /scala
- /react-native
- /lisp
- /sublime-text
- /textmate
- /nixos
- /debian
- /agda
- /django
- /deno
- /kubuntu
- /arch-linux
- /nodejs
- /spring
- /ubuntu
- /revery
- /manjaro
- /lua
- /julia
- /diversity
- /markdown
- /v










