repo: hackvr-turbo
action: commit
revision: 
path_from: 
revision_from: df48654e145c8c47567606ba3338397ce83ee6d9:
path_to: 
revision_to: 
git.thebackupbox.net
hackvr-turbo
git clone git://git.thebackupbox.net/hackvr-turbo
commit df48654e145c8c47567606ba3338397ce83ee6d9
Author: Felix (xq) Queißner 
Date:   Sat Jul 4 18:43:07 2020 +0200

    Changes rayTriangleIntersection algorithm.

diff --git a/build.zig b/build.zig
index 2a8acbe80bca3511f15d79268a3984bfc7753d58..
index ..9d3841d4ad3455ad75634126cfb5fc0b09d0cc29 100644
--- a/build.zig
+++ b/build.zig
@@ -39,6 +39,7 @@ pub fn build(b: *std.build.Builder) void {
     exe.setTarget(target);
     exe.setBuildMode(mode);

+    exe.linkLibC();
     exe.linkSystemLibrary("SDL2");
     exe.linkSystemLibrary("epoxy");

diff --git a/src/main.zig b/src/main.zig
index a04bf46568115e5ac9cbef9eb78747e561b141f2..
index ..34e66132acf038ffd3907d5edf9f3fd1155f3f35 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -778,66 +778,90 @@ fn isDataOnStdInAvailable() !bool {
     return false;
 }

-fn rayTriangleIntersect(orig: zlm.Vec3, dir: zlm.Vec3, v0: zlm.Vec3, v1: zlm.Vec3, v2: zlm.Vec3) ?f32 {
-    const kEpsilon = 1e-10;
-
-    // compute plane's normal
-    const v0v1 = v1.sub(v0);
-    const v0v2 = v2.sub(v0);
-    // no need to normalize
-    const N = v0v1.cross(v0v2); // N
-    const area2 = N.length();
-
-    // Step 1: finding P
-
-    // check if ray and plane are parallel ?
-    const NdotRayDirection = N.dot(dir);
-    if (std.math.absFloat(NdotRayDirection) < kEpsilon) { // almost 0
-        return null; // they are parallel so they don't intersect !
-    }
-
-    // compute d parameter using equation 2
-    const d = N.dot(v0);
-
-    // compute t (equation 3)
-    const t = (N.dot(orig) + d) / NdotRayDirection;
-    // check if the triangle is in behind the ray
-    if (t < 0) {
-        return null; // the triangle is behind
-    }
+fn rayTriangleIntersect(ro: zlm.Vec3, rd: zlm.Vec3, v0: zlm.Vec3, v1: zlm.Vec3, v2: zlm.Vec3) ?f32 {
+    const a = v0.sub(v1);
+    const b = v2.sub(v1);
+    const p = v0.sub(ro);
+    const n = b.cross(a);
+    const q = p.cross(rd);

-    // compute the intersection point using equation 1
-    const P = orig.add(dir.scale(t));
+    const idet = 1.0 / rd.dot(n);

-    // Step 2: inside-outside test
+    const u = q.dot(b) * idet;
+    const v = q.dot(a) * idet;
+    const t = n.dot(p) * idet;

-    // edge 0
-    const edge0 = v1.sub(v0);
-    const vp0 = P.sub(v0);
-    const C0 = edge0.cross(vp0); // vector perpendicular to triangle's plane
-    const side_0 = (N.dot(C0) < 0);
-    if (side_0)
-        return null;
-
-    // edge 1
-    const edge1 = v2.sub(v1);
-    const vp1 = P.sub(v1);
-    const C1 = edge1.cross(vp1);
-    const side_1 = (N.dot(C1) < 0);
-    if (side_1)
+    if (t < 0) {
         return null;
+    }

-    // edge 2
-    const edge2 = v0.sub(v2);
-    const vp2 = P.sub(v2);
-    const C2 = edge2.cross(vp2);
-    const side_2 = (N.dot(C2) < 0);
-    if (side_2)
+    if (u < 0 or u > 1 or v < 0 or (u + v) > 1) {
         return null;
+    }

-    return t; // this ray hits the triangle
+    return t;
 }

+// fn rayTriangleIntersect(orig: zlm.Vec3, dir: zlm.Vec3, v0: zlm.Vec3, v1: zlm.Vec3, v2: zlm.Vec3) ?f32 {
+//     const kEpsilon = 1e-10;
+
+//     // compute plane's normal
+//     const v0v1 = v1.sub(v0);
+//     const v0v2 = v2.sub(v0);
+//     // no need to normalize
+//     const N = v0v1.cross(v0v2); // N
+//     const area2 = N.length();
+
+//     // Step 1: finding P
+
+//     // check if ray and plane are parallel ?
+//     const NdotRayDirection = N.dot(dir);
+//     if (std.math.absFloat(NdotRayDirection) < kEpsilon) { // almost 0
+//         return null; // they are parallel so they don't intersect !
+//     }
+
+//     // compute d parameter using equation 2
+//     const d = N.dot(v0);
+
+//     // compute t (equation 3)
+//     const t = (N.dot(orig) + d) / NdotRayDirection;
+//     // check if the triangle is in behind the ray
+//     if (t < 0) {
+//         return null; // the triangle is behind
+//     }
+
+//     // compute the intersection point using equation 1
+//     const P = orig.add(dir.scale(t));
+
+//     // Step 2: inside-outside test
+
+//     // edge 0
+//     const edge0 = v1.sub(v0);
+//     const vp0 = P.sub(v0);
+//     const C0 = edge0.cross(vp0); // vector perpendicular to triangle's plane
+//     const side_0 = (N.dot(C0) < 0);
+//     if (side_0)
+//         return null;
+
+//     // edge 1
+//     const edge1 = v2.sub(v1);
+//     const vp1 = P.sub(v1);
+//     const C1 = edge1.cross(vp1);
+//     const side_1 = (N.dot(C1) < 0);
+//     if (side_1)
+//         return null;
+
+//     // edge 2
+//     const edge2 = v0.sub(v2);
+//     const vp2 = P.sub(v2);
+//     const C2 = edge2.cross(vp2);
+//     const side_2 = (N.dot(C2) < 0);
+//     if (side_2)
+//         return null;
+
+//     return t; // this ray hits the triangle
+// }
+
 fn invertMatrix(mat: zlm.Mat4) ?zlm.Mat4 {
     const m = @bitCast([16]f32, mat.fields);
     var inv: [16]f32 = undefined;

-----END OF PAGE-----