1
votes

I want to map textures triangles for 3D rendering in my ray tracer. I am using barycentric coordinates to locate points on the triangles. But the result isn't correct. This is what I did :

3 triangle vertices : a b c

1 intersection point (Raytracing) : p

vertex coordinates are a.x a.y a.z / b.x b.y b.z / c.x c.y c.z

texture coordinates are a.tx a.ty / b.tx b.ty / c.tx c.ty

Firstly , I get the distance between the intersection point and one of the triangle vertices:

d1 = get_distance(p, a);
d2 = get_distance(p, b);
d3 = get_distance(p, c);

Secondly , I calculate the barycentric ratio for each vertices :

r1 = d1 / (d1 + d2 + d3);
r2 = d2 / (d1 + d2 + d3);
r3 = d3 / (d1 + d2 + d3);

And finally , I determine the coordinates of the point on the image :

x_img = (r1 * a.tx) + (r2 * b.tx) + (r3 * c.tx);
y_img = (r1 * a.ty) + (r2 * b.ty) + (r3 * c.ty);

But it doesn't work. Can you explain me why please ?

The render of Kik-Ass Head :

![kickass]: http://imgur.com/3LAjrWm

1
Can you define what you mean by "not correct"? - Arne Bergene Fossaa
When I do a render the color are not in the right place - Atom
Do you have an example output image? - Arne Bergene Fossaa
@ArneBergeneFossaa Yes , I just need 3 minutes to have a right render :s - Atom

1 Answers

0
votes

Your barycentric calculation seems wrong - from how I ready your code, a component will influence more the longer away it is from the vertex.

I guess it should be something on the form

r1 = (1/d1) / ( (1/d1) + (1/d2) + (1/d3) )

although that would have some problems with the divisor being zero or close to zero. Have a look at http://en.wikipedia.org/wiki/Barycentric_coordinate_system#Converting_to_barycentric_coordinates for a better solution to how to calculate this. enter image description hereenter image description hereenter image description here