Sign in to follow this  

Lightmapper ray casting

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?

Please help!

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 this post

Link to post
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):

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this