diff options
author | Isaac Clayton | 2022-01-22 10:13:03 +0100 |
---|---|---|
committer | Isaac Clayton | 2022-01-22 10:13:03 +0100 |
commit | 449089e80cd294cbf2016ca8b76ba2a14c6637ab (patch) | |
tree | bfb2ac459fccb9b74fe207c6dc2abb427e11b0b4 /src | |
parent | 4c947cbed372159fccffbdf6dd639262404ca6e8 (diff) |
working on writing quadtree to texture
Diffstat (limited to 'src')
-rw-r--r-- | src/ctx.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 5 | ||||
-rw-r--r-- | src/quad.rs | 37 | ||||
-rw-r--r-- | src/render.rs | 10 |
4 files changed, 54 insertions, 2 deletions
@@ -20,6 +20,10 @@ impl Ctx { todo!("Turn Base Cell into a vector B"); } + pub fn color_base<A>(&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<u8, u8> = 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<A: Default, B: Copy> { /// 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<A: Default, B: Copy> { @@ -23,6 +24,42 @@ pub struct Node<A: Default, B: Copy> { } impl<A: Default, B: Copy> Node<A, B> { + /// 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()); |