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 <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 )
{
return glewInit() == GLEW_OK;
}
int init_context(void) { return glewInit() == GLEW_OK; }
void clean_context( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}
void clean_context(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); }

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,8 +11,7 @@ int window_height;
versor q = GLM_QUAT_IDENTITY_INIT;
vec3 axis[3] =
{
vec3 axis[3] = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
@@ -20,61 +19,59 @@ vec3 axis[3] =
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;
if( GLFW_KEY_0<key && key<=GLFW_KEY_9 )
if (GLFW_KEY_0 < key && key <= GLFW_KEY_9)
{
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;
if( selected_coord == projection.x )
if (selected_coord == projection.x)
return;
if( selected_coord == projection.y )
if (selected_coord == projection.y)
return;
if( selected_coord == projection.z )
if (selected_coord == projection.z)
return;
selected_coord = projection.w;
}
if( projection.w >= projection.m )
if (projection.w >= projection.m)
return;
switch( key )
switch (key)
{
unsigned char tmp;
unsigned char tmp;
case GLFW_KEY_P:
tmp=projection.w;
projection.w=projection.x;
projection.x=tmp;
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;
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;
tmp = projection.w;
projection.w = projection.z;
projection.z = tmp;
break;
}
set_projection_mesh( projection );
set_projection_mesh(projection);
return;
}
void __window_callback_input(GLFWwindow * window, int w, int h)
void __window_callback_input(GLFWwindow *window, int w, int h)
{
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);
}
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;
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_input(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]);
@@ -122,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;
}

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

@@ -2,41 +2,51 @@
#include "data/axis.h"
#include "data/shaders.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 512
#define HEIGHT 512
float * generate_data_surface(unsigned int, unsigned char *);
float * generate_normals_surface(float *, unsigned char);
float *generate_data_surface(unsigned int, unsigned char *);
float *generate_normals_surface(float *, unsigned char);
unsigned char palette[] =
{
0xEB,0xD3,0xF8,0xff,
0xEB,0xD4,0xF8,0xff,
0xEB,0xD5,0xF8,0xff,
0x7A,0x1C,0xAC,0xff,
unsigned char palette[] = {
0xEB,
0xD3,
0xF8,
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 =
{
.x=0,
.y=1,
.z=2,
.w=3,
struct projection projection = {
.x = 0,
.y = 1,
.z = 2,
.w = 3,
};
void mlog( char * msg )
void mlog(char *msg)
{
#ifdef DEBUG
printf(msg);
#endif
}
int main( void )
int main(void)
{
id_t shader, texture, shader_plain;
mesh_t m_surface, m_axis;
@@ -44,7 +54,7 @@ int main( void )
mlog("[VENTANA] Inicializando...\n");
{
if(!(window = init_window(WIDTH, HEIGHT, wname)))
if (!(window = init_window(WIDTH, HEIGHT, wname)))
{
mlog("[VENTANA] Error al inicializar...\n");
goto error_window;
@@ -56,88 +66,87 @@ int main( void )
mlog("[CONTEXT] Inicializando...\n");
{
if(!(init_context()))
if (!(init_context()))
{
mlog("[CONTEXT] Error al inicializar...\n");
goto error_context;
goto error_context;
}
}
mlog("[TEXTURE] Inicializando...\n");
{
texture=create_palette_texture(palette, 4);
texture = create_palette_texture(palette, 4);
use_texture(texture);
}
mlog("[SHADER] Inicializando...\n");
{
if(!(shader = create_shader()))
{
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);
load_fix_matrix(shader, (float)WIDTH / HEIGHT);
}
mlog("[SHADER] Inicializando...\n");
{
if(!(shader_plain = create_shader()))
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);
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);
float *n_surface, *d_surface;
d_surface = generate_data_surface(16, &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");
goto error_mesh_surface;
goto error_mesh_surface;
}
projection.mesh=m_surface;
projection.mesh = m_surface;
free(n_surface);
free(d_surface);
}
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");
goto error_mesh_axis;
goto error_mesh_axis;
}
}
mlog("[MAIN LOOP] Inicializando...\n");
while(is_open_window(window))
while (is_open_window(window))
{
quat_t q;
q=poll_input(window);
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);
draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 1, 1);
draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 2, 2);
@@ -164,7 +173,6 @@ int main( void )
close_window(window);
return 0;
mlog("[MESH] Destruyendo...\n");
destroy_mesh(m_axis);
error_mesh_axis:

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);
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_mat4_to_shader( 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 );
load_mat4_to_shader( shader, "rot", (mat4_t)m );
glm_quat_mat4(q, m);
load_mat4_to_shader(shader, "rot", (mat4_t)m);
}

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

@@ -1,118 +1,117 @@
#include "main.h"
#include <GL/glew.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
struct obj
struct obj
{
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;
p=projection.mesh;
struct obj *p;
p = projection.mesh;
glBindVertexArray( p->vao );
glBindVertexArray(p->vao);
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)) );
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)));
glBindBuffer( GL_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)) );
glBindBuffer(GL_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)));
}
/*
/*
In this function we load all the vertex and normal datas on two
diferents buffers, so we can access the coordanates that we want
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.
*/
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;
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 );
glBindVertexArray( p->vao );
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 );
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 )
if (n)
{
glGenBuffers( 1, &p->n_vbo );
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1,
GL_STATIC_DRAW );
glGenBuffers(1, &p->n_vbo);
glBindBuffer(GL_ARRAY_BUFFER, p->n_vbo);
glBufferData(GL_ARRAY_BUFFER, p->vertex * m * sizeof(float), n + 1,
GL_STATIC_DRAW);
}
for( i=0; i<4; ++i )
for (i = 0; i < 4; ++i)
{
glEnableVertexAttribArray(i);
glEnableVertexAttribArray(i+4);
glEnableVertexAttribArray(i + 4);
}
{
struct projection projection =
{
.x=0,
.y=1,
.z=2,
.w=3,
struct projection projection = {
.x = 0,
.y = 1,
.z = 2,
.w = 3,
};
projection.m=m;
projection.mesh=p;
set_projection_mesh( projection );
}
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

@@ -5,47 +5,34 @@
#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 load_program_to_shader( 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 +43,14 @@ unsigned char load_program_to_shader( unsigned int program, const char * src,
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 );
glUniform1f( glGetUniformLocation( program, var ), f );
glUseProgram(program);
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 );
glUniformMatrix4fv( glGetUniformLocation( program, var ), 1, 0, mat );
glUseProgram(program);
glUniformMatrix4fv(glGetUniformLocation(program, var), 1, 0, mat);
}

View File

@@ -9,12 +9,11 @@
#define M_PI 3.14159265358979323846
#endif
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);
@@ -23,97 +22,95 @@ void mobius(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);
}
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;
function_t f;
float * d_surface;
float *d_surface;
f = klein;
*m = 4;
size = grid_size*grid_size*6*(*m);
d_surface = malloc((size+1)*sizeof(float));
size = grid_size * grid_size * 6 * (*m);
d_surface = malloc((size + 1) * sizeof(float));
d_surface[0] = size;
for( i = 0; i < grid_size; i++)
{
for( j = 0; j < grid_size; j++)
for (i = 0; i < grid_size; i++)
{
for (j = 0; j < grid_size; j++)
{
// triangle 1, Front
f(&d_surface[k + 1], i, j, grid_size);
k+=*m;
k += *m;
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);
k+=*m;
k += *m;
// triangle 2, Back
f(&d_surface[k + 1], i, j, grid_size);
k+=*m;
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;
}
}
return d_surface;
}
static
void __calculate_normal(float* p1, float* p2, float* p3,float* normal, unsigned char n)
static void __calculate_normal(
float *p1, float *p2, float *p3, float *normal, unsigned char n)
{
float alpha;
vec4 v1, v2, v3;
vec4 u1, u2, u3;
vec4 u1, u2, u3;
switch (n)
{
case 3:
glm_vec3_sub(p2, p1, v1);
glm_vec3_sub(p3, p1, v2);
glm_vec3_sub(p2, p1, v1);
glm_vec3_sub(p3, p1, v2);
glm_vec3_cross(v1, v2, normal);
glm_vec3_cross(v1, v2, normal);
glm_vec3_normalize(normal);
return;
case 4:
/*
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.
/*
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.
*/
glm_vec4_sub(p2, p1, v1);
glm_vec4_sub(p3, p1, v2);
glm_vec4_copy(p1, v3);
glm_vec4_sub(p2, p1, v1);
glm_vec4_sub(p3, p1, v2);
glm_vec4_copy(p1, v3);
/* Setup U1 */
{
glm_vec4_copy(v1, u1);
@@ -126,7 +123,7 @@ void __calculate_normal(float* p1, float* p2, float* p3,float* normal, unsigned
alpha = glm_vec4_dot(v2, u1) / glm_vec4_dot(u1, u1);
glm_vec4_scale(u1, alpha, proj);
glm_vec4_sub(v2, proj, u2);
glm_vec4_sub(v2, proj, u2);
}
/* Setup U3 */
@@ -135,35 +132,35 @@ void __calculate_normal(float* p1, float* p2, float* p3,float* normal, unsigned
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;
}
}
}
float * generate_normals_surface( float * d, unsigned char m )
float *generate_normals_surface(float *d, unsigned char m)
{
float * n;
n = malloc( (*d+1)*sizeof(float));
float *n;
n = malloc((*d + 1) * sizeof(float));
*n = *d;
for (int i = 0; i < *d; i += 3*m)
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 );
__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

@@ -3,36 +3,32 @@
#define TYPE GL_TEXTURE_2D_ARRAY
static
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, unsigned char n)
{
return glDeleteTextures( 1, &texture );
}
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);
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

@@ -8,7 +8,7 @@ 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
@@ -16,8 +16,7 @@ window_t init_window(unsigned int w, unsigned int h, const char * name);
para lograr los fps deseados.
*/
static
void __limit_fps_window(int max_fps)
static void __limit_fps_window(int max_fps)
{
static double previous_time = 0.0;
double current_time;
@@ -45,7 +44,7 @@ window_t init_window(unsigned int width, unsigned int height, const char *title)
{
window_t window;
if( !glfwInit() )
if (!glfwInit())
return NULL;
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;
}
glfwMakeContextCurrent((GLFWwindow*)(window));
glfwMakeContextCurrent((GLFWwindow *)(window));
glfwSetWindowSizeCallback((GLFWwindow*)window, __window_callback_input);
glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback_input);
glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback_input);
glfwSetKeyCallback((GLFWwindow*)window, __key_callback_input);
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 );
__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();
__limit_fps_window(60);
return !glfwWindowShouldClose((void*)window);
return !glfwWindowShouldClose((void *)window);
}
void close_window(window_t window)