From 9d33d175b5cd564e77bda07fcbce252477f7c40f Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sun, 19 Sep 2021 19:53:42 -0400 Subject: Decently working autumn theme --- themes/TreeShape.h | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'themes/TreeShape.h') diff --git a/themes/TreeShape.h b/themes/TreeShape.h index fbbb950..a8e3eeb 100644 --- a/themes/TreeShape.h +++ b/themes/TreeShape.h @@ -7,13 +7,22 @@ struct TreeBranch { float32 height = 0.f; Vector2 position; // Center point float32 rotation = 0; // How much we are rotated off of the center point in radians - Vector4 color = Vector4 { 1.f, 0.f, 0.f, 1.f }; + Vector4 color = Vector4(101,56,24, 1.f).toNormalizedColor(); + + // Calculated while filling in vertices + Vector2 bottomLeft; + Vector2 bottomRight; + Vector2 topLeft; + Vector2 topRight; + Vector2 topMidpoint; void fillVertices(Renderer2dVertex* vertices) { - Vector2 bottomLeft = Vector2 { position.x - width / 2.f, position.y }; - Vector2 bottomRight = Vector2 { position.x + width / 2.f, position.y }; - Vector2 topLeft = (Vector2 { position.x - width / 2.f, position.y + height }).rotate(rotation); - Vector2 topRight = (Vector2 { position.x + width / 2.f, position.y + height }).rotate(rotation); + bottomLeft = Vector2 { position.x - width / 2.f, position.y }.rotateAbout(rotation, position); + bottomRight = Vector2 { position.x + width / 2.f, position.y }.rotateAbout(rotation, position); + topLeft = (Vector2 { position.x - width / 2.f, position.y + height }).rotateAbout(rotation, position); + topRight = (Vector2 { position.x + width / 2.f, position.y + height }).rotateAbout(rotation, position); + + topMidpoint = topLeft + (topRight - topLeft) / 2.f; vertices[0] = { bottomLeft, color }; vertices[1] = { bottomRight, color }; @@ -28,19 +37,19 @@ struct TreeShape { Renderer2dShape shape; float32 height = 100.f; // Height of the whole tree - float32 width = 50.f; // Width of the whole tree + float32 width = 40.f; // Width of the whole tree int32 divisionsPerBranch = 2; // How many branches to split into at each branch split int32 numBranchLevels = 8; // How many branch levels to display float32 animateTimeSeconds = 2.f; // How long it takes the tree to form void load(Renderer2d* renderer) { - int32 numBranches = divisionsPerBranch * numBranchLevels; + int32 numBranches = pow(divisionsPerBranch, numBranchLevels + 1); TreeBranch* branches = new TreeBranch[numBranches]; int32 numVertices = 6 * numBranches; Renderer2dVertex* vertices = new Renderer2dVertex[numVertices]; int32 branchIndex = 0; - createBranch(branches, numBranches, &branchIndex, 0, width, height, Vector2 { 0.f, 0.f }, 0, vertices); + createBranch(branches, numBranches, &branchIndex, 0, width, height, Vector2 { 100.f, 50.f }, 0, vertices); shape.load(vertices, numVertices, renderer); @@ -61,13 +70,17 @@ struct TreeShape { } float32 branchWidth = width / divisionsPerBranch; - float32 branchHeight = height / divisionsPerBranch; + float32 branchHeight = height / (0.7 * divisionsPerBranch); + float32 branchRotationAmount = (PI / 4.f); + Vector2 topBranchPos = branch->topMidpoint; for (int division = 0; division < divisionsPerBranch; division++) { - float32 branchXPosition = position.x + (((divisionsPerBranch - division) / (divisionsPerBranch - 1)) * width) - width / 2.0; - Vector2 branchPosition = (Vector2 { branchXPosition, position.y + height }).rotate(rotation); - float32 branchRotation = 0; + float32 weight = static_cast(division) / static_cast(divisionsPerBranch - 1); + + float32 branchXPosition = topBranchPos.x - width / 2.f + weight * width; + Vector2 branchPosition = (Vector2 { branchXPosition, topBranchPos.y }); + float32 branchRotation = branch->rotation + (weight * branchRotationAmount - branchRotationAmount / 2.f); (*branchIndex)++; - createBranch(branchList, numBranches, branchIndex, branchLevel + 1, branchWidth, branchHeight, branchPosition, branchRotation, vertices); + createBranch(branchList, numBranches, branchIndex, branchLevel + 1, branchWidth, branchHeight, branchPosition, -branchRotation, vertices); } } -- cgit v1.2.1