From 449089e80cd294cbf2016ca8b76ba2a14c6637ab Mon Sep 17 00:00:00 2001 From: Isaac Clayton Date: Sat, 22 Jan 2022 10:13:03 +0100 Subject: working on writing quadtree to texture --- src/ctx.rs | 4 ++++ src/main.rs | 5 +++++ src/quad.rs | 37 +++++++++++++++++++++++++++++++++++++ src/render.rs | 10 ++++++++-- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/ctx.rs b/src/ctx.rs index 38aa4ae..3eebbec 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -20,6 +20,10 @@ impl Ctx { todo!("Turn Base Cell into a vector B"); } + pub fn color_base(&mut self, base: &A) -> [u8; 4] { + todo!(); + } + /// Compresses a single node into a vector representation. /// Returns `None` if node has already been compressed and trimmed from tree. /// To recover a trimmed node, use `expand` on the compressed representation. diff --git a/src/main.rs b/src/main.rs index 735c25d..aa5a312 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,11 @@ mod render; fn main() { println!("Warming up..."); + + // let mut ctx = ctx::Ctx::new_empty(); + // let quad: quad::Node = quad::Node::new_empty(&mut ctx); + render::graphics(); + println!("Hello!"); } diff --git a/src/quad.rs b/src/quad.rs index 25fda47..640b1f9 100644 --- a/src/quad.rs +++ b/src/quad.rs @@ -14,6 +14,7 @@ pub enum Quad { /// Represends a node in a quadtree. /// Has a depth denoting the number of nodes below it. +/// The base node has a depth of 0 /// Nodes should only be siblings of nodes with the same depth. /// Data stored inside a quadtree node, including children, are in `data`. pub struct Node { @@ -23,6 +24,42 @@ pub struct Node { } impl Node { + /// Bytes must be of size (2**depth)**2 * 4 + pub fn write_rgba8( + self, + ctx: &mut Ctx, + bytes: &mut [u8], + depth: usize, + x: usize, + y: usize, + ) -> Self { + let mut expanded = ctx.expand(self); + expanded.data = match expanded.data { + // Base cell in grid. + Quad::Base(base) => { + let mut color = ctx.color_base(&base); + let idx = ((1 << depth) * y + x) * 4; + bytes[idx..(idx + 4)].swap_with_slice(&mut color); + Quad::Base(base) + }, + // Node with 4 children. + Quad::Node(children) => { + let size = 1 << expanded.depth; + let half = size / 2; + let c = [ + children[0].write_rgba8(ctx, bytes, depth, x, y ), + children[1].write_rgba8(ctx, bytes, depth, x + half, y ), + children[2].write_rgba8(ctx, bytes, depth, x, y + half), + children[3].write_rgba8(ctx, bytes, depth, x + half, y + half), + ]; + todo!() + }, + // Children may be generated from Node. + Cached => { unreachable!(); }, + }; + expanded + } + /// Creates a new tree from a single base node pub fn new_base(base: A, ctx: &mut Ctx) -> Self { Node { diff --git a/src/render.rs b/src/render.rs index 964a022..6939ee8 100644 --- a/src/render.rs +++ b/src/render.rs @@ -14,6 +14,7 @@ struct Vertex { struct Stage { pipeline: Pipeline, bindings: Bindings, + texture: Texture, } impl Stage { @@ -51,12 +52,17 @@ impl Stage { shader, ); - Stage { pipeline, bindings } + Stage { pipeline, bindings, texture } } } impl EventHandler for Stage { - fn update(&mut self, _ctx: &mut Context) {} + fn update(&mut self, ctx: &mut Context) { + let t = date::now(); + let bright = t.sin()*0.5+0.5; + let bytes = [(bright * 255.0) as u8; 512*512*4]; + self.texture.update(ctx, &bytes) + } fn draw(&mut self, ctx: &mut Context) { ctx.begin_default_pass(Default::default()); -- cgit v1.2.3-70-g09d2