include alpha component in triangle per-vertex-color blending modes

otherwise the blend is wrong (basically doesn't blend at all)
This commit is contained in:
Gered 2023-04-03 13:50:00 -04:00
parent f8d00135b2
commit ac9393d3a4

View file

@ -2,6 +2,7 @@ use crate::graphics::bitmap::rgb::RgbaBitmap;
use crate::graphics::bitmap::triangles::{edge_function, per_pixel_triangle_2d};
use crate::graphics::color::{from_rgb32_normalized, multiply_argb32, tint_argb32, to_rgb32_normalized, BlendFunction};
use crate::math::vector2::Vector2;
use crate::prelude::{from_argb32_normalized, to_argb32_normalized};
#[derive(Debug, Clone, PartialEq)]
pub enum RgbaTriangle2d<'a> {
@ -110,19 +111,20 @@ impl RgbaBitmap {
}
SolidMultiColorBlended { position, color, blend } => {
let inverse_area = 1.0 / edge_function(position[0], position[1], position[2]);
let (r1, g1, b1) = from_rgb32_normalized(color[0]);
let (r2, g2, b2) = from_rgb32_normalized(color[1]);
let (r3, g3, b3) = from_rgb32_normalized(color[2]);
let (a1, r1, g1, b1) = from_argb32_normalized(color[0]);
let (a2, r2, g2, b2) = from_argb32_normalized(color[1]);
let (a3, r3, g3, b3) = from_argb32_normalized(color[2]);
per_pixel_triangle_2d(
self, //
position[0],
position[1],
position[2],
|dest_pixels, w0, w1, w2| {
let a = (w0 * a1 + w1 * a2 + w2 * a3) * inverse_area;
let r = (w0 * r1 + w1 * r2 + w2 * r3) * inverse_area;
let g = (w0 * g1 + w1 * g2 + w2 * g3) * inverse_area;
let b = (w0 * b1 + w1 * b2 + w2 * b3) * inverse_area;
*dest_pixels = blend.blend(to_rgb32_normalized(r, g, b), *dest_pixels)
*dest_pixels = blend.blend(to_argb32_normalized(a, r, g, b), *dest_pixels)
},
)
}
@ -191,21 +193,22 @@ impl RgbaBitmap {
}
SolidTexturedMultiColoredBlended { position, texcoord, color, bitmap, blend } => {
let inverse_area = 1.0 / edge_function(position[0], position[1], position[2]);
let (r1, g1, b1) = from_rgb32_normalized(color[0]);
let (r2, g2, b2) = from_rgb32_normalized(color[1]);
let (r3, g3, b3) = from_rgb32_normalized(color[2]);
let (a1, r1, g1, b1) = from_argb32_normalized(color[0]);
let (a2, r2, g2, b2) = from_argb32_normalized(color[1]);
let (a3, r3, g3, b3) = from_argb32_normalized(color[2]);
per_pixel_triangle_2d(
self, //
position[0],
position[1],
position[2],
|dest_pixels, w0, w1, w2| {
let a = (w0 * a1 + w1 * a2 + w2 * a3) * inverse_area;
let r = (w0 * r1 + w1 * r2 + w2 * r3) * inverse_area;
let g = (w0 * g1 + w1 * g2 + w2 * g3) * inverse_area;
let b = (w0 * b1 + w1 * b2 + w2 * b3) * inverse_area;
let u = (w0 * texcoord[0].x + w1 * texcoord[1].x + w2 * texcoord[2].x) * inverse_area;
let v = (w0 * texcoord[0].y + w1 * texcoord[1].y + w2 * texcoord[2].y) * inverse_area;
let src = multiply_argb32(bitmap.sample_at(u, v), to_rgb32_normalized(r, g, b));
let src = multiply_argb32(bitmap.sample_at(u, v), to_argb32_normalized(a, r, g, b));
*dest_pixels = blend.blend(src, *dest_pixels)
},
)