]> Dogcows Code - chaz/rasterize/blobdiff - vec.h
add opengl support
[chaz/rasterize] / vec.h
diff --git a/vec.h b/vec.h
index 2c1269dccc88eb567e1962ecbcda2d8e5d082f56..15de4845c5f87843f4c8a37390d33e385b4b0ae8 100644 (file)
--- a/vec.h
+++ b/vec.h
@@ -57,10 +57,21 @@ vec_t vec_new(scal_t x, scal_t y, scal_t z)
     return vec_new2(x, y, z, S(1.0));
 }
 
-#define VEC_ZERO    vec_new(S(0.0), S(0.0), S(0.0))
-#define VEC_ORTHO_X vec_new(S(1.0), S(0.0), S(0.0))
-#define VEC_ORTHO_Y vec_new(S(0.0), S(1.0), S(0.0))
-#define VEC_ORTHO_Z vec_new(S(0.0), S(0.0), S(1.0))
+#define VEC_ZERO      vec_new(S(0.0), S(0.0), S(0.0))
+#define VEC_ORTHO_X   vec_new(S(1.0), S(0.0), S(0.0))
+#define VEC_ORTHO_Y   vec_new(S(0.0), S(1.0), S(0.0))
+#define VEC_ORTHO_Z   vec_new(S(0.0), S(0.0), S(1.0))
+#define VEC_ZERO_FREE vec_new2(S(0.0), S(0.0), S(0.0), S(0.0))
+
+
+/*
+ * Get a pointer to the vector data.
+ */
+INLINE_MAYBE
+const scal_t* vec_data(const vec_t* v)
+{
+    return &v->x;
+}
 
 
 /*
@@ -106,6 +117,21 @@ bool vec_isequal(vec_t a, vec_t b)
     return (a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w);
 }
 
+/*
+ * Determine whether or not two vectors are mostly equal.
+ */
+INLINE_MAYBE
+bool vec_isequal2(vec_t a, vec_t b, scal_t epsilon)
+{
+    return scal_isequal2(a.x, b.x, epsilon) &&
+           scal_isequal2(a.y, b.y, epsilon) &&
+           scal_isequal2(a.z, b.z, epsilon) &&
+           scal_isequal2(a.w, b.w, epsilon);
+}
+
+/*
+ * Determine if one vector is "less than" another, for purposes of sorting.
+ */
 INLINE_MAYBE
 int vec_compare(vec_t a, vec_t b)
 {
@@ -140,6 +166,15 @@ vec_t vec_add(vec_t a, vec_t b)
     return a;
 }
 
+/*
+ * Add three vectors together.
+ */
+INLINE_MAYBE
+vec_t vec_add2(vec_t a, vec_t b, vec_t c)
+{
+    return vec_add(vec_add(a, b), c);
+}
+
 /*
  * Subtract a vector from another vector.
  */
@@ -224,5 +259,36 @@ vec_t vec_homodiv(vec_t v)
 }
 
 
+/*
+ * Interpolate smoothly between three vectors with barycentric coordinates.
+ */
+INLINE_MAYBE
+vec_t vec_interp(vec_t v1, vec_t v2, vec_t v3, scal_t b[3])
+{
+    return vec_new(v1.x * b[0] + v2.x * b[1] + v3.x * b[2],
+                   v1.y * b[0] + v2.y * b[1] + v3.y * b[2],
+                   v1.z * b[0] + v2.z * b[1] + v3.z * b[2]);
+}
+
+/*
+ * Interpolate smoothly between three texture coordinates with barycentric
+ * coordinates and perspective correction.
+ */
+INLINE_MAYBE
+vec_t vec_tinterp(vec_t v1, vec_t v2, vec_t v3, scal_t b[3])
+{
+    scal_t denom = (S(1.0) / v1.w) * b[0]
+                 + (S(1.0) / v2.w) * b[1]
+                 + (S(1.0) / v3.w) * b[2];
+    return vec_new(scal_clamp(((v1.x / v1.w) * b[0]
+                             + (v2.x / v2.w) * b[1]
+                             + (v3.x / v3.w) * b[2]) / denom, S(0.0), S(1.0)),
+                   scal_clamp(((v1.y / v1.w) * b[0]
+                             + (v2.y / v2.w) * b[1]
+                             + (v3.y / v3.w) * b[2]) / denom, S(0.0), S(1.0)),
+                   S(0.0));
+}
+
+
 #endif // _VEC_H_
 
This page took 0.022965 seconds and 4 git commands to generate.