Triangle is not visible - PathTracing

Started by
8 comments, last by Krypt0n 8 years, 11 months ago

It looks like my triangle intersection code:


                    var EPSILON = 0.000001;
                    var e1 = this.t2.sub(this.t1);
                    var e2 = this.t3.sub(this.t1);
                    
                    var P = ray.direction.cross(e2);
                    var det = e1.dot(P);
                    
                    if(det > -EPSILON && det < EPSILON)
                        return -1.0;
                    var inv_det = 1.0 / det;
                    
                    var T = ray.origin.sub(this.t1);
                    var u = T.dot(P) * inv_det;
                    if(u < 0.0 || u > 1.0)
                        return -1.0;
                    
                    var Q = T.cross(e1);
                    
                    var v = ray.direction.dot(Q) * inv_det;
                    
                    if(v < 0.0 || u+v > 1.0)
                        return -1.0;
                    
                    var t = e2.dot(Q) * inv_det;
                    
                    if(t > EPSILON)
                        return t;
                    return -1.0;

is not working properly. If i use the triangle as an emitter it looks like the first image. But if i use it as a normal object it looks like the second image.

Any ideas?

Advertisement


If i use the triangle as an emitter it looks like the first image. But if i use it as a normal object it looks like the second image.

this means that problem is in shading part and not in ray triangle intersection.

Hey and thanks for your answer :)

Okay, maybe my triangle normal vector calculation is not correct?


                var u = this.t1.sub(this.t3);
                var v = this.t2.sub(this.t3);
                var normal = u.cross(v);
                return normal;

where do you normalize the normal?

Which vector?
the normal, for shading you need to normalize it somewhere, otherwise it would be of different size depending on the size of the triangle area.

If i change the code to this:


 var hitPoint = ray.origin.add(ray.direction.multFloat(hitDistance * 0.99));
 var normal = hitObject.normal(hitPoint).normalize();

it still doesn't work...

Check if the normal of the lower triangle is actually pointing in the right direction. If you just copied/translated/mirrored the vertices from the top triangle, the normal will be the same. Try reversing the vertex order.

blah :)

Hi and thanks for your reply,

My triangle before:


new Vector3D(-6,5.5,5),
new Vector3D(6,5.5,5),
new Vector3D(0,5.5,1)

and now:


new Vector3D(0, 5.5, 1),
new Vector3D(6, 5.5, 5),
new Vector3D(-6,5.5, 5)

but the problem still exists.

I don't have another guess, but guessing the problem is probably the most unproductive way for you of debugging it ;)

You're now at the beginning, and while beginning might seem to be the most difficult part of it, later on tiny implementation issues can lead to the most weird results that nobody has seen before and you won't get any guesses.

So, I'll rather share you the way I debug it usually.

My way to debug tracers is to add a pixel picker function, then you can click on some problematic area and call a pick function with your breakpoint inside. it also comes in handy to have a mouse-over functions that prints out the color to the console, sometimes there are just single wrong pixel and it's not trivial to click on those ;) .

once you trap into that breakpoint, you can step through the functions and you validate the outcome with your knowledge, e.g. here you know you've clicked on the triangle, so the intersection result should be 'true' right? you can step further to the secondary rays and edit the ray direction to actually point to the lightsource (as you know where it is) in your debugger and check if you hit it. once that is working, you need to check if the result is processed (aka shading) as you'd expect.

I know, this is a bit of a dive into it all, but it's a very deterministic way to find the bugs (there might be more than one and finding them by random guesses might not show up the result, so you won't even know 100% if you've fixed an issue and if a previously checked part is now working).

This topic is closed to new replies.

Advertisement