Port to web for free

This commit is contained in:
PedroEdiaz
2024-11-19 21:54:22 -06:00
parent c4016d239a
commit 61b014a803
4 changed files with 71 additions and 27 deletions

View File

@@ -1,10 +1,15 @@
#include "main.h"
#ifdef EMSCRIPTEN
#include <GL/gl.h>
#else
#ifdef GLAD
#include <GLFW/glfw3.h>
#include <glad.h>
#else
#include <GL/glew.h>
#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); }

View File

@@ -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;"

64
src/main.c Executable file → Normal file
View File

@@ -8,6 +8,10 @@
#define WIDTH 512
#define HEIGHT 512
#ifdef EMSCRIPTEN
#include <emscripten.h>
#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");