2020-11-14 20:19:36 +00:00
|
|
|
//
|
|
|
|
// File: foldhaus_animation_renderer.cpp
|
|
|
|
// Author: Peter Slattery
|
|
|
|
// Creation Date: 2020-11-14
|
|
|
|
//
|
|
|
|
#ifndef FOLDHAUS_ANIMATION_RENDERER_CPP
|
|
|
|
|
|
|
|
internal pixel
|
|
|
|
LedBlend_Overwrite(pixel PixelA, pixel PixelB)
|
|
|
|
{
|
|
|
|
return PixelB;
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:41:27 +00:00
|
|
|
internal pixel
|
|
|
|
LedBlend_Overwrite(pixel PixelA, pixel PixelB, r32 Opacity)
|
|
|
|
{
|
|
|
|
pixel Result = {};
|
|
|
|
|
|
|
|
r32 BOpacity = 1.0f - Opacity;
|
|
|
|
Result.R = (u8)((PixelA.R * Opacity) + (PixelB.R * BOpacity));
|
|
|
|
Result.G = (u8)((PixelA.G * Opacity) + (PixelB.G * BOpacity));
|
|
|
|
Result.B = (u8)((PixelA.B * Opacity) + (PixelB.B * BOpacity));
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:19:36 +00:00
|
|
|
internal pixel
|
|
|
|
LedBlend_Add(pixel PixelA, pixel PixelB)
|
|
|
|
{
|
|
|
|
pixel Result = {};
|
|
|
|
|
|
|
|
u32 R = (u32)PixelA.R + (u32)PixelB.R;
|
|
|
|
u32 G = (u32)PixelA.G + (u32)PixelB.G;
|
|
|
|
u32 B = (u32)PixelA.B + (u32)PixelB.B;
|
|
|
|
|
|
|
|
Result.R = (u8)Min(R, (u32)255);
|
|
|
|
Result.G = (u8)Min(G, (u32)255);
|
|
|
|
Result.B = (u8)Min(B, (u32)255);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal pixel
|
|
|
|
LedBlend_Multiply(pixel PixelA, pixel PixelB)
|
|
|
|
{
|
|
|
|
pixel Result = {};
|
|
|
|
|
|
|
|
r32 DR = (r32)PixelA.R / 255.f;
|
|
|
|
r32 DG = (r32)PixelA.G / 255.f;
|
|
|
|
r32 DB = (r32)PixelA.B / 255.f;
|
|
|
|
|
|
|
|
r32 SR = (r32)PixelB.R / 255.f;
|
|
|
|
r32 SG = (r32)PixelB.G / 255.f;
|
|
|
|
r32 SB = (r32)PixelB.B / 255.f;
|
|
|
|
|
|
|
|
Result.R = (u8)((DR * SR) * 255.f);
|
|
|
|
Result.G = (u8)((DG * SG) * 255.f);
|
|
|
|
Result.B = (u8)((DB * SB) * 255.f);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal pixel
|
|
|
|
LedBlend_Overlay(pixel PixelA, pixel PixelB)
|
|
|
|
{
|
|
|
|
pixel Result = {};
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal led_blend_proc*
|
|
|
|
LedBlend_GetProc(blend_mode BlendMode)
|
|
|
|
{
|
|
|
|
led_blend_proc* Result = 0;
|
|
|
|
switch (BlendMode)
|
|
|
|
{
|
|
|
|
case BlendMode_Overwrite: { Result = LedBlend_Overwrite; }break;
|
|
|
|
case BlendMode_Add: { Result = LedBlend_Add; }break;
|
|
|
|
case BlendMode_Multiply: { Result = LedBlend_Multiply; }break;
|
|
|
|
InvalidDefaultCase;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:41:27 +00:00
|
|
|
internal void
|
2021-01-11 00:25:35 +00:00
|
|
|
AnimationSystem_RenderBlockToLedBuffer(animation_system* System, animation_block Block, led_buffer* Buffer, assembly Assembly, animation_pattern_array Patterns, gs_memory_arena* Transient)
|
2020-11-14 21:41:27 +00:00
|
|
|
{
|
|
|
|
u32 FramesIntoBlock = System->CurrentFrame - Block.Range.Min;
|
|
|
|
r32 SecondsIntoBlock = FramesIntoBlock * System->SecondsPerFrame;
|
|
|
|
|
2021-01-11 00:25:35 +00:00
|
|
|
animation_pattern Pattern = Patterns_GetPattern(Patterns, Block.AnimationProcHandle);
|
|
|
|
Pattern.Proc(Buffer, Assembly, SecondsIntoBlock, Transient);
|
2020-11-14 21:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 20:19:36 +00:00
|
|
|
internal void
|
|
|
|
AnimationSystem_RenderToLedBuffers(animation_system* System, assembly_array Assemblies,
|
|
|
|
led_system* LedSystem,
|
2021-01-11 00:25:35 +00:00
|
|
|
animation_pattern_array Patterns,
|
2020-11-14 20:19:36 +00:00
|
|
|
gs_memory_arena* Transient)
|
|
|
|
{
|
|
|
|
s32 CurrentFrame = System->CurrentFrame;
|
|
|
|
r32 FrameTime = CurrentFrame * System->SecondsPerFrame;
|
|
|
|
|
2020-11-14 20:47:51 +00:00
|
|
|
animation* ActiveAnim = AnimationSystem_GetActiveAnimation(System);
|
2020-11-14 20:19:36 +00:00
|
|
|
animation_frame CurrFrame = AnimationSystem_CalculateAnimationFrame(System, Transient);
|
|
|
|
|
2020-11-14 21:41:27 +00:00
|
|
|
// NOTE(pjs): This mirrors animation_layer_frame to account
|
|
|
|
// for overlapping
|
|
|
|
struct layer_led_buffer
|
|
|
|
{
|
|
|
|
led_buffer HotBuffer;
|
|
|
|
led_buffer NextHotBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
layer_led_buffer* LayerBuffers = PushArray(Transient, layer_led_buffer, CurrFrame.LayersCount);
|
2020-11-14 20:47:51 +00:00
|
|
|
|
2020-11-14 20:19:36 +00:00
|
|
|
for (u32 AssemblyIndex = 0; AssemblyIndex < Assemblies.Count; AssemblyIndex++)
|
|
|
|
{
|
|
|
|
assembly* Assembly = &Assemblies.Values[AssemblyIndex];
|
|
|
|
led_buffer* AssemblyLedBuffer = LedSystemGetBuffer(LedSystem, Assembly->LedBufferIndex);
|
|
|
|
|
2020-11-14 20:47:51 +00:00
|
|
|
// Create the LayerLEDBuffers
|
2020-11-14 21:41:27 +00:00
|
|
|
for (u32 Layer = 0; Layer < CurrFrame.LayersCount; Layer++)
|
2020-11-14 20:47:51 +00:00
|
|
|
{
|
2020-11-14 21:41:27 +00:00
|
|
|
layer_led_buffer TempBuffer = {};
|
|
|
|
if (CurrFrame.Layers[Layer].HasHot)
|
|
|
|
{
|
|
|
|
TempBuffer.HotBuffer.LedCount = AssemblyLedBuffer->LedCount;
|
|
|
|
TempBuffer.HotBuffer.Positions = AssemblyLedBuffer->Positions;
|
|
|
|
TempBuffer.HotBuffer.Colors = PushArray(Transient, pixel, TempBuffer.HotBuffer.LedCount);
|
|
|
|
LedBuffer_ClearToBlack(&TempBuffer.HotBuffer);
|
|
|
|
}
|
2020-11-14 20:47:51 +00:00
|
|
|
|
2020-11-14 21:41:27 +00:00
|
|
|
if (CurrFrame.Layers[Layer].HasNextHot)
|
|
|
|
{
|
|
|
|
TempBuffer.NextHotBuffer.LedCount = AssemblyLedBuffer->LedCount;
|
|
|
|
TempBuffer.NextHotBuffer.Positions = AssemblyLedBuffer->Positions;
|
|
|
|
TempBuffer.NextHotBuffer.Colors = PushArray(Transient, pixel, TempBuffer.HotBuffer.LedCount);
|
|
|
|
LedBuffer_ClearToBlack(&TempBuffer.NextHotBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerBuffers[Layer] = TempBuffer;
|
2020-11-14 20:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render Each layer's block to the appropriate temp buffer
|
2020-11-14 21:41:27 +00:00
|
|
|
for (u32 Layer = 0; Layer < CurrFrame.LayersCount; Layer++)
|
2020-11-14 20:19:36 +00:00
|
|
|
{
|
2020-11-14 21:41:27 +00:00
|
|
|
animation_layer_frame LayerFrame = CurrFrame.Layers[Layer];
|
|
|
|
if (LayerFrame.HasHot)
|
|
|
|
{
|
|
|
|
led_buffer TempBuffer = LayerBuffers[Layer].HotBuffer;
|
|
|
|
animation_block Block = LayerFrame.Hot;
|
|
|
|
AnimationSystem_RenderBlockToLedBuffer(System, Block, &TempBuffer, *Assembly, Patterns, Transient);
|
|
|
|
}
|
2020-11-14 20:19:36 +00:00
|
|
|
|
2020-11-14 21:41:27 +00:00
|
|
|
if (LayerFrame.HasNextHot)
|
|
|
|
{
|
|
|
|
led_buffer TempBuffer = LayerBuffers[Layer].NextHotBuffer;
|
|
|
|
animation_block Block = LayerFrame.NextHot;
|
|
|
|
AnimationSystem_RenderBlockToLedBuffer(System, Block, &TempBuffer, *Assembly, Patterns, Transient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blend together any layers that have a hot and next hot buffer
|
|
|
|
for (u32 Layer = 0; Layer < CurrFrame.LayersCount; Layer++)
|
|
|
|
{
|
|
|
|
animation_layer_frame LayerFrame = CurrFrame.Layers[Layer];
|
|
|
|
layer_led_buffer LayerBuffer = LayerBuffers[Layer];
|
|
|
|
if (LayerFrame.HasNextHot)
|
|
|
|
{
|
|
|
|
for (u32 LED = 0; LED < AssemblyLedBuffer->LedCount; LED++)
|
|
|
|
{
|
|
|
|
pixel A = LayerBuffer.HotBuffer.Colors[LED];
|
|
|
|
pixel B = LayerBuffer.NextHotBuffer.Colors[LED];
|
|
|
|
LayerBuffer.HotBuffer.Colors[LED] = LedBlend_Overwrite(A, B, LayerFrame.HotOpacity);
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 20:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Consolidate Temp Buffers
|
|
|
|
// We do this in reverse order so that they go from top to bottom
|
2020-11-14 21:48:17 +00:00
|
|
|
for (u32 Layer = 0; Layer < CurrFrame.LayersCount; Layer++)
|
2020-11-14 20:19:36 +00:00
|
|
|
{
|
2020-11-14 21:48:17 +00:00
|
|
|
if (CurrFrame.Layers[Layer].HasHot)
|
2020-11-14 20:19:36 +00:00
|
|
|
{
|
2020-11-14 21:48:17 +00:00
|
|
|
led_blend_proc* Blend = LedBlend_GetProc(ActiveAnim->Layers.Values[Layer].BlendMode);
|
|
|
|
Assert(Blend != 0);
|
|
|
|
for (u32 LED = 0; LED < AssemblyLedBuffer->LedCount; LED++)
|
|
|
|
{
|
|
|
|
pixel A = AssemblyLedBuffer->Colors[LED];
|
|
|
|
pixel B = LayerBuffers[Layer].HotBuffer.Colors[LED];
|
|
|
|
AssemblyLedBuffer->Colors[LED] = Blend(A, B);
|
|
|
|
}
|
2020-11-14 20:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 08:02:42 +00:00
|
|
|
|
|
|
|
System->LastUpdatedFrame = System->CurrentFrame;
|
2020-11-14 20:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define FOLDHAUS_ANIMATION_RENDERER_CPP
|
|
|
|
#endif // FOLDHAUS_ANIMATION_RENDERER_CPP
|