From cc8bbef4bc8257b65757396e4431e2ca9b5f30ff Mon Sep 17 00:00:00 2001 From: mattkae Date: Sun, 9 Oct 2022 15:11:13 -0400 Subject: At least I have a working sun theme, but this will have to wait until next summer I suppose --- themes/Renderer2d.cpp | 34 +++++++++++++++++++++--- themes/Renderer2d.h | 7 +++++ themes/SummerTheme.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ themes/SummerTheme.h | 23 +++++++++++++++++ themes/dist/output.wasm | Bin 130417 -> 135264 bytes themes/main.cpp | 27 ++++++++++++++++++- themes/types.h | 2 +- 7 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 themes/SummerTheme.cpp create mode 100644 themes/SummerTheme.h (limited to 'themes') diff --git a/themes/Renderer2d.cpp b/themes/Renderer2d.cpp index dacebe3..606b7fc 100644 --- a/themes/Renderer2d.cpp +++ b/themes/Renderer2d.cpp @@ -5,7 +5,7 @@ #include // Note: In the 'transform' attribute, the transform.x is the scale, -// transform.y is the rotation, and transform.zw is the translatiob. +// transform.y is the rotation, and transform.zw is the translation. const char* Vertex2DShader = "attribute vec2 position; \n" "attribute vec4 color; \n" @@ -64,6 +64,7 @@ void Renderer2d::unload() { void Mesh2D::load(Vertex2D* inVertices, u32 inNumVertices, Renderer2d* renderer) { + ebo = 0; numVertices = inNumVertices; useShader(renderer->shader); @@ -89,15 +90,40 @@ void Mesh2D::load(Vertex2D* inVertices, u32 inNumVertices, Renderer2d* renderer) glBindVertexArray(0); } -void Mesh2D::render(Renderer2d* renderer, GLenum drawType) { - setShaderMat4(renderer->uniforms.model, model); +void Mesh2D::load(Vertex2D* vertices, + u32 numVertices, + u32* indices, + u32 inNumIndices, + Renderer2d* renderer) { + load(vertices, numVertices, renderer); glBindVertexArray(vao); - glDrawArrays(drawType, 0, numVertices); + glGenBuffers(1, &ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, inNumIndices * sizeof(u32), &indices[0], GL_STATIC_DRAW); + numIndices = inNumIndices; glBindVertexArray(0); } +void Mesh2D::render(Renderer2d* renderer, GLenum drawType) { + setShaderMat4(renderer->uniforms.model, model); + + if (ebo == 0) { + glBindVertexArray(vao); + glDrawArrays(drawType, 0, numVertices); + glBindVertexArray(0); + } + else { + glBindVertexArray(vao); + glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); + } +} + void Mesh2D::unload() { glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); + if (ebo) { + glDeleteBuffers(1, &ebo); + } } diff --git a/themes/Renderer2d.h b/themes/Renderer2d.h index 82af673..909f088 100644 --- a/themes/Renderer2d.h +++ b/themes/Renderer2d.h @@ -38,10 +38,17 @@ struct Vertex2D { struct Mesh2D { u32 vao; u32 vbo; + u32 ebo = 0; u32 numVertices = 0; + u32 numIndices = 0; Mat4x4 model; void load(Vertex2D* vertices, u32 numVertices, Renderer2d* renderer); + void load(Vertex2D* vertices, + u32 numVertices, + u32* indices, + u32 numIndices, + Renderer2d* renderer); void render(Renderer2d* renderer, GLenum drawType = GL_TRIANGLES); void unload(); }; diff --git a/themes/SummerTheme.cpp b/themes/SummerTheme.cpp new file mode 100644 index 0000000..b23cc71 --- /dev/null +++ b/themes/SummerTheme.cpp @@ -0,0 +1,67 @@ +#include "SummerTheme.h" +#include "Renderer2d.h" +#include "list.h" +#include "mathlib.h" +#include + +void SummerTheme::load(Renderer2d* renderer) { + renderer->clearColor = Vector4(0, 181, 286, 255.f).toNormalizedColor(); + sun.sectors = 180; + sun.radius = renderer->context->width / 4.f; + sun.load(renderer); +} + +void SummerTheme::update(f32 dtSeconds) { + sun.update(dtSeconds); +} + +void SummerTheme::render(Renderer2d* renderer) { + sun.render(renderer); +} + +void SummerTheme::unload() { + sun.unload(); +} + + +void Sun::load(Renderer2d* renderer) { + matte::List vertices; + matte::List indices; + Vector4 sunColor = Vector4(249, 215, 28, 255).toNormalizedColor(); + vertices.add({ Vector2(0, 0), sunColor, Mat4x4() }); + + f32 radiansPerSector = (2.f * PI) / sectors; + for (i32 i = 0; i <= sectors; i++) { + f32 radians = radiansPerSector * i; + f32 cosAngle = cosf(radians); + f32 sinAngle = sinf(radians); + Vector2 vertex = Vector2(radius * cosAngle, radius * sinAngle); + vertices.add({ vertex, sunColor, Mat4x4() }); + + u32 first = i; + u32 second = 0; + u32 third = i + 1; + indices.add(first); + indices.add(second); + indices.add(third); + } + + mesh.load(&vertices.data[0], vertices.numElements, &indices.data[0], indices.numElements, renderer); + mesh.model = Mat4x4().translateByVec2(Vector2(renderer->context->width / 2.f, renderer->context->height / 2.f)); + vertices.deallocate(); +} + +void Sun::update(f32 dtSeconds) { + +} + +void Sun::render(Renderer2d* renderer) { + setShaderMat4(renderer->uniforms.model, mesh.model); + glBindVertexArray(mesh.vao); + glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); +} + +void Sun::unload() { + mesh.unload(); +} diff --git a/themes/SummerTheme.h b/themes/SummerTheme.h new file mode 100644 index 0000000..1d9093a --- /dev/null +++ b/themes/SummerTheme.h @@ -0,0 +1,23 @@ +#pragma once +#include "types.h" +#include "Renderer2d.h" +#include + +struct Sun { + f32 radius = 20.f; + i32 sectors = 180; + Mesh2D mesh; + + void load(Renderer2d* renderer); + void update(f32 dtSeconds); + void render(Renderer2d* renderer); + void unload(); +}; + +struct SummerTheme { + Sun sun; + void load(Renderer2d* renderer); + void update(f32 dtSeconds); + void render(Renderer2d* renderer); + void unload(); +}; diff --git a/themes/dist/output.wasm b/themes/dist/output.wasm index 1cb0170..ced481d 100755 Binary files a/themes/dist/output.wasm and b/themes/dist/output.wasm differ diff --git a/themes/main.cpp b/themes/main.cpp index ae91ba7..4e82ca0 100644 --- a/themes/main.cpp +++ b/themes/main.cpp @@ -5,6 +5,7 @@ #include "mathlib.h" #include "types.h" #include "TreeShape.h" +#include "SummerTheme.h" #include "LeafParticleRender.h" #include "Snowflake.h" #include @@ -15,7 +16,8 @@ enum Theme { Default = 0, Autumn, Winter, - Spring + Spring, + Summer }; struct AutumnTheme { @@ -74,6 +76,7 @@ EM_BOOL selectNone(int eventType, const EmscriptenMouseEvent* mouseEvent, void* EM_BOOL selectAutumn(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); EM_BOOL selectWinter(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); EM_BOOL selectSpring(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); +EM_BOOL selectSummer(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); WebglContext context; Renderer2d renderer2d; @@ -83,6 +86,7 @@ Theme activeTheme = Theme::Default; AutumnTheme autumnTheme; WinterTheme winterTheme; SpringTheme springTheme; +SummerTheme summerTheme; int main() { context.init("#theme_canvas"); @@ -90,6 +94,7 @@ int main() { emscripten_set_click_callback("#theme_button_autumn", NULL, false, selectAutumn); emscripten_set_click_callback("#theme_button_winter", NULL, false, selectWinter); emscripten_set_click_callback("#theme_button_spring", NULL, false, selectSpring); + emscripten_set_click_callback("#theme_button_summer", NULL, false, selectSummer); return 0; } @@ -119,6 +124,10 @@ void load(Theme theme) { renderer3d.load(&context); springTheme.load(&renderer3d); break; + case Theme::Summer: + renderer2d.load(&context); + summerTheme.load(&renderer2d); + break; default: break; } @@ -136,6 +145,9 @@ void update(f32 dtSeconds, void* userData) { case Theme::Spring: springTheme.update(dtSeconds); break; + case Theme::Summer: + summerTheme.update(dtSeconds); + break; default: break; } @@ -154,6 +166,10 @@ void update(f32 dtSeconds, void* userData) { renderer3d.render(); springTheme.render(&renderer3d); break; + case Theme::Summer: + renderer2d.render(); + summerTheme.render(&renderer2d); + break; default: break; } @@ -170,6 +186,9 @@ void unload() { case Theme::Spring: springTheme.unload(); break; + case Theme::Summer: + summerTheme.unload(); + break; default: break; } @@ -207,6 +226,12 @@ EM_BOOL selectSpring(int eventType, const EmscriptenMouseEvent* mouseEvent, void return true; } +EM_BOOL selectSummer(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { + printf("Summer theme selected\n"); + load(Theme::Summer); + return true; +} + // -- Autumn theme void AutumnTheme::load(Renderer2d* renderer) { renderer->clearColor = Vector4(252, 210, 153, 255).toNormalizedColor(); diff --git a/themes/types.h b/themes/types.h index d5c7938..ae8aa4e 100644 --- a/themes/types.h +++ b/themes/types.h @@ -1,6 +1,6 @@ #pragma once -#include +#include typedef int8_t i8; typedef int16_t i16; -- cgit v1.2.1