Improve code style in ray.h
2 spaces instead of 4, for indentation
Newline after return
Check if pointer is null
CGLM_INLINE
bool
glm_ray_triangle(vec3 origin, vec3 direction, vec3 v0, vec3 v1, vec3 v2, float *d) {
- const float epsilon = 0.000001;
vec3 edge1, edge2, p, t, q;
- float det, inv_det, u, v;
+ float det, inv_det, u, v, dist;
+ const float epsilon = 0.000001;
glm_vec3_sub(v1, v0, edge1);
glm_vec3_sub(v2, v0, edge2);
@@ -43,6 +43,7 @@ glm_ray_triangle(vec3 origin, vec3 direction, vec3 v0, vec3 v1, vec3 v2, float *
if (det > -epsilon && det < epsilon)
return 0;
+
inv_det = 1.0 / det;
glm_vec3_sub(origin, v0, t);
@@ -59,8 +60,16 @@ glm_ray_triangle(vec3 origin, vec3 direction, vec3 v0, vec3 v1, vec3 v2, float *
if (v < 0.0 || u + v > 1.0)
return 0;
- *d = inv_det * glm_vec3_dot(edge2, q);
- return *d > epsilon;
+ dist = inv_det * glm_vec3_dot(edge2, q);
+ if (dist > epsilon) {
+ if (d != NULL) {
+ *d = dist;
+ }
+
+ return 1;
+ }
+
+ return 0;
}
#endif
