kporceil

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 !

Where Next?

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=> (create-migration "guestbook") Execution error (Ill...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New

Other popular topics Top

wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New
Margaret
Ask Me Anything with Mark Volkmann @mvolkmann On February 24 and 25, we are giving you a chance to ask questions of PragProg author M...
New
mindriot
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

Sub Categories: