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/quad.rs | |
parent | 4c947cbed372159fccffbdf6dd639262404ca6e8 (diff) |
working on writing quadtree to texture
Diffstat (limited to 'src/quad.rs')
-rw-r--r-- | src/quad.rs | 37 |
1 files changed, 37 insertions, 0 deletions
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 { |