# Raytracer texture mapping (to triangle mesh) leaving artifacts

So I am trying to get OBJ loading working in my raytracer. Loading OBJs works fine, but I am having some trouble with getting the texture mapping working.

Here is an image of my result. It is supposed to be a black sphere with colored "latitude and longitude" lines, with a black spot in the middle. But it seems like every second triangle is left black. You can see the result here:

My prof said that it looks like the normals are backwards, but I don't think that is the case because the shape is still being hit - and the color of the "wrong" triangles is the color of background color of the texture (ie. black in this case).

When I load the OBJ, each vertex has a UV coord associated with it. What I do to get a UV coord when a ray hits the shape is as follows:

T: the triangle that was hit
hp: where on the triangle the ray hit
v1,v2,v3: the vertices of the triangle, each has a UV coord UV1, UV2, UV3

find the distance to each v[i] from hp (d1,d2,d3 respectively)
find the weight of each of these (w1 = d1/(d1+d2+d3), same for d2,d3)
find the weighted UV coord: UV1/w1 + UV2/w2 + UV3/w3

find the pixel color based on this weighted coord


Does anyone have any ideas what might be going on? I can post parts of my code if you think that would help.

• Have you verified that the OBJ loader is working correctly, i.e. that the UVs in your model data are correct after loading? Can you reproduce the issue with a much simpler model, e.g. one single quad, and step through it in the debugger to see where it is going wrong? – Nathan Reed Apr 9 '13 at 23:12

## 2 Answers

What kind of shading do you use. I assume some kind of blinn-phong. If that's the case, your shading depends on the dot product of your normal and the half vector of reflected light: (http://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model)

The dot-product will be below zero on your triangles if your prof is right and your normals are pointing in the wrong direction. Blinn-Phong assumes no reflection in this case making your triangles pitch black.

• I am indeed using blinn-phong. I have it turned off right now to debug this issue - but you are totally right, if the normals were off, the shading would be wrong! And the shading is right, so it must be something else. I have an idea I am going to try. Thansk! – Toadums Apr 9 '13 at 19:01

I am not sure this is the actual answer to your problem (although it might be related).

I noticed that you are generating UV of the hit in the triangle by the (normalised) weighted sum of distances from the vertices.

This is wrong.

Think what you get if your ray exactly hits v1: d1=0 therefore w1=0 and you get a divide by zero.

What you should do is get the distance to the opposite edge. So d1 = distance to edge created between v2 and v3.

Normalise the result: d1,d2,d3 /= d1+d2+d3

Then UV(hit) = UV(1)*d1 + UV(2)*d2 + UV(3)*d3 where d1=1, d2=0, d3=0 which gives exactl y the right answer.

This is called barycentric coordinates. It may not solve your immediate bug, but it will fix your uv generation.