diff options
Diffstat (limited to '3d/rigidbody/main.cpp')
-rw-r--r-- | 3d/rigidbody/main.cpp | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/3d/rigidbody/main.cpp b/3d/rigidbody/main.cpp new file mode 100644 index 0000000..8fe0691 --- /dev/null +++ b/3d/rigidbody/main.cpp @@ -0,0 +1,133 @@ +#include "../../shared_cpp/Renderer3d.h" +#include "../../shared_cpp/Camera3d.h" +#include "../../shared_cpp/types.h" +#include "../../shared_cpp/WebglContext.h" +#include "../../shared_cpp/mathlib.h" +#include "../../shared_cpp/MainLoop.h" +#include <cstdio> +#include <cmath> +#include <emscripten/html5.h> +#include <unistd.h> +#include <pthread.h> +#include <cmath> +#include <cfloat> + +struct Cube { + Mesh3d mesh; + + void load(Renderer3d* renderer) { + Vertex3d cubeVertices[] = { + { + Vector3 { -1.0, -1.0, 1.0 }, + Vector3() + }, + { + Vector3 { 1.0, -1.0, 1.0 }, + Vector3() + }, + { + Vector3 { -1.0, 1.0, 1.0 }, + Vector3() + }, + { + Vector3 { 1.0, 1.0, 1.0 }, + Vector3() + }, + { + Vector3 { -1.0, -1.0, -1.0 }, + Vector3() + }, + { + Vector3 { 1.0, -1.0, -1.0 }, + Vector3() + }, + { + Vector3 { -1.0, 1.0, -1.0 }, + Vector3() + }, + { + Vector3 { 1.0, 1.0, -1.0 }, + Vector3() + } + }; + + + uint32 cubeIndices[] = { + 0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1 + }; + + + mesh.load(&cubeVertices[0], 8, cubeIndices, 14, renderer); + } + + void update(float32 dtSeconds) { + mesh.model = mesh.model.rotate2D((PI / 8.f) * dtSeconds); + } + + void render(Renderer3d* renderer) { + mesh.render(renderer); + } + + void unload() { + mesh.unload(); + } +}; + +EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); +EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); + +void load(); +void update(float32 time, void* userData); +void unload(); + +WebglContext context; +Renderer3d renderer; +Camera3d camera; +MainLoop mainLoop; +bool isIntersectingPointer = false; +Cube cube; + +int main() { + context.init("#gl_canvas"); + emscripten_set_click_callback("#gl_canvas_play", NULL, false, onPlayClicked); + emscripten_set_click_callback("#gl_canvas_stop", NULL, false, onStopClicked); + return 0; +} + +void load() { + renderer.load(&context); + cube.load(&renderer); + camera.projection = camera.projection.getPerspectiveProjection(0.1f, 100.f, PI / 2.f, 800.f / 600.f); + camera.view = camera.view.translate({ 0, 0, -10 }); + mainLoop.run(update); +} + +void update(float32 deltaTimeSeconds, void* userData) { + cube.update(deltaTimeSeconds); + + // Renderer + renderer.render(&camera); + cube.render(&renderer); +} + +void unload() { + mainLoop.stop(); + renderer.unload(); + cube.unload(); +} + +// +// Interactions with DOM handled below +// +EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { + printf("Play clicked\n"); + + load(); + return true; +} + +EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { + printf("Stop clicked\n"); + unload(); + return true; +} |