Browse Source

Fix division by zero in mix_colors3

rexim 3 years ago
parent
commit
8fe82d1a7f
5 changed files with 10 additions and 5 deletions
  1. 1 0
      demos/vc.c
  2. 9 5
      olive.c
  3. BIN
      wasm/3d.wasm
  4. BIN
      wasm/squish.wasm
  5. BIN
      wasm/triangle.wasm

+ 1 - 0
demos/vc.c

@@ -147,6 +147,7 @@ static size_t scaled_down_width = 0;
 static size_t scaled_down_height = 0;
 static size_t scaled_down_height = 0;
 static char *char_canvas = 0;
 static char *char_canvas = 0;
 
 
+// TODO: use ANSI terminal colors for color_to_char
 char color_to_char(uint32_t pixel)
 char color_to_char(uint32_t pixel)
 {
 {
     size_t r = OLIVEC_RED(pixel);
     size_t r = OLIVEC_RED(pixel);

+ 9 - 5
olive.c

@@ -387,12 +387,16 @@ uint32_t mix_colors3(uint32_t c1, uint32_t c2, uint32_t c3, int t1, int t2, int
     int64_t b3 = OLIVEC_BLUE(c3);
     int64_t b3 = OLIVEC_BLUE(c3);
     int64_t a3 = OLIVEC_ALPHA(c3);
     int64_t a3 = OLIVEC_ALPHA(c3);
 
 
-    int64_t r4 = (r1*t1 + r2*t2 + r3*t3)/den;
-    int64_t g4 = (g1*t1 + g2*t2 + g3*t3)/den;
-    int64_t b4 = (b1*t1 + b2*t2 + b3*t3)/den;
-    int64_t a4 = (a1*t1 + a2*t2 + a3*t3)/den;
+    if (den != 0) {
+        int64_t r4 = (r1*t1 + r2*t2 + r3*t3)/den;
+        int64_t g4 = (g1*t1 + g2*t2 + g3*t3)/den;
+        int64_t b4 = (b1*t1 + b2*t2 + b3*t3)/den;
+        int64_t a4 = (a1*t1 + a2*t2 + a3*t3)/den;
 
 
-    return OLIVEC_RGBA(r4, g4, b4, a4);
+        return OLIVEC_RGBA(r4, g4, b4, a4);
+    }
+
+    return 0;
 }
 }
 
 
 void barycentric(int x1, int y1, int x2, int y2, int x3, int y3,
 void barycentric(int x1, int y1, int x2, int y2, int x3, int y3,

BIN
wasm/3d.wasm


BIN
wasm/squish.wasm


BIN
wasm/triangle.wasm