feat: render interactive manuscript from Endor through generic graphics
This commit is contained in:
@@ -69,6 +69,16 @@ impl SoftwareRenderer {
|
||||
|
||||
for command in list.commands() {
|
||||
match command {
|
||||
DrawCommand::Image {
|
||||
image,
|
||||
origin,
|
||||
rect,
|
||||
angle,
|
||||
opacity,
|
||||
tint,
|
||||
} => {
|
||||
canvas.draw_image(image, *origin, *rect, *angle, *opacity, *tint);
|
||||
}
|
||||
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;
|
||||
@@ -163,6 +173,58 @@ struct Canvas<'a> {
|
||||
}
|
||||
|
||||
impl Canvas<'_> {
|
||||
/// Inverse-map only clipped destination pixels. Images use straight RGBA;
|
||||
/// bilinear interpolation happens in premultiplied alpha to avoid dark halos.
|
||||
fn draw_image(
|
||||
&mut self,
|
||||
image: &crate::render::image::Image,
|
||||
origin: Point,
|
||||
rect: Rect,
|
||||
angle: f32,
|
||||
opacity: f32,
|
||||
tint: Option<Color>,
|
||||
) {
|
||||
if rect.width <= 0. || rect.height <= 0. || !angle.is_finite() || !opacity.is_finite() {
|
||||
return;
|
||||
}
|
||||
let (sin, cos) = angle.sin_cos();
|
||||
let corners = [
|
||||
(rect.x, rect.y),
|
||||
(rect.x + rect.width, rect.y),
|
||||
(rect.x, rect.y + rect.height),
|
||||
(rect.x + rect.width, rect.y + rect.height),
|
||||
];
|
||||
let mut lo = Point::new(f32::INFINITY, f32::INFINITY);
|
||||
let mut hi = Point::new(f32::NEG_INFINITY, f32::NEG_INFINITY);
|
||||
for (x, y) in corners {
|
||||
let px = origin.x + cos * x - sin * y;
|
||||
let py = origin.y + sin * x + cos * y;
|
||||
lo.x = lo.x.min(px);
|
||||
lo.y = lo.y.min(py);
|
||||
hi.x = hi.x.max(px);
|
||||
hi.y = hi.y.max(py);
|
||||
}
|
||||
let (left, top, right, bottom) =
|
||||
self.clamped_bounds(Rect::new(lo.x, lo.y, hi.x - lo.x, hi.y - lo.y));
|
||||
for y in top..bottom {
|
||||
for x in left..right {
|
||||
let dx = x as f32 + 0.5 - origin.x;
|
||||
let dy = y as f32 + 0.5 - origin.y;
|
||||
let u = (cos * dx + sin * dy - rect.x) / rect.width;
|
||||
let v = (-sin * dx + cos * dy - rect.y) / rect.height;
|
||||
if (0. ..1.).contains(&u) && (0. ..1.).contains(&v) {
|
||||
let mut sample = image.sample(u, v, opacity.clamp(0., 1.));
|
||||
if let Some(tint) = tint {
|
||||
sample.red = tint.red;
|
||||
sample.green = tint.green;
|
||||
sample.blue = tint.blue;
|
||||
}
|
||||
self.blend_pixel(x, y, sample);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 清空本对象维护的当前内容或缓存,后续状态由调用方重新建立。
|
||||
fn clear(&mut self, color: Color) {
|
||||
self.pixels.fill(pack(color));
|
||||
|
||||
Reference in New Issue
Block a user