mirror of
https://github.com/bend-n/fimg.git
synced 2024-12-22 18:38:21 -06:00
optimize tri further
This commit is contained in:
parent
853b51fc50
commit
1c218cd1a9
|
@ -1,9 +1,7 @@
|
||||||
//! trongle drawing
|
//! trongle drawing
|
||||||
|
|
||||||
use crate::math::madd;
|
use crate::math::madd;
|
||||||
use std::cmp::{max, min};
|
|
||||||
|
|
||||||
use crate::Image;
|
use crate::Image;
|
||||||
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
|
impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
|
||||||
/// Draw a (filled) triangle
|
/// Draw a (filled) triangle
|
||||||
|
@ -17,31 +15,26 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
|
||||||
/// (1.0, 8.0), // point c
|
/// (1.0, 8.0), // point c
|
||||||
/// [255] // white
|
/// [255] // white
|
||||||
/// );
|
/// );
|
||||||
/// # assert_eq!(a.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
|
/// # assert_eq!(a.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
|
||||||
/// ```
|
/// ```
|
||||||
pub fn tri(
|
pub fn tri(
|
||||||
&mut self,
|
&mut self,
|
||||||
(x1, y1): (f32, f32),
|
|
||||||
(x2, y2): (f32, f32),
|
(x2, y2): (f32, f32),
|
||||||
|
(x1, y1): (f32, f32),
|
||||||
(x3, y3): (f32, f32),
|
(x3, y3): (f32, f32),
|
||||||
c: [u8; CHANNELS],
|
c: [u8; CHANNELS],
|
||||||
) {
|
) {
|
||||||
// TODO optimize
|
|
||||||
let ymin = max(y1.min(y2).min(y3) as u32, 0);
|
let ymin = max(y1.min(y2).min(y3) as u32, 0);
|
||||||
let ymax = min(y1.max(y2).max(y3) as u32, self.height());
|
let ymax = min(y1.max(y2).max(y3) as u32, self.height());
|
||||||
let xmin = max(x1.min(x2).min(x3) as u32, 0);
|
let xmin = max(x1.min(x2).min(x3) as u32, 0);
|
||||||
let xmax = min(x1.max(x2).max(x3) as u32, self.width());
|
let xmax = min(x1.max(x2).max(x3) as u32, self.width());
|
||||||
for y in ymin..ymax {
|
for y in ymin..ymax {
|
||||||
for x in xmin..xmax {
|
for x in xmin..xmax {
|
||||||
let s = madd(x1 - x3, y as f32 - y3, -(y1 - y2) * (x as f32 - x3));
|
// algorithm from https://web.archive.org/web/20050408192410/http://sw-shader.sourceforge.net/rasterizer.html, but im too dumb to implement the faster ones
|
||||||
let t = madd(x2 - x1, y as f32 - y1, -(y2 - y1) * (x as f32 - x1));
|
if madd(x1 - x2, y as f32 - y1, -(y1 - y2) * (x as f32 - x1)) > 0.
|
||||||
|
&& madd(x2 - x3, y as f32 - y2, -(y2 - y3) * (x as f32 - x2)) > 0.
|
||||||
if (s < 0.0) != (t < 0.0) && s != 0.0 && t != 0.0 {
|
&& madd(x3 - x1, y as f32 - y3, -(y3 - y1) * (x as f32 - x3)) > 0.
|
||||||
continue;
|
{
|
||||||
}
|
|
||||||
|
|
||||||
let d = madd(x3 - x2, y as f32 - y2, -(y3 - y2) * (x as f32 - x2));
|
|
||||||
if d == 0.0 || (d < 0.0) == (s + t <= 0.0) {
|
|
||||||
// SAFETY: x, y are bounded
|
// SAFETY: x, y are bounded
|
||||||
unsafe { self.set_pixel(x, y, c) };
|
unsafe { self.set_pixel(x, y, c) };
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
//! utility math
|
||||||
/// Calculates `a * b + c`, with hardware support if possible.
|
/// Calculates `a * b + c`, with hardware support if possible.
|
||||||
|
#[allow(clippy::suboptimal_flops)]
|
||||||
pub fn madd(a: f32, b: f32, c: f32) -> f32 {
|
pub fn madd(a: f32, b: f32, c: f32) -> f32 {
|
||||||
if cfg!(target_feature = "fma") {
|
if cfg!(target_feature = "fma") {
|
||||||
a.mul_add(b, c)
|
a.mul_add(b, c)
|
||||||
|
|
Loading…
Reference in a new issue