Files
manigraph/src/main.c
2024-12-01 00:13:26 -06:00

225 lines
4.5 KiB
C

#include "main.h"
#include "data/axis.h"
#include "data/shaders.h"
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 512
#define HEIGHT 512
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
#ifndef M_PI
#define M_PI 3.14159
#endif
float *generate_data_surface(unsigned char *);
float *generate_normals_surface(float *, unsigned char);
struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3};
const char *wname = "manigraph: manifold grapher";
unsigned char palette[][4] = {
{0xEB, 0xD3, 0xF8, 0xff},
{0xEB, 0xD4, 0xF8, 0xff},
{0xEB, 0xD5, 0xF8, 0xff},
{0x2F, 0x3C, 0x7E, 0xff},
};
void mlog(char *msg)
{
#ifdef DEBUG
printf(msg);
#endif
}
window_t window;
mesh_t m_surface, m_axis;
id_t shader, shader_plain;
unsigned char m;
extern volatile unsigned char animate_index;
#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);
{
static float angle = 0;
if (animate_index && m)
{
angle += 0.01;
load_uint_to_shader(shader, "i", animate_index - 1);
load_uint_to_shader(shader_plain, "i", animate_index - 1);
load_float_to_shader(shader, "angle", angle);
load_float_to_shader(shader_plain, "angle", angle);
}
if (angle > M_PI / 2 && angle)
{
animate_index = 0;
angle = 0;
}
}
clean_context();
#ifndef DEBUG
load_mdl_matrix(shader_plain, 0, 0);
draw_mesh(m_axis, 1);
load_mdl_matrix(shader_plain, 1, 1);
draw_mesh(m_axis, 1);
load_mdl_matrix(shader_plain, 2, 2);
draw_mesh(m_axis, 1);
#endif
load_mdl_matrix(shader, 0, 3);
draw_mesh(m_surface, 0);
load_mdl_matrix(shader_plain, 0, 3);
draw_mesh(m_surface, 1);
}
int main(void)
{
id_t texture;
mlog("[VENTANA] Inicializando...\n");
{
if (!(window = init_window(WIDTH, HEIGHT, wname)))
{
mlog("[VENTANA] Error al inicializar...\n");
goto error_window;
}
use_window(window);
}
mlog("[CONTEXT] Inicializando...\n");
{
if (!(init_context()))
{
mlog("[CONTEXT] Error al inicializar...\n");
goto error_context;
}
set_clean_color_context(0xFB, 0xEA, 0xEB);
}
mlog("[TEXTURE] Inicializando...\n");
{
texture = create_palette_texture(palette, 4);
use_texture(texture);
}
mlog("[SHADER] Inicializando...\n");
{
if (!(shader = create_shader()))
{
mlog("[SHADER] Error al inicializar...\n");
goto error_shader;
}
load_program_to_shader(shader, vs, VERTEX);
load_program_to_shader(shader, fs, FRAGMENT);
load_fix_matrix(shader, (float)WIDTH / HEIGHT);
}
mlog("[SHADER] Inicializando...\n");
{
if (!(shader_plain = create_shader()))
{
mlog("[SHADER] Error al inicializar...\n");
goto error_shader_plain;
}
load_program_to_shader(shader_plain, vs, VERTEX);
load_program_to_shader(shader_plain, fs_plain, FRAGMENT);
load_fix_matrix(shader_plain, (float)WIDTH / HEIGHT);
}
mlog("[MESH] Inicializando...\n");
{
float *n_surface, *d_surface;
d_surface = generate_data_surface(&m);
n_surface = generate_normals_surface(d_surface, m);
if (!(m_surface = create_mesh(d_surface, n_surface, m)))
{
mlog("[MESH] Error al inicializar...\n");
goto error_mesh_surface;
}
projection.m = m;
projection.mesh = m_surface;
set_projection_mesh(projection);
free(n_surface);
free(d_surface);
}
mlog("[MESH] Inicializando...\n");
{
if (!(m_axis = create_mesh(d_axis, NULL, 3)))
{
mlog("[MESH] Error al inicializar...\n");
goto error_mesh_axis;
}
}
mlog("[MAIN LOOP] Inicializando...\n");
#ifdef EMSCRIPTEN
emscripten_set_main_loop(&main_loop, 0, 1);
#else
while (is_open_window(window))
main_loop();
#endif
mlog("[MAIN LOOP] Terminando...\n");
mlog("[MESH] Destruyendo...\n");
destroy_mesh(m_axis);
mlog("[MESH] Destruyendo...\n");
destroy_mesh(m_surface);
mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader_plain);
mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader);
mlog("[TEXTURE] Destruyendo...\n");
destroy_texture(texture);
mlog("[WINDOW] Destruyendo...\n");
close_window(window);
return 0;
mlog("[MESH] Destruyendo...\n");
destroy_mesh(m_axis);
error_mesh_axis:
mlog("[MESH] Destruyendo...\n");
destroy_mesh(m_surface);
error_mesh_surface:
mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader_plain);
error_shader_plain:
mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader);
error_shader:
mlog("[TEXTURE] Destruyendo...\n");
destroy_texture(texture);
error_context:
mlog("[WINDOW] Destruyendo...\n");
close_window(window);
error_window:
return 1;
}