# Lightmapper ray casting

This topic is 2863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, I am writing a lightmapper. As part of my shadow calculation stage I take a ray from the lumel to the light and check for intersections with faces. Unfortunately an intersection is called even if it is behind the ray origin + direction. So objects that are behind my face being lightmapped are marked as an intersection and the current lumel is set to shadow despite it being ridiculous that it would occlude it.

Above is a crude diagram of what is going on. the face is behind the face being lightmapped intersects the ray so my lumel is marked as shadow.

So how can I avoid this? My first thought was that I could check the distance between the intersection point and the light VS the lumel and light but I think this would be less then optimal. Is there another way I can determine if it is behind it or discount intersections behind my ray origin and direction or is distance the best bet?

Here is my code for ray intersection to help:

static inline BOOL rayIntersectsTriangle(point3D origin, point3D direction, plane3D triangle){
point3D e1,e2,h,s,q;
float a,f,u,v;

e1 = vectorSubtract(triangle.secondPoint, triangle.firstPoint);
e2 = vectorSubtract(triangle.thirdPoint, triangle.firstPoint);

h = crossProduct(direction, e2);

a = e1.x * h.x + e1.y * h.y + e1.z * h.z;

if (a > -0.00001 && a < 0.00001)
return NO;

f = 1 / a;

s = vectorSubtract(origin, triangle.firstPoint);

u = f * (s.x * h.x + s.y * h.y + s.z * h.z);

if (u < 0.0 || u > 1.0)
return NO;

q = crossProduct(s, e1);

v = f * (direction.x * q.x + direction.y * q.y + direction.z * q.z);

if (v < 0.0 || u + v > 1.0)
return NO;

return YES;
}

##### Share on other sites
You'll want to find the "t" parameter for the hit, ie how far along the ray the hitpoint is (hitpoint = origin + t*direction). This parameter should be positive to prevent the situation you're in.

In addition you should make sure that the hit is not beyond the light source. Easiest way to do this is to normalize the ray direction. In that case, the "t" parameter should be less or equal to the distance to the light (plus a small epsilon).

You can see how to calculate your "t" parameter here (it's very similar to the code you got now): http://jgt.akpeters.com/papers/MollerTrumbore97/code.html

Thanks!

1. 1
2. 2
Rutin
22
3. 3
4. 4
5. 5

• 16
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632928
• Total Posts
3009264
• ### Who's Online (See full list)

There are no registered users currently online

×