Compare commits
3 commits
ba91baa302
...
d7e87bdb1e
Author | SHA1 | Date | |
---|---|---|---|
|
d7e87bdb1e | ||
|
c3f2ae9a56 | ||
|
635585dd09 |
4 changed files with 162 additions and 18 deletions
104
Cargo.lock
generated
104
Cargo.lock
generated
|
@ -2,6 +2,24 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"r-efi",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "justerror"
|
||||
version = "1.1.0"
|
||||
|
@ -13,6 +31,21 @@ dependencies = [
|
|||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.175"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
|
||||
dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.101"
|
||||
|
@ -31,6 +64,41 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "r-efi"
|
||||
version = "5.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
|
||||
dependencies = [
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
|
@ -78,6 +146,7 @@ name = "tinyrender"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"justerror",
|
||||
"rand",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
|
@ -86,3 +155,38 @@ name = "unicode-ident"
|
|||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.14.3+wasi-0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a51ae83037bdd272a9e28ce236db8c07016dd0d50c27038b3f407533c030c95"
|
||||
dependencies = [
|
||||
"wit-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen"
|
||||
version = "0.45.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c573471f125075647d03df72e026074b7203790d41351cd6edc96f46bcccd36"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.106",
|
||||
]
|
||||
|
|
|
@ -5,4 +5,5 @@ edition = "2024"
|
|||
|
||||
[dependencies]
|
||||
justerror = "1.1.0"
|
||||
rand = "0.9.2"
|
||||
thiserror = "2.0.16"
|
||||
|
|
71
src/main.rs
71
src/main.rs
|
@ -2,6 +2,7 @@
|
|||
extern crate justerror;
|
||||
|
||||
mod tga;
|
||||
use rand::Rng;
|
||||
use tga::*;
|
||||
|
||||
mod point;
|
||||
|
@ -32,6 +33,15 @@ fn main() {
|
|||
let h = 64;
|
||||
let mut framebuffer = TGAImage::new(w, h, TGAFormat::RGB);
|
||||
|
||||
//bench(&mut framebuffer);
|
||||
baseline(&mut framebuffer);
|
||||
|
||||
framebuffer
|
||||
.write_file("framebuffer.tga", true, true)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn baseline(framebuffer: &mut TGAImage) {
|
||||
let ax = 7;
|
||||
let ay = 3;
|
||||
let bx = 12;
|
||||
|
@ -47,23 +57,52 @@ fn main() {
|
|||
let b = Point::new(bx, by);
|
||||
let c = Point::new(cx, cy);
|
||||
|
||||
line(a, b, &mut framebuffer, BLUE);
|
||||
line(c, b, &mut framebuffer, GREEN);
|
||||
line(c, a, &mut framebuffer, YELLOW);
|
||||
line(a, c, &mut framebuffer, RED);
|
||||
|
||||
framebuffer
|
||||
.write_file("framebuffer.tga", true, true)
|
||||
.unwrap();
|
||||
line(a, b, framebuffer, BLUE);
|
||||
line(c, b, framebuffer, GREEN);
|
||||
line(c, a, framebuffer, YELLOW);
|
||||
line(a, c, framebuffer, RED);
|
||||
}
|
||||
|
||||
fn line(a: Point, b: Point, fb: &mut TGAImage, color: TGAColor) {
|
||||
let mut t = 0.0;
|
||||
let step = 0.02;
|
||||
while t < 1.0 {
|
||||
let x = (a.x as f32 + (b.x - a.x) as f32 * t).round_ties_even() as u32;
|
||||
let y = (a.y as f32 + (b.y - a.y) as f32 * t).round_ties_even() as u32;
|
||||
fb.set(x, y, color);
|
||||
t += step;
|
||||
fn bench(fb: &mut TGAImage) {
|
||||
let mut rng = rand::rng();
|
||||
|
||||
for _ in 0..1 << 24 {
|
||||
let ax = rng.random_range(0..fb.width);
|
||||
let ay = rng.random_range(0..fb.height);
|
||||
let a = Point::new(ax, ay);
|
||||
|
||||
let bx = rng.random_range(0..fb.width);
|
||||
let by = rng.random_range(0..fb.height);
|
||||
let b = Point::new(bx, by);
|
||||
|
||||
line(
|
||||
a,
|
||||
b,
|
||||
fb,
|
||||
[rng.random(), rng.random(), rng.random(), rng.random()].into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn line(mut a: Point, mut b: Point, fb: &mut TGAImage, color: TGAColor) {
|
||||
let is_steep = (a.x - b.x).abs() < (a.y - b.y).abs();
|
||||
if is_steep {
|
||||
std::mem::swap(&mut a.x, &mut a.y);
|
||||
std::mem::swap(&mut b.x, &mut b.y);
|
||||
}
|
||||
if a.x > b.x {
|
||||
std::mem::swap(&mut a.x, &mut b.x);
|
||||
std::mem::swap(&mut a.y, &mut b.y);
|
||||
}
|
||||
|
||||
let mut y = a.y as f32;
|
||||
let step = (b.y - a.y) as f32 / (b.x - a.x) as f32;
|
||||
for x in (a.x)..b.x {
|
||||
if is_steep {
|
||||
fb.set(y as u32, x as u32, color);
|
||||
} else {
|
||||
fb.set(x as u32, y as u32, color);
|
||||
}
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,8 +31,8 @@ impl Sub for Point {
|
|||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
x: self.x.saturating_sub(rhs.x),
|
||||
y: self.y.saturating_sub(rhs.y),
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue