From a6c8d0c9550f16f179567ac842661544a47c5acb Mon Sep 17 00:00:00 2001 From: PS Date: Sat, 14 Nov 2020 12:47:51 -0800 Subject: [PATCH] refactored layer blending --- src/app/engine/animation/foldhaus_animation.h | 13 +++++++++ .../animation/foldhaus_animation_renderer.cpp | 28 +++++++++++++------ src/app/engine/assembly/foldhaus_assembly.h | 11 ++++++++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/app/engine/animation/foldhaus_animation.h b/src/app/engine/animation/foldhaus_animation.h index 80117db..85173d5 100644 --- a/src/app/engine/animation/foldhaus_animation.h +++ b/src/app/engine/animation/foldhaus_animation.h @@ -41,6 +41,7 @@ enum blend_mode BlendMode_Count, }; +// TODO(pjs): Add Opacity to this typedef pixel led_blend_proc(pixel PixelA, pixel PixelB); global gs_const_string BlendModeStrings[] = { @@ -84,10 +85,22 @@ struct animation_array u32 CountMax; }; +struct animation_layer_frame +{ + animation_block Hot; + animation_block NextHot; + bool HasNextHot; +}; + // NOTE(pjs): This is an evaluated frame - across all layers in an // animation, these are the blocks that need to be run struct animation_frame { + animation_layer_frame* Layers; + b8* LayersFilled; + u32 LayersCount; + u32 LayersCountMax; + // NOTE(pjs): These are all parallel arrays of equal length animation_block* Blocks; b8* BlocksFilled; diff --git a/src/app/engine/animation/foldhaus_animation_renderer.cpp b/src/app/engine/animation/foldhaus_animation_renderer.cpp index dfd8ab6..7839da6 100644 --- a/src/app/engine/animation/foldhaus_animation_renderer.cpp +++ b/src/app/engine/animation/foldhaus_animation_renderer.cpp @@ -77,35 +77,47 @@ AnimationSystem_RenderToLedBuffers(animation_system* System, assembly_array Asse s32 CurrentFrame = System->CurrentFrame; r32 FrameTime = CurrentFrame * System->SecondsPerFrame; + animation* ActiveAnim = AnimationSystem_GetActiveAnimation(System); animation_frame CurrFrame = AnimationSystem_CalculateAnimationFrame(System, Transient); - led_buffer* LayerLEDBuffers = PushArray(Transient, led_buffer, CurrFrame.BlocksCountMax); + led_buffer* LayerLedBuffers = PushArray(Transient, led_buffer, CurrFrame.BlocksCountMax); + for (u32 AssemblyIndex = 0; AssemblyIndex < Assemblies.Count; AssemblyIndex++) { assembly* Assembly = &Assemblies.Values[AssemblyIndex]; led_buffer* AssemblyLedBuffer = LedSystemGetBuffer(LedSystem, Assembly->LedBufferIndex); + // Create the LayerLEDBuffers for (u32 Layer = 0; Layer < CurrFrame.BlocksCountMax; Layer++) { + led_buffer TempBuffer = {}; + TempBuffer.LedCount = AssemblyLedBuffer->LedCount; + TempBuffer.Positions = AssemblyLedBuffer->Positions; + TempBuffer.Colors = PushArray(Transient, pixel, TempBuffer.LedCount); + LedBuffer_ClearToBlack(&TempBuffer); + + LayerLedBuffers[Layer] = TempBuffer; + } + + // Render Each layer's block to the appropriate temp buffer + for (u32 Layer = 0; Layer < CurrFrame.BlocksCountMax; Layer++) + { + led_buffer TempBuffer = LayerLedBuffers[Layer]; + if (!CurrFrame.BlocksFilled[Layer]) { continue; } animation_block Block = CurrFrame.Blocks[Layer]; - // Prep Temp Buffer - LayerLEDBuffers[Layer] = *AssemblyLedBuffer; - LayerLEDBuffers[Layer].Colors = PushArray(Transient, pixel, AssemblyLedBuffer->LedCount); - u32 FramesIntoBlock = CurrentFrame - Block.Range.Min; r32 SecondsIntoBlock = FramesIntoBlock * System->SecondsPerFrame; // :AnimProcHandle u32 AnimationProcIndex = Block.AnimationProcHandle - 1; animation_proc* AnimationProc = Patterns[AnimationProcIndex].Proc; - AnimationProc(&LayerLEDBuffers[Layer], *Assembly, SecondsIntoBlock, Transient); + AnimationProc(&TempBuffer, *Assembly, SecondsIntoBlock, Transient); } // Consolidate Temp Buffers // We do this in reverse order so that they go from top to bottom - animation* ActiveAnim = AnimationSystem_GetActiveAnimation(System); for (u32 Layer = 0; Layer < CurrFrame.BlocksCountMax; Layer++) { if (!CurrFrame.BlocksFilled[Layer]) { continue; } @@ -115,7 +127,7 @@ AnimationSystem_RenderToLedBuffers(animation_system* System, assembly_array Asse for (u32 LED = 0; LED < AssemblyLedBuffer->LedCount; LED++) { pixel A = AssemblyLedBuffer->Colors[LED]; - pixel B = LayerLEDBuffers[Layer].Colors[LED]; + pixel B = LayerLedBuffers[Layer].Colors[LED]; AssemblyLedBuffer->Colors[LED] = Blend(A, B); } } diff --git a/src/app/engine/assembly/foldhaus_assembly.h b/src/app/engine/assembly/foldhaus_assembly.h index 94982a5..f1d7ba9 100644 --- a/src/app/engine/assembly/foldhaus_assembly.h +++ b/src/app/engine/assembly/foldhaus_assembly.h @@ -168,6 +168,17 @@ LedSystemGetBuffer(led_system* System, u32 Index) return Result; } +internal void +LedBuffer_ClearToBlack(led_buffer* Buffer) +{ + for (u32 i = 0; i < Buffer->LedCount; i++) + { + Buffer->Colors[i].R = 0; + Buffer->Colors[i].G = 0; + Buffer->Colors[i].B = 0; + } +} + internal u32 StripGenData_CountLeds(strip_gen_data Data) {