From b17c518155fae64083eb5b56b78b9eec57603ac0 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sat, 27 Mar 2021 21:12:42 -0400 Subject: Drawing pretty line segments in easter colors --- frontend/2d/_collisions/pill_line/dist/output.wasm | Bin 37565 -> 40465 bytes frontend/2d/_collisions/pill_line/main.cpp | 43 ++++++++++++++++++++- frontend/shared_cpp/OrthographicRenderer.cpp | 13 ++----- frontend/shared_cpp/OrthographicRenderer.h | 3 +- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/frontend/2d/_collisions/pill_line/dist/output.wasm b/frontend/2d/_collisions/pill_line/dist/output.wasm index 92ee1f2..e282f2c 100755 Binary files a/frontend/2d/_collisions/pill_line/dist/output.wasm and b/frontend/2d/_collisions/pill_line/dist/output.wasm differ diff --git a/frontend/2d/_collisions/pill_line/main.cpp b/frontend/2d/_collisions/pill_line/main.cpp index df20fa0..4187435 100644 --- a/frontend/2d/_collisions/pill_line/main.cpp +++ b/frontend/2d/_collisions/pill_line/main.cpp @@ -9,6 +9,8 @@ #include #include +// Side note: It is Eastertime, so I chose this easter color palette. Enjoy: https://htmlcolors.com/palette/144/easter + // // Pill object // @@ -52,6 +54,11 @@ struct Pill { } shape.load(vertices, numVertices, renderer); + delete[] vertices; + } + + void render(OrthographicRenderer* renderer) { + shape.render(renderer); } void unload() { @@ -63,6 +70,25 @@ struct Pill { } }; +struct LineSegment { + OrthographicShape shape; + Vector2 start; + Vector2 end; + OrthographicVertex vertices[2]; + + void load(OrthographicRenderer* renderer, Vector4 color, Vector2 start, Vector2 end) { + vertices[0].position = start; + vertices[0].color = color; + vertices[1].position = end; + vertices[1].color = color; + shape.load(vertices, 2, renderer); + } + + void render(OrthographicRenderer* renderer) { + shape.render(renderer, GL_LINES); + } +}; + EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); EM_BOOL update(float time, void* userData); @@ -72,6 +98,7 @@ WebglContext context; OrthographicRenderer renderer; Pill pill; MainLoop mainLoop; +LineSegment segmentList[4]; int main() { context.init("#gl_canvas"); @@ -83,6 +110,11 @@ int main() { EM_BOOL update(float deltaTimeSeconds, void* userData) { renderer.render(); pill.shape.render(&renderer); + + for (int segmentIndex = 0; segmentIndex < 4; segmentIndex++) { + segmentList[segmentIndex].render(&renderer); + } + return true; } @@ -93,10 +125,19 @@ EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, voi printf("Play clicked\n"); renderer.load(&context); + + // Add the pill pill.width = 100.f; pill.height = 50.f; - pill.shape.model = pill.shape.model.translateByVec2(Vector2 { context.width / 2.f, context.height / 2.f }); + pill.shape.model = Mat4x4().translateByVec2(Vector2 { context.width / 2.f, context.height / 2.f }); pill.load(&renderer, 64); + + // Add the lines + segmentList[0].load(&renderer, Vector4().fromColor(191, 251, 146, 255.f), Vector2 { 50.f, 0.f }, Vector2 { 50.f, static_cast(context.height) }); + segmentList[1].load(&renderer, Vector4().fromColor(159, 224, 210, 255.f), Vector2 { context.width - 50.f, 0.f }, Vector2 { context.width - 50.f, static_cast(context.height) }); + segmentList[2].load(&renderer, Vector4().fromColor(248, 255, 156, 255.f), Vector2 { 50.f, 50.f }, Vector2 { context.width - 50.f, 150.f }); + segmentList[3].load(&renderer, Vector4().fromColor(205, 178, 214, 255.f), Vector2 { 50.f, 150.f }, Vector2 { context.width - 50.f, 50.f }); + mainLoop.run(update); return true; } diff --git a/frontend/shared_cpp/OrthographicRenderer.cpp b/frontend/shared_cpp/OrthographicRenderer.cpp index 1d62c04..21c13a1 100644 --- a/frontend/shared_cpp/OrthographicRenderer.cpp +++ b/frontend/shared_cpp/OrthographicRenderer.cpp @@ -53,7 +53,6 @@ void OrthographicRenderer::unload() { void OrthographicShape::load(OrthographicVertex* inVertices, uint32 inNumVertices, OrthographicRenderer* renderer) { - vertices = inVertices; numVertices = inNumVertices; useShader(renderer->shader); @@ -62,7 +61,7 @@ void OrthographicShape::load(OrthographicVertex* inVertices, uint32 inNumVertice glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(OrthographicVertex), &vertices[0], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(OrthographicVertex), &inVertices[0], GL_STATIC_DRAW); glEnableVertexAttribArray(renderer->attributes.position); glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)0); @@ -74,21 +73,15 @@ void OrthographicShape::load(OrthographicVertex* inVertices, uint32 inNumVertice glBindVertexArray(0); } -void OrthographicShape::render(OrthographicRenderer* renderer) { +void OrthographicShape::render(OrthographicRenderer* renderer, GLenum drawType) { setShaderMat4(renderer->uniforms.model, model); glBindVertexArray(vao); - glDrawArrays(GL_TRIANGLES, 0, numVertices); + glDrawArrays(drawType, 0, numVertices); glBindVertexArray(0); } void OrthographicShape::unload() { glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); - - if (vertices != NULL) { - delete vertices; - } - - vertices = NULL; } \ No newline at end of file diff --git a/frontend/shared_cpp/OrthographicRenderer.h b/frontend/shared_cpp/OrthographicRenderer.h index 0cdfc78..cef5305 100644 --- a/frontend/shared_cpp/OrthographicRenderer.h +++ b/frontend/shared_cpp/OrthographicRenderer.h @@ -35,10 +35,9 @@ struct OrthographicShape { uint32 vao; uint32 vbo; uint32 numVertices = 0; - OrthographicVertex* vertices = NULL; Mat4x4 model; void load(OrthographicVertex* vertices, uint32 numVertices, OrthographicRenderer* renderer); - void render(OrthographicRenderer* renderer); + void render(OrthographicRenderer* renderer, GLenum drawType = GL_TRIANGLES); void unload(); }; \ No newline at end of file -- cgit v1.2.1