Merge branch 'main' into roberto

This commit is contained in:
PedroEdiaz
2024-11-20 11:14:59 -06:00
24 changed files with 5956 additions and 616 deletions

View File

@@ -1,14 +1,35 @@
#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 )
void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b)
{
glEnable( GL_DEPTH_TEST );
glEnable(GL_DEPTH_TEST);
glClearColor( (float)r/0xff, (float)g/0xff, (float)b/0xff, 1.0 );
glClearColor((float)r / 0xff, (float)g / 0xff, (float)b / 0xff, 1.0);
}
void clean_context( void )
int init_context(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
#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,31 +0,0 @@
#define A -1,-1,-1,
#define B -1,-1, 1,
#define C -1, 1,-1,
#define D -1, 1, 1,
#define E 1,-1,-1,
#define F 1,-1, 1,
#define G 1, 1,-1,
#define H 1, 1, 1,
float d_cube[] =
{
3*3*2*6,
A C E
G E C
E G F
H F G
F H B
D B H
B D A
C A D
C D G
H G D
E B A
B E F
};

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;"
@@ -67,19 +83,9 @@ const char * fs =
" vec3 lightPos = viewPos;\n"
" vec3 lightDir = normalize(lightPos - FragPos);\n"
" float specular = 0.0;\n"
" if (true)"
" {"
" vec3 halfwayDir = normalize(lightDir + viewDir);\n"
" specular = pow(max(dot(normalize(Normal), halfwayDir), 0.0), 16.0);\n"
" }"
" else"
" {"
" vec3 reflectDir = reflect(-lightDir, normalize(Normal));\n"
" specular = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);\n"
" }"
" vec3 halfwayDir = normalize(lightDir + viewDir);\n"
" float specular = pow(max(dot(normalize(Normal), halfwayDir), 0.0), 16.0);\n"
" float diffuse = max(dot(normalize(Normal), lightDir), 0.0);\n"
" vec3 result = (0.5 + 0.5 * diffuse + specular) * color.rgb;\n"

View File

@@ -1,9 +1,9 @@
#include "main.h"
#include <cglm/quat.h>
#include <GLFW/glfw3.h>
#include <cglm/quat.h>
#include <stdio.h>
#define ANGLE ((float)0x01/0xff*2*GLM_PI)
#define ANGLE ((float)0x01 / 0xff * 2 * GLM_PI)
unsigned char selected_axis = 0;
int window_width;
@@ -11,25 +11,67 @@ int window_height;
versor q = GLM_QUAT_IDENTITY_INIT;
vec3 axis[3] =
{
vec3 axis[3] = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
};
void __key_callback(GLFWwindow * window, int key, int scancode, int action, int mods )
extern struct projection projection;
void __key_callback_input(
GLFWwindow *window, int key, int scancode, int action, int mods)
{
if( action != GLFW_PRESS)
if (action != GLFW_PRESS)
return;
if( GLFW_KEY_0 <= key && key <= GLFW_KEY_9 )
if (GLFW_KEY_0 < key && key <= GLFW_KEY_9)
{
printf("%d\n", key-GLFW_KEY_0 );
unsigned char selected_coord;
selected_coord = key - GLFW_KEY_0 - 1;
if (selected_coord >= projection.m)
return;
if (selected_coord == projection.x)
return;
if (selected_coord == projection.y)
return;
if (selected_coord == projection.z)
return;
selected_coord = projection.w;
}
if (projection.w >= projection.m)
return;
switch (key)
{
unsigned char tmp;
case GLFW_KEY_P:
tmp = projection.w;
projection.w = projection.x;
projection.x = tmp;
break;
case GLFW_KEY_O:
tmp = projection.w;
projection.w = projection.y;
projection.y = tmp;
break;
case GLFW_KEY_I:
tmp = projection.w;
projection.w = projection.z;
projection.z = tmp;
break;
}
set_projection_mesh(projection);
return;
}
void __window_callback(GLFWwindow * window, int w, int h)
void __window_callback_input(GLFWwindow *window, int w, int h)
{
int m;
@@ -40,32 +82,33 @@ void __window_callback(GLFWwindow * window, int w, int h)
glViewport((w - m) / 2, (h - m) / 2, m, m);
}
void __mouse_callback(GLFWwindow* window, int button, int action, int mods)
void __mouse_callback_input(
GLFWwindow *window, int button, int action, int mods)
{
unsigned char green_value;
double xpos, ypos;
if( button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS )
return;
if (button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS)
return;
glfwGetCursorPos(window, &xpos, &ypos);
glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN,
glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN,
GL_UNSIGNED_BYTE, &green_value);
switch(green_value)
switch (green_value)
{
case 0xD3:
case 0xD4:
case 0xD5:
selected_axis = green_value-0xD3;
selected_axis = green_value - 0xD3;
}
}
void __scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset)
{
versor p = GLM_QUAT_IDENTITY_INIT;
glm_quatv(p, yoffset*ANGLE, axis[selected_axis]);
glm_quatv(p, yoffset * ANGLE, axis[selected_axis]);
glm_quat_mul(p, q, q);
glm_quat_rotatev(p, axis[0], axis[0]);
@@ -77,43 +120,42 @@ quat_t poll_input(window_t window)
{
versor p = GLM_QUAT_IDENTITY_INIT;
if( glfwGetKey((GLFWwindow*)window, 'Q') == GLFW_PRESS )
if (glfwGetKey((GLFWwindow *)window, 'Q') == GLFW_PRESS)
{
glm_quatv(p, ANGLE, axis[0]);
goto end;
}
if( glfwGetKey((GLFWwindow*)window, 'W') == GLFW_PRESS )
if (glfwGetKey((GLFWwindow *)window, 'W') == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, axis[0]);
goto end;
}
if( glfwGetKey((GLFWwindow*)window, 'A') == GLFW_PRESS )
if (glfwGetKey((GLFWwindow *)window, 'A') == GLFW_PRESS)
{
glm_quatv(p, ANGLE, axis[1]);
goto end;
}
if( glfwGetKey((GLFWwindow*)window, 'S') == GLFW_PRESS )
if (glfwGetKey((GLFWwindow *)window, 'S') == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, axis[1]);
goto end;
}
if( glfwGetKey((GLFWwindow*)window, 'Z') == GLFW_PRESS )
if (glfwGetKey((GLFWwindow *)window, 'Z') == GLFW_PRESS)
{
glm_quatv(p, ANGLE, axis[2]);
goto end;
}
if( glfwGetKey((GLFWwindow*)window, 'X') == GLFW_PRESS )
if (glfwGetKey((GLFWwindow *)window, 'X') == GLFW_PRESS)
{
glm_quatv(p, -ANGLE, axis[2]);
goto end;
}
end:
glm_quat_mul( p, q, q );
glm_quat_rotatev( p, axis[0], axis[0] );
glm_quat_rotatev( p, axis[1], axis[1] );
glm_quat_rotatev( p, axis[2], axis[2] );
glm_quat_normalize( q );
glm_quat_mul(p, q, q);
glm_quat_rotatev(p, axis[0], axis[0]);
glm_quat_rotatev(p, axis[1], axis[1]);
glm_quat_rotatev(p, axis[2], axis[2]);
glm_quat_normalize(q);
return q;
}

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

@@ -2,231 +2,194 @@
#include "data/axis.h"
#include "data/shaders.h"
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#define CGLM_ALL_UNALIGNED
#include <cglm/vec3.h>
#include <cglm/vec4.h>
#include <string.h>
#define WIDTH 512
#define HEIGHT 512
unsigned char coordanate[4] = {0,1,2,3};
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
unsigned char palette[] =
{
16,
0xEB,0xD3,0xF8,0xff,
0xEB,0xD4,0xF8,0xff,
0xEB,0xD5,0xF8,0xff,
0x7A,0x1C,0xAC,0xff,
float *generate_data_surface(unsigned int, unsigned char *);
float *generate_normals_surface(float *, unsigned char);
const char *wname = "manigraph: manifold grapher";
struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3};
unsigned char palette[][4] = {
{0xEB, 0xD3, 0xF8, 0xff},
{0xEB, 0xD4, 0xF8, 0xff},
{0xEB, 0xD5, 0xF8, 0xff},
{0x7A, 0x1C, 0xAC, 0xff},
};
void calc_normal(float* p1, float* p2, float* p3, float* normal, unsigned char n) {
float **u;
float alpha;
vec4 v1, v2, v3;
vec4 u1, u2, u3;
switch (n) {
case 3:
glm_vec3_sub(p2, p1, v1);
glm_vec3_sub(p3, p1, v2);
glm_vec3_cross(v1, v2, normal);
glm_vec3_normalize(normal);
return;
#if 0
case 4:
glm_vec4_sub(p2, p1, v1);
glm_vec4_sub(p3, p1, v2);
glm_vec4_copy(p1, v3);
glm_vec4_copy(v1, u1);
{
vec4 proj;
alpha = glm_vec4_dot(v2, u1) / glm_vec4_dot(u1, u1);
glm_vec4_scale(u1, alpha, proj);
glm_vec4_sub(v2, proj, u2);
}
{
vec4 proj1, proj2;
alpha = glm_vec4_dot(v3, u1) / glm_vec4_dot(u1, u1);
glm_vec4_scale(u1, alpha, proj1);
alpha = glm_vec4_dot(v3, u2) / glm_vec4_dot(u2, u2);
glm_vec4_scale(u2, alpha, proj2);
glm_vec4_sub(v3, proj1, u3);
glm_vec4_sub(u3, proj2, u3);
}
glm_vec4_copy(u3, normal);
glm_vec4_normalize(normal);
return;
void mlog(char *msg)
{
#ifdef DEBUG
printf(msg);
#endif
default:
u = malloc((n - 1) * sizeof(float*));
for (unsigned char i = 0; i < n - 1; i++) {
u[i] = malloc(n * sizeof(float));
}
for (unsigned char i = 0; i < n - 1; i++) {
float* vi = malloc(n * sizeof(float));
for (unsigned char j = 0; j < n; j++) {
vi[j] = p2[j] - p1[j];
}
for (unsigned char j = 0; j < i; j++) {
float dot_vu = 0.0f, dot_uu = 0.0f;
for (unsigned char k = 0; k < n; k++) {
dot_vu += vi[k] * u[j][k];
dot_uu += u[j][k] * u[j][k];
}
for (unsigned char k = 0; k < n; k++) {
vi[k] -= (dot_vu / dot_uu) * u[j][k];
}
}
memcpy(u[i], vi, n * sizeof(float));
free(vi);
}
memcpy(normal, u[n - 2], n * sizeof(float));
float norm = 0.0f;
for (unsigned char i = 0; i < n; i++) {
norm += normal[i] * normal[i];
}
norm = sqrtf(norm);
for (unsigned char i = 0; i < n; i++) {
normal[i] /= norm;
}
for (unsigned char i = 0; i < n - 1; i++) {
free(u[i]);
}
free(u);
return;
}
}
float * fill_normal( float * d, unsigned char m )
window_t window;
mesh_t m_surface, m_axis;
id_t shader, shader_plain;
#ifndef EMSCRIPTEN
static inline
#endif
void
main_loop(void)
{
float * n;
n = malloc( (*d+1)*sizeof(float));
*n = *d;
for (int i = 0; i < *d; i += 3*m)
{
quat_t q;
vec4 norm_vec;
q = poll_input(window);
load_rot_matrix(shader, q);
load_rot_matrix(shader_plain, q);
calc_normal((d+1)+i, (d+1)+i+m, (d+1)+i+2*m, norm_vec, m);
glm_vec3_copy( norm_vec, (n+1)+i );
glm_vec3_copy( norm_vec, (n+1)+i+m );
glm_vec3_copy( norm_vec, (n+1)+i+2*m );
}
return n;
}
const char * wname = "manigraph: manifold grapher";
float * generate_surface();
int main( void )
{
id_t shader, texture, shader_plain;
mesh_t m_cube, m_axis;
window_t window;
if( !( window = init_window( WIDTH, HEIGHT, wname ) ) )
goto error_window;
use_window( window );
set_clean_color_context( 0x2E, 0x07, 0x3F );
glewInit();
texture=create_palette_texture( palette );
use_texture( texture );
if( !( shader = create_shader() ) )
goto error_shader;
gload_program( shader, vs, VERTEX );
gload_program( shader, fs, FRAGMENT );
if( !( shader_plain = create_shader() ) )
goto error_shader_plain;
gload_program( shader_plain, vs, VERTEX );
gload_program( shader_plain, fs_plain, FRAGMENT );
load_fix_matrix( shader, (float)WIDTH/HEIGHT );
load_fix_matrix( shader_plain, (float)WIDTH/HEIGHT );
/* Fill m_cube */
{
float * n_cube, *d_cube;
d_cube = generate_surface(16);
n_cube = fill_normal( d_cube, 4 );
if( !( m_cube = create_mesh( d_cube, n_cube, coordanate, 4 ) ) )
goto error_mesh_cube;
free( n_cube );
free( d_cube);
}
/* Fill m_axis */
{
float * n_axis;
n_axis = fill_normal( d_axis, 3 );
if( !( m_axis = create_mesh( d_axis, n_axis, coordanate, 3 ) ) )
goto error_mesh_axis;
free( n_axis );
}
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();
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_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);
}
load_mdl_matrix( shader, 0, 3 );
draw_mesh( m_cube );
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);
}
destroy_mesh( m_axis );
destroy_mesh( m_cube );
destroy_shader( shader_plain );
destroy_shader( shader );
destroy_texture( texture );
close_window( window );
mlog("[CONTEXT] Inicializando...\n");
{
if (!(init_context()))
{
mlog("[CONTEXT] Error al inicializar...\n");
goto error_context;
}
set_clean_color_context(0x2E, 0x07, 0x3F);
}
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");
{
unsigned char m;
float *n_surface, *d_surface;
d_surface = generate_data_surface(16, &m);
n_surface = generate_normals_surface(d_surface, m);
projection.m = m;
if (!(m_surface = create_mesh(d_surface, n_surface, m)))
{
mlog("[MESH] Error al inicializar...\n");
goto error_mesh_surface;
}
projection.mesh = m_surface;
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:
destroy_mesh( m_cube );
error_mesh_cube:
destroy_shader( shader_plain );
mlog("[MESH] Destruyendo...\n");
destroy_mesh(m_surface);
error_mesh_surface:
mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader_plain);
error_shader_plain:
destroy_shader( shader );
mlog("[SHADER] Destruyendo...\n");
destroy_shader(shader);
error_shader:
close_window( window );
destroy_texture( texture );
mlog("[TEXTURE] Destruyendo...\n");
destroy_texture(texture);
error_context:
mlog("[WINDOW] Destruyendo...\n");
close_window(window);
error_window:
return 1;
}

View File

@@ -1,5 +1,11 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
/*
If DEBUG is set, we show the triangles of the mesh,
without illumination, and we write the compilation
error of the shaders.
*/
/* #define DEBUG */
/* #define GLAD */
typedef const void * window_t;
typedef unsigned int id_t;
@@ -7,13 +13,29 @@ typedef void * mesh_t;
typedef float * quat_t;
typedef float * mat4_t;
typedef unsigned char narray_u8_t[];
/*
This struct represent the proyection, where:
mesh: data of surface.
m: the dimention of the surface.
x: the coordanate of the x axis.
y: the coordanate of the y axis.
z: the coordanate of the z axis.
w: the coordanate of the w axis.
*/
enum
struct projection
{
VERTEX, FRAGMENT
mesh_t mesh;
unsigned char m, x, y, z, w;
};
/*
Init window:
w: default width;
h: default height;
name: Name of the window.
*/
window_t init_window(unsigned int w, unsigned int h, const char * name);
void use_window(window_t window);
@@ -22,32 +44,92 @@ int is_open_window(window_t window);
void close_window(window_t window);
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m );
/*
Create mesh:
d: array of floats with the vertex data.
n: array of floats with the normal data.
m: Dimention of mesh
*/
mesh_t create_mesh( float * d, float * n, unsigned char m );
void set_projection_mesh( struct projection );
void destroy_mesh(mesh_t p);
void draw_mesh(mesh_t p);
void set_clean_color_context(unsigned char, unsigned char, unsigned char);
/*
Set background color:
r: red value in hex.
g: green value in hex.
b: blue value in hex.
*/
void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b);
void clean_context(void);
int init_context( void );
void destroy_shader(id_t shader);
id_t create_shader(void);
void use_shader(id_t program);
void use_shader(id_t shader);
unsigned char gload_program(id_t program, const char * src, unsigned int type);
enum
{
VERTEX, FRAGMENT
};
void gload_float(id_t program, char * var, float f);
/*
Load program to shader:
src: GLSL source code as string.
type: VERTEX or FRAGMENT
*/
void gload_mat4(id_t program, char * var, mat4_t m);
unsigned char load_program_to_shader(id_t shader, const char * src, unsigned int type);
/*
load float to shader:
var: name of glsl variable.
f: float to load
*/
void load_float_to_shader(id_t shader, char * var, float f);
/*
load matrix 4 to shader:
var: name of glsl variable.
m: Matrix to load
*/
void load_mat4_to_shader(id_t shader, char * var, mat4_t m);
/*
Generate and load fix matrix, this matrix
has the information of the perspective and
camera information.
ratio: default ratio of window.
*/
void load_fix_matrix(id_t shader, float ratio);
/*
Generate and load model matrix, it also sets the color
to draw.
i: From {0,1,2} select one of 3 ortogonal rotations,
One for each axis.
c: Color index of the pallete.
*/
void load_mdl_matrix(id_t shader, unsigned char i, unsigned char c);
/*
Generate and load rotation matrix.
q: quaterinon describing the rotation.
*/
void load_rot_matrix(id_t shader, quat_t q);
id_t config_texture(unsigned short type);
@@ -56,6 +138,11 @@ void use_texture(id_t texture);
void destroy_texture(id_t texture);
id_t create_palette_texture(const narray_u8_t colors);
/*
Set color palette as texture:
colors: array of color values (rgba in hex ).
n: number of color on colors.
*/
id_t create_palette_texture(const unsigned char colors[][4], unsigned char n );
quat_t poll_input(window_t window);

View File

@@ -1,51 +1,50 @@
#include "main.h"
#include <cglm/mat4.h>
#include <cglm/cam.h>
#include <cglm/mat4.h>
#include <cglm/quat.h>
mat4 ortho[] =
{
mat4 ortho[] = {
{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 },
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
},
{
{ 0, 1, 0, 0 },
{-1, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 },
{0, 1, 0, 0},
{-1, 0, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
},
{
{ 0, 0, 1, 0 },
{ 0, 1, 0, 0 },
{-1, 0, 0, 0 },
{ 0, 0, 0, 1 },
{0, 0, 1, 0},
{0, 1, 0, 0},
{-1, 0, 0, 0},
{0, 0, 0, 1},
},
};
void load_fix_matrix( id_t shader, float ratio )
void load_fix_matrix(id_t shader, float ratio)
{
mat4 m, n;
const int d = 7;
glm_lookat( (vec3){0,0,-d}, (vec3){0,0,0}, (vec3){0,1,0}, m );
glm_perspective( CGLM_PI/4, ratio, d-3, d+3, n );
glm_mat4_mul( n, m, m );
glm_lookat((vec3){0, 0, -d}, (vec3){0, 0, 0}, (vec3){0, 1, 0}, m);
glm_perspective(CGLM_PI / 4, ratio, d - 3, d + 3, n);
glm_mat4_mul(n, m, m);
gload_mat4( shader, "fix", (mat4_t) m );
load_mat4_to_shader(shader, "fix", (mat4_t)m);
}
void load_mdl_matrix( id_t shader, unsigned char i, unsigned char c )
void load_mdl_matrix(id_t shader, unsigned char i, unsigned char c)
{
gload_float( shader, "idx", c );
gload_mat4( shader, "mdl", (mat4_t)ortho[i] );
load_float_to_shader(shader, "idx", c);
load_mat4_to_shader(shader, "mdl", (mat4_t)ortho[i]);
}
void load_rot_matrix( id_t shader, quat_t q )
void load_rot_matrix(id_t shader, quat_t q)
{
mat4 m;
glm_quat_mat4( q, m );
gload_mat4( shader, "rot", (mat4_t)m );
glm_quat_mat4(q, m);
load_mat4_to_shader(shader, "rot", (mat4_t)m);
}

132
src/mesh.c Executable file → Normal file
View File

@@ -1,75 +1,121 @@
#include "main.h"
#ifdef GLAD
#include <glad.h>
#else
#include <GL/glew.h>
#endif
#include <stdio.h>
#include <stdlib.h>
struct obj
struct obj
{
unsigned int vertex, vao, n_vbo, d_vbo;
};
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m )
void set_projection_mesh(struct projection projection)
{
struct obj * p;
struct obj *p;
p = projection.mesh;
p=malloc(sizeof(struct obj));
glBindVertexArray(p->vao);
p->vertex=(*d)/m;
glBindBuffer(GL_ARRAY_BUFFER, p->d_vbo);
glVertexAttribPointer(0, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float *)(projection.x * sizeof(float)));
glVertexAttribPointer(1, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float *)(projection.y * sizeof(float)));
glVertexAttribPointer(2, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float *)(projection.z * sizeof(float)));
glVertexAttribPointer(3, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float *)(projection.w * sizeof(float)));
glGenVertexArrays( 1, &p->vao );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, p->n_vbo);
glVertexAttribPointer(4, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float *)(projection.x * sizeof(float)));
glVertexAttribPointer(5, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float *)(projection.y * sizeof(float)));
glVertexAttribPointer(6, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float *)(projection.z * sizeof(float)));
glVertexAttribPointer(7, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float *)(projection.w * sizeof(float)));
}
glGenBuffers( 1, &p->d_vbo );
glBindVertexArray( p->vao );
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), d+1,
GL_STATIC_DRAW );
glVertexAttribPointer( 0,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) );
glEnableVertexAttribArray(0);
glVertexAttribPointer( 1,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[1]*sizeof(float)) );
glEnableVertexAttribArray(1);
glVertexAttribPointer( 2,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[2]*sizeof(float)) );
glEnableVertexAttribArray(2);
glVertexAttribPointer( 3,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[3]*sizeof(float)) );
glEnableVertexAttribArray(3);
/*
In this function we load all the vertex and ELEMENT_ARRAY datas on two
diferents buffers, so we can access the coordanates that we want
to display using the layout location in GLSL.
This trick can be done with glVertexAttribPointer.
*/
glGenBuffers( 1, &p->n_vbo );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, p->n_vbo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1,
GL_STATIC_DRAW );
glVertexAttribPointer( 4,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) );
glEnableVertexAttribArray(4);
glVertexAttribPointer( 5,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[1]*sizeof(float)) );
glEnableVertexAttribArray(5);
glVertexAttribPointer( 6,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[2]*sizeof(float)) );
glEnableVertexAttribArray(6);
glVertexAttribPointer( 7,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[3]*sizeof(float)) );
glEnableVertexAttribArray(7);
mesh_t create_mesh(float *d, float *n, unsigned char m)
{
unsigned char i;
struct obj *p;
p = malloc(sizeof(struct obj));
p->vertex = (*d) / m;
glGenVertexArrays(1, &p->vao);
glBindVertexArray(p->vao);
glGenBuffers(1, &p->d_vbo);
glBindBuffer(GL_ARRAY_BUFFER, p->d_vbo);
glBufferData(
GL_ARRAY_BUFFER, p->vertex * m * sizeof(float), d + 1, GL_STATIC_DRAW);
if (n)
{
glGenBuffers(1, &p->n_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, p->n_vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, p->vertex * m * sizeof(float),
n + 1, GL_STATIC_DRAW);
}
for (i = 0; i < 4; ++i)
{
glEnableVertexAttribArray(i);
glEnableVertexAttribArray(i + 4);
}
{
struct projection projection = {
.x = 0,
.y = 1,
.z = 2,
.w = 3,
};
projection.m = m;
projection.mesh = p;
set_projection_mesh(projection);
}
return p;
}
void destroy_mesh( mesh_t p )
void destroy_mesh(mesh_t p)
{
struct obj * obj ;
struct obj *obj;
obj = p;
glDeleteVertexArrays( 1, &obj->vao );
glDeleteBuffers( 1, &obj->d_vbo );
glDeleteBuffers( 1, &obj->n_vbo );
free( p );
glDeleteVertexArrays(1, &obj->vao);
glDeleteBuffers(1, &obj->d_vbo);
glDeleteBuffers(1, &obj->n_vbo);
free(p);
}
void draw_mesh( mesh_t p )
void draw_mesh(mesh_t p)
{
struct obj * obj=p;
struct obj *obj = p;
glBindVertexArray( obj->vao );
glBindVertexArray(obj->vao);
#ifdef DEBUG
{
int i;
for( i=0; i<obj->vertex;i+=3 )
glDrawArrays(GL_LINE_LOOP, i, 3 );
for (i = 0; i < obj->vertex; i += 3)
glDrawArrays(GL_LINE_LOOP, i, 3);
}
#else
glDrawArrays(GL_TRIANGLES, 0, obj->vertex );
glDrawArrays(GL_TRIANGLES, 0, obj->vertex);
#endif
}

View File

@@ -1,51 +1,43 @@
#include "main.h"
#ifdef GLAD
#include <glad.h>
#else
#include <GL/glew.h>
#endif
#ifdef DEBUG
#include <stdio.h>
#endif
void destroy_shader( unsigned int shader )
{
return glDeleteProgram( shader );
}
void destroy_shader(unsigned int shader) { return glDeleteProgram(shader); }
unsigned int create_shader( void )
{
return glCreateProgram();
}
unsigned int create_shader(void) { return glCreateProgram(); }
void use_shader( unsigned int program )
{
return glUseProgram( program );
}
void use_shader(unsigned int program) { return glUseProgram(program); }
unsigned char gload_program( unsigned int program, const char * src,
unsigned int i )
unsigned char load_program_to_shader(
unsigned int program, const char *src, unsigned int i)
{
int shader, status;
unsigned int type[] =
{
[VERTEX]=GL_VERTEX_SHADER,
[FRAGMENT]=GL_FRAGMENT_SHADER
};
unsigned int type[] = {
[VERTEX] = GL_VERTEX_SHADER, [FRAGMENT] = GL_FRAGMENT_SHADER};
if( !src )
if (!src)
return 0;
shader = glCreateShader(type[i]);
glShaderSource( shader, 1, (const GLchar **)&src, ((void*)0 ));
glShaderSource(shader, 1, (const GLchar **)&src, ((void *)0));
glCompileShader(shader);
glGetShaderiv( shader, GL_COMPILE_STATUS, &status );
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if( !status )
if (!status)
{
#ifdef DEBUG
char log[256];
glGetShaderInfoLog( shader, 256, NULL, log );
printf( "%s", log );
glGetShaderInfoLog(shader, 256, NULL, log);
printf("%s", log);
#endif
return 0;
}
@@ -56,14 +48,14 @@ unsigned char gload_program( unsigned int program, const char * src,
return 1;
}
void gload_float( unsigned int program, char * var, float f )
void load_float_to_shader(unsigned int program, char *var, float f)
{
glUseProgram( program );
glUniform1f( glGetUniformLocation( program, var ), f );
glUseProgram(program);
glUniform1f(glGetUniformLocation(program, var), f);
}
void gload_mat4( unsigned int program, char * var, float * mat )
void load_mat4_to_shader(unsigned int program, char *var, float *mat)
{
glUseProgram( program );
glUniformMatrix4fv( glGetUniformLocation( program, var ), 1, 0, mat );
glUseProgram(program);
glUniformMatrix4fv(glGetUniformLocation(program, var), 1, 0, mat);
}

View File

@@ -1,78 +1,211 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define CGLM_ALL_UNALIGNED
#include <cglm/vec3.h>
#include <cglm/vec4.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
typedef void(*function_t)(float*,int, int, int);
void mobius(float *d_surface, int i, int j, int grid_size)
{
const float width = 0.5;
float u = (2*M_PI) * ((float)i/grid_size );
float v = (2*width) * ((float)j/grid_size ) - width;
float u = (2 * M_PI) * ((float)i / grid_size);
float v = (2 * width) * ((float)j / grid_size) - width;
d_surface[0] = cos(u) + v * cos(u / 2) * cos(u);
d_surface[1] = sin(u) + v * cos(u / 2) * sin(u);
d_surface[2] = v * sin(u / 2);
}
void toro(float *d_surface, int i, int j, int grid_size)
void torus(float *d_surface, int i, int j, int grid_size)
{
float u = (2*M_PI) * ((float)i/grid_size );
float v = (2*M_PI) * ((float)j/grid_size );
float u = (2 * M_PI) * ((float)i / grid_size);
float v = (2 * M_PI) * ((float)j / grid_size);
d_surface[0] = (1 + 0.5*cos(v))*cos(u);
d_surface[1] = (1 + 0.5*cos(v))*sin(u);
d_surface[2] = 0.5*sin(v);
d_surface[0] = (1 + 0.5 * cos(v)) * cos(u);
d_surface[1] = (1 + 0.5 * cos(v)) * sin(u);
d_surface[2] = 0.5 * sin(v);
}
void klein(float *d_surface, int i, int j, int grid_size)
{
float u = (2*M_PI) * ((float)i/grid_size );
float v = (2*M_PI) * ((float)j/grid_size );
float u = (2 * M_PI) * ((float)i / grid_size);
float v = (2 * M_PI) * ((float)j / grid_size);
d_surface[0] = (0.5*cos(v) + 0.5) * cos(u);
d_surface[1] = (0.5*cos(v) + 0.5) * sin(u);
d_surface[2] = sin(v) * cos(u/2);
d_surface[3] = sin(v)*sin(u/2);
d_surface[0] = (0.5 * cos(v) + 0.5) * cos(u);
d_surface[1] = (0.5 * cos(v) + 0.5) * sin(u);
d_surface[2] = sin(v) * cos(u / 2);
d_surface[3] = sin(v) * sin(u / 2);
}
float * generate_surface(int grid_size)
typedef void (*function_t)(float *, int, int, int);
float *generate_data_surface(int grid_size, unsigned char *m)
{
unsigned char m = 4;
function_t f = klein;
unsigned int i, j, k = 0;
long size;
function_t f;
float *d_surface;
long size = grid_size*grid_size*6*m;
float * d_surface;
int k=0;
d_surface = malloc((size+1)*sizeof(float));
f = klein;
*m = 4;
size = grid_size * grid_size * 6 * (*m);
d_surface = malloc((size + 1) * sizeof(float));
d_surface[0] = size;
for (int i = 0; i < grid_size; i++)
{
for (int j = 0; j < grid_size; j++)
for (i = 0; i < grid_size; i++)
{
for (j = 0; j < grid_size; j++)
{
// triangle 1, back
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
k+=m;
// triangle 1, Front
f(&d_surface[k + 1], i, j, grid_size);
k += *m;
f(&d_surface[k + 1], i + 1, j, grid_size);
k+=m;
f(&d_surface[k + 1], i, j, grid_size);
k+=m;
// triangle 2, font
f(&d_surface[k + 1], i, j, grid_size);
k+=m;
f(&d_surface[k + 1], i, j + 1, grid_size);
k+=m;
k += *m;
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
k+=m;
k += *m;
// triangle 2, Back
f(&d_surface[k + 1], i, j, grid_size);
k += *m;
f(&d_surface[k + 1], i, j + 1, grid_size);
k += *m;
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
k += *m;
}
}
return d_surface;
}
static void __calculate_normal(
float *p1, float *p2, float *p3, float *normal, unsigned char n)
{
float **u;
float alpha;
vec4 v1, v2, v3;
vec4 u1, u2, u3;
switch (n)
{
case 3:
glm_vec3_sub(p2, p1, v1);
glm_vec3_sub(p3, p1, v2);
glm_vec3_cross(v1, v2, normal);
glm_vec3_normalize(normal);
return;
#if 0
/*
In Grant-Shmidth we need 3 linearly independian vector that forms a
basis, so we can have a ortonormal version of that basis, since, we
must have v1 = p3 - p1 v2 = p2 - p1 Then v3 = p1, will most certantly
be linerly independiant to v1 and v2.
*/
case 4:
glm_vec4_sub(p2, p1, v1);
glm_vec4_sub(p3, p1, v2);
glm_vec4_copy(p1, v3);
glm_vec4_copy(v1, u1);
{
vec4 proj;
alpha = glm_vec4_dot(v2, u1) / glm_vec4_dot(u1, u1);
glm_vec4_scale(u1, alpha, proj);
glm_vec4_sub(v2, proj, u2);
}
{
vec4 proj1, proj2;
alpha = glm_vec4_dot(v3, u1) / glm_vec4_dot(u1, u1);
glm_vec4_scale(u1, alpha, proj1);
alpha = glm_vec4_dot(v3, u2) / glm_vec4_dot(u2, u2);
glm_vec4_scale(u2, alpha, proj2);
glm_vec4_sub(v3, proj1, u3);
glm_vec4_sub(u3, proj2, u3);
}
glm_vec4_copy(u3, normal);
glm_vec4_normalize(normal);
return;
#endif
default:
u = malloc((n - 1) * sizeof(float *));
for (unsigned char i = 0; i < n - 1; i++)
{
u[i] = malloc(n * sizeof(float));
}
for (unsigned char i = 0; i < n - 1; i++)
{
float *vi = malloc(n * sizeof(float));
for (unsigned char j = 0; j < n; j++)
{
vi[j] = p2[j] - p1[j];
}
for (unsigned char j = 0; j < i; j++)
{
float dot_vu = 0.0f, dot_uu = 0.0f;
for (unsigned char k = 0; k < n; k++)
{
dot_vu += vi[k] * u[j][k];
dot_uu += u[j][k] * u[j][k];
}
for (unsigned char k = 0; k < n; k++)
{
vi[k] -= (dot_vu / dot_uu) * u[j][k];
}
}
memcpy(u[i], vi, n * sizeof(float));
free(vi);
}
memcpy(normal, u[n - 2], n * sizeof(float));
float norm = 0.0f;
for (unsigned char i = 0; i < n; i++)
{
norm += normal[i] * normal[i];
}
norm = sqrtf(norm);
for (unsigned char i = 0; i < n; i++)
{
normal[i] /= norm;
}
for (unsigned char i = 0; i < n - 1; i++)
{
free(u[i]);
}
free(u);
return;
}
}
float *generate_normals_surface(float *d, unsigned char m)
{
float *n;
n = malloc((*d + 1) * sizeof(float));
*n = *d;
for (int i = 0; i < *d; i += 3 * m)
{
vec4 norm_vec;
__calculate_normal(
(d + 1) + i, (d + 1) + i + m, (d + 1) + i + 2 * m, norm_vec, m);
glm_vec3_copy(norm_vec, (n + 1) + i);
glm_vec3_copy(norm_vec, (n + 1) + i + m);
glm_vec3_copy(norm_vec, (n + 1) + i + 2 * m);
}
return n;
}

View File

@@ -1,37 +1,38 @@
#include "main.h"
#ifdef GLAD
#include <glad.h>
#else
#include <GL/glew.h>
#endif
#define TYPE GL_TEXTURE_2D_ARRAY
id_t config_texture( unsigned short type )
static id_t __config_texture(unsigned short type)
{
id_t texture;
glGenTextures( 1, &texture );
glBindTexture( TYPE, texture );
glGenTextures(1, &texture);
glBindTexture(TYPE, texture);
{
glTexParameteri( TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri(TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
return texture;
}
void use_texture( id_t texture )
void use_texture(id_t texture) { return glBindTexture(TYPE, texture); }
void destroy_texture(unsigned int texture)
{
return glBindTexture( TYPE, texture );
return glDeleteTextures(1, &texture);
}
void destroy_texture( unsigned int texture )
id_t create_palette_texture(const unsigned char colors[][4], unsigned char n)
{
return glDeleteTextures( 1, &texture );
}
id_t create_palette_texture( const unsigned char * colors )
{
id_t texture = config_texture( TYPE );
glTexImage3D( TYPE, 0, GL_RGBA,
1, 1, (*colors)/4, 0, GL_RGBA, GL_UNSIGNED_BYTE, colors+1);
id_t texture = __config_texture(TYPE);
glTexImage3D(
TYPE, 0, GL_RGBA, 1, 1, n, 0, GL_RGBA, GL_UNSIGNED_BYTE, colors);
return texture;
}

View File

@@ -1,55 +1,88 @@
#include "main.h"
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <time.h>
void __window_callback(GLFWwindow *, int, int);
void __mouse_callback(GLFWwindow *, int, int, int);
void __scroll_callback(GLFWwindow *, double, double);
void __key_callback(GLFWwindow *, int, int, int, int);
void __window_callback_input(GLFWwindow *, int, int);
void __mouse_callback_input(GLFWwindow *, int, int, int);
void __scroll_callback_input(GLFWwindow *, double, double);
void __key_callback_input(GLFWwindow *, int, int, int, int);
window_t init_window(unsigned int w, unsigned int h, const char * name)
window_t init_window(unsigned int w, unsigned int h, const char *name);
/*
Limitamos los FPS, contando el tiempo en el que
se ejecuta el main loop, y esperando el tiempo restante
para lograr los fps deseados.
*/
static void __limit_fps_window(int max_fps)
{
void * window;
static double previous_time = 0.0;
double current_time;
double frame_time;
double elapsed_time;
if( !glfwInit() )
current_time = glfwGetTime();
frame_time = 1.0 / max_fps;
elapsed_time = current_time - previous_time;
if (elapsed_time < frame_time)
{
struct timespec sleep_time;
sleep_time.tv_sec = 0;
sleep_time.tv_nsec = (long)((frame_time - elapsed_time) * 1e9);
nanosleep(&sleep_time, NULL);
current_time = glfwGetTime();
}
previous_time = current_time;
}
window_t init_window(unsigned int width, unsigned int height, const char *title)
{
window_t window;
if (!glfwInit())
return NULL;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(w, h, name, NULL, NULL);
if( !window )
window = (window_t)glfwCreateWindow(width, height, title, NULL, NULL);
if (!(window))
{
glfwTerminate();
return NULL;
}
glfwSetWindowSizeCallback(window, __window_callback);
glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback);
glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback);
glfwSetKeyCallback((GLFWwindow*)window, __key_callback);
glfwMakeContextCurrent((GLFWwindow *)(window));
__window_callback( window, w, h );
glfwSetWindowSizeCallback((GLFWwindow *)window, __window_callback_input);
glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input);
glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input);
glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input);
__window_callback_input((GLFWwindow *)window, width, height);
return window;
}
void use_window(window_t window)
{
glfwMakeContextCurrent((void*)window);
}
void use_window(window_t window) { glfwMakeContextCurrent((void *)window); }
int is_open_window(window_t window)
{
glfwSwapBuffers((void*)window);
glfwSwapBuffers((void *)window);
glfwPollEvents();
return !glfwWindowShouldClose((void*)window);
__limit_fps_window(60);
return !glfwWindowShouldClose((void *)window);
}
void close_window(window_t window)
{
glfwDestroyWindow((void*)window);
glfwDestroyWindow((GLFWwindow *)window);
glfwTerminate();
}