From c36d05d5aed2f8f7c6342b174692146e2d11c386 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Mon, 22 Mar 2021 20:54:51 -0400 Subject: Refactored frontend, beginnings of general cpp layer, and beginning roadmap --- frontend/_wasm/wasm.cpp | 223 ------------------------------------------------ 1 file changed, 223 deletions(-) delete mode 100644 frontend/_wasm/wasm.cpp (limited to 'frontend/_wasm/wasm.cpp') diff --git a/frontend/_wasm/wasm.cpp b/frontend/_wasm/wasm.cpp deleted file mode 100644 index 254a5e2..0000000 --- a/frontend/_wasm/wasm.cpp +++ /dev/null @@ -1,223 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "mathlib.h" -#include "Shader.h" - -// Shader code inlined -const char* orthographicVertex = -"attribute vec2 position; \n" -"attribute vec4 color; \n" -"uniform mat4 projection; \n" -"uniform mat4 model; \n" -"varying lowp vec4 VertexColor; \n" -"void main() { \n" -" vec4 fragmentPosition = projection * model * vec4(position, 1, 1); \n" -" gl_Position = fragmentPosition; \n" -" VertexColor = color; \n" -"}"; - -const char* orthographicFragment = -"varying lowp vec4 VertexColor; \n" -"void main() { \n" -" gl_FragColor = VertexColor; \n" -"}"; - - -struct OrthographicProgramData { - GLuint shader; - - struct { - int position; - int color; - } attributes; - - struct { - int projection; - int model; - } uniformVariables; -}; - -struct OrthographicVertex { - Vector2 position; - Vector4 color; -}; - -// Create a triangle -struct TriangleObject { - OrthographicVertex vertices[3]; - Vector2 velocity; - Vector2 position; - GLuint mVao; - GLuint mVbo; - Mat4x4 model; - - void initialize(OrthographicProgramData* programData) { - useShader(programData->shader); - - glGenVertexArrays(1, &mVao); - glBindVertexArray(mVao); - - glGenBuffers(1, &mVbo); - glBindBuffer(GL_ARRAY_BUFFER, mVbo); - glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(OrthographicVertex), &vertices[0], GL_STATIC_DRAW); - - glEnableVertexAttribArray(programData->attributes.position); - glVertexAttribPointer(programData->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)0); - - glEnableVertexAttribArray(programData->attributes.color); - glVertexAttribPointer(programData->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)offsetof(OrthographicVertex, color)); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - } - - void render(OrthographicProgramData* programData) { - setShaderMat4(programData->uniformVariables.model, model); - - glBindVertexArray(mVao); - glDrawArrays(GL_TRIANGLES, 0, 3); - glBindVertexArray(0); - } - -}; - -struct Scene { - Mat4x4 projection; - TriangleObject triangleObject; - OrthographicProgramData programData; - bool isTerminated = false; -}; - -long long timeInMilliseconds(void) { - struct timeval tv; - - gettimeofday(&tv,NULL); - return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000); -} - -long elapsedTime = 0; -long lastTime = 0; -int numFrames = 0; - -EM_BOOL update(double time, void* userData); -EM_BOOL runScene(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); -EM_BOOL terminateScene(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData); - -int main() { - printf("Initializing the canvas...\n"); - emscripten_set_canvas_element_size( "#wasm_canvas", 640, 480 ); - - EmscriptenWebGLContextAttributes attrs; - emscripten_webgl_init_context_attributes(&attrs); - - attrs.enableExtensionsByDefault = 1; - attrs.majorVersion = 3; - attrs.minorVersion = 0; - - EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context( "#wasm_canvas", &attrs ); - - if (!context) - { - attrs.majorVersion = 1; - context = emscripten_webgl_create_context( "#wasm_canvas", &attrs ); - if (context) printf("Skipping test: WebGL 2.0 is not available.\n"); - else printf("Test failed: WebGL is not available!\n"); - return 0; - } - emscripten_webgl_make_context_current(context); - printf("Canvas ready.\n"); - - emscripten_set_click_callback("#wasm_canvas_play_button", NULL, false, runScene); - emscripten_set_click_callback("#wasm_canvas_stop_button", NULL, false, terminateScene); - return EXIT_SUCCESS; -} - -Scene scene; - -EM_BOOL runScene(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { - scene.programData = OrthographicProgramData(); - printf("Compiling shaders...\n"); - scene.programData.shader = loadShader(orthographicVertex, orthographicFragment); - printf("Shaders compiled.\n"); - - - printf("Initializing scene...\n"); - useShader(scene.programData.shader); - - scene.programData.attributes.position = getShaderAttribute(scene.programData.shader, "position"); - scene.programData.attributes.color = getShaderAttribute(scene.programData.shader, "color"); - scene.programData.uniformVariables.projection = getShaderUniform(scene.programData.shader, "projection"); - scene.programData.uniformVariables.model = getShaderUniform(scene.programData.shader, "model"); - - scene.triangleObject = TriangleObject(); - scene.triangleObject.vertices[0] = { Vector2 { -100, -50 }, Vector4 { 1.f, 0.f, 0.f, 1.f }}; - scene.triangleObject.vertices[1] = { Vector2 { 0, 50 }, Vector4 { 0.f, 1.f, 0.f, 1.f }}; - scene.triangleObject.vertices[2] = { Vector2 { 100, -50 }, Vector4 { 0.f, 0.f, 1.f, 1.f }}; - scene.triangleObject.position = { 320.f, 480.f - 100.f }; - scene.triangleObject.initialize(&scene.programData); - - scene.projection = Mat4x4().getOrthographicMatrix(0, 640, 0, 480); - scene.isTerminated = false; - printf("Scene initialized.\n"); - - emscripten_request_animation_frame_loop(update, &scene); - return true; -} - -EM_BOOL update(double time, void* userData) { - Scene* scene = (Scene*)userData; - if (scene->isTerminated) { - return false; - } - - if (lastTime == 0) { - lastTime = time; - return true; - } - - long deltaTime = time - lastTime; - lastTime = time; - elapsedTime += deltaTime; - numFrames++; - - if (elapsedTime >= 1000.0) { - printf("Frames Per Second: %d\n", numFrames); - numFrames = 0; - elapsedTime = 0.0; - } - - float deltaTimeSeconds = static_cast(deltaTime) / 1000.f; - - // Update - scene->triangleObject.velocity = scene->triangleObject.velocity + Vector2 { 0, static_cast(-9.8 * deltaTimeSeconds) }; - scene->triangleObject.position = scene->triangleObject.position + scene->triangleObject.velocity * deltaTimeSeconds; - scene->triangleObject.model = Mat4x4().translateByVec2(scene->triangleObject.position); - - // Render - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); - glDepthMask(GL_TRUE); - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClearDepth(1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - useShader(scene->programData.shader); - setShaderMat4(scene->programData.uniformVariables.projection, scene->projection); - scene->triangleObject.render(&scene->programData); - - return true; -} - -EM_BOOL terminateScene(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) { - scene.isTerminated = true; - glDeleteVertexArrays(1, &scene.triangleObject.mVao); - glDeleteBuffers(1, &scene.triangleObject.mVbo); - glDeleteProgram(scene.programData.shader); - return true; -} -- cgit v1.2.1