feat: render programmable 3d scenes cameras and picking

This commit is contained in:
2026-11-10 23:19:26 +08:00
parent 040b779f5f
commit ad2c01bc29
33 changed files with 2463 additions and 42 deletions
+44 -5
View File
@@ -7,7 +7,12 @@ use crate::{
pub struct SoftwareRenderer;
impl SoftwareRenderer {
pub fn render(list: &DisplayList, target: &mut [u32], width: u32, height: u32) {
pub fn render(
list: &DisplayList,
target: &mut [u32],
width: u32,
height: u32,
) -> Result<(), &'static str> {
let mut canvas = Canvas {
pixels: target,
width,
@@ -18,6 +23,39 @@ impl SoftwareRenderer {
for command in list.commands() {
match command {
DrawCommand::Scene3D { rect, view } => {
let left = rect.x.max(canvas.clip.x).max(0.).floor() as u32;
let top = rect.y.max(canvas.clip.y).max(0.).floor() as u32;
let right = (rect.x + rect.width)
.min(canvas.clip.x + canvas.clip.width)
.min(width as f32)
.ceil()
.max(0.) as u32;
let bottom = (rect.y + rect.height)
.min(canvas.clip.y + canvas.clip.height)
.min(height as f32)
.ceil()
.max(0.) as u32;
if left >= right || top >= bottom || rect.width <= 0. || rect.height <= 0. {
continue;
}
let lod = (2048. / rect.width.max(rect.height)).min(1.);
let w = (rect.width * lod).ceil().max(1.) as u32;
let h = (rect.height * lod).ceil().max(1.) as u32;
let surface = crate::scene3d::surface(view, w, h)?;
for y in top..bottom {
for x in left..right {
let u = (((x as f32 + 0.5 - rect.x) / rect.width) * w as f32)
.clamp(0., (w - 1) as f32)
as u32;
let v = (((y as f32 + 0.5 - rect.y) / rect.height) * h as f32)
.clamp(0., (h - 1) as f32)
as u32;
canvas.pixels[y as usize * width as usize + x as usize] =
surface[v as usize * w as usize + u as usize];
}
}
}
DrawCommand::PushClip(rect) => {
clips.push(canvas.clip);
let x = canvas.clip.x.max(rect.x);
@@ -66,6 +104,7 @@ impl SoftwareRenderer {
} => canvas.draw_text(*origin, value, *style, *color),
}
}
Ok(())
}
}
@@ -245,7 +284,7 @@ mod tests {
color: Color::rgb(0, 0, 255),
});
let mut pixels = vec![0; 80 * 60];
SoftwareRenderer::render(&list, &mut pixels, 80, 60);
SoftwareRenderer::render(&list, &mut pixels, 80, 60).unwrap();
let mut second_line = false;
for y in 0..60 {
for x in 0..80 {
@@ -270,7 +309,7 @@ mod tests {
color: Color::rgb(255, 0, 0),
});
let mut pixels = vec![0; 16];
SoftwareRenderer::render(&list, &mut pixels, 4, 4);
SoftwareRenderer::render(&list, &mut pixels, 4, 4).unwrap();
assert_eq!(pixels[5], 0x00ff0000);
assert_eq!(pixels[0], 0);
}
@@ -286,7 +325,7 @@ mod tests {
color: Color::rgba(255, 255, 255, 128),
});
let mut pixels = vec![0; 256 * 48];
SoftwareRenderer::render(&list, &mut pixels, 256, 48);
SoftwareRenderer::render(&list, &mut pixels, 256, 48).unwrap();
assert!(pixels.iter().any(|pixel| *pixel > 0));
assert!(pixels.iter().all(|pixel| (pixel & 255) <= 128));
assert!(pixels.iter().any(|pixel| (1..128).contains(&(pixel & 255))));
@@ -299,7 +338,7 @@ mod tests {
color: Color::rgba(255, 255, 255, 0),
});
let mut pixels = vec![0; 256 * 48];
SoftwareRenderer::render(&invisible, &mut pixels, 256, 48);
SoftwareRenderer::render(&invisible, &mut pixels, 256, 48).unwrap();
assert!(pixels.iter().all(|pixel| *pixel == 0));
}
}