From cc05fdc7396532b329f30decde5583853da92a44 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sat, 27 Mar 2021 20:54:27 -0400 Subject: Drawing an ellipse --- frontend/2d/_collisions/pill_line/dist/output.wasm | Bin 27360 -> 37565 bytes frontend/2d/_collisions/pill_line/main.cpp | 85 +++++++++++++++++---- 2 files changed, 71 insertions(+), 14 deletions(-) (limited to 'frontend/2d') diff --git a/frontend/2d/_collisions/pill_line/dist/output.wasm b/frontend/2d/_collisions/pill_line/dist/output.wasm index be1afc5..92ee1f2 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 321a4ba..df20fa0 100644 --- a/frontend/2d/_collisions/pill_line/main.cpp +++ b/frontend/2d/_collisions/pill_line/main.cpp @@ -7,16 +7,70 @@ #include #include #include +#include -typedef OrthographicShape<3> TriangleShape; +// +// Pill object +// +struct Pill { + OrthographicShape shape; + float32 width = 1.f; + float32 height = 1.f; + + void load(OrthographicRenderer* renderer, float32 numSegments) { + // Note that a so-called "pill" is simply an ellipse. + // Equation of an ellipse is: + // + // x^2 / a^2 + y^2 / b^2 = 1 + // + // or, in parametric form: + // + // x = a * cos(t), y = b * sin(t) + // + float32 angleIncrements = (2.f * PI) / numSegments; + uint32 numVertices = static_cast(numSegments * 3.f); + OrthographicVertex* vertices = new OrthographicVertex[numVertices]; + + float32 a = width / 2.f; + float32 b = height / 2.f; + + Vector4 color = Vector4().fromColor(243,166,207, 255); + + for (uint32 vertexIndex = 0; vertexIndex < numVertices; vertexIndex += 3) { + // Create a single "slice" of the ellipse (like a pizza) + float32 currAngle = (vertexIndex / 3.f) * angleIncrements; + float32 nextAngle = (vertexIndex / 3.f + 1.f) * angleIncrements; + + vertices[vertexIndex].position = Vector2 { 0.f, 0.f }; + vertices[vertexIndex].color = color; + + vertices[vertexIndex + 1].position = Vector2 { a * cosf(currAngle), b * sinf(currAngle) }; + vertices[vertexIndex + 1].color = color; + + vertices[vertexIndex + 2].position = Vector2 { a * cosf(nextAngle), b * sinf(nextAngle) }; + vertices[vertexIndex + 2].color = color; + } + + shape.load(vertices, numVertices, renderer); + } + + void unload() { + shape.unload(); + } + + float32 getArea() { + return 0.f; + } +}; 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); +// Global Variables WebglContext context; OrthographicRenderer renderer; -TriangleShape shape; +Pill pill; MainLoop mainLoop; int main() { @@ -26,14 +80,23 @@ int main() { return 0; } +EM_BOOL update(float deltaTimeSeconds, void* userData) { + renderer.render(); + pill.shape.render(&renderer); + return true; +} + +// +// Interactions with DOM handled below +// EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { printf("Play clicked\n"); - renderer.load(); - shape.vertices[0] = { Vector2 { -1, -1 }, Vector4 { 1, 0, 0, 1 } }; - shape.vertices[1] = { Vector2 { 0, 1 }, Vector4{ 0, 1, 0, 1 } }; - shape.vertices[2] = { Vector2 { 1, -1 }, Vector4 { 0, 0, 1, 1 } }; - shape.load(&renderer); + renderer.load(&context); + 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.load(&renderer, 64); mainLoop.run(update); return true; } @@ -41,13 +104,7 @@ EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, voi EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { printf("Stop clicked\n"); mainLoop.stop(); - shape.unload(); + pill.unload(); renderer.unload(); return true; -} - -EM_BOOL update(float deltaTimeSeconds, void* userData) { - renderer.render(); - shape.render(&renderer); - return true; } \ No newline at end of file -- cgit v1.2.1