]> Dogcows Code - chaz/rasterize/blobdiff - tri.h
preliminary support for obj files
[chaz/rasterize] / tri.h
diff --git a/tri.h b/tri.h
index de36ae66a8988a3ddd78492e1093e9b6f3d2445e..ce0977b5c7382d7d61867b4f463c99d641effda1 100644 (file)
--- a/tri.h
+++ b/tri.h
@@ -71,6 +71,10 @@ tri_t tri_transform(tri_t t, mat_t m)
     t.a.v = mat_apply(m, t.a.v);
     t.b.v = mat_apply(m, t.b.v);
     t.c.v = mat_apply(m, t.c.v);
+    t.a.n.w = t.b.n.w = t.c.n.w = S(0.0);
+    t.a.n = vec_normalize(mat_apply(m, t.a.n));
+    t.b.n = vec_normalize(mat_apply(m, t.b.n));
+    t.c.n = vec_normalize(mat_apply(m, t.c.n));
     return t;
 }
 
@@ -93,7 +97,8 @@ tri_t tri_homodiv(tri_t t)
 INLINE_MAYBE
 vec_t tri_normal(tri_t t)
 {
-    return vec_cross(vec_sub(t.b.v, t.a.v), vec_sub(t.c.v, t.a.v));
+    vec_t n = vec_cross(vec_sub(t.b.v, t.a.v), vec_sub(t.c.v, t.a.v));
+    return n;
 }
 
 
@@ -137,15 +142,6 @@ bool tri_barycentric(tri_t t, scal_t* b, vec_t v)
 }
 
 
-/*
- * Get an interpolated z-value at the barycentric coordinates.
- */
-INLINE_MAYBE
-scal_t tri_z(tri_t t, scal_t b[3])
-{
-    return t.a.v.z * b[0] + t.b.v.z * b[1] + t.c.v.z * b[2];
-}
-
 /*
  * Find the midpoint of the triangle.
  */
@@ -168,5 +164,46 @@ color_t tri_color(tri_t t)
 }
 
 
+/*
+ * Get an interpolated z-value at the barycentric coordinates.
+ */
+INLINE_MAYBE
+scal_t tri_z(tri_t t, scal_t b[3])
+{
+    return t.a.v.z * b[0] + t.b.v.z * b[1] + t.c.v.z * b[2];
+}
+
+/*
+ * Calculate an interpolated point.
+ */
+INLINE_MAYBE
+vec_t tri_point(tri_t t, scal_t b[3])
+{
+    return vec_interp(t.a.v, t.b.v, t.c.v, b);
+}
+
+/*
+ * Calculate an interpolated normal.
+ */
+INLINE_MAYBE
+vec_t tri_normal2(tri_t t, scal_t b[3])
+{
+    return vec_normalize(vec_interp(t.a.n, t.b.n, t.c.n, b));
+}
+
+/*
+ * Calculate an entirely new vertex within the triangle based on barycentric
+ * coordinates.
+ */
+INLINE_MAYBE
+vert_t tri_interp(tri_t t, scal_t b[3])
+{
+    vert_t v = vert_new(tri_point(t, b));
+    v.c = color_interp2(t.a.c, t.b.c, t.c.c, b);
+    v.n = tri_normal2(t, b);
+    return v;
+}
+
+
 #endif // _TRI_H_
 
This page took 0.019777 seconds and 4 git commands to generate.