diff --git a/include/base.h b/include/base.h index 66797d7..18ebfb7 100644 --- a/include/base.h +++ b/include/base.h @@ -29,6 +29,8 @@ #include #include #include +#include +#define G_APPLICATION_DEFAULT_FLAGS 0 enum { @@ -60,12 +62,7 @@ static inline char *read_file(char *filename) } filesize = lseek(fd, 0, SEEK_END) + 1 ; - contents = malloc(filesize * sizeof(char)); - - if (contents == NULL) { - perror("Not enough memory to allocate file"); - return NULL; - } + contents = g_malloc(filesize * sizeof(char)); lseek(fd, 0, SEEK_SET); read(fd,contents,filesize); diff --git a/include/graphics_cube.h b/include/graphics_cube.h index e0cb8a8..3f8deed 100644 --- a/include/graphics_cube.h +++ b/include/graphics_cube.h @@ -27,26 +27,36 @@ /* -------------------------------------------------------------------------- */ -// v4----- v5 +// v5----- v4 // /| /| // v1------v0| // | | | | -// | |v7---|-|v6 +// | |v6---|-|v7 // |/ |/ // v2------v3 // -static GLfloat vertex_base[] = { - 0.5, 0.5, 0.5, // v0 - -0.5, 0.5, 0.5, // v1 - -0.5,-0.5, 0.5, // v2 - 0.5,-0.5, 0.5, // v3 - 0.5, 0.5,-0.5, // v4 - -0.5, 0.5,-0.5, // v5 - -0.5,-0.5,-0.5, // v6 - 0.5,-0.5,-0.5, // v7 +// static GLfloat vertex_base[] = { +// 0.5, 0.5, 0.5, // v0 +// -0.5, 0.5, 0.5, // v1 +// -0.5,-0.5, 0.5, // v2 +// 0.5,-0.5, 0.5, // v3 +// 0.5, 0.5,-0.5, // v4 +// -0.5, 0.5,-0.5, // v5 +// -0.5,-0.5,-0.5, // v6 +// 0.5,-0.5,-0.5, // v7 +// }; +static GLfloat vertex_base_ignore[] = { + 0.5, .0f, .0f, // v0 + -0.5, .0f, .0f, // v1 + -0.5,-0.5, .0f, // v2 + 0.5,-0.5, .0f, // v3 + 0.5, .0f,-.0f, // v4 + -0.5, .0f,-.0f, // v5 + -0.5,-0.5,-.0f, // v6 + 0.5,-0.5,-.0f, // v7 }; -static GLubyte indices[] = { +static GLubyte indices_ignore[] = { 0,1, 1,2, 2,3, @@ -61,7 +71,21 @@ static GLubyte indices[] = { 1,5, 2,6, 3,7, + + // 2,4, + // 3,5, + // 0,6, + // 1,7, + }; +// v5------v4 +// /| /| +// v1------v0| +// | | | | +// | v6----|-v7 +// |/ |/ +// v2------v3 +// static GLfloat color_base[] = { @@ -76,3 +100,4 @@ static GLfloat color_base[] = { }; /* -------------------------------------------------------------------------- */ + diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index 08dbbe8..c29c87d 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -22,6 +22,7 @@ * along with this program. If not, see . */ +#include #include #include #include @@ -41,6 +42,15 @@ #define VERTEX_SHADER_FILE "src/shaders/shader.vert" #define FRAG_SHADER_FILE "src/shaders/shader.frag" +#define K 0 + +GLfloat *vertex_base = NULL; +GLubyte *indices = NULL; +int dim = 0; +int state_size = 0; +int vertex_nb = 0; +int segments_nb = 0; +int arrows_nb = 0; /* -------------------------------------------------------------------------- */ @@ -245,18 +255,11 @@ bool graphics_init(const char *gl_area) // If it does not exist, allocs it if (gl_area_array == NULL) { - gl_area_array = calloc(2, sizeof(struct gl_area_entry *)); + gl_area_array = g_malloc0(sizeof(struct gl_area_entry *) * 2); - // check if an error occured during allocation - if (gl_area_array == NULL) { - printf("gl_area_array : %p", gl_area_array); - perror("Not enough memory to allocate gl_area_array"); - return false; - } - - // If it does exist, reallocs it + // If it does exist, g_reallocs it } else { - gl_area_array = realloc(gl_area_array, + gl_area_array = g_realloc(gl_area_array, (array_size + 1) * sizeof(struct gl_area_entry *)); if (gl_area_array == NULL) { @@ -269,12 +272,7 @@ bool graphics_init(const char *gl_area) } // Alloc new entry - gl_area_array[array_size] = calloc(1, sizeof(struct gl_area_entry)); - - if (gl_area_array[array_size] == NULL) { - perror("Not enough memory to allocate gl_area_array entry"); - return false; - } + gl_area_array[array_size] = g_malloc0(sizeof(struct gl_area_entry)); strcpy(gl_area_array[array_size]->name, gl_area); @@ -311,23 +309,16 @@ bool graphics_shutdown(const void *gl_area) glDeleteProgram(entry->program); // Liberate - free(entry); - - if (errno) { - perror("Impossible to free the gl_area_array entry"); - return false; - } + g_free(entry); entry = NULL; - gl_area_array = realloc(gl_area_array, + gl_area_array = g_realloc(gl_area_array, (gl_area_size()) * sizeof(struct gl_area_entry *)); - if (errno) { - perror("Impossible to shrink gl_area_array"); - return false; - } + g_free(vertex_base); + g_free(indices); return true; } @@ -418,14 +409,14 @@ static inline GLuint create_shader(int type, const char *src) glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len); - buffer = malloc(log_len + 1); + buffer = g_malloc(log_len + 1); glGetShaderInfoLog(shader, log_len, NULL, buffer); g_warning("Compile failure in %s shader:\n%s", type == GL_VERTEX_SHADER ? "vertex" : "fragment", buffer); - free(buffer); + g_free(buffer); glDeleteShader(shader); @@ -473,7 +464,7 @@ bool graphics_init_shaders(const void *gl_area) if(vertex == 0) { entry->program = 0; - free(vertex_shader); + g_free(vertex_shader); return false; } @@ -486,8 +477,8 @@ bool graphics_init_shaders(const void *gl_area) if(fragment == 0) { glDeleteShader(vertex); entry->program = 0; - free(vertex_shader); - free(fragment_shader); + g_free(vertex_shader); + g_free(fragment_shader); return false; } @@ -506,12 +497,12 @@ bool graphics_init_shaders(const void *gl_area) glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len); - buffer = malloc(log_len + 1); + buffer = g_malloc(log_len + 1); glGetProgramInfoLog(program, log_len, NULL, buffer); g_warning("Linking failure:\n%s", buffer); - free(buffer); + g_free(buffer); glDeleteProgram(program); program = 0; @@ -519,8 +510,8 @@ bool graphics_init_shaders(const void *gl_area) glDeleteShader(vertex); glDeleteShader(fragment); - free(vertex_shader); - free(fragment_shader); + g_free(vertex_shader); + g_free(fragment_shader); return false; } @@ -541,56 +532,336 @@ bool graphics_init_shaders(const void *gl_area) entry->v = v; entry->p = p; - free(vertex_shader); - free(fragment_shader); + g_free(vertex_shader); + g_free(fragment_shader); return true; } -/* - * Initializes the buffer of a gl_area - * +#define A 0 +static bool compute_arrow_2D(struct gl_area_entry *entry, int state_size, int vgap, int igap, int weight, int site, int x, int y, int z){ + + float zero = 0.0f; + float center = (1.0f / state_size) * (2 * x - state_size + 2); + float tip = center + (2 * site - 1) * (1.0f / state_size); + float base = center + (2 * site - 1) * (0.1f / state_size); + + printf("compute_arrow_2D vgap = %d vertex_nb = %d\n", vgap, vertex_nb); + + assert(vgap < vertex_nb); + assert(vgap + 9 < vertex_nb); + + *(vertex_base + vgap + 0) = center + (2 * site - 1) * (1.0f / state_size - 0.01f); + *(vertex_base + vgap + 1) = zero; + *(vertex_base + vgap + 2) = zero; + + *(vertex_base + vgap + 3) = base; + *(vertex_base + vgap + 4) = 0.4f / state_size; + *(vertex_base + vgap + 5) = zero; + + *(vertex_base + vgap + 6) = base; + *(vertex_base + vgap + 7) = - 0.4f / state_size; + *(vertex_base + vgap + 8) = zero; + + if (A) printf("x = %d site = %d center = %f tip = %f base = %f\n",\ + x, site, center, tip, base); // vgap=%d igap=%d + + assert(igap < entry->indices_nb); + assert(igap + 5 < entry->indices_nb); + + *(indices + igap + 0) = vgap / 3 + 0; + *(indices + igap + 1) = vgap / 3 + 1; + *(indices + igap + 2) = vgap / 3 + 0; + *(indices + igap + 3) = vgap / 3 + 2; + *(indices + igap + 4) = vgap / 3 + 1; + *(indices + igap + 5) = vgap / 3 + 2; + + return 1; +} + +static bool compute_arrow_3D(struct gl_area_entry *entry, int state_size, int vgap, int igap, int weight, int site, int x, int y, int z){ + + float zero = 0.0f; + float center = (1.0f / state_size) * (2 * x - state_size + 2); + float tip = center + (2 * site - 1) * (1.0f / state_size); + float base = center + (2 * site - 1) * (0.1f / state_size); + + printf("compute_arrow_3D vgap = %d vertex_nb = %d\n", vgap, vertex_nb); + + assert(vgap < vertex_nb); + assert(vgap + 9 < vertex_nb); + + *(vertex_base + vgap + 0) = center + (2 * site - 1) * (1.0f / state_size - 0.01f); + *(vertex_base + vgap + 1) = zero; + *(vertex_base + vgap + 2) = zero; + + *(vertex_base + vgap + 3) = base; + *(vertex_base + vgap + 4) = 0.4f / state_size; + *(vertex_base + vgap + 5) = 0.4f / state_size; + + *(vertex_base + vgap + 6) = base; + *(vertex_base + vgap + 7) = 0.4f / state_size; + *(vertex_base + vgap + 8) = - 0.4f / state_size; + + *(vertex_base + vgap + 9) = base; + *(vertex_base + vgap + 10) = - 0.4f / state_size; + *(vertex_base + vgap + 11) = - 0.4f / state_size; + + *(vertex_base + vgap + 12) = base; + *(vertex_base + vgap + 13) = - 0.4f / state_size; + *(vertex_base + vgap + 14) = 0.4f / state_size; + + if (A) printf("x = %d site = %d center = %f tip = %f base = %f\n",\ + x, site, center, tip, base); // vgap=%d igap=%d + + assert(igap < entry->indices_nb); + assert(igap + 5 < entry->indices_nb); + + *(indices + igap + 0) = vgap / 3 + 0; + *(indices + igap + 1) = vgap / 3 + 1; + + *(indices + igap + 2) = vgap / 3 + 0; + *(indices + igap + 3) = vgap / 3 + 2; + + *(indices + igap + 4) = vgap / 3 + 0; + *(indices + igap + 5) = vgap / 3 + 3; + + *(indices + igap + 6) = vgap / 3 + 0; + *(indices + igap + 7) = vgap / 3 + 4; + + *(indices + igap + 8) = vgap / 3 + 1; + *(indices + igap + 9) = vgap / 3 + 2; + + *(indices + igap + 10) = vgap / 3 + 2; + *(indices + igap + 11) = vgap / 3 + 3; + + *(indices + igap + 12) = vgap / 3 + 3; + *(indices + igap + 13) = vgap / 3 + 4; + + *(indices + igap + 14) = vgap / 3 + 4; + *(indices + igap + 15) = vgap / 3 + 1; + + return 1; +} + +#define V 1 +#define I 0 +#define EDGE 0.999999 +static bool compute_space_2D(struct gl_area_entry *entry, int state_size){ + + float x = 0; + // for (int k = 0; k < state_size + 1; k++) <=> + 1 car + // on passe du nombre de cases = (n) + // au nombre de séparations entre les cases + les deux bords = (n + 1) + + if (V) printf("vertices "); + for (int k = 0; k < state_size; k++){ // barres verticales + // + x = ((state_size % 2) * (state_size / 2 - k) + + (state_size % 2 - 1) * (k + 0.5f - state_size / 2)) / state_size * 2 * EDGE; + + if (V) printf("[%1.1f] ", x); + + assert(k * 6 < vertex_nb); + assert(k * 6 + 5 < vertex_nb); + + *(vertex_base + k * 6 + 0) = - x; + *(vertex_base + k * 6 + 1) = 1.0f / state_size; + *(vertex_base + k * 6 + 2) = 0.0f; + + *(vertex_base + k * 6 + 3) = - x; + *(vertex_base + k * 6 + 4) = - 1.0f / state_size; + *(vertex_base + k * 6 + 5) = 0.0f; + } + + if (V) printf(" n = %d x 2 côté = [%1.1f]\n", state_size + 1, 2.0f / state_size); + + *(indices + 0) = 0; + *(indices + 1) = state_size * 2 - 2; // barre horizontale du bas + + *(indices + 2) = 1; + *(indices + 3) = state_size * 2 - 1; // barre horizontale du haut + + for (int k = 0; k < state_size * 2; k++){ + + assert(k < entry->indices_nb); + assert(k + 4 < entry->indices_nb); + + *(indices + k + 4) = k; + } + + /* *(indices + state_size * 2 + 6 + 4) = 0; // diagonales pour marquer la case zéro */ + /* *(indices + state_size * 2 + 6 + 5) = 3; */ + /* *(indices + state_size * 2 + 6 + 6) = 1; */ + /* *(indices + state_size * 2 + 6 + 7) = 2; */ + + if (I) printf("indices (%d - state_size(0)) x (%d - state_size(1)) ", 0, 1); + for (int v = 0; v < state_size + 2; v++) { + if (I) printf("(%d-%d) ", *(indices + v), *(indices + v + 1)); + } + + if (I) printf(" n = 4 + (%d x 2)\n", state_size + 2); + + return 1; +} + +static bool compute_space_3D(struct gl_area_entry *entry, int state_size){ + + float x = 0; + // for (int k = 0; k < state_size + 1; k++) <=> + 1 car + // on passe du nombre de cases = (n) + // au nombre de séparations entre les cases + les deux bords = (n + 1) + + if (V) printf("vertices "); + for (int k = 0; k < state_size; k++){ // barres verticales + // + x = ((state_size % 2) * (state_size / 2 - k) + + (state_size % 2 - 1) * (k + 0.5f - state_size / 2)) / state_size * 2 * EDGE; + + if (V) printf("[%1.1f] ", x); + + assert(k * 12 < vertex_nb); + assert(k * 12 + 11 < vertex_nb); + + *(vertex_base + k * 12 + 0) = - x; + *(vertex_base + k * 12 + 1) = 1.0f / state_size; + *(vertex_base + k * 12 + 2) = 1.0f / state_size; + + *(vertex_base + k * 12 + 3) = - x; + *(vertex_base + k * 12 + 4) = - 1.0f / state_size; + *(vertex_base + k * 12 + 5) = - 1.0f / state_size; + + *(vertex_base + k * 12 + 6) = - x; + *(vertex_base + k * 12 + 7) = 1.0f / state_size; + *(vertex_base + k * 12 + 8) = - 1.0f / state_size; + + *(vertex_base + k * 12 + 9) = - x; + *(vertex_base + k * 12 + 10) = - 1.0f / state_size; + *(vertex_base + k * 12 + 11) = 1.0f / state_size; + } + + if (V) printf(" n = %d x 2 côté = [%1.1f]\n", state_size + 1, 2.0f / state_size); + + *(indices + 4) = 0; + *(indices + 5) = state_size * 4 - 4; // barre horizontale du bas arr + + *(indices + 6) = 1; + *(indices + 7) = state_size * 4 - 3; // barre horizontale du haut arr + + *(indices + 0) = 2; + *(indices + 1) = state_size * 4 - 2; // barre horizontale du bas av + + *(indices + 2) = 3; + *(indices + 3) = state_size * 4 - 1; // barre horizontale du haut av + + for (int k = 8; k <= state_size * 8; k += 8){ // for (int k = 8; k < state_size * 8; k += 8){ + + + assert(k < entry->indices_nb); + assert(k + 8 < entry->indices_nb); + + *(indices + k + 0) = k / 2 - 4; // + 0; + *(indices + k + 1) = k / 2 - 2; // + 2; + + *(indices + k + 2) = k / 2 - 3; // + 1; + *(indices + k + 3) = k / 2 - 1; // + 3; + + *(indices + k + 4) = k / 2 - 4; // + 0; + *(indices + k + 5) = k / 2 - 1; // + 3; + + *(indices + k + 6) = k / 2 - 3; // + 1; + *(indices + k + 7) = k / 2 - 2; // + 2; + } + + + *(indices + entry->indices_nb - 4) = 0 + 0; // diagonales pour marquer la case zéro + *(indices + entry->indices_nb - 3) = 0 + 1; + + *(indices + entry->indices_nb - 2) = 0 + 2; // diagonales pour marquer la case zéro + *(indices + entry->indices_nb - 1) = 0 + 3; + + + if (I) printf("indices (%d - state_size(0)) x (%d - state_size(1)) ", 0, 1); + for (int v = 0; v < state_size + 2; v++) { + if (I) printf("(%d-%d) ", *(indices + v), *(indices + v + 1)); + } + + if (I) printf(" n = 4 + (%d x 2)\n", state_size + 2); + + return 1; +} + +/* Initializes the buffer of a gl_area * @param gl_area, ptr to the gl_area widget - * * Note : indices[] is defined in graphics_cube.h - * * @return void */ + + void graphics_init_buffers(const void *gl_area) { - GLuint vao, vertex_buffer, color_buffer; struct gl_area_entry *entry; - int vertex_nb; + GLuint vao, vertex_buffer, color_buffer; + int vgap = 0, igap = 0; entry = find_entry_from_ptr(gl_area); - entry->indices_nb = sizeof(indices) / sizeof(indices[0]); - vertex_nb = entry->indices_nb * 3; - printf("Initialization of buffers with %u indices (=%u vertex)\n", - entry->indices_nb, - vertex_nb); - printf("Real vertex number is %lu long\n", - sizeof(vertex_base) / sizeof(vertex_base[0])); + + //{1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21}; + dim = 1; + state_size = 13; // 2 < state_size < 32 + arrows_nb = 6; + + vertex_nb = (state_size + 1) * 6 + arrows_nb * 9; + vertex_nb = (state_size + 1) * 12 + arrows_nb * 15; + segments_nb = (2 + state_size) + (arrows_nb * 3); + segments_nb = (4 + state_size * 4) + (arrows_nb * 8) + 2; // + 2; pour les 2 diagonales + + entry->indices_nb = segments_nb * 2; + + printf("Initialization of buffers with %u indices and %u vertices & state_size = %d\n", + entry->indices_nb, vertex_nb, state_size); + + //XXX g_malloc + vertex_base = g_malloc0(vertex_nb * sizeof(GLfloat) * 2); + indices = g_malloc0(segments_nb * 2 * sizeof(GLubyte) * 2); + +// compute_space_2D(entry, state_size); vgap += (6 * state_size); igap += state_size * 2 + 4; + + compute_space_3D(entry, state_size); vgap += (12 * state_size); igap += state_size * 8 + 8; + + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 9; igap += 6; ++arrows_nb; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 0, 0, 0); vgap += 9; igap += 6; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 9; igap += 6; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 9; igap += 6; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 9; igap += 6; */ + /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 9; igap += 6; */ + + compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 15; igap += 16; ++arrows_nb; + compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 0, 0, 0); vgap += 15; igap += 16; + compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 15; igap += 16; + compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 15; igap += 16; + compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 15; igap += 16; + compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 15; igap += 16; // We only use one VAO, so we always keep it bound glGenVertexArrays(1, &vao); glBindVertexArray(vao); - // vertices glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_base), vertex_base, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, vertex_nb * sizeof(vertex_base[0]), vertex_base, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // colors - glGenBuffers(1, &color_buffer); - glBindBuffer(GL_ARRAY_BUFFER, color_buffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(color_base), color_base, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); + /* glGenBuffers(1, &color_buffer); */ + /* glBindBuffer(GL_ARRAY_BUFFER, color_buffer); */ + /* glBufferData(GL_ARRAY_BUFFER, sizeof(color_base), color_base, GL_STATIC_DRAW); */ + /* glBindBuffer(GL_ARRAY_BUFFER, 0); */ if(entry != NULL) { entry->vao = vao; entry->position_buffer = vertex_buffer; - entry->color_buffer = color_buffer; + //entry->color_buffer = color_buffer; } } @@ -644,9 +915,9 @@ void graphics_draw(const void *gl_area) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); // couleurs - glEnableVertexAttribArray(1); - glBindBuffer(GL_ARRAY_BUFFER, entry->color_buffer); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); + /* glEnableVertexAttribArray(1); */ + /* glBindBuffer(GL_ARRAY_BUFFER, entry->color_buffer); */ + /* glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); */ glEnable(GL_DEPTH_TEST); @@ -660,3 +931,4 @@ void graphics_draw(const void *gl_area) glFlush(); } + diff --git a/src/main.c b/src/main.c index 235a97e..da36c5e 100644 --- a/src/main.c +++ b/src/main.c @@ -36,8 +36,8 @@ int main(int argc, char **argv) int res; // bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); - // bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); - // textdomain (GETTEXT_PACKAGE); + // bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); + // textdomain (GETTEXT_PACKAGE); app = gem_graph_client_application_new("org.alec.gemgraph", G_APPLICATION_DEFAULT_FLAGS); @@ -46,3 +46,5 @@ int main(int argc, char **argv) return res; } + +