Add .clang-format and .gitignore

This commit is contained in:
PedroEdiaz
2024-10-24 11:21:07 -06:00
parent 2a5a101080
commit 3e27f0d165
11 changed files with 315 additions and 318 deletions

19
.clang-format Normal file
View File

@@ -0,0 +1,19 @@
---
PointerAlignment: Right
AllowShortBlocksOnASingleLine: Never
UseTab: Always
IndentWidth: 4
TabWidth: 4
ColumnLimit: 80
AlignAfterOpenBracket: false
AlignOperands: AlignAfterOperator
AlwaysBreakBeforeMultilineStrings: true
BreakBeforeBraces: Allman
BreakStringLiterals: true
IndentCaseLabels: false
IndentGotoLabels: false
InsertBraces: false
...

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
manigraph
**.o
**.so
**.exe
**.dll

View File

@@ -1,19 +1,13 @@
#include "main.h" #include "main.h"
#include <GL/glew.h> #include <GL/glew.h>
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);
} }
int init_context( void ) int init_context(void) { return glewInit() == GLEW_OK; }
{
return glewInit() == GLEW_OK;
}
void clean_context( void ) void clean_context(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); }
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}

View File

@@ -1,9 +1,9 @@
#include "main.h" #include "main.h"
#include <cglm/quat.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <cglm/quat.h>
#include <stdio.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; unsigned char selected_axis = 0;
int window_width; int window_width;
@@ -11,8 +11,7 @@ int window_height;
versor q = GLM_QUAT_IDENTITY_INIT; versor q = GLM_QUAT_IDENTITY_INIT;
vec3 axis[3] = vec3 axis[3] = {
{
{1, 0, 0}, {1, 0, 0},
{0, 1, 0}, {0, 1, 0},
{0, 0, 1}, {0, 0, 1},
@@ -20,61 +19,59 @@ vec3 axis[3] =
extern struct projection projection; extern struct projection projection;
void __key_callback_input(GLFWwindow * window, int key, int scancode, int action, int mods ) void __key_callback_input(
GLFWwindow *window, int key, int scancode, int action, int mods)
{ {
if( action != GLFW_PRESS) if (action != GLFW_PRESS)
return; return;
if (GLFW_KEY_0 < key && key <= GLFW_KEY_9)
if( GLFW_KEY_0<key && key<=GLFW_KEY_9 )
{ {
unsigned char selected_coord; unsigned char selected_coord;
selected_coord = key-GLFW_KEY_0-1; selected_coord = key - GLFW_KEY_0 - 1;
if( selected_coord >= projection.m ) if (selected_coord >= projection.m)
return; return;
if( selected_coord == projection.x ) if (selected_coord == projection.x)
return; return;
if( selected_coord == projection.y ) if (selected_coord == projection.y)
return; return;
if( selected_coord == projection.z ) if (selected_coord == projection.z)
return; return;
selected_coord = projection.w; selected_coord = projection.w;
} }
if( projection.w >= projection.m ) if (projection.w >= projection.m)
return; return;
switch( key ) switch (key)
{ {
unsigned char tmp; unsigned char tmp;
case GLFW_KEY_P: case GLFW_KEY_P:
tmp=projection.w; tmp = projection.w;
projection.w=projection.x; projection.w = projection.x;
projection.x=tmp; projection.x = tmp;
break; break;
case GLFW_KEY_O: case GLFW_KEY_O:
tmp=projection.w; tmp = projection.w;
projection.w=projection.y; projection.w = projection.y;
projection.y=tmp; projection.y = tmp;
break; break;
case GLFW_KEY_I: case GLFW_KEY_I:
tmp=projection.w; tmp = projection.w;
projection.w=projection.z; projection.w = projection.z;
projection.z=tmp; projection.z = tmp;
break; break;
} }
set_projection_mesh( projection ); set_projection_mesh(projection);
return; return;
} }
void __window_callback_input(GLFWwindow * window, int w, int h) void __window_callback_input(GLFWwindow *window, int w, int h)
{ {
int m; int m;
@@ -85,32 +82,33 @@ void __window_callback_input(GLFWwindow * window, int w, int h)
glViewport((w - m) / 2, (h - m) / 2, m, m); glViewport((w - m) / 2, (h - m) / 2, m, m);
} }
void __mouse_callback_input(GLFWwindow* window, int button, int action, int mods) void __mouse_callback_input(
GLFWwindow *window, int button, int action, int mods)
{ {
unsigned char green_value; unsigned char green_value;
double xpos, ypos; double xpos, ypos;
if( button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS ) if (button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS)
return; return;
glfwGetCursorPos(window, &xpos, &ypos); 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); GL_UNSIGNED_BYTE, &green_value);
switch(green_value) switch (green_value)
{ {
case 0xD3: case 0xD3:
case 0xD4: case 0xD4:
case 0xD5: case 0xD5:
selected_axis = green_value-0xD3; selected_axis = green_value - 0xD3;
} }
} }
void __scroll_callback_input(GLFWwindow* window, double xoffset, double yoffset) void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset)
{ {
versor p = GLM_QUAT_IDENTITY_INIT; 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_mul(p, q, q);
glm_quat_rotatev(p, axis[0], axis[0]); glm_quat_rotatev(p, axis[0], axis[0]);
@@ -122,43 +120,42 @@ quat_t poll_input(window_t window)
{ {
versor p = GLM_QUAT_IDENTITY_INIT; 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]); glm_quatv(p, ANGLE, axis[0]);
goto end; goto end;
} }
if( glfwGetKey((GLFWwindow*)window, 'W') == GLFW_PRESS ) if (glfwGetKey((GLFWwindow *)window, 'W') == GLFW_PRESS)
{ {
glm_quatv(p, -ANGLE, axis[0]); glm_quatv(p, -ANGLE, axis[0]);
goto end; goto end;
} }
if( glfwGetKey((GLFWwindow*)window, 'A') == GLFW_PRESS ) if (glfwGetKey((GLFWwindow *)window, 'A') == GLFW_PRESS)
{ {
glm_quatv(p, ANGLE, axis[1]); glm_quatv(p, ANGLE, axis[1]);
goto end; goto end;
} }
if( glfwGetKey((GLFWwindow*)window, 'S') == GLFW_PRESS ) if (glfwGetKey((GLFWwindow *)window, 'S') == GLFW_PRESS)
{ {
glm_quatv(p, -ANGLE, axis[1]); glm_quatv(p, -ANGLE, axis[1]);
goto end; goto end;
} }
if( glfwGetKey((GLFWwindow*)window, 'Z') == GLFW_PRESS ) if (glfwGetKey((GLFWwindow *)window, 'Z') == GLFW_PRESS)
{ {
glm_quatv(p, ANGLE, axis[2]); glm_quatv(p, ANGLE, axis[2]);
goto end; goto end;
} }
if( glfwGetKey((GLFWwindow*)window, 'X') == GLFW_PRESS ) if (glfwGetKey((GLFWwindow *)window, 'X') == GLFW_PRESS)
{ {
glm_quatv(p, -ANGLE, axis[2]); glm_quatv(p, -ANGLE, axis[2]);
goto end; goto end;
} }
end: end:
glm_quat_mul( p, q, q ); glm_quat_mul(p, q, q);
glm_quat_rotatev( p, axis[0], axis[0] ); glm_quat_rotatev(p, axis[0], axis[0]);
glm_quat_rotatev( p, axis[1], axis[1] ); glm_quat_rotatev(p, axis[1], axis[1]);
glm_quat_rotatev( p, axis[2], axis[2] ); glm_quat_rotatev(p, axis[2], axis[2]);
glm_quat_normalize( q ); glm_quat_normalize(q);
return q; return q;
} }

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

@@ -2,41 +2,51 @@
#include "data/axis.h" #include "data/axis.h"
#include "data/shaders.h" #include "data/shaders.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#define WIDTH 512 #define WIDTH 512
#define HEIGHT 512 #define HEIGHT 512
float * generate_data_surface(unsigned int, unsigned char *); float *generate_data_surface(unsigned int, unsigned char *);
float * generate_normals_surface(float *, unsigned char); float *generate_normals_surface(float *, unsigned char);
unsigned char palette[] = unsigned char palette[] = {
{ 0xEB,
0xEB,0xD3,0xF8,0xff, 0xD3,
0xEB,0xD4,0xF8,0xff, 0xF8,
0xEB,0xD5,0xF8,0xff, 0xff,
0x7A,0x1C,0xAC,0xff, 0xEB,
0xD4,
0xF8,
0xff,
0xEB,
0xD5,
0xF8,
0xff,
0x7A,
0x1C,
0xAC,
0xff,
}; };
const char * wname = "manigraph: manifold grapher"; const char *wname = "manigraph: manifold grapher";
struct projection projection = struct projection projection = {
{ .x = 0,
.x=0, .y = 1,
.y=1, .z = 2,
.z=2, .w = 3,
.w=3,
}; };
void mlog( char * msg ) void mlog(char *msg)
{ {
#ifdef DEBUG #ifdef DEBUG
printf(msg); printf(msg);
#endif #endif
} }
int main( void ) int main(void)
{ {
id_t shader, texture, shader_plain; id_t shader, texture, shader_plain;
mesh_t m_surface, m_axis; mesh_t m_surface, m_axis;
@@ -44,7 +54,7 @@ int main( void )
mlog("[VENTANA] Inicializando...\n"); mlog("[VENTANA] Inicializando...\n");
{ {
if(!(window = init_window(WIDTH, HEIGHT, wname))) if (!(window = init_window(WIDTH, HEIGHT, wname)))
{ {
mlog("[VENTANA] Error al inicializar...\n"); mlog("[VENTANA] Error al inicializar...\n");
goto error_window; goto error_window;
@@ -56,7 +66,7 @@ int main( void )
mlog("[CONTEXT] Inicializando...\n"); mlog("[CONTEXT] Inicializando...\n");
{ {
if(!(init_context())) if (!(init_context()))
{ {
mlog("[CONTEXT] Error al inicializar...\n"); mlog("[CONTEXT] Error al inicializar...\n");
goto error_context; goto error_context;
@@ -65,59 +75,58 @@ int main( void )
mlog("[TEXTURE] Inicializando...\n"); mlog("[TEXTURE] Inicializando...\n");
{ {
texture=create_palette_texture(palette, 4); texture = create_palette_texture(palette, 4);
use_texture(texture); use_texture(texture);
} }
mlog("[SHADER] Inicializando...\n"); mlog("[SHADER] Inicializando...\n");
{ {
if(!(shader = create_shader())) if (!(shader = create_shader()))
{ {
mlog("[SHADER] Error al inicializar...\n"); mlog("[SHADER] Error al inicializar...\n");
goto error_shader; goto error_shader;
} }
load_program_to_shader(shader, vs, VERTEX); load_program_to_shader(shader, vs, VERTEX);
load_program_to_shader(shader, fs, FRAGMENT); load_program_to_shader(shader, fs, FRAGMENT);
load_fix_matrix(shader, (float)WIDTH/HEIGHT); load_fix_matrix(shader, (float)WIDTH / HEIGHT);
} }
mlog("[SHADER] Inicializando...\n"); mlog("[SHADER] Inicializando...\n");
{ {
if(!(shader_plain = create_shader())) if (!(shader_plain = create_shader()))
{ {
mlog("[SHADER] Error al inicializar...\n"); mlog("[SHADER] Error al inicializar...\n");
goto error_shader_plain; goto error_shader_plain;
} }
load_program_to_shader(shader_plain, vs, VERTEX); load_program_to_shader(shader_plain, vs, VERTEX);
load_program_to_shader(shader_plain, fs_plain, FRAGMENT); load_program_to_shader(shader_plain, fs_plain, FRAGMENT);
load_fix_matrix(shader_plain, (float)WIDTH/HEIGHT); load_fix_matrix(shader_plain, (float)WIDTH / HEIGHT);
} }
mlog("[MESH] Inicializando...\n"); mlog("[MESH] Inicializando...\n");
{ {
unsigned char m; unsigned char m;
float * n_surface, *d_surface; float *n_surface, *d_surface;
d_surface = generate_data_surface(16,&m); d_surface = generate_data_surface(16, &m);
n_surface = generate_normals_surface(d_surface, m); n_surface = generate_normals_surface(d_surface, m);
projection.m=m; projection.m = m;
if( !(m_surface = create_mesh(d_surface, n_surface, m))) if (!(m_surface = create_mesh(d_surface, n_surface, m)))
{ {
mlog("[MESH] Error al inicializar...\n"); mlog("[MESH] Error al inicializar...\n");
goto error_mesh_surface; goto error_mesh_surface;
} }
projection.mesh=m_surface; projection.mesh = m_surface;
free(n_surface); free(n_surface);
free(d_surface); free(d_surface);
} }
mlog("[MESH] Inicializando...\n"); mlog("[MESH] Inicializando...\n");
{ {
if(!(m_axis = create_mesh(d_axis, NULL, 3))) if (!(m_axis = create_mesh(d_axis, NULL, 3)))
{ {
mlog("[MESH] Error al inicializar...\n"); mlog("[MESH] Error al inicializar...\n");
goto error_mesh_axis; goto error_mesh_axis;
@@ -125,11 +134,11 @@ int main( void )
} }
mlog("[MAIN LOOP] Inicializando...\n"); mlog("[MAIN LOOP] Inicializando...\n");
while(is_open_window(window)) while (is_open_window(window))
{ {
quat_t q; quat_t q;
q=poll_input(window); q = poll_input(window);
load_rot_matrix(shader, q); load_rot_matrix(shader, q);
load_rot_matrix(shader_plain, q); load_rot_matrix(shader_plain, q);
@@ -137,7 +146,7 @@ int main( void )
#ifndef DEBUG #ifndef DEBUG
load_mdl_matrix(shader_plain, 0, 0); load_mdl_matrix(shader_plain, 0, 0);
draw_mesh( m_axis); draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 1, 1); load_mdl_matrix(shader_plain, 1, 1);
draw_mesh(m_axis); draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 2, 2); load_mdl_matrix(shader_plain, 2, 2);
@@ -164,7 +173,6 @@ int main( void )
close_window(window); close_window(window);
return 0; return 0;
mlog("[MESH] Destruyendo...\n"); mlog("[MESH] Destruyendo...\n");
destroy_mesh(m_axis); destroy_mesh(m_axis);
error_mesh_axis: error_mesh_axis:

View File

@@ -1,51 +1,50 @@
#include "main.h" #include "main.h"
#include <cglm/mat4.h>
#include <cglm/cam.h> #include <cglm/cam.h>
#include <cglm/mat4.h>
#include <cglm/quat.h> #include <cglm/quat.h>
mat4 ortho[] = mat4 ortho[] = {
{
{ {
{ 1, 0, 0, 0 }, {1, 0, 0, 0},
{ 0, 1, 0, 0 }, {0, 1, 0, 0},
{ 0, 0, 1, 0 }, {0, 0, 1, 0},
{ 0, 0, 0, 1 }, {0, 0, 0, 1},
}, },
{ {
{ 0, 1, 0, 0 }, {0, 1, 0, 0},
{-1, 0, 0, 0 }, {-1, 0, 0, 0},
{ 0, 0, 1, 0 }, {0, 0, 1, 0},
{ 0, 0, 0, 1 }, {0, 0, 0, 1},
}, },
{ {
{ 0, 0, 1, 0 }, {0, 0, 1, 0},
{ 0, 1, 0, 0 }, {0, 1, 0, 0},
{-1, 0, 0, 0 }, {-1, 0, 0, 0},
{ 0, 0, 0, 1 }, {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; mat4 m, n;
const int d = 7; const int d = 7;
glm_lookat( (vec3){0,0,-d}, (vec3){0,0,0}, (vec3){0,1,0}, 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_perspective(CGLM_PI / 4, ratio, d - 3, d + 3, n);
glm_mat4_mul( n, m, m ); glm_mat4_mul(n, m, m);
load_mat4_to_shader( 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)
{ {
load_float_to_shader( shader, "idx", c ); load_float_to_shader(shader, "idx", c);
load_mat4_to_shader( shader, "mdl", (mat4_t)ortho[i] ); 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; mat4 m;
glm_quat_mat4( q, m ); glm_quat_mat4(q, m);
load_mat4_to_shader( shader, "rot", (mat4_t)m ); load_mat4_to_shader(shader, "rot", (mat4_t)m);
} }

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

@@ -1,39 +1,39 @@
#include "main.h" #include "main.h"
#include <GL/glew.h> #include <GL/glew.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
struct obj struct obj
{ {
unsigned int vertex, vao, n_vbo, d_vbo; unsigned int vertex, vao, n_vbo, d_vbo;
}; };
void set_projection_mesh( struct projection projection ) void set_projection_mesh(struct projection projection)
{ {
struct obj * p; struct obj *p;
p=projection.mesh; p = projection.mesh;
glBindVertexArray( p->vao ); glBindVertexArray(p->vao);
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo ); glBindBuffer(GL_ARRAY_BUFFER, p->d_vbo);
glVertexAttribPointer( 0,1,GL_FLOAT, 0, projection.m*sizeof(float), glVertexAttribPointer(0, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float*)(projection.x*sizeof(float)) ); (float *)(projection.x * sizeof(float)));
glVertexAttribPointer( 1,1,GL_FLOAT, 0, projection.m*sizeof(float), glVertexAttribPointer(1, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float*)(projection.y*sizeof(float)) ); (float *)(projection.y * sizeof(float)));
glVertexAttribPointer( 2,1,GL_FLOAT, 0, projection.m*sizeof(float), glVertexAttribPointer(2, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float*)(projection.z*sizeof(float)) ); (float *)(projection.z * sizeof(float)));
glVertexAttribPointer( 3,1,GL_FLOAT, 0, projection.m*sizeof(float), glVertexAttribPointer(3, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float*)(projection.w*sizeof(float)) ); (float *)(projection.w * sizeof(float)));
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo ); glBindBuffer(GL_ARRAY_BUFFER, p->n_vbo);
glVertexAttribPointer( 4,1,GL_FLOAT, 0, projection.m*sizeof(float), glVertexAttribPointer(4, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float*)(projection.x*sizeof(float)) ); (float *)(projection.x * sizeof(float)));
glVertexAttribPointer( 5,1,GL_FLOAT, 0, projection.m*sizeof(float), glVertexAttribPointer(5, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float*)(projection.y*sizeof(float)) ); (float *)(projection.y * sizeof(float)));
glVertexAttribPointer( 6,1,GL_FLOAT, 0, projection.m*sizeof(float), glVertexAttribPointer(6, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float*)(projection.z*sizeof(float)) ); (float *)(projection.z * sizeof(float)));
glVertexAttribPointer( 7,1,GL_FLOAT, 0, projection.m*sizeof(float), glVertexAttribPointer(7, 1, GL_FLOAT, 0, projection.m * sizeof(float),
(float*)(projection.w*sizeof(float)) ); (float *)(projection.w * sizeof(float)));
} }
/* /*
@@ -43,76 +43,75 @@ void set_projection_mesh( struct projection projection )
This trick can be done with glVertexAttribPointer. This trick can be done with glVertexAttribPointer.
*/ */
mesh_t create_mesh( float * d, float * n, unsigned char m ) mesh_t create_mesh(float *d, float *n, unsigned char m)
{ {
unsigned char i; unsigned char i;
struct obj * p; struct obj *p;
p=malloc(sizeof(struct obj)); p = malloc(sizeof(struct obj));
p->vertex=(*d)/m; p->vertex = (*d) / m;
glGenVertexArrays( 1, &p->vao ); glGenVertexArrays(1, &p->vao);
glBindVertexArray( p->vao ); glBindVertexArray(p->vao);
glGenBuffers( 1, &p->d_vbo ); glGenBuffers(1, &p->d_vbo);
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo ); glBindBuffer(GL_ARRAY_BUFFER, p->d_vbo);
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), d+1, glBufferData(
GL_STATIC_DRAW ); GL_ARRAY_BUFFER, p->vertex * m * sizeof(float), d + 1, GL_STATIC_DRAW);
if( n ) if (n)
{ {
glGenBuffers( 1, &p->n_vbo ); glGenBuffers(1, &p->n_vbo);
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo ); glBindBuffer(GL_ARRAY_BUFFER, p->n_vbo);
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1, glBufferData(GL_ARRAY_BUFFER, p->vertex * m * sizeof(float), n + 1,
GL_STATIC_DRAW ); GL_STATIC_DRAW);
} }
for( i=0; i<4; ++i ) for (i = 0; i < 4; ++i)
{ {
glEnableVertexAttribArray(i); glEnableVertexAttribArray(i);
glEnableVertexAttribArray(i+4); glEnableVertexAttribArray(i + 4);
} }
{ {
struct projection projection = struct projection projection = {
{ .x = 0,
.x=0, .y = 1,
.y=1, .z = 2,
.z=2, .w = 3,
.w=3,
}; };
projection.m=m; projection.m = m;
projection.mesh=p; projection.mesh = p;
set_projection_mesh( projection ); set_projection_mesh(projection);
} }
return p; return p;
} }
void destroy_mesh( mesh_t p ) void destroy_mesh(mesh_t p)
{ {
struct obj * obj ; struct obj *obj;
obj = p; obj = p;
glDeleteVertexArrays( 1, &obj->vao ); glDeleteVertexArrays(1, &obj->vao);
glDeleteBuffers( 1, &obj->d_vbo ); glDeleteBuffers(1, &obj->d_vbo);
glDeleteBuffers( 1, &obj->n_vbo ); glDeleteBuffers(1, &obj->n_vbo);
free( p ); 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 #ifdef DEBUG
{ {
int i; int i;
for( i=0; i<obj->vertex;i+=3 ) for (i = 0; i < obj->vertex; i += 3)
glDrawArrays(GL_LINE_LOOP, i, 3 ); glDrawArrays(GL_LINE_LOOP, i, 3);
} }
#else #else
glDrawArrays(GL_TRIANGLES, 0, obj->vertex ); glDrawArrays(GL_TRIANGLES, 0, obj->vertex);
#endif #endif
} }

View File

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

View File

@@ -9,12 +9,11 @@
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
#endif #endif
void mobius(float *d_surface, int i, int j, int grid_size) void mobius(float *d_surface, int i, int j, int grid_size)
{ {
const float width = 0.5; const float width = 0.5;
float u = (2*M_PI) * ((float)i/grid_size ); float u = (2 * M_PI) * ((float)i / grid_size);
float v = (2*width) * ((float)j/grid_size ) - width; float v = (2 * width) * ((float)j / grid_size) - width;
d_surface[0] = cos(u) + v * cos(u / 2) * cos(u); d_surface[0] = cos(u) + v * cos(u / 2) * cos(u);
d_surface[1] = sin(u) + v * cos(u / 2) * sin(u); d_surface[1] = sin(u) + v * cos(u / 2) * sin(u);
@@ -23,68 +22,67 @@ void mobius(float *d_surface, int i, int j, int grid_size)
void torus(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 u = (2 * M_PI) * ((float)i / grid_size);
float v = (2*M_PI) * ((float)j/grid_size ); float v = (2 * M_PI) * ((float)j / grid_size);
d_surface[0] = (1 + 0.5*cos(v))*cos(u); d_surface[0] = (1 + 0.5 * cos(v)) * cos(u);
d_surface[1] = (1 + 0.5*cos(v))*sin(u); d_surface[1] = (1 + 0.5 * cos(v)) * sin(u);
d_surface[2] = 0.5*sin(v); d_surface[2] = 0.5 * sin(v);
} }
void klein(float *d_surface, int i, int j, int grid_size) void klein(float *d_surface, int i, int j, int grid_size)
{ {
float u = (2*M_PI) * ((float)i/grid_size ); float u = (2 * M_PI) * ((float)i / grid_size);
float v = (2*M_PI) * ((float)j/grid_size ); float v = (2 * M_PI) * ((float)j / grid_size);
d_surface[0] = (0.5*cos(v) + 0.5) * cos(u); d_surface[0] = (0.5 * cos(v) + 0.5) * cos(u);
d_surface[1] = (0.5*cos(v) + 0.5) * sin(u); d_surface[1] = (0.5 * cos(v) + 0.5) * sin(u);
d_surface[2] = sin(v) * cos(u/2); d_surface[2] = sin(v) * cos(u / 2);
d_surface[3] = sin(v)*sin(u/2); d_surface[3] = sin(v) * sin(u / 2);
} }
typedef void(*function_t)(float*,int, int, int); typedef void (*function_t)(float *, int, int, int);
float * generate_data_surface(int grid_size, unsigned char *m ) float *generate_data_surface(int grid_size, unsigned char *m)
{ {
unsigned int i,j,k=0; unsigned int i, j, k = 0;
long size; long size;
function_t f; function_t f;
float * d_surface; float *d_surface;
f = klein; f = klein;
*m = 4; *m = 4;
size = grid_size*grid_size*6*(*m); size = grid_size * grid_size * 6 * (*m);
d_surface = malloc((size+1)*sizeof(float)); d_surface = malloc((size + 1) * sizeof(float));
d_surface[0] = size; d_surface[0] = size;
for (i = 0; i < grid_size; i++)
for( i = 0; i < grid_size; i++)
{ {
for( j = 0; j < grid_size; j++) for (j = 0; j < grid_size; j++)
{ {
// triangle 1, Front // triangle 1, Front
f(&d_surface[k + 1], i, j, grid_size); f(&d_surface[k + 1], i, j, grid_size);
k+=*m; k += *m;
f(&d_surface[k + 1], i + 1, j, grid_size); f(&d_surface[k + 1], i + 1, j, grid_size);
k+=*m; k += *m;
f(&d_surface[k + 1], i + 1, j + 1, grid_size); f(&d_surface[k + 1], i + 1, j + 1, grid_size);
k+=*m; k += *m;
// triangle 2, Back // triangle 2, Back
f(&d_surface[k + 1], i, j, grid_size); f(&d_surface[k + 1], i, j, grid_size);
k+=*m; k += *m;
f(&d_surface[k + 1], i, j + 1, grid_size); 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); f(&d_surface[k + 1], i + 1, j + 1, grid_size);
k+=*m; k += *m;
} }
} }
return d_surface; return d_surface;
} }
static static void __calculate_normal(
void __calculate_normal(float* p1, float* p2, float* p3,float* normal, unsigned char n) float *p1, float *p2, float *p3, float *normal, unsigned char n)
{ {
float alpha; float alpha;
vec4 v1, v2, v3; vec4 v1, v2, v3;
@@ -104,11 +102,10 @@ void __calculate_normal(float* p1, float* p2, float* p3,float* normal, unsigned
case 4: case 4:
/* /*
In Grant-Shmidth we need 3 linearly independian vector that forms a basis, In Grant-Shmidth we need 3 linearly independian vector that forms a
so we can have a ortonormal version of that basis, since, we must have basis, so we can have a ortonormal version of that basis, since, we
v1 = p3 - p1 must have v1 = p3 - p1 v2 = p2 - p1 Then v3 = p1, will most certantly
v2 = p2 - p1 be linerly independiant to v1 and v2.
Then v3 = p1, will most certantly be linerly independiant to v1 and v2.
*/ */
glm_vec4_sub(p2, p1, v1); glm_vec4_sub(p2, p1, v1);
glm_vec4_sub(p3, p1, v2); glm_vec4_sub(p3, p1, v2);
@@ -149,21 +146,21 @@ void __calculate_normal(float* p1, float* p2, float* p3,float* normal, unsigned
} }
} }
float * generate_normals_surface( float * d, unsigned char m ) float *generate_normals_surface(float *d, unsigned char m)
{ {
float * n; float *n;
n = malloc( (*d+1)*sizeof(float)); n = malloc((*d + 1) * sizeof(float));
*n = *d; *n = *d;
for (int i = 0; i < *d; i += 3*m) for (int i = 0; i < *d; i += 3 * m)
{ {
vec4 norm_vec; vec4 norm_vec;
__calculate_normal((d+1)+i, (d+1)+i+m, (d+1)+i+2*m, norm_vec, m); __calculate_normal(
glm_vec3_copy( norm_vec, (n+1)+i ); (d + 1) + i, (d + 1) + i + m, (d + 1) + i + 2 * m, norm_vec, m);
glm_vec3_copy( norm_vec, (n+1)+i+m ); glm_vec3_copy(norm_vec, (n + 1) + i);
glm_vec3_copy( norm_vec, (n+1)+i+2*m ); glm_vec3_copy(norm_vec, (n + 1) + i + m);
glm_vec3_copy(norm_vec, (n + 1) + i + 2 * m);
} }
return n; return n;
} }

View File

@@ -3,36 +3,32 @@
#define TYPE GL_TEXTURE_2D_ARRAY #define TYPE GL_TEXTURE_2D_ARRAY
static static id_t __config_texture(unsigned short type)
id_t __config_texture( unsigned short type )
{ {
id_t texture; id_t texture;
glGenTextures( 1, &texture ); glGenTextures(1, &texture);
glBindTexture( TYPE, texture ); glBindTexture(TYPE, texture);
{ {
glTexParameteri( TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri(TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri( TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexParameteri(TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} }
return texture; 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, unsigned char n)
{ {
return glDeleteTextures( 1, &texture ); id_t texture = __config_texture(TYPE);
} glTexImage3D(
TYPE, 0, GL_RGBA, 1, 1, n, 0, GL_RGBA, GL_UNSIGNED_BYTE, colors);
id_t create_palette_texture( const unsigned char * colors, unsigned char n )
{
id_t texture = __config_texture( TYPE );
glTexImage3D( TYPE, 0, GL_RGBA,
1, 1, n, 0, GL_RGBA, GL_UNSIGNED_BYTE, colors);
return texture; return texture;
} }

View File

@@ -8,7 +8,7 @@ void __mouse_callback_input(GLFWwindow *, int, int, int);
void __scroll_callback_input(GLFWwindow *, double, double); void __scroll_callback_input(GLFWwindow *, double, double);
void __key_callback_input(GLFWwindow *, int, int, int, int); 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 Limitamos los FPS, contando el tiempo en el que
@@ -16,8 +16,7 @@ window_t init_window(unsigned int w, unsigned int h, const char * name);
para lograr los fps deseados. para lograr los fps deseados.
*/ */
static static void __limit_fps_window(int max_fps)
void __limit_fps_window(int max_fps)
{ {
static double previous_time = 0.0; static double previous_time = 0.0;
double current_time; double current_time;
@@ -45,7 +44,7 @@ window_t init_window(unsigned int width, unsigned int height, const char *title)
{ {
window_t window; window_t window;
if( !glfwInit() ) if (!glfwInit())
return NULL; return NULL;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@@ -59,30 +58,27 @@ window_t init_window(unsigned int width, unsigned int height, const char *title)
return NULL; return NULL;
} }
glfwMakeContextCurrent((GLFWwindow*)(window)); glfwMakeContextCurrent((GLFWwindow *)(window));
glfwSetWindowSizeCallback((GLFWwindow*)window, __window_callback_input); glfwSetWindowSizeCallback((GLFWwindow *)window, __window_callback_input);
glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback_input); glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input);
glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback_input); glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input);
glfwSetKeyCallback((GLFWwindow*)window, __key_callback_input); glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input);
__window_callback_input((GLFWwindow*)window, width, height ); __window_callback_input((GLFWwindow *)window, width, height);
return window; return window;
} }
void use_window(window_t window) void use_window(window_t window) { glfwMakeContextCurrent((void *)window); }
{
glfwMakeContextCurrent((void*)window);
}
int is_open_window(window_t window) int is_open_window(window_t window)
{ {
glfwSwapBuffers((void*)window); glfwSwapBuffers((void *)window);
glfwPollEvents(); glfwPollEvents();
__limit_fps_window(60); __limit_fps_window(60);
return !glfwWindowShouldClose((void*)window); return !glfwWindowShouldClose((void *)window);
} }
void close_window(window_t window) void close_window(window_t window)