diff --git a/Makefile b/Makefile index 89e4d3f..b2e71e1 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ help: @echo " $(MAKE) linux-x11" @echo " $(MAKE) linux-wayland" @echo " $(MAKE) cocoa" + @echo " $(MAKE) CC=emcc wasm" @echo "Para limpiar los archivos compilados se puede usar" @echo " $(MAKE) clean" @echo "Para ejecturar el programa sin instalarlos se puede usar:" @@ -72,6 +73,9 @@ cocoa: $(OBJ) $(MAKE) DFLAGS="-D_GLFW_COCOA" libglfw.so $(CC) -framework OpenGL -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw +wasm: $(OBJ) + $(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o mani.html $(OBJ) + libglfw.so: $(CC) -fPIC -shared $(DFLAGS) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@ @@ -81,5 +85,4 @@ clean: .SUFFIXES: .c .o .c.o: src/main.h - clang-format -i $< $(CC) $(CFLAGS) -c -o $@ $< diff --git a/src/context.c b/src/context.c index 39c2169..c9f00b8 100644 --- a/src/context.c +++ b/src/context.c @@ -1,10 +1,15 @@ #include "main.h" + +#ifdef EMSCRIPTEN +#include +#else #ifdef GLAD #include #include #else #include #endif +#endif void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b) { @@ -15,12 +20,16 @@ void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b) int init_context(void) { +#ifdef EMSCRIPTEN + return 1; +#else #ifdef GLAD return gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); #else return glewInit(); #endif +#endif } void clean_context(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } diff --git a/src/data/shaders.h b/src/data/shaders.h index 1f058ce..e166136 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -1,6 +1,10 @@ const char * vs = +#ifdef EMSCRIPTEN + "#version 300 es\n" + "precision highp float;" +#else "#version 330 core\n" - +#endif "layout (location = 0) in float aPos_x;" "layout (location = 1) in float aPos_y;" "layout (location = 2) in float aPos_z;" @@ -32,8 +36,14 @@ const char * vs = const char * fs_plain = - "#version 330 core\n" +#ifdef EMSCRIPTEN + "#version 300 es\n" + "precision highp float;" + "precision highp sampler2DArray;" +#else + "#version 330 core\n" +#endif "uniform sampler2DArray palette;" "in float index;" @@ -47,7 +57,13 @@ const char * fs_plain = "}"; const char * fs = +#ifdef EMSCRIPTEN + "#version 300 es\n" + "precision highp float;" + "precision highp sampler2DArray;" +#else "#version 330 core\n" +#endif "uniform sampler2DArray palette;" diff --git a/src/main.c b/src/main.c old mode 100755 new mode 100644 index 30084f2..4e6790f --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,10 @@ #define WIDTH 512 #define HEIGHT 512 +#ifdef EMSCRIPTEN +#include +#endif + float *generate_data_surface(unsigned int, unsigned char *); float *generate_normals_surface(float *, unsigned char); @@ -29,11 +33,40 @@ void mlog(char *msg) #endif } +window_t window; +mesh_t m_surface, m_axis; +id_t shader, shader_plain; + +#ifndef EMSCRIPTEN +static inline +#endif +void main_loop(void) +{ + quat_t q; + + q = poll_input(window); + load_rot_matrix(shader, q); + load_rot_matrix(shader_plain, q); + + clean_context(); + +#ifndef DEBUG + load_mdl_matrix(shader_plain, 0, 0); + draw_mesh(m_axis); + load_mdl_matrix(shader_plain, 1, 1); + draw_mesh(m_axis); + load_mdl_matrix(shader_plain, 2, 2); + draw_mesh(m_axis); + load_mdl_matrix(shader, 0, 3); +#else + load_mdl_matrix(shader_plain, 0, 3); +#endif + draw_mesh(m_surface); +} + int main(void) { - id_t shader, texture, shader_plain; - mesh_t m_surface, m_axis; - window_t window; + id_t texture; mlog("[VENTANA] Inicializando...\n"); { @@ -117,29 +150,12 @@ int main(void) } mlog("[MAIN LOOP] Inicializando...\n"); - while (is_open_window(window)) - { - quat_t q; - - q = poll_input(window); - load_rot_matrix(shader, q); - load_rot_matrix(shader_plain, q); - - clean_context(); - -#ifndef DEBUG - load_mdl_matrix(shader_plain, 0, 0); - draw_mesh(m_axis); - load_mdl_matrix(shader_plain, 1, 1); - draw_mesh(m_axis); - load_mdl_matrix(shader_plain, 2, 2); - draw_mesh(m_axis); - load_mdl_matrix(shader, 0, 3); +#ifdef EMSCRIPTEN + emscripten_set_main_loop(&main_loop, 0, 1); #else - load_mdl_matrix(shader_plain, 0, 3); + while (is_open_window(window)) + main_loop(); #endif - draw_mesh(m_surface); - } mlog("[MAIN LOOP] Terminando...\n"); mlog("[MESH] Destruyendo...\n");