medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 303874ee59bcf29e417ee5b21c55387b32fe90e4
parent ed1ec2f1dcadfe26ec9e1722438daa1701b6b76e
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Mar 18 11:18:48 +0200

M-T ray_vs_triangle

Diffstat:
heightmap.cc | 25++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/heightmap.cc b/heightmap.cc @@ -155,15 +155,26 @@ v3 triangle_normal( v3 a, v3 b, v3 c ) { } bool ray_vs_triangle( v3 ray_origin, v3 ray_dir, v3 p0, v3 p1, v3 p2, float * t ) { - v3 plane_normal = triangle_normal( p0, p1, p2 ); - float plane_dist = dot( p0, plane_normal ); + v3 e01 = p1 - p0; + v3 e02 = p2 - p0; + v3 p = cross( ray_dir, e02 ); + float det = dot( e01, p ); - if( !ray_vs_plane( ray_origin, ray_dir, plane_normal, plane_dist, t ) ) { - return false; - } + const float epsilon = 0.001f; + if( fabsf( det ) < epsilon ) return false; + + float inv_det = 1.0f / det; + + v3 d = ray_origin - p0; + float u = dot( d, p ) * inv_det; + if( u < 0 || u > 1 ) return false; - v3 xpoint = ray_origin + ray_dir * *t; - return point_in_triangle( xpoint, p0, p1, p2 ); + v3 q = cross( d, e01 ); + float v = dot( ray_dir, q ) * inv_det; + if( u < 0 || u + v > 1 ) return false; + + *t = dot( e01, q ) * inv_det; + return true; } AABBu32 AABBu32::quadrant( int q ) const {