add blended triangle_2d drawing operations for IndexedBitmaps

This commit is contained in:
Gered 2023-04-04 18:25:38 -04:00
parent d1fb0c6b2e
commit f564721e46

View file

@ -1,5 +1,6 @@
use crate::graphics::bitmap::indexed::IndexedBitmap; use crate::graphics::bitmap::indexed::IndexedBitmap;
use crate::graphics::bitmap::triangles::{edge_function, per_pixel_triangle_2d}; use crate::graphics::bitmap::triangles::{edge_function, per_pixel_triangle_2d};
use crate::graphics::blendmap::BlendMap;
use crate::math::vector2::Vector2; use crate::math::vector2::Vector2;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -8,11 +9,22 @@ pub enum IndexedTriangle2d<'a> {
position: [Vector2; 3], // position: [Vector2; 3], //
color: u8, color: u8,
}, },
SolidBlended {
position: [Vector2; 3], //
color: u8,
blendmap: &'a BlendMap,
},
SolidTextured { SolidTextured {
position: [Vector2; 3], // position: [Vector2; 3], //
texcoord: [Vector2; 3], texcoord: [Vector2; 3],
bitmap: &'a IndexedBitmap, bitmap: &'a IndexedBitmap,
}, },
SolidTexturedBlended {
position: [Vector2; 3], //
texcoord: [Vector2; 3],
bitmap: &'a IndexedBitmap,
blendmap: &'a BlendMap,
},
} }
impl IndexedBitmap { impl IndexedBitmap {
@ -28,6 +40,18 @@ impl IndexedBitmap {
|dest_pixels, _w0, _w1, _w2| *dest_pixels = *color, |dest_pixels, _w0, _w1, _w2| *dest_pixels = *color,
) )
} }
SolidBlended { position, color, blendmap } => {
per_pixel_triangle_2d(
self, //
position[0],
position[1],
position[2],
|dest_pixels, _w0, _w1, _w2| {
*dest_pixels =
if let Some(blended) = blendmap.blend(*color, *dest_pixels) { blended } else { *color };
},
)
}
SolidTextured { position, texcoord, bitmap } => { SolidTextured { position, texcoord, bitmap } => {
let inverse_area = 1.0 / edge_function(position[0], position[1], position[2]); let inverse_area = 1.0 / edge_function(position[0], position[1], position[2]);
per_pixel_triangle_2d( per_pixel_triangle_2d(
@ -42,6 +66,22 @@ impl IndexedBitmap {
}, },
) )
} }
SolidTexturedBlended { position, texcoord, bitmap, blendmap } => {
let inverse_area = 1.0 / edge_function(position[0], position[1], position[2]);
per_pixel_triangle_2d(
self, //
position[0],
position[1],
position[2],
|dest_pixels, w0, w1, w2| {
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 texel = bitmap.sample_at(u, v);
*dest_pixels =
if let Some(blended) = blendmap.blend(texel, *dest_pixels) { blended } else { texel };
},
)
}
} }
} }