0

I am trying to make my own raytracer in Javascript. So far the spheres work very well. I now want to expand the capabilities to include triangles, and from there I can go to squares, cubes and beyond. The code I have for finding intersections with triangles is as follows

function triangleIntersection(t, r) {
var norm = triangleNormal(t);
var dist = triangleDistance(t);
var a = Vector.dotProduct(r.vector, norm);

if (a === 0) {
    return -1;
} else {
    var b = Vector.dotProduct(norm, Vector.add(r.point, Vector.negative(Vector.multiply(norm, dist))));
    var d = -1 * b / a;

    var qx = Vector.scale(r.vector, d).x + r.point.x;
    var qy = Vector.scale(r.vector, d).y + r.point.y;
    var qz = Vector.scale(r.vector, d).z + r.point.z;

    var q = new Vector(qx, qy, qz);

    var ca = Vector.subtract(t.points[2], t.points[0]);
    var qa = Vector.subtract(q, t.points[0]);
    var t1 = Vector.dotProduct(Vector.crossProduct(ca, qa), norm);

    var bc = Vector.subtract(t.points[1], t.points[2]);
    var qc = Vector.subtract(q, t.points[2]);
    var t2 = Vector.dotProduct(Vector.crossProduct(bc, qc), norm);

    var ab = Vector.subtract(t.points[0], t.points[1]);
    var qb = Vector.subtract(q, t.points[1]);
    var t3 = Vector.dotProduct(Vector.crossProduct(ab, qb), norm);

    if ((t1 >= 0) && (t2 >= 0) && (t3 >= 0)) {
        return 1 * b / a;
    } else {
        return -1;
    }
}

}

Triangle objects have a point array (points[]) and item 0 is point A, item 1 is point B and item 2 is point C. The parameter t is one of these triangles. The parameter r is a ray object, with properties point which is the origin, and vector, which is the direction.

I also have these functions for finding normal and distance of a triangle.

function triangleNormal(s) {
var ca = Vector.subtract(s.points[2], s.points[0]);
var ba = Vector.subtract(s.points[1], s.points[0]);
var norm = Vector.unitVector(Vector.crossProduct(ca, ba));

return norm;
}

function triangleDistance(t) {
    return Vector.dotProduct(triangleNormal(t, 0), t.points[0]);
}

When I render my scene the triangle I use in my scene is red colored. No matter how far I move my camera back the triangle fills the whole scene red. I do not know why this happens.

Adam
  • 77
  • 7
  • 1
    Why not try the *Moller-Trumbore* algorithm? https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm – meowgoesthedog Aug 18 '17 at 19:13
  • @meowgoesthedog Okay i will try this. Thx so much :) – Adam Aug 18 '17 at 19:49
  • @meowgoesthedog Ugh the same error. i guess i will have to start from scratch again... – Adam Aug 19 '17 at 00:04
  • you should test the intersection code with some artificial cases, to make sure that it is not the problem. In fact I recommend doing unit tests like this on all stages in the pipeline until you isolate the source of error. – meowgoesthedog Aug 19 '17 at 00:14
  • see [Reflection and refraction impossible without recursive ray tracing?](https://stackoverflow.com/a/45140313/2521214) and look for `//compute ray triangle intersection` comment `v0,v1,v2` are the triangle vertexes and `ray[i].dir,ray[i].pos` is the i-th ray direction and position. – Spektre Aug 19 '17 at 07:18
  • @Spektre i am completely sure the intersection is not the problem... it must be the ray tracer itself... Hmm can you look at [the raytracer tutorial i am following](https://github.com/tmcw/literate-raytracer) and see if u can implement my code in it,but make it work? I am very stumped.. – Adam Aug 19 '17 at 15:08
  • @Adam what about the *projection* part of your code? i.e. where it converts a screen coordinate to a ray. That could be where the problem lies. – meowgoesthedog Aug 19 '17 at 16:54
  • @meowgoesthedog I checked that, even created a plane with normal 0, 1, 0, and distance -1, then sent a ray originating at 0, 10, 0 with direction 0, -1, 0 and the WinningObjectIndex (index of the collision in an array of all collisions) is still zero. I can send you my source code if you want it :) – Adam Aug 19 '17 at 17:24
  • @Adam yeah at this stage there's really no way for us to help you other than to look at your code. Post it on pastebin if you could – meowgoesthedog Aug 19 '17 at 17:44
  • @meowgoesthedog I worked on a new version. see if you can help with this, it doesnt work either... why am i so bad at making stuff.. lol but see if you can find any errors, this doesnt detect any collisions. u can use console.log(wobj); at line 173 to c what i mean. Heres the link https://pastebin.com/7864idGh – Adam Aug 19 '17 at 20:00

1 Answers1

0

An important error in your plane intersection code is this comparison operation:

a === 0

There are two things wrong with it:

  1. For ray tracing you want the ray to hit planes in front of its source, not behind it, so you need a < 0.

  2. Even if you did want the ray to hit planes behind it, you must never do equality operations between floating point values, because floating point calculations are not exact. (You must do something like abs(a) < 1e-6f or some small value)

meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40