From 8130b663d84e7433189c31672f4834c4fb2417ff Mon Sep 17 00:00:00 2001 From: Adrien 'neox' Bourmault Date: Wed, 6 Sep 2023 18:14:05 +0200 Subject: [PATCH 1/2] src/graphics/*: full refactoring of source code src/graphics.c : contains all primitives on buffers and control src/arrows.c : contains all complex writing for arrows constructions src/grid.c : contains all complexe writing for grid construction Co-developed-by: Jean Sirmai Signed-off-by: Adrien Bourmault --- include/arrows.h | 90 +++- include/base.h | 26 +- include/graphics.h | 209 ++++++-- include/{buffers.h => grid.h} | 57 ++- include/space.h | 61 --- src/graphics/arrows.c | 580 ++++++---------------- src/graphics/buffers.c | 574 ---------------------- src/graphics/graphics.c | 886 +++++++++++++++------------------- src/graphics/grid.c | 169 +++++++ src/graphics/scories.c.forget | 394 --------------- 10 files changed, 1039 insertions(+), 2007 deletions(-) rename include/{buffers.h => grid.h} (50%) delete mode 100644 include/space.h delete mode 100644 src/graphics/buffers.c create mode 100644 src/graphics/grid.c delete mode 100644 src/graphics/scories.c.forget diff --git a/include/arrows.h b/include/arrows.h index e06d38a..98ac46d 100644 --- a/include/arrows.h +++ b/include/arrows.h @@ -4,6 +4,7 @@ * Desc: OpenGL utils header * * Copyright (C) 2023 Jean Sirmai + * Copyright (C) 2023 Adrien Bourmault * * This file is part of Gem-graph. * @@ -26,17 +27,88 @@ #include #include -void check_arrows_data(int x, int y, int z, - GLuint *arrows, int arrows_nb, int central_stars_nb); +/* + * Writes basis and tip for all arrows into vertex and color buffers + * + * @param dimensions (x,y,z) + * + * @return void + */ +void arrows_write_terminations (long x, long y, long z); -long compute_vertex_nb(int vertex_nb, int x, int y, int z); +/* + * Writes lines for arrow oriented to the east into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_east (long s, int weight, int site); -long compute_lines_nb(int lines_nb, int arrows_nb, int central_stars_nb); +/* + * Writes lines for arrow oriented to the west into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_west (long s, int weight, int site); -bool alloc_arrows_vertex (long x, long y, long z, - GLfloat *vertex_origin, GLfloat *colors_origin, - int vertex_index, int colors_index); +/* + * Writes lines for arrow oriented to the zenith into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_zenith (long s, int weight, int site); -bool alloc_arrows_lines (long space_size_x, long space_size_y, long space_size_z, - GLuint *arrows, int arrows_nb); +/* + * Writes lines for arrow oriented to the nadir into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_nadir (long s, int weight, int site); +/* + * Writes lines for arrow oriented to the south into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_south (long s, int weight, int site); + +/* + * Writes lines for arrow oriented to the north into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_north (long s, int weight, int site); + +/* + * Writes lines for arrow basis into lines buffer + * + * @param n, + * weight, + * site + * + * @return void + */ +void arrows_write_basis(long n); diff --git a/include/base.h b/include/base.h index 18ebfb7..6d3eeaf 100644 --- a/include/base.h +++ b/include/base.h @@ -23,13 +23,18 @@ */ #pragma once +#include +#include +#include +#include #include #include +#include #include -#include +#include +#include #include -#include -#include +#include #define G_APPLICATION_DEFAULT_FLAGS 0 enum @@ -73,3 +78,18 @@ static inline char *read_file(char *filename) return contents; } + +/* I'm standing on Earth (any planet or star or spinning spheroid, in fact) + * and looking towards its North pole + * + * X - X = EAST - WEST = rouge - cyan + * Y - Y = ZENITH - NADIR = vert - magenta + * Z - Z = NORTH - SOUTH = bleu - jaune + */ + +#define EAST 0 // + x rouge +#define WEST 1 // - x cyan +#define ZENITH 2 // + y vert +#define NADIR 3 // - y magenta +#define SOUTH 4 // - z jaune +#define NORTH 5 // + z bleu diff --git a/include/graphics.h b/include/graphics.h index 160a088..0686350 100644 --- a/include/graphics.h +++ b/include/graphics.h @@ -26,7 +26,9 @@ #pragma once #include #include +#include #include +#include /* * Structure describing a gl_area and its parameters, used to create a table @@ -46,6 +48,16 @@ struct gl_area_entry { }; +/* + * Find gl_area_entry from a gl_area ptr + * + * @param gl_area, ptr to the gl_area widget + * + * + * @return entry ptr + */ +struct gl_area_entry *graphics_find_glarea_from_ptr(const char *name); + /* * Initializes a gl_area * @@ -78,51 +90,180 @@ bool graphics_shutdown(const void *gl_area); * * @param gl_area, ptr to the gl_area widget * - * * @return true if initialized */ bool graphics_init_shaders(const void *gl_area); -/* - * Initializes the buffer of a gl_area - * +/* Initializes the buffer of a gl_area + * Calls according to the user preferences * @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); /* - * 2023-09-05 Sur la branche dev/graphics2 - * >>> Restructuration [state // arrows] + * Writes values to describe a line from a to b into the line buffer * - * Le calcul du nombre et de la position des vertex et des couleurs - * est fait au début de chaque session de travail en fonction de : - * - x,y,z - * - user preferences + * @param coords GLuint (a,b) * - * Le premier but de la restructuration est que : - * le calcul des valeurs des vertex départ/arrivée des lignes - * et l'allocation du buffer lignes (! LIGNES ! <> pas vertex) - * soit fait par des realloc(), - * (une realloc à chaque écriture (ou effacement) d'une flèche) - * et non, comme actuellement, en une seule fois lors de l'initialisation - * bdad3857a9ede7f96dac430505c4f3c5da39744f - * - * Cette restructuration sera plus lisible si le code qui décrit les flèches - * est nettement séparé de celui qui décrit l'espace. - * - * Certaines fonctions des actuels fichiers '/graphics' et '/buffers' - * doivent donc être transférées vers deux nouveaux fichiers : - * '/space' et '/arrows' - * - * La fonction draw_line(a,b) sera utilisée pour tracer toutes les lignes - * (qu'elles décrivent l'espace ou les flèches). - * Elle calcule les adresses mémoire des vertex (a) et (b) - * en fonction de leur position dans l'espace - * et réalloue le buffer lines avant d'y écrire : + * @return void */ -void draw_line (int a, int b); +void graphics_write_line (GLuint a, GLuint b); +/* + * Writes values to describe a vertex at (x,y,z) into the vertex buffer + * + * @param coords GLfloat(x,y,z) + * + * @return void + */ +void graphics_write_vertex (GLfloat x, GLfloat y, GLfloat z); + +/* + * Writes values to describe a color (r,g,b) into the color buffer + * + * @param color GLfloat(r,g,b) + * + * @return void + */ +void graphics_write_color (GLfloat r, GLfloat g, GLfloat b); + +/* + * Writes values to describe an (a,b,c) plan (triangle) into the plan buffer + * + * @param coords GLuint (a,b,c) + * + * @return void + */ +void graphics_write_plan (GLuint a, GLuint b, GLuint c); + +/* + * Initializes an identity matrix + * + * @param res, target ptr + * + * @return void + */ +static inline void compute_i(float *res) +{ + /* initialize to the identity matrix */ + res[0] = 1.f; res[4] = 0.f; res[8] = 0.f; res[12] = 0.f; + res[1] = 0.f; res[5] = 1.f; res[9] = 0.f; res[13] = 0.f; + res[2] = 0.f; res[6] = 0.f; res[10] = 1.f; res[14] = 0.f; + res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f; +} + +/* + * Computes the projection matrix + * + * @param res, target ptr + * phi, XXX + * theta, XXX + * psi, XXX + * + * @return void + */ +static inline void compute_mvp(float *res, float phi, float theta, float psi) +{ + float x = phi *(G_PI / 180.f); + float y = theta *(G_PI / 180.f); + float z = psi *(G_PI / 180.f); + float c1 = cosf(x), s1 = sinf(x); + float c2 = cosf(y), s2 = sinf(y); + float c3 = cosf(z), s3 = sinf(z); + float c3c2 = c3 * c2; + float s3c1 = s3 * c1; + float c3s2s1 = c3 * s2 * s1; + float s3s1 = s3 * s1; + float c3s2c1 = c3 * s2 * c1; + float s3c2 = s3 * c2; + float c3c1 = c3 * c1; + float s3s2s1 = s3 * s2 * s1; + float c3s1 = c3 * s1; + float s3s2c1 = s3 * s2 * c1; + float c2s1 = c2 * s1; + float c2c1 = c2 * c1; + + compute_i(res); + + /* apply all three rotations using the three matrices: + * + * ⎡ c3 s3 0 ⎤ ⎡ c2 0 -s2 ⎤ ⎡ 1 0 0 ⎤ + * ⎢ -s3 c3 0 ⎥ ⎢ 0 1 0 ⎥ ⎢ 0 c1 s1 ⎥ + * ⎣ 0 0 1 ⎦ ⎣ s2 0 c2 ⎦ ⎣ 0 -s1 c1 ⎦ + */ + res[0] = c3c2; res[4] = s3c1 + c3s2s1; res[8] = s3s1 - c3s2c1; res[12] = 0.f; + res[1] = -s3c2; res[5] = c3c1 - s3s2s1; res[9] = c3s1 + s3s2c1; res[13] = 0.f; + res[2] = s2; res[6] = -c2s1; res[10] = c2c1; res[14] = 0.f; + res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f; +} + +/* + * Created and compile a shader + * + * @param type, shader type + * src, source code of shader + * + * @return shader id + */ +static inline GLuint create_shader(int type, const char *src) +{ + GLuint shader; + int status; + + shader = glCreateShader(type); + glShaderSource(shader, 1, &src, NULL); + glCompileShader(shader); + + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + if(status == GL_FALSE) { + int log_len; + char *buffer; + + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len); + + 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); + + g_free(buffer); + + glDeleteShader(shader); + + return 0; + } + + return shader; +} + +/* + * Find the gl_area_entry size + * + * @param void + * + * @return size of array + */ +extern struct gl_area_entry **gl_area_array; +static inline short gl_area_size(void) +{ + struct gl_area_entry **cur; + short size; + + // Check uninitialized and quit + if (gl_area_array == NULL) return 0; + + cur = gl_area_array; + while(*cur) { + cur++; + size++; + } + + return size; +} + +/*** TEST ***/ + +void main_test_graphics (void); diff --git a/include/buffers.h b/include/grid.h similarity index 50% rename from include/buffers.h rename to include/grid.h index 512e9d8..f8d7d11 100644 --- a/include/buffers.h +++ b/include/grid.h @@ -3,6 +3,7 @@ * * Desc: OpenGL utils header * + * Copyright (C) 2023 Adrien Bourmault * Copyright (C) 2023 Jean Sirmai * * This file is part of Gem-graph. @@ -26,22 +27,48 @@ #include #include -bool compute_space_and_arrows(long model_space_size_x, - long model_space_size_y, - long model_space_size_z, +/* + * Writes grid lines intersections to vertex and color buffers + * + * @param coords long (x,y,z) + * + * @return void + */ +void grid_write_intersections (long x, long y, long z); - GLuint *arrows, - int model_arrows_nb, - - int pref_show_grid, - - GLfloat *vertex_origin, - GLfloat *color_origin, - GLuint *line_origin, - GLuint *plan_origin); +/* + * Writes grid ridges to vertex and color buffers + * + * @param coords long (x,y,z), step_x, step_y, step_z + * + * @return void + */ +void grid_write_ridges (long x, long y, long z); -long get_buffer_lines_size(); -void inc_buffer_lines_size(long inc); -void dec_buffer_lines_size(long dec); +/* + * Writes grid lines for x axis to vertex and color buffers + * + * @param coords long (x,y,z) + * + * @return void + */ +void grid_write_x (long x, long y, long z); +/* + * Writes grid lines for y axis to vertex and color buffers + * + * @param coords long (x,y,z) + * + * @return void + */ +void grid_write_y (long x, long y, long z); + +/* + * Writes grid lines for z axis to vertex and color buffers + * + * @param coords long (x,y,z) + * + * @return void + */ +void grid_write_z (long x, long y, long z); diff --git a/include/space.h b/include/space.h deleted file mode 100644 index 81d6243..0000000 --- a/include/space.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Gem-graph OpenGL experiments - * - * Desc: OpenGL utils header - * - * Copyright (C) 2023 Jean Sirmai - * - * This file is part of Gem-graph. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#pragma once -#include -#include -#include - -void check_space_data_and_user_preferences(int x, int y, int z, - int grid, int diagonal); - -// void do_alloc_buffers(){}; // ?!? Pourquoi ne dois-je pas déclarer cette fonction ici ? - -void compute_buffers_sizes(int model_space_size_x, - int model_space_size_y, - int model_space_size_z, - long central_stars_nb, - long arrows_nb, - int pref_test_diagonal); - -long compute_space_vertex_nb(int x, int y, int z); - -long compute_space_lines_nb(int x, int y, int z, int pref_show_grid); - -// bool alloc_space_vertex (long x, long y, long z, -// GLfloat *vertex_origin, GLfloat *colors_origin, -// int vertex_index, int colors_index); -bool alloc_space_vertex_and_colors (long x, long y, long z); - -void draw_a_cube_at(long u, long v, long w, - long size_x, long size_y, long size_z, bool diagonals); - -void draw_ridges_3D (long x, long y, long z, - long step_x, long step_y, long step_z); - -bool draw_grids_3D (long x, long y, long z, - long step_x, long step_y, long step_z, - int pref_show_grid); - -void diagonal_test(long x, long y, long z, - long step_x, long step_y, long step_z); diff --git a/src/graphics/arrows.c b/src/graphics/arrows.c index aba388d..32f0346 100644 --- a/src/graphics/arrows.c +++ b/src/graphics/arrows.c @@ -4,6 +4,7 @@ * Desc: OpenGL utils header * * Copyright (C) 2023 Jean Sirmai + * Copyright (C) 2023 Arien Bourmault * * This file is part of Gem-graph. * @@ -21,470 +22,197 @@ * aint with this program. If not, see . */ -//#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../../include/ui.h" +#include "../../include/base.h" #include "../../include/graphics.h" -bool alloc_arrows_vertex (long x, long y, long z, - GLfloat *vertex_origin, GLfloat *colors_origin, - int vertex_index, int colors_index) +/* I'm standing on Earth (any planet or star or spinning spheroid, in fact) + * and looking towards its North pole + * + * X - X = EAST - WEST = rouge - cyan + * Y - Y = ZENITH - NADIR = vert - magenta + * Z - Z = NORTH - SOUTH = bleu - jaune + */ + +/* + * Writes basis and tip for all arrows into vertex and color buffers + * + * @param dimensions (x,y,z) + * + * @return void + */ +void arrows_write_terminations (long x, long y, long z) { + float max, i, j, k, vx, vy, vz, arrow_basis_width, arrow_tip_padding; -// X - X = EAST - WEST = rouge - cyan -// Y - Y = ZENITH - NADIR = vert - magenta -// Z - Z = SOUTH - NORTH = jaune - bleu + max = fmax(x, y); max = fmax(max, z); + // arrow_basis_width donne la dimension de l'étoile centrale + arrow_basis_width = 0.3f * (1 / max), - float max = fmax(x, y); - max = fmax(max, z); - - float i, j, k, vx, vy, vz, - ad = 1 / max, - a_third = 0.3f * ad, // a_third donne la dimension de l'étoile centrale - eps = 0.1 * ad; // eps(ilon) servira à décaler légèrement les pointes des flèches - // pour qu'elles n'aillent pas jusqu'aux faces des cubes - - - if (0) printf("arrows_anchors 1 / max = %5.2f max / 2 = %5.2f\n", ad, max / 2); + // décale légèrement les pointes des flèches + // pour qu'elles n'aillent pas jusqu'aux faces des cubes + arrow_tip_padding = 0.1 * (1 / max); for (i = 0; i < x; i++) for (j = 0; j < y; j++) for (k = 0; k < z; k++){ - vx = (2 * i / x - 1) * x / max + ad; - vy = (2 * j / y - 1) * y / max + ad; - vz = (2 * k / z - 1) * z / max + ad; + vx = (2 * i / x - 1) * x / max + (1 / max); + vy = (2 * j / y - 1) * y / max + (1 / max); + vz = (2 * k / z - 1) * z / max + (1 / max); - // X - X axis Central Star + // X - X axis - central star (basis) + graphics_write_vertex(vx + arrow_basis_width, vy, vz); + graphics_write_vertex(vx - arrow_basis_width, vy, vz); + graphics_write_color(0.3f, 0.3f, 0.3f); + graphics_write_color(0.3f, 0.3f, 0.3f); - *(vertex_origin + vertex_index + 0) = vx + a_third; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz; + // Y - Y axis - central star (basis) + graphics_write_vertex(vx, vy + arrow_basis_width, vz); + graphics_write_vertex(vx, vy - arrow_basis_width, vz); + graphics_write_color(0.3f, 0.3f, 0.3f); + graphics_write_color(0.3f, 0.3f, 0.3f); - vertex_index += 3; + // Z - Z axis - central star (basis) + graphics_write_vertex(vx, vy, vz + arrow_basis_width); + graphics_write_vertex(vx, vy, vz - arrow_basis_width); + graphics_write_color(0.3f, 0.3f, 0.3f); + graphics_write_color(0.3f, 0.3f, 0.3f); - *(vertex_origin + vertex_index + 0) = vx - a_third; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - - // Y - Y axis Central Star - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy + a_third; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy - a_third; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - - // Z - Z axis Central Star - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz + a_third; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz - a_third; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - //-------------------------------------------------------------- - - // X - X axis arrows tips near the faces centers EAST - WEST - - *(vertex_origin + vertex_index + 0) = vx + ad - eps; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx - ad + eps; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 1; - *(colors_origin + colors_index + 1) = 0; - *(colors_origin + colors_index + 2) = 0; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 0; - *(colors_origin + colors_index + 1) = 1; - *(colors_origin + colors_index + 2) = 1; - - colors_index += 3; - - - // Y - Y axis arrows tips near the faces centers ZENITH - NADIR - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy + ad - eps; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy - ad + eps; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0; - *(colors_origin + colors_index + 1) = 1; - *(colors_origin + colors_index + 2) = 0; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 1; - *(colors_origin + colors_index + 1) = 0; - *(colors_origin + colors_index + 2) = 1; - - colors_index += 3; + // X - X (EAST - WEST) axis - arrows tips + graphics_write_vertex (vx + (1 / max) - arrow_tip_padding, vy, vz); + graphics_write_vertex (vx - (1 / max) + arrow_tip_padding, vy, vz); + graphics_write_color(1.0f, 0.0f, 0.0f); + graphics_write_color(0.0f, 1.0f, 1.0f); + // Y - Y (ZENITH - NADIR) axis - arrows tips + graphics_write_vertex (vx, vy + (1 / max) - arrow_tip_padding, vz); + graphics_write_vertex (vx, vy - (1 / max) + arrow_tip_padding, vz); + graphics_write_color(0.0f, 1.0f, 0.0f); + graphics_write_color(1.0f, 0.0f, 1.0f); // Z - Z axis arrows tips near the faces centers NORTH - SOUTH - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz + ad - eps; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz - ad + eps; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0; - *(colors_origin + colors_index + 1) = 0; - *(colors_origin + colors_index + 2) = 1; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 1; - *(colors_origin + colors_index + 1) = 1; - *(colors_origin + colors_index + 2) = 0; - - colors_index += 3; - - + graphics_write_vertex (vx, vy, vz + (1 / max) - arrow_tip_padding); + graphics_write_vertex (vx, vy, vz - (1 / max) + arrow_tip_padding); + graphics_write_color(0.0f, 0.0f, 1.0f); + graphics_write_color(1.0f, 1.0f, 0.0f); }; - - - return 1; } -void check_arrows_data(int x, int y, int z, - GLuint *arrows, int arrows_nb, int central_stars_nb) +/* + * Writes lines for arrow oriented to the east into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_east (long s, int weight, int site) { -// TODO assert : l'emplacement de chaque flèche est contraint -// par model_space_size_x, y, z et le nombre de sites possibles. - -// ! WARNING ! Pour l'instant égal au nombre de flèches ! -// Les "central stars" sont donc systématiquement redessinées -// s'il y a plusieurs flèches d'orientation différente dans le même cube. - - printf("ARROWS x,y,z = (%d,%d,%d) arrows_nb = %d central_stars_nb = %d\n",\ - x, y, z, arrows_nb, central_stars_nb); - - if (arrows_nb > 0) printf("\n[ n] load site x y z ---- < arrows array >\ - ------------------------------------------------------------\n"); - for (int i = 0; i < arrows_nb; i++) - printf("[%2d] = %2d, %2d, %2d, %2d, %2d \n",\ - i, *(arrows + i * 5 + 0), *(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\ - *(arrows + i * 5 + 3), *(arrows + i * 5 + 4)); + graphics_write_line (s + 2, s + 6 + site % 2); + graphics_write_line (s + 3, s + 6 + site % 2); + graphics_write_line (s + 4, s + 6 + site % 2); + graphics_write_line (s + 5, s + 6 + site % 2); } -long compute_vertex_nb(int vertex_nb, int x, int y, int z) +/* + * Writes lines for arrow oriented to the west into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_west (long s, int weight, int site) { -// int vertex_nb = (x + 1) * (y + 1) * (z + 1); <> cf graphics/space.c - int unit_cubes_nb = x * y * z; - vertex_nb += 12 * unit_cubes_nb; // 6 for the central star - // 6 for the unit cubes faces centers - - return vertex_nb; + graphics_write_line (s + 2, s + 6 + site % 2); + graphics_write_line (s + 3, s + 6 + site % 2); + graphics_write_line (s + 4, s + 6 + site % 2); + graphics_write_line (s + 5, s + 6 + site % 2); } -long compute_lines_nb(int lines_nb, int arrows_nb, int central_stars_nb) +/* + * Writes lines for arrow oriented to the zenith into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_zenith (long s, int weight, int site) { -// TODO central_stars_nb sera à calculer pour éviter de redessiner les central_stars - central_stars_nb = arrows_nb; // ! WARNING ! Pour l'instant égal au nombre de flèches ! -// Les "central stars" sont donc systématiquement redessinées -// s'il y a plusieurs flèches d'orientation différente dans le même cube. - - lines_nb = 3 * 2 * central_stars_nb; // test central stars - lines_nb += 4 * 2 * arrows_nb; // flèches - - return lines_nb; + graphics_write_line (s + 0, s + 8 + site % 2); + graphics_write_line (s + 1, s + 8 + site % 2); + graphics_write_line (s + 4, s + 8 + site % 2); + graphics_write_line (s + 5, s + 8 + site % 2); } -#define VERBOSE 0 -static bool rewrite_arrow (GLuint *arrows, long arrows_nb, long address, long weight, - long site, long x, long y, long z) +/* + * Writes lines for arrow oriented to the nadir into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_nadir (long s, int weight, int site) { - if (VERBOSE) printf("write the arrow weight to %ld at (%ld, %ld, %ld, %ld)\n",\ - weight, site, x, y, z); - - *(arrows + address + 0) = weight; - - return 1; /* replaces the load of the existing arrow by the load 'weight' */ + graphics_write_line (s + 0, s + 8 + site % 2); + graphics_write_line (s + 1, s + 8 + site % 2); + graphics_write_line (s + 4, s + 8 + site % 2); + graphics_write_line (s + 5, s + 8 + site % 2); } -static bool create_arrow (GLuint *arrows, long arrows_nb, long weight, long site, long x, long y, long z) +/* + * Writes lines for arrow oriented to the south into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_south (long s, int weight, int site) { - if (VERBOSE) printf("create a new arrow with weight = %ld at (%ld, %ld, %ld, %ld)\n",\ - weight, site, x, y, z); - - arrows_nb ++; - inc_buffer_lines_size ((3 + 4) * 2); - set_buffer_lines_origin (g_realloc (get_buffer_lines_origin(), get_buffer_lines_size() * 2 * sizeof(GLuint) * 2)); - - *(arrows + arrows_nb + 0) = weight; - *(arrows + arrows_nb + 1) = site; - *(arrows + arrows_nb + 2) = x; - *(arrows + arrows_nb + 3) = y; - *(arrows + arrows_nb + 4) = z; - - return 1; + graphics_write_line (s + 0, s + 10 + site % 2); + graphics_write_line (s + 1, s + 10 + site % 2); + graphics_write_line (s + 2, s + 10 + site % 2); + graphics_write_line (s + 3, s + 10 + site % 2); } -static bool erase_arrow (GLuint *arrows, long arrows_nb, long address, - long site, long x, long y, long z) +/* + * Writes lines for arrow oriented to the north into lines buffer + * + * @param s, + * weight, + * site + * + * @return void + */ +void write_arrow_lines_north (long s, int weight, int site) { - if (VERBOSE) printf("erase arrow at address %ld with values (%ld, %ld, %ld, %ld)",\ - address / 5, site, x, y, z); - if (VERBOSE) printf(" last arrow is %d, %d, %d, %d, %d\n", - *(arrows + (arrows_nb - 1) * 5 + 0), - *(arrows + (arrows_nb - 1) * 5 + 1), - *(arrows + (arrows_nb - 1) * 5 + 2), - *(arrows + (arrows_nb - 1) * 5 + 3), - *(arrows + (arrows_nb - 1) * 5 + 4)); - - *(arrows + address + 0) = *(arrows + (arrows_nb - 1) * 5 + 0); - *(arrows + address + 1) = *(arrows + (arrows_nb - 1) * 5 + 1); - *(arrows + address + 2) = *(arrows + (arrows_nb - 1) * 5 + 2); - *(arrows + address + 3) = *(arrows + (arrows_nb - 1) * 5 + 3); - *(arrows + address + 4) = *(arrows + (arrows_nb - 1) * 5 + 4); - - arrows_nb --; - dec_buffer_lines_size ((3 + 4) * 2); - set_buffer_lines_origin (g_realloc (get_buffer_lines_origin(), get_buffer_lines_size() * 2 * sizeof(GLuint) * 2)); - - return 1; + graphics_write_line (s + 0, s + 10 + site % 2); + graphics_write_line (s + 1, s + 10 + site % 2); + graphics_write_line (s + 2, s + 10 + site % 2); + graphics_write_line (s + 3, s + 10 + site % 2); } -static long check_for_arrow (GLuint *arrows, long arrows_nb, long site, long x, long y, long z) +/* + * Writes lines for arrow basis into lines buffer + * + * @param n, + * weight, + * site + * + * @return void + */ +void arrows_write_basis(long n) { - if (arrows_nb == 0) return -1; - - long address = -1, f_weight = -1, f_site = -1, f_x = -1, f_y = -1, f_z = -1; - for (int i = 0; i < arrows_nb; i++) - { - f_weight = *(arrows + i * 5 + 0); - f_site = *(arrows + i * 5 + 1); - f_x = *(arrows + i * 5 + 2); - f_y = *(arrows + i * 5 + 3); - f_z = *(arrows + i * 5 + 4); - - if (f_site == site && f_x == x && f_y == y && f_z == z) { - address = i * 5; - if (VERBOSE) printf("arrows_nb = %ld checked arrow (%ld,%ld,%ld,%ld) found at address %ld (line %ld)",\ - arrows_nb, site, x, y, z, address, address / 5); - if (VERBOSE && f_weight > 1) printf(" weight = %ld", f_weight); - if (VERBOSE) printf("\n"); - } - } - return address; /* returns the address of the arrow if there is one - * or (-1) if there is none */ + graphics_write_line (n + 0, n + 1); + graphics_write_line (n + 2, n + 3); + graphics_write_line (n + 4, n + 5); } - - -static void show_arrows_array (GLuint *arrows, long arrows_nb, long address, long weight, long site, long x, long y, long z) -{ - printf("\n[ n] load | site x y z ----- < arrows_nb = %ld > ------", arrows_nb); - if (arrows_nb == 0) printf("--------------------------- no arrows\n"); - else if (address == -1) printf("--------------------------- create new arrow\n"); - else printf("--------------------------- erase arrow at address = %ld / 5\n", address); - - for (int i = 0; i < arrows_nb; i++) - { - printf("[%2d] = %2d | %2d, %2d, %2d, %2d ",\ - i, *(arrows + i * 5 + 0), *(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\ - *(arrows + i * 5 + 3), *(arrows + i * 5 + 4)); - if (weight == 0 && i == address / 5) printf(" <<<| < - > (%d, %d, %d, %d) @ [%ld]",\ - *(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\ - *(arrows + i * 5 + 3), *(arrows + i * 5 + 4), address / 5); - if (weight > 0 && i == arrows_nb - 1) printf(" <<< < + > (%d, %d, %d, %d) @ [%ld]",\ - *(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\ - *(arrows + i * 5 + 3), *(arrows + i * 5 + 4), address / 5); - if (weight == 0 && i > address / 5 && i < arrows_nb - 1) printf(" |"); - if (weight == 0 && i == arrows_nb - 1 && i != address / 5) printf(" >>>|"); - printf("\n"); - } -} - - -static bool set_arrow (GLuint *arrows, long arrows_nb, long weight, long site, long x, long y, long z) -{ - long address = (check_for_arrow (arrows, arrows_nb, site, x, y, z)); - show_arrows_array(arrows, arrows_nb, address, weight, site, x, y, z); - - if (address == -1 && weight == 0) return 0; - if (address == -1 && weight > 0) return create_arrow (arrows, arrows_nb, weight, site, x, y, z); - if (address >= 0 && weight == 0) return erase_arrow (arrows, arrows_nb, address, site, x, y, z); - if (address >= 0 && weight > 0) return rewrite_arrow (arrows, arrows_nb, address, weight, site, x, y, z); - - return 0; -} - -static void old_draw_line (int a, int b) -{ -// *(lines_origin + lines_index) = a; lines_index ++; - -// *(get_buffer_lines_origin() + get_buffer_lines_size()) = a; inc_buffer_lines_size(1); -// *(get_buffer_lines_origin() + get_buffer_lines_size()) = b; inc_buffer_lines_size(1); -} - -static void draw_an_arrow_East_or_West (long s, int weight, int site) -{ - old_draw_line (s + 2, s + 6 + site % 2); - old_draw_line (s + 3, s + 6 + site % 2); - - old_draw_line (s + 4, s + 6 + site % 2); - old_draw_line (s + 5, s + 6 + site % 2); -} - -static void draw_an_arrow_Zenith_or_Nadir (long s, int weight, int site) -{ - old_draw_line (s + 0, s + 8 + site % 2); - old_draw_line (s + 1, s + 8 + site % 2); - - old_draw_line (s + 4, s + 8 + site % 2); - old_draw_line (s + 5, s + 8 + site % 2); -} - -static void draw_an_arrow_North_or_South (long s, int weight, int site) -{ - old_draw_line (s + 0, s + 10 + site % 2); - old_draw_line (s + 1, s + 10 + site % 2); - - old_draw_line (s + 2, s + 10 + site % 2); - old_draw_line (s + 3, s + 10 + site % 2); -} - -static void draw_a_central_star(long n) -{ - old_draw_line (n + 0, n + 1); - old_draw_line (n + 2, n + 3); - old_draw_line (n + 4, n + 5); -} - -// I'm standing on Earth (any planet or star or spinning spheroid, in fact) -// and looking towards its North pole - -#define EAST 0 // + x rouge -#define WEST 1 // - x cyan -#define ZENITH 2 // + y vert -#define NADIR 3 // - y magenta -#define SOUTH 4 // + z jaune -#define NORTH 5 // - z bleu - -bool alloc_arrows_lines (long space_size_x, long space_size_y, long space_size_z, - GLuint *arrows, int arrows_nb) -{ - long step_z = 1, - step_y = space_size_z + 1, - step_x = (space_size_z + 1) * (space_size_y + 1); - - long s = step_z * space_size_z + step_y * space_size_y - + step_x * space_size_x; - - long site = 0, x = 0, y = 0, z = 0; - - for (int i = 0; i < arrows_nb; i++) - { - site = *(arrows + i * 5 + 1); - x = *(arrows + i * 5 + 2); y = *(arrows + i * 5 + 3); z = *(arrows + i * 5 + 4); - - draw_a_central_star (s + 1 + 12 * (step_x * x + step_y * y + step_z * z)); - - if (site >-1 && site < 2) draw_an_arrow_East_or_West (s + 1 + 12 * (step_x * x + step_y * y + step_z * z), 1, site); - if (site > 3 && site < 6) draw_an_arrow_North_or_South (s + 1 + 12 * (step_x * x + step_y * y + step_z * z), 1, site); - if (site > 1 && site < 4) draw_an_arrow_Zenith_or_Nadir (s + 1 + 12 * (step_x * x + step_y * y + step_z * z), 1, site); - } - return 1; -} - -// X - X = EAST - WEST = rouge - cyan -// Y - Y = ZENITH - NADIR = vert - magenta -// Z - Z = SOUTH - NORTH = jaune - bleu - - - - - diff --git a/src/graphics/buffers.c b/src/graphics/buffers.c deleted file mode 100644 index 45061aa..0000000 --- a/src/graphics/buffers.c +++ /dev/null @@ -1,574 +0,0 @@ -/* - * Gem-graph OpenGL experiments - * - * Desc: OpenGL utils header - * - * Copyright (C) 2023 Jean Sirmai - * - * This file is part of Gem-graph. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -//#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../../include/ui.h" -#include "../../include/graphics.h" -#include "../../include/buffers.h" - -static long vertex_index = 0; -static long colors_index = 0; -static long primitive_lines_index = 0; - -long get_buffer_lines_size() {return primitive_lines_index / 2;} -void inc_buffer_lines_size(long inc) {primitive_lines_index += inc * 2;} -void dec_buffer_lines_size(long dec) {primitive_lines_index -= dec * 2;} - -static long plans_index = 0; - - - -static bool grids_intersections (long x, long y, long z, - GLfloat *vertex_origin, GLfloat *colors_origin) -{ - - float i, j, k, vx, vy, vz, max = fmax(x, y); - max = fmax(max, z); - - for (i = 0; i <= x; i++) - for (j = 0; j <= y; j++) - for (k = 0; k <= z; k++){ - - vx = (2 * i / x - 1) * x / max; - vy = (2 * j / y - 1) * y / max; - vz = (2 * k / z - 1) * z / max; - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0.64f;// 3 * vx / 2; - *(colors_origin + colors_index + 1) = 0.64f;// 3 * vy / 2; - *(colors_origin + colors_index + 2) = 0.64f;// 3 * vz / 2; - - colors_index += 3; - }; - - return 1; -} - - -static bool arrows_anchors (long x, long y, long z, - GLfloat *vertex_origin, GLfloat *colors_origin) -{ - /* - X - X = EAST - WEST = rouge - cyan - Y - Y = ZENITH - NADIR = vert - magenta - Z - Z = NORTH - SOUTH = bleu - jaune - */ - - float max = fmax(x, y); - max = fmax(max, z); - - float i, j, k, vx, vy, vz, - ad = 1 / max, - a_third = 0.3f * ad, // a_third donne la dimension de l'étoile centrale - eps = 0.1 * ad; // eps(ilon) servira à décaler légèrement les pointes des flèches - // pour qu'elles n'aillent pas jusqu'aux faces des cubes - - for (i = 0; i < x; i++) - for (j = 0; j < y; j++) - for (k = 0; k < z; k++){ - - vx = (2 * i / x - 1) * x / max + ad; - vy = (2 * j / y - 1) * y / max + ad; - vz = (2 * k / z - 1) * z / max + ad; - - // X - X axis Central Star - - *(vertex_origin + vertex_index + 0) = vx + a_third; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx - a_third; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - - // Y - Y axis Central Star - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy + a_third; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy - a_third; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - - // Z - Z axis Central Star - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz + a_third; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz - a_third; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 0.3f; - *(colors_origin + colors_index + 1) = 0.3f; - *(colors_origin + colors_index + 2) = 0.3f; - - colors_index += 3; - - //-------------------------------------------------------------- - - // X - X axis arrows tips near the faces centers EAST - WEST - - *(vertex_origin + vertex_index + 0) = vx + ad - eps; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx - ad + eps; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 1; - *(colors_origin + colors_index + 1) = 0; - *(colors_origin + colors_index + 2) = 0; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 0; - *(colors_origin + colors_index + 1) = 1; - *(colors_origin + colors_index + 2) = 1; - - colors_index += 3; - - - // Y - Y axis arrows tips near the faces centers ZENITH - NADIR - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy + ad - eps; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy - ad + eps; - *(vertex_origin + vertex_index + 2) = vz; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0; - *(colors_origin + colors_index + 1) = 1; - *(colors_origin + colors_index + 2) = 0; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 1; - *(colors_origin + colors_index + 1) = 0; - *(colors_origin + colors_index + 2) = 1; - - colors_index += 3; - - - // Z - Z axis arrows tips near the faces centers NORTH - SOUTH - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz + ad - eps; - - vertex_index += 3; - - *(vertex_origin + vertex_index + 0) = vx; - *(vertex_origin + vertex_index + 1) = vy; - *(vertex_origin + vertex_index + 2) = vz - ad + eps; - - vertex_index += 3; - - *(colors_origin + colors_index + 0) = 0; - *(colors_origin + colors_index + 1) = 0; - *(colors_origin + colors_index + 2) = 1; - - colors_index += 3; - - *(colors_origin + colors_index + 0) = 1; - *(colors_origin + colors_index + 1) = 1; - *(colors_origin + colors_index + 2) = 0; - - colors_index += 3; - - - }; - - - return 1; -} - - -static void primitive_draw_line (GLuint *lines_origin, int a, int b) -{ - *(lines_origin + primitive_lines_index) = a; primitive_lines_index ++; - *(lines_origin + primitive_lines_index) = b; primitive_lines_index ++; -} - -static void draw_a_cube_at(long u, long v, long w, GLuint *lines_origin, - long size_x, long size_y, long size_z, bool diagonals) -{ - long step_z = 1, step_y = size_z, step_x = size_y * size_z, x = u + 1, y = v + 1, z = w + 1; - -// draw_line (0,21); draw_line (21,42); -// ce qui suit ne vaut que dans un espace 3 x 3 x 3 (le reste est à corriger - ou à oublier ?) - draw_line (21,22); draw_line (21,25); - draw_line (25,26); draw_line (22,26); - draw_line (37,41); draw_line (41,42); - draw_line (38,42); draw_line (37,38); - draw_line (25,41); draw_line (21,37); - draw_line (26,42); draw_line (22,38); - - /* draw_line (step_z * w + step_y * v + step_x * u, step_z * (w + 0) + step_y * (v + 0) + step_x * (u + 1)); */ - /* draw_line (step_z * w + step_y * v + step_x * u, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 0)); */ - /* draw_line (step_z * w + step_y * v + step_x * u, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 0)); */ - /* draw_line (step_z * z + step_y * y + step_x * x, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 0)); */ - /* draw_line (step_z * z + step_y * y + step_x * x, step_z * (w + 1) + step_y * (v + 0) + step_x * (u + 1)); */ - /* draw_line (step_z * z + step_y * y + step_x * x, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 1)); */ - /* draw_line (step_z * w + step_y * v + step_x * x, step_z * (w + 1) + step_y * (v + 0) + step_x * (u + 1)); */ - /* draw_line (step_z * w + step_y * v + step_x * x, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 1)); */ - /* draw_line (step_z * w + step_y * y + step_x * u, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 1)); */ - /* draw_line (step_z * z + step_y * v + step_x * u, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 0)); */ - /* draw_line (step_z * z + step_y * v + step_x * u, step_z * (w + 1) + step_y * (v + 0) + step_x * (u + 1)); */ - /* draw_line (step_z * z + step_y * y + step_x * u, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 0)); */ - - if (diagonals){ - draw_line (step_z * w + step_y * v + step_x * u, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 1)); - draw_line (step_z * w + step_y * v + step_x * x, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 0)); - draw_line (step_z * z + step_y * v + step_x * u, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 1)); - draw_line (step_z * w + step_y * y + step_x * u, step_z * (w + 1) + step_y * (v + 0) + step_x * (u + 1)); - } -} - - -static void draw_ridges_3D (long x, long y, long z, - long step_x, long step_y, long step_z, - GLuint *lines_origin) -{ - draw_line (step_z * 0 + step_y * 0 + step_x * 0, step_z * 0 + step_y * 0 + step_x * x); - draw_line (step_z * 0 + step_y * 0 + step_x * 0, step_z * 0 + step_y * y + step_x * 0); - draw_line (step_z * 0 + step_y * 0 + step_x * 0, step_z * z + step_y * 0 + step_x * 0); - draw_line (step_z * z + step_y * y + step_x * x, step_z * 0 + step_y * y + step_x * x); - draw_line (step_z * z + step_y * y + step_x * x, step_z * z + step_y * y + step_x * 0); - draw_line (step_z * z + step_y * y + step_x * x, step_z * z + step_y * 0 + step_x * x); - draw_line (step_z * 0 + step_y * 0 + step_x * x, step_z * 0 + step_y * y + step_x * x); - draw_line (step_z * 0 + step_y * 0 + step_x * x, step_z * z + step_y * 0 + step_x * x); - draw_line (step_z * z + step_y * 0 + step_x * 0, step_z * z + step_y * y + step_x * 0); - draw_line (step_z * 0 + step_y * y + step_x * 0, step_z * 0 + step_y * y + step_x * x); - draw_line (step_z * 0 + step_y * y + step_x * 0, step_z * z + step_y * y + step_x * 0); - draw_line (step_z * z + step_y * 0 + step_x * 0, step_z * z + step_y * 0 + step_x * x); -} - - -static bool draw_grids_3D (long x, long y, long z, - long step_x, long step_y, long step_z, - GLuint *lines_origin, - int pref_show_grid) -{ - x ++; y ++; z ++; - float calcul = 0; - - if (pref_show_grid % 2 == 0) - for (int u = 0; u < y; u ++){ - for (int w = 0; w < x; w ++){ - calcul = step_x * w + step_y * u; - draw_line (calcul, calcul + step_y - step_z); - } - } - - if (pref_show_grid % 3 == 0) - for (int u = 0; u < z; u ++){ - for (int w = 0; w < x; w ++){ - calcul = step_x * w + step_z * u; - draw_line (calcul, calcul + step_x - step_y); - } - } - - if (pref_show_grid % 5 == 0) - for (int u = 0; u < z; u ++){ - for (int w = 0; w < y; w ++){ - calcul = step_y * w + step_z * u; - draw_line (calcul, calcul + step_x * (x - 1)); - } - } - - return 0; -} - - - - - -static void diagonal_test(long x, long y, long z, - long step_x, long step_y, long step_z, - GLuint *lines_origin) -{ - draw_line (0, step_z * z + step_y * y + step_x * x); -} - - - -static void draw_an_arrow_East_or_West (GLuint *lines_origin, long s, int weight, int site) -{ - draw_line (s + 2, s + 6 + site % 2); - draw_line (s + 3, s + 6 + site % 2); - - draw_line (s + 4, s + 6 + site % 2); - draw_line (s + 5, s + 6 + site % 2); -} - -static void draw_an_arrow_Zenith_or_Nadir (GLuint *lines_origin, long s, int weight, int site) -{ - draw_line (s + 0, s + 8 + site % 2); - draw_line (s + 1, s + 8 + site % 2); - - draw_line (s + 4, s + 8 + site % 2); - draw_line (s + 5, s + 8 + site % 2); -} - -static void draw_an_arrow_North_or_South (GLuint *lines_origin, long s, int weight, int site) -{ - draw_line (s + 0, s + 10 + site % 2); - draw_line (s + 1, s + 10 + site % 2); - - draw_line (s + 2, s + 10 + site % 2); - draw_line (s + 3, s + 10 + site % 2); -} - - -static void draw_a_central_star(GLuint *lines_origin, long n) -{ - draw_line (n + 0, n + 1); - draw_line (n + 2, n + 3); - draw_line (n + 4, n + 5); -} - -// I'm standing on Earth (any planet or star or spinning spheroid, in fact) -// and looking towards its North pole - -#define EAST 0 // + x rouge -#define WEST 1 // - x cyan -#define ZENITH 2 // + y vert -#define NADIR 3 // - y magenta -#define SOUTH 4 // - z jaune -#define NORTH 5 // + z bleu - -static void draw_some_arrows_demo (GLuint *lines_origin, - long s, long x, long y, long z, - GLuint *arrows, int arrows_nb) -{ - long stx = z * y, sty = z, stz = 1; - long a = 0, b = 0, c = 0; - - a = 0, b = 1, c = 0; - draw_a_central_star (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c)); - draw_an_arrow_East_or_West (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c), 1, EAST); - a = 1, b = 1, c = 0; - draw_a_central_star (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c)); - draw_an_arrow_East_or_West (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c), 1, WEST); - a = 1, b = 1, c = 1; - draw_a_central_star (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c)); - draw_an_arrow_Zenith_or_Nadir (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c), 1, ZENITH); - a = 1, b = 2, c = 1; - draw_a_central_star (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c)); - draw_an_arrow_Zenith_or_Nadir (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c), 1, NADIR); - a = 2, b = 2, c = 1; - draw_a_central_star (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c)); - draw_an_arrow_North_or_South (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c), 1, NORTH); - a = 2, b = 2, c = 2; - draw_a_central_star (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c)); - draw_an_arrow_North_or_South (lines_origin, s + 1 + 12 * (stx * a + sty * b + stz * c), 1, SOUTH); -} - -static void draw_some_arrows (GLuint *lines_origin, long s, long stx, long sty, - GLuint *arrows, int arrows_nb) -{ - long stz = 1; - long site = 0, x = 0, y = 0, z = 0; - - for (int i = 0; i < arrows_nb; i++) - { - site = *(arrows + i * 5 + 1); - x = *(arrows + i * 5 + 2); y = *(arrows + i * 5 + 3); z = *(arrows + i * 5 + 4); - - printf("[%d] site = %d x = %ld y = %ld z = %ld step x = %ld step y = %ld\n", site, i, x, y, z, stx, sty); - - draw_a_central_star (lines_origin, s + 1 + 12 * (stx * x + sty * y + stz * z)); - - if (site >-1 && site < 2) draw_an_arrow_East_or_West (lines_origin, s + 1 + 12 * (stx * x + sty * y + stz * z), 1, site); - if (site > 3 && site < 6) draw_an_arrow_North_or_South (lines_origin, s + 1 + 12 * (stx * x + sty * y + stz * z), 1, site); - if (site > 1 && site < 4) draw_an_arrow_Zenith_or_Nadir (lines_origin, s + 1 + 12 * (stx * x + sty * y + stz * z), 1, site); - } -} - - - -// X - X = EAST - WEST = rouge - cyan -// Y - Y = ZENITH - NADIR = vert - magenta -// Z - Z = NORTH - SOUTH = bleu - jaune - - - - - - - - - - - - - -static void show_user_choices(long model_size_x, - long model_size_y, - long model_size_z, - - GLuint *arrows, - int model_arrows_nb, - - int pref_show_grid) -{ - printf("model + user constraints : space size x,y,z = %ld,%ld,%ld ", model_size_x, model_size_y, model_size_z); - if (model_arrows_nb > 0) printf("[%d] arrows ", model_arrows_nb); - if (pref_show_grid == 1) printf("pref_show_grid = %d <> show no grid ", pref_show_grid); - if (pref_show_grid == 0) printf("pref_show_grid = %d <> show all grids ", pref_show_grid); - if (pref_show_grid == 2) printf("pref_show_grid = %d <> show grid xy ", pref_show_grid); - if (pref_show_grid == 3) printf("pref_show_grid = %d <> show grid xz ", pref_show_grid); - if (pref_show_grid == 5) printf("pref_show_grid = %d <> show grid yz ", pref_show_grid); - if (pref_show_grid == 6) printf("pref_show_grid = %d <> show grids xy & xz ", pref_show_grid); - if (pref_show_grid == 10) printf("pref_show_grid = %d <> show grids xy & yz ", pref_show_grid); - if (pref_show_grid == 15) printf("pref_show_grid = %d <> show grids xz & yz ", pref_show_grid); - if (model_arrows_nb == 0) printf("\n"); - if (model_arrows_nb > 0) printf("\n[ n] load site x y z ---- < arrows array >\ - ------------------------------------------------------------\n"); - for (int i = 0; i < model_arrows_nb; i++) - printf("[%2d] = %2d, %2d, %2d, %2d, %2d \n",\ - i, *(arrows + i * 5 + 0), *(arrows + i * 5 + 1), *(arrows + i * 5 + 2), *(arrows + i * 5 + 3), *(arrows + i * 5 + 4)); -} - - - - - - - - - -bool compute_space_and_arrows(long model_size_x, long model_size_y, long model_size_z, - - GLuint *arrows, int model_arrows_nb, - - int pref_show_grid, - - GLfloat *vertex_origin, GLfloat *colors_origin, - GLuint *lines_origin, GLuint *plans_origin) - -{ - show_user_choices(model_size_x, model_size_y, model_size_z, - arrows, model_arrows_nb, - pref_show_grid); - - grids_intersections (model_size_x, model_size_y, model_size_z, vertex_origin, colors_origin); - arrows_anchors (model_size_x, model_size_y, model_size_z, vertex_origin, colors_origin); - - long step_z = 1, - step_y = model_size_z + 1, - step_x = (model_size_z + 1) * (model_size_y + 1); - - draw_grids_3D (model_size_x, model_size_y, model_size_z, step_x, step_y, step_z, lines_origin, pref_show_grid); - if (pref_show_grid > 0) draw_ridges_3D (model_size_x, model_size_y, model_size_z, step_x, step_y, step_z, lines_origin); - - long s = step_z * model_size_z + step_y * model_size_y + step_x * model_size_x; - - draw_some_arrows_demo (lines_origin, s, model_size_x, model_size_y, model_size_z, arrows, model_arrows_nb); - draw_some_arrows (lines_origin, s, model_size_z * model_size_y, model_size_z, arrows, model_arrows_nb); - - return 0; -} - -// X - X = EAST - WEST = rouge - cyan -// Y - Y = ZENITH - NADIR = vert - magenta -// Z - Z = NORTH - SOUTH = bleu - jaune - diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index c581c1d..813b8e9 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -23,28 +23,58 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -//#include "../../include/base.h" +#include "../../include/base.h" #include "../../include/ui.h" #include "../../include/graphics.h" -#include "../../include/buffers.h" +#include "../../include/arrows.h" +#include "../../include/grid.h" #define VERTEX_SHADER_FILE "src/shaders/shader.vert" #define FRAG_SHADER_FILE "src/shaders/shader.frag" -/* ----------------- V A R I A B L E S G L O B A L E S ------------------ */ +static GLuint arrows[] = { + 1, 0, 1, 2, 4, + 1, 1, 1, 2, 4, + 1, 2, 1, 2, 4, + 1, 3, 1, 2, 4, + 1, 4, 1, 2, 4, + 1, 5, 1, 2, 4, + + 1, 1, 0, 0, 0, + 1, 3, 0, 0, 0, + 1, 5, 0, 0, 0, + + 1, 0, 2, 0, 0, + 1, 3, 2, 0, 0, + 1, 5, 2, 0, 0, + + 1, 1, 0, 4, 0, + 1, 2, 0, 4, 0, + 1, 5, 0, 4, 0, + + 1, 1, 0, 0, 6, + 1, 3, 0, 0, 6, + 1, 4, 0, 0, 6, + + 1, 0, 2, 4, 0, + 1, 2, 2, 4, 0, + 1, 5, 2, 4, 0, + + 1, 0, 2, 0, 6, + 1, 3, 2, 0, 6, + 1, 4, 2, 0, 6, + + 1, 1, 0, 4, 6, + 1, 2, 0, 4, 6, + 1, 4, 0, 4, 6, + + + 1, 0, 2, 4, 6, + 1, 2, 2, 4, 6, + 1, 4, 2, 4, 6, + +// load, site, x, y, z +}; static int model_space_size_x = 0; static int model_space_size_y = 0; @@ -52,7 +82,6 @@ static int model_space_size_z = 0; static int model_arrows_nb = 0; static int central_stars_nb = 0; - static GLfloat *buffer_vertex_origin = NULL; static GLfloat *buffer_colors_origin = NULL; static GLuint *buffer_lines_origin = NULL; @@ -60,46 +89,17 @@ static GLuint *buffer_plans_origin = NULL; static int pref_show_grid = 0; -static int buffer_vertex_size = 0; +static volatile int buffer_vertex_size = 0; static int buffer_colors_size = 0; static int buffer_lines_size = 0; static int buffer_plans_size = 0; +/* + * Dynamic array of ptrs to dynamically allocated gl_area_entry + */ +struct gl_area_entry **gl_area_array = NULL; - -static void set_vertex_xyz(float x, float y, float z){ - // *(buffer_vertex_origin + buffer_vertex_size) = x; buffer_vertex_size ++; - // *(buffer_vertex_origin + buffer_vertex_size) = y; buffer_vertex_size ++; - // *(buffer_vertex_origin + buffer_vertex_size) = z; buffer_vertex_size ++; -} - -static void set_color_xyz(float x, float y, float z){ - // *(buffer_colors_origin + buffer_colors_size) = x; buffer_colors_size ++; - // *(buffer_colors_origin + buffer_colors_size) = y; buffer_colors_size ++; - // *(buffer_colors_origin + buffer_colors_size) = z; buffer_colors_size ++; -} - -static void draw_triangle(long a, long b, long c){ - // *(buffer_plans_origin + buffer_plans_size) = a; buffer_plans_size ++; - // *(buffer_plans_origin + buffer_plans_size) = b; buffer_plans_size ++; - // *(buffer_plans_origin + buffer_plans_size) = c; buffer_plans_size ++; -} - -void draw_line (int a, int b) -{ - buffer_lines_origin = g_realloc (buffer_lines_origin, - (buffer_lines_size + 2) * sizeof(GLuint)); - - assert (buffer_lines_origin); - - buffer_lines_origin[buffer_lines_size + 0] = a; - buffer_lines_origin[buffer_lines_size + 1] = b; - - buffer_lines_size += 2; -} - - -/* ----------------- O P E N G L D E B U G M E S S A G E S ------------ */ +/* -------------------------------------------------------------------------- */ /* * Prints verbose human-readable debug messages @@ -162,270 +162,156 @@ static void graphics_debug_callback(GLenum source, GLenum type, GLuint id, /* -------------------------------------------------------------------------- */ -/* - * Dynamic array of ptrs to dynamically allocated gl_area_entry - */ -struct gl_area_entry **gl_area_array = NULL; - - -/* - * Find gl_area_entry from a gl_area ptr - * - * @param gl_area, ptr to the gl_area widget - * - * - * @return entry ptr - */ -struct gl_area_entry *find_entry_from_ptr(const char *name) +static void get_model_data_and_user_preferences() { - struct gl_area_entry **cur; + model_space_size_x = 3; // 0 < model_space_size_x + model_space_size_y = 5; // 0 < model_space_size_y + model_space_size_z = 7; // 0 < model_space_size_z - // Check uninitialized and quit - if (gl_area_array == NULL) return NULL; + model_arrows_nb = sizeof(arrows) / (5 * 4); + // assert : l'emplacement des flèches est contraint + // par model_space_size_x, y, z et le nombre de sites - // Seek in array - cur = gl_area_array; - while(*cur) { - if (strncmp((*cur)->name, name, sizeof((*cur)->name)) == 0) return *cur; - cur++; - } + central_stars_nb = 0; // à calculer TODO + // ! WARNING ! Pour l'instant égal au nombre de flèches ! (central stars réécrites) + central_stars_nb = model_arrows_nb; - // Nothing found - return NULL; + pref_show_grid = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc + // xyz, 0, x, y, z, xy, xz, yz, xyz +} + +/* -------------------------------------------------------------------------- */ + +/* + * Writes values to describe a line from a to b into the line buffer + * + * @param coords GLuint (a,b) + * + * @return void + */ +void graphics_write_line (GLuint a, GLuint b) +{ + buffer_lines_origin = g_realloc (buffer_lines_origin, + (buffer_lines_size + 2) * sizeof(GLuint)); + + assert (buffer_lines_origin); + + buffer_lines_origin[buffer_lines_size + 0] = a; + buffer_lines_origin[buffer_lines_size + 1] = b; + + buffer_lines_size += 2; } /* - * Find the gl_area_entry size + * Writes values to describe a vertex at (x,y,z) into the vertex buffer * - * @param void + * @param coords GLfloat(x,y,z) * - * @return size of array + * @return void */ -static inline short gl_area_size(void) +void graphics_write_vertex (GLfloat x, GLfloat y, GLfloat z) { - struct gl_area_entry **cur; - short size; + buffer_vertex_origin = g_realloc (buffer_vertex_origin, + (buffer_vertex_size + 3) * sizeof(GLfloat)); - // Check uninitialized and quit - if (gl_area_array == NULL) return 0; + assert (buffer_vertex_origin); - cur = gl_area_array; - while(*cur) { - cur++; - size++; - } + buffer_vertex_origin[buffer_vertex_size + 0] = x; + buffer_vertex_origin[buffer_vertex_size + 1] = y; + buffer_vertex_origin[buffer_vertex_size + 2] = z; - return size; + buffer_vertex_size += 3; + + printf("Created vertex (%f,%f,%f), size is %d\n", x,y,z, buffer_vertex_size); } /* - * Initializes a gl_area + * Writes values to describe a color (r,g,b) into the color buffer * - * @param gl_area, ptr to the gl_area widget + * @param color GLfloat(r,g,b) * - * @return true if initialized + * @return void */ -bool graphics_init(const char *gl_area) +void graphics_write_color (GLfloat r, GLfloat g, GLfloat b) { - short array_size; + buffer_colors_origin = g_realloc (buffer_colors_origin, + (buffer_colors_size + 3) * sizeof(GLfloat)); - glEnable(GL_DEBUG_OUTPUT); - glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); - glEnable(GL_MULTISAMPLE); + assert (buffer_colors_origin); - // Check if not already initialized - if (find_entry_from_ptr(gl_area)) { - errno = EEXIST; - perror("gl_area_array already exists"); - return false; - } + buffer_colors_origin[buffer_colors_size + 0] = r; + buffer_colors_origin[buffer_colors_size + 1] = g; + buffer_colors_origin[buffer_colors_size + 2] = b; - // Get actual array size - array_size = gl_area_size(); - - // If it does not exist, allocs it - if (gl_area_array == NULL) { - gl_area_array = g_malloc0(sizeof(struct gl_area_entry *) * 2); - - // If it does exist, g_reallocs it - } else { - gl_area_array = g_realloc(gl_area_array, - (array_size + 1) - * sizeof(struct gl_area_entry *)); - if (gl_area_array == NULL) { - perror("Not enough memory to allocate gl_area_array"); - return false; - } - - explicit_bzero(gl_area_array + array_size * sizeof(struct gl_area_entry *), - sizeof(struct gl_area_entry *)); - } - - // Alloc new entry - gl_area_array[array_size] = g_malloc0(sizeof(struct gl_area_entry)); - - strcpy(gl_area_array[array_size]->name, gl_area); - - if (!graphics_init_shaders(gl_area)) return false; - - graphics_init_buffers(gl_area); - - glDebugMessageCallback(graphics_debug_callback, NULL); - - return true; + buffer_colors_size += 3; } /* - * Shutdowns a gl_area + * Writes values to describe an (a,b,c) plan (triangle) into the plan buffer * - * @param gl_area, ptr to the gl_area widget + * @param coords GLuint (a,b,c) * - * @return true if success + * @return void */ -bool graphics_shutdown(const void *gl_area) +void graphics_write_plan (GLuint a, GLuint b, GLuint c) +{ + buffer_plans_origin = g_realloc (buffer_plans_origin, + (buffer_plans_size + 3) * sizeof(GLuint)); + + assert (buffer_plans_origin); + + buffer_plans_origin[buffer_plans_size + 0] = a; + buffer_plans_origin[buffer_plans_size + 1] = b; + buffer_plans_origin[buffer_plans_size + 2] = c; + + buffer_plans_size += 3; +} + +/* Initializes the buffer of a gl_area + * Calls according to the user preferences + * @param gl_area, ptr to the gl_area widget + * @return void + */ +void graphics_init_buffers(const void *gl_area) { struct gl_area_entry *entry; + entry = graphics_find_glarea_from_ptr(gl_area); - entry = find_entry_from_ptr(gl_area); + get_model_data_and_user_preferences(); - if (entry == NULL) { - errno = EFAULT; - perror("gl_area_array entry not found"); - return false; + //XXX + main_test_graphics (); + + GLuint vao, vertex_buffer, color_buffer; + + glGenBuffers(1, &vertex_buffer); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, buffer_vertex_size * sizeof(buffer_vertex_origin[0]), buffer_vertex_origin, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + + // colors + glGenBuffers(1, &color_buffer); + glBindBuffer(GL_ARRAY_BUFFER, color_buffer); + glBufferData(GL_ARRAY_BUFFER, buffer_colors_size * sizeof(buffer_colors_origin), buffer_colors_origin, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + // We only use one VAO, so we always keep it bound + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + if(entry != NULL) { + entry->vao = vao; + entry->position_buffer = vertex_buffer; + entry->color_buffer = color_buffer; } - - glDeleteBuffers(1, &entry->position_buffer); - glDeleteBuffers(1, &entry->color_buffer); - glDeleteProgram(entry->program); - - // Liberate - g_free(entry); - - entry = NULL; - - gl_area_array = g_realloc(gl_area_array, - (gl_area_size()) - * sizeof(struct gl_area_entry *)); - - g_free(buffer_vertex_origin); - g_free(buffer_colors_origin); - g_free(buffer_lines_origin); - g_free(buffer_plans_origin); - return true; } -/* -------------------------------------------------------------------------- */ - -/* - * Initializes an identity matrix - * - * @param res, target ptr - * - * @return void - */ -static inline void compute_i(float *res) -{ - /* initialize to the identity matrix */ - res[0] = 1.f; res[4] = 0.f; res[8] = 0.f; res[12] = 0.f; - res[1] = 0.f; res[5] = 1.f; res[9] = 0.f; res[13] = 0.f; - res[2] = 0.f; res[6] = 0.f; res[10] = 1.f; res[14] = 0.f; - res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f; -} - -/* - * Computes the projection matrix - * - * @param res, target ptr - * phi, XXX - * theta, XXX - * psi, XXX - * - * @return void - */ -static inline void compute_mvp(float *res, float phi, float theta, float psi) -{ - float x = phi *(G_PI / 180.f); - float y = theta *(G_PI / 180.f); - float z = psi *(G_PI / 180.f); - float c1 = cosf(x), s1 = sinf(x); - float c2 = cosf(y), s2 = sinf(y); - float c3 = cosf(z), s3 = sinf(z); - float c3c2 = c3 * c2; - float s3c1 = s3 * c1; - float c3s2s1 = c3 * s2 * s1; - float s3s1 = s3 * s1; - float c3s2c1 = c3 * s2 * c1; - float s3c2 = s3 * c2; - float c3c1 = c3 * c1; - float s3s2s1 = s3 * s2 * s1; - float c3s1 = c3 * s1; - float s3s2c1 = s3 * s2 * c1; - float c2s1 = c2 * s1; - float c2c1 = c2 * c1; - - compute_i(res); - - /* apply all three rotations using the three matrices: - * - * ⎡ c3 s3 0 ⎤ ⎡ c2 0 -s2 ⎤ ⎡ 1 0 0 ⎤ - * ⎢ -s3 c3 0 ⎥ ⎢ 0 1 0 ⎥ ⎢ 0 c1 s1 ⎥ - * ⎣ 0 0 1 ⎦ ⎣ s2 0 c2 ⎦ ⎣ 0 -s1 c1 ⎦ - */ - res[0] = c3c2; res[4] = s3c1 + c3s2s1; res[8] = s3s1 - c3s2c1; res[12] = 0.f; - res[1] = -s3c2; res[5] = c3c1 - s3s2s1; res[9] = c3s1 + s3s2c1; res[13] = 0.f; - res[2] = s2; res[6] = -c2s1; res[10] = c2c1; res[14] = 0.f; - res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f; -} - -/* - * Created and compile a shader - * - * @param type, shader type - * src, source code of shader - * - * @return shader id - */ -static inline GLuint create_shader(int type, const char *src) -{ - GLuint shader; - int status; - - shader = glCreateShader(type); - glShaderSource(shader, 1, &src, NULL); - glCompileShader(shader); - - glGetShaderiv(shader, GL_COMPILE_STATUS, &status); - if(status == GL_FALSE) { - int log_len; - char *buffer; - - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len); - - 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); - - g_free(buffer); - - glDeleteShader(shader); - - return 0; - } - - return shader; -} - -/* -------------------------------------------------------------------------- */ - /* * Initializes the shaders of a gl_area and link them to a program * * @param gl_area, ptr to the gl_area widget * - * * @return true if initialized */ bool graphics_init_shaders(const void *gl_area) @@ -440,7 +326,7 @@ bool graphics_init_shaders(const void *gl_area) GLuint v = 0; GLuint p = 0; - entry = find_entry_from_ptr(gl_area); + entry = graphics_find_glarea_from_ptr(gl_area); if (entry == NULL) { errno = EFAULT; @@ -531,221 +417,6 @@ bool graphics_init_shaders(const void *gl_area) return true; } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -GLuint arrows[] = { - 1, 0, 0, 0, 0, - 1, 1, 0, 0, 0, - 1, 2, 0, 0, 0, - 1, 3, 0, 0, 0, - 1, 4, 0, 0, 0, - 1, 5, 0, 0, 0, - /* 1, 1, 1, 0, 0, */ - /* 1, 2, 2, 1, 1, */ - /* 1, 3, 2, 2, 1, */ - /* 1, 4, 3, 0, 1, */ - /* 1, 5, 3, 0, 2, */ - /* 1, 1, 3, 0, 2, */ - /* 1, 0, 2, 0, 2, */ - /* 1, 5, 2, 1, 1, */ -// load, site, x, y, z -}; - -static void get_model_data_and_user_preferences(){ - - - model_space_size_x = 1; // 0 < model_space_size_x - model_space_size_y = 1; // 0 < model_space_size_y - model_space_size_z = 1; // 0 < model_space_size_z - - // XXX ONLY space drawed, no arrows yet - - model_arrows_nb = sizeof(arrows) / (5 * 4); // assert : l'emplacement des flèches est contraint - // par model_space_size_x, y, z et le nombre de sites - - central_stars_nb = 0; // à calculer TODO - // ! WARNING ! Pour l'instant égal au nombre de flèches ! (central stars réécrites) - central_stars_nb = model_arrows_nb; - - pref_show_grid = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc - // xyz, 0, x, y, z, xy, xz, yz, xyz -} - - - - - - - -static void compute_buffers_sizes(int model_space_size_x, - int model_space_size_y, - int model_space_size_z, - - int model_arrows_nb) -{ - long grids_intersections = (model_space_size_x + 1) - * (model_space_size_y + 1) - * (model_space_size_z + 1); - - long arrows_anchors = (model_space_size_x) - * (model_space_size_y) - * (model_space_size_z) - * (6 + 6); // 6 is to draw the small central star - // 6 is for the six faces centers - - long cubes_nb = model_space_size_x * model_space_size_y * model_space_size_z; - - buffer_vertex_size = grids_intersections * 3; // 3 numbers per vertex - - buffer_vertex_size += arrows_anchors * 3; - - - buffer_colors_size = buffer_vertex_size; - - buffer_plans_size = 0; - - long grids_lines = - (pref_show_grid % 2 == 0) * (model_space_size_x + 1) * (model_space_size_y + 1) - + (pref_show_grid % 3 == 0) * (model_space_size_x + 1) * (model_space_size_z + 1) - + (pref_show_grid % 5 == 0) * (model_space_size_y + 1) * (model_space_size_z + 1); - - if (pref_show_grid != 1) inc_buffer_lines_size(grids_lines * 2); - if (pref_show_grid > 0) inc_buffer_lines_size(12 * 2); // space ridges - // sinon, les arêtes de l'espace sont tracées par les grilles - - printf("allocated buffers sizes :%4d/3 = %3d vertices, %4d/3 = %3d colors,\ - %4d/2 = %3d lines, %4d/3 = %3d plans.\n", - buffer_vertex_size, buffer_vertex_size / 3, - buffer_colors_size, buffer_colors_size / 3, - buffer_lines_size, buffer_lines_size / 2, - buffer_plans_size, buffer_plans_size / 3); -} - - - - - - - - - - - - - - - - - - - - - - - - - -/* Initializes the buffer of a gl_area - * Calls according to the user preferences - * @param gl_area, ptr to the gl_area widget - * Note : buffer_lines_origin[] is no more defined in graphics_cube.h - * @return void - */ -#define A 0 -void graphics_init_buffers(const void *gl_area) -{ - struct gl_area_entry *entry; - entry = find_entry_from_ptr(gl_area); - - get_model_data_and_user_preferences(); - - compute_buffers_sizes (model_space_size_x, - model_space_size_y, - model_space_size_z, - - model_arrows_nb); - - buffer_vertex_origin = g_malloc0 (buffer_vertex_size * sizeof(GLfloat) * 2); - buffer_colors_origin = g_malloc0 (buffer_colors_size * sizeof(GLfloat) * 2); - buffer_lines_origin = g_malloc0 (buffer_lines_size * 2 * sizeof(GLuint) * 2); - buffer_plans_origin = g_malloc0 (buffer_plans_size * 3 * sizeof(GLuint) * 2); - - compute_space_and_arrows (model_space_size_x, - model_space_size_y, - model_space_size_z, - - arrows, - model_arrows_nb, - - pref_show_grid, - - buffer_vertex_origin, - buffer_colors_origin, - buffer_lines_origin, - buffer_plans_origin); - - GLuint vao, vertex_buffer, color_buffer; - - glGenBuffers(1, &vertex_buffer); - glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, buffer_vertex_size * sizeof(buffer_vertex_origin[0]), buffer_vertex_origin, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - - - // colors - glGenBuffers(1, &color_buffer); - glBindBuffer(GL_ARRAY_BUFFER, color_buffer); - glBufferData(GL_ARRAY_BUFFER, buffer_colors_size * sizeof(buffer_colors_origin), buffer_colors_origin, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - - // We only use one VAO, so we always keep it bound - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - if(entry != NULL) { - entry->vao = vao; - entry->position_buffer = vertex_buffer; - entry->color_buffer = color_buffer; - } -} - /* * Draws the current buffer to a gl_area * @@ -760,7 +431,7 @@ void graphics_draw(const void *gl_area) float p[16]; struct gl_area_entry *entry; - entry = find_entry_from_ptr(gl_area); + entry = graphics_find_glarea_from_ptr(gl_area); if (entry == NULL) { errno = EFAULT; @@ -814,4 +485,237 @@ void graphics_draw(const void *gl_area) glFlush(); } +/* -------------------------------------------------------------------------- */ + +/* + * DEBUG + * + */ +static void show_user_choices(long model_space_size_x, + long model_space_size_y, + long model_space_size_z, + + GLuint *arrows, + int model_arrows_nb, + + int pref_show_grid) +{ + printf("model + user constraints : space size x,y,z = %ld,%ld,%ld ", model_space_size_x, model_space_size_y, model_space_size_z); + if (model_arrows_nb > 0) printf("[%d] arrows ", model_arrows_nb); + if (pref_show_grid == 1) printf("pref_show_grid = %d <> show no grid ", pref_show_grid); + if (pref_show_grid == 0) printf("pref_show_grid = %d <> show all grids ", pref_show_grid); + if (pref_show_grid == 2) printf("pref_show_grid = %d <> show grid xy ", pref_show_grid); + if (pref_show_grid == 3) printf("pref_show_grid = %d <> show grid xz ", pref_show_grid); + if (pref_show_grid == 5) printf("pref_show_grid = %d <> show grid yz ", pref_show_grid); + if (pref_show_grid == 6) printf("pref_show_grid = %d <> show grids xy & xz ", pref_show_grid); + if (pref_show_grid == 10) printf("pref_show_grid = %d <> show grids xy & yz ", pref_show_grid); + if (pref_show_grid == 15) printf("pref_show_grid = %d <> show grids xz & yz ", pref_show_grid); + if (model_arrows_nb == 0) printf("\tbuffer_vertex_size = %d\t lines = %d\n",\ + buffer_vertex_size, buffer_lines_size); + if (model_arrows_nb > 0) printf("\n[ n] load site x y z ---- < arrows array >\ + ------------------------------------------------------------\n"); + for (int i = 0; i < model_arrows_nb; i++) + printf("[%2d] = %2d, %2d, %2d, %2d, %2d \n",\ + i, *(arrows + i * 5 + 0), *(arrows + i * 5 + 1), *(arrows + i * 5 + 2), *(arrows + i * 5 + 3), *(arrows + i * 5 + 4)); +} + +/* + * Find gl_area_entry from a gl_area ptr + * + * @param gl_area, ptr to the gl_area widget + * + * + * @return entry ptr + */ +struct gl_area_entry *graphics_find_glarea_from_ptr(const char *name) +{ + struct gl_area_entry **cur; + + // Check uninitialized and quit + if (gl_area_array == NULL) return NULL; + + // Seek in array + cur = gl_area_array; + while(*cur) { + if (strncmp((*cur)->name, name, sizeof((*cur)->name)) == 0) return *cur; + cur++; + } + + // Nothing found + return NULL; +} + +/* + * Initializes a gl_area + * + * @param gl_area, ptr to the gl_area widget + * + * @return true if initialized + */ +bool graphics_init(const char *gl_area) +{ + short array_size; + + glEnable(GL_DEBUG_OUTPUT); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); + glEnable(GL_MULTISAMPLE); + + // Check if not already initialized + if (graphics_find_glarea_from_ptr(gl_area)) { + errno = EEXIST; + perror("gl_area_array already exists"); + return false; + } + + // Get actual array size + array_size = gl_area_size(); + + // If it does not exist, allocs it + if (gl_area_array == NULL) { + gl_area_array = g_malloc0(sizeof(struct gl_area_entry *) * 2); + + // If it does exist, g_reallocs it + } else { + gl_area_array = g_realloc(gl_area_array, + (array_size + 1) + * sizeof(struct gl_area_entry *)); + if (gl_area_array == NULL) { + perror("Not enough memory to allocate gl_area_array"); + return false; + } + + explicit_bzero(gl_area_array + array_size * sizeof(struct gl_area_entry *), + sizeof(struct gl_area_entry *)); + } + + // Alloc new entry + gl_area_array[array_size] = g_malloc0(sizeof(struct gl_area_entry)); + + strcpy(gl_area_array[array_size]->name, gl_area); + + if (!graphics_init_shaders(gl_area)) return false; + + graphics_init_buffers(gl_area); + + glDebugMessageCallback(graphics_debug_callback, NULL); + + return true; +} + +/* + * Shutdowns a gl_area + * + * @param gl_area, ptr to the gl_area widget + * + * @return true if success + */ +bool graphics_shutdown(const void *gl_area) +{ + struct gl_area_entry *entry; + + entry = graphics_find_glarea_from_ptr(gl_area); + + if (entry == NULL) { + errno = EFAULT; + perror("gl_area_array entry not found"); + return false; + } + + glDeleteBuffers(1, &entry->position_buffer); + glDeleteBuffers(1, &entry->color_buffer); + glDeleteProgram(entry->program); + + // Liberate + g_free(entry); + + entry = NULL; + + gl_area_array = g_realloc(gl_area_array, + (gl_area_size()) + * sizeof(struct gl_area_entry *)); + + g_free(buffer_vertex_origin); + g_free(buffer_colors_origin); + g_free(buffer_lines_origin); + g_free(buffer_plans_origin); + return true; +} + +/* -------------------------------------------------------------------------- */ + +void main_test_graphics (void) +{ + long stx = model_space_size_z * model_space_size_y, + sty = model_space_size_z, + stz = 1; + + long site = 0, x = 0, y = 0, z = 0; + + show_user_choices(model_space_size_x, model_space_size_y, model_space_size_z, + arrows, model_arrows_nb, + pref_show_grid); + + printf("offset_before_grid = %d\n", buffer_vertex_size / 3); + + /* GRID */ + + grid_write_intersections (model_space_size_x, model_space_size_y, model_space_size_z); + + if (pref_show_grid % 2 == 0) + grid_write_x (model_space_size_x, model_space_size_y, model_space_size_z); + if (pref_show_grid % 3 == 0) + grid_write_y (model_space_size_x, model_space_size_y, model_space_size_z); + if (pref_show_grid % 5 == 0) + grid_write_z (model_space_size_x, model_space_size_y, model_space_size_z); + + if (pref_show_grid > 0) + grid_write_ridges (model_space_size_x, model_space_size_y, model_space_size_z); + + int offset_after_grid = buffer_vertex_size / 3; + printf("offset_after_grid = %d\n", offset_after_grid); + + /* ARROWS */ + arrows_write_terminations (model_space_size_x, model_space_size_y, model_space_size_z); + + for (int i = 0; i < model_arrows_nb; i++) { + site = arrows[i * 5 + 1]; + x = arrows[i * 5 + 2]; + y = arrows[i * 5 + 3]; + z = arrows[i * 5 + 4]; + + // printf("[%ld]\tsite = %d\tx = %ld\ty = %ld\tz = %ld\tstep x = %ld\tstep y = %ld\n", site, i, x, y, z, stx, sty); + + arrows_write_basis (offset_after_grid + 12 * (stx * x + sty * y + stz * z)); // XXX 1 + 12 ? + + switch(site){ + case EAST: + write_arrow_lines_east(offset_after_grid + 12 * (stx * x + sty * y + stz * z), 1, site); + break; + case WEST: + write_arrow_lines_west(offset_after_grid + 12 * (stx * x + sty * y + stz * z), 1, site); + break; + case ZENITH: + write_arrow_lines_zenith(offset_after_grid + 12 * (stx * x + sty * y + stz * z), 1, site); + break; + case NADIR: + write_arrow_lines_nadir(offset_after_grid + 12 * (stx * x + sty * y + stz * z), 1, site); + break; + case SOUTH: + write_arrow_lines_south(offset_after_grid + 12 * (stx * x + sty * y + stz * z), 1, site); + break; + case NORTH: + write_arrow_lines_north(offset_after_grid + 12 * (stx * x + sty * y + stz * z), 1, site); + break; + default: + // XXX ? + break; + } + } + + show_user_choices(model_space_size_x, model_space_size_y, model_space_size_z, + arrows, model_arrows_nb, + pref_show_grid); + +} + diff --git a/src/graphics/grid.c b/src/graphics/grid.c new file mode 100644 index 0000000..4c632bd --- /dev/null +++ b/src/graphics/grid.c @@ -0,0 +1,169 @@ +/* + * Gem-graph + * + * Desc: OpenGL grid functions + * + * Copyright (C) 2023 Jean Sirmai + * Copyright (C) 2023 Adrien Bourmault + * + * This file is part of Gem-graph. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * aint with this program. If not, see . + */ + +#include "../../include/base.h" +#include "../../include/graphics.h" + +/* + * Writes grid lines intersections to vertex and color buffers + * + * @param coords long (x,y,z) + * + * @return void + */ +void grid_write_intersections (long x, long y, long z) +{ + + float i, j, k, vx, vy, vz, max = fmax(x, y); + max = fmax(max, z); + + for (i = 0; i <= x; i++) + for (j = 0; j <= y; j++) + for (k = 0; k <= z; k++){ + + vx = (2 * i / x - 1) * x / max; + vy = (2 * j / y - 1) * y / max; + vz = (2 * k / z - 1) * z / max; + + graphics_write_vertex (vx, vy, vz); + graphics_write_color (0.64f,0.64f,0.64f); + }; +} + +/* + * Writes grid ridges to vertex and color buffers + * + * @param coords long (x,y,z), step_x, step_y, step_z + * + * @return void + */ +void grid_write_ridges (long x, + long y, + long z) +{ + long step_x = (z + 1) * (y + 1), + step_y = z + 1, + step_z = 1; + + graphics_write_line (step_z * 0 + step_y * 0 + step_x * 0, + step_z * 0 + step_y * 0 + step_x * x); + graphics_write_line (step_z * 0 + step_y * 0 + step_x * 0, + step_z * 0 + step_y * y + step_x * 0); + graphics_write_line (step_z * 0 + step_y * 0 + step_x * 0, + step_z * z + step_y * 0 + step_x * 0); + + graphics_write_line (step_z * z + step_y * y + step_x * x, + step_z * 0 + step_y * y + step_x * x); + graphics_write_line (step_z * z + step_y * y + step_x * x, + step_z * z + step_y * y + step_x * 0); + graphics_write_line (step_z * z + step_y * y + step_x * x, + step_z * z + step_y * 0 + step_x * x); + + graphics_write_line (step_z * 0 + step_y * 0 + step_x * x, + step_z * 0 + step_y * y + step_x * x); + graphics_write_line (step_z * 0 + step_y * 0 + step_x * x, + step_z * z + step_y * 0 + step_x * x); + graphics_write_line (step_z * z + step_y * 0 + step_x * 0, + step_z * z + step_y * y + step_x * 0); + + graphics_write_line (step_z * 0 + step_y * y + step_x * 0, + step_z * 0 + step_y * y + step_x * x); + graphics_write_line (step_z * 0 + step_y * y + step_x * 0, + step_z * z + step_y * y + step_x * 0); + graphics_write_line (step_z * z + step_y * 0 + step_x * 0, + step_z * z + step_y * 0 + step_x * x); +} + +/* + * Writes grid lines for x axis to vertex and color buffers + * + * @param coords long (x,y,z) + * + * @return void + */ +void grid_write_x (long x, long y, long z) +{ + long step_z = 1, + step_y = z + 1, + step_x = (z + 1) * (y + 1); + + x ++; y ++; z ++; + float calcul = 0; + + for (int u = 0; u < y; u ++){ + for (int w = 0; w < x; w ++){ + calcul = step_x * w + step_y * u; + graphics_write_line (calcul, calcul + step_y - step_z); + } + } +} + +/* + * Writes grid lines for y axis to vertex and color buffers + * + * @param coords long (x,y,z) + * + * @return void + */ +void grid_write_y (long x, long y, long z) +{ + long step_z = 1, + step_y = z + 1, + step_x = (z + 1) * (y + 1); + + x ++; y ++; z ++; + float calcul = 0; + + for (int u = 0; u < z; u++) { + for (int w = 0; w < x; w ++){ + calcul = step_x * w + step_z * u; + graphics_write_line (calcul, calcul + step_x - step_y); + } + } +} + +/* + * Writes grid lines for z axis to vertex and color buffers + * + * @param coords long (x,y,z) + * + * @return void + */ +void grid_write_z (long x, long y, long z) +{ + long step_z = 1, + step_y = z + 1, + step_x = (z + 1) * (y + 1); + + x ++; y ++; z ++; + float calcul = 0; + + for (int u = 0; u < z; u ++){ + for (int w = 0; w < y; w ++){ + calcul = step_y * w + step_z * u; + graphics_write_line (calcul, calcul + step_x * (x - 1)); + } + } +} + diff --git a/src/graphics/scories.c.forget b/src/graphics/scories.c.forget deleted file mode 100644 index 2c71b83..0000000 --- a/src/graphics/scories.c.forget +++ /dev/null @@ -1,394 +0,0 @@ -/* - * Gem-graph OpenGL experiments - * - * Desc: OpenGL utils header - * - * Copyright (C) 2023 Jean Sirmai - * - * This file was part of Gem-graph. Forget it now. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -//#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../../include/ui.h" -#include "../../include/graphics.h" -#include "../../include/buffers.h" - -static long vertex_index = 0; -static long colors_index = 0; -static long lines_index = 0; -static long plans_index = 0; - - -#define V 0 - -static bool compute_space_2D(int size_x, - int size_y, - int size_z, - - int pref_mark_unit_space, - int pref_style_lines_planes, - int pref_style_mix_colors, - - GLfloat *vertex_origin, - GLfloat *colors_origin, - GLuint *lines_origin, - GLuint *plans_origin) -{ - - float x = 0; - // for (int k = 0; k < size_x + 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("space_2D vertex "); - for (int k = 0; k < size_x; k++){ // barres verticales - // - x = ((size_x % 2) * (size_x / 2 - k) - + (size_x % 2 - 1) * (k + 0.5f - size_x / 2)) / size_x * 2; - - if (V) printf("[%1.1f] ", x); - - *(vertex_origin + k * 6 + 0) = - x; - *(vertex_origin + k * 6 + 1) = 1.0f / size_x; - *(vertex_origin + k * 6 + 2) = 0.0f; - - *(vertex_origin + k * 6 + 3) = - x; - *(vertex_origin + k * 6 + 4) = - 1.0f / size_x; - *(vertex_origin + k * 6 + 5) = 0.0f; - - // fun > if (k == size_x - 1) *(vertex_origin + k * 6 + 5) = 0.1f; - - vertex_index += 6; if (V) printf(" => vertex_index = %3ld space 2D\n", vertex_index); - - *(colors_origin + k * 6 + 0) = 1; - *(colors_origin + k * 6 + 1) = 0; - *(colors_origin + k * 6 + 2) = 0; - - *(colors_origin + k * 6 + 3) = 1; - *(colors_origin + k * 6 + 4) = 1; - *(colors_origin + k * 6 + 5) = 0; - - colors_index += 6; - - *(lines_origin + k * 2 + 0) = k * 2 + 0; - *(lines_origin + k * 2 + 1) = k * 2 + 1; - - lines_index += 2; if (V) printf(" => lines_index = %3ld space 2D\n", lines_index); - } - - *(colors_origin + 0) = 0; - *(colors_origin + 1) = 0; - *(colors_origin + 2) = 0; - - *(colors_origin + 3) = 1; - *(colors_origin + 4) = 1; - *(colors_origin + 5) = 1; - - *(colors_origin + (size_x - 1) * 6 + 0) = 1; - *(colors_origin + (size_x - 1) * 6 + 1) = 1; - *(colors_origin + (size_x - 1) * 6 + 2) = 1; - - *(colors_origin + (size_x - 1) * 6 + 3) = 0; - *(colors_origin + (size_x - 1) * 6 + 4) = 0; - *(colors_origin + (size_x - 1) * 6 + 5) = 0; - - colors_index += 12; if (V) printf(" => colors_index = %ld\n", colors_index); - - *(lines_origin + lines_index + 0) = 0; - *(lines_origin + lines_index + 1) = size_x * 2 - 2; // barre horizontale du bas - - *(lines_origin + lines_index + 2) = 1; - *(lines_origin + lines_index + 3) = size_x * 2 - 1; // barre horizontale du haut - - lines_index += 4; if (V) printf("H=> lines_index = %3ld space 2D H\n", lines_index); - - if (pref_mark_unit_space == 1 || pref_mark_unit_space == 3) // diagonales to mark first space unit - { - *(lines_origin + lines_index + 0) = 0; - *(lines_origin + lines_index + 1) = 3; - - *(lines_origin + lines_index + 2) = 1; - *(lines_origin + lines_index + 3) = 2; - - lines_index += 4; if (V) printf("X=> lines_index = %3ld space 2D X\n", lines_index); - - } - - if (pref_mark_unit_space == 2 || pref_mark_unit_space == 3) // diagonales to mark last space unit - { - *(lines_origin + lines_index + 0) = (size_x - 2) * 2 + 0; - *(lines_origin + lines_index + 1) = (size_x - 2) * 2 + 3; - - *(lines_origin + lines_index + 2) = (size_x - 2) * 2 + 1; - *(lines_origin + lines_index + 3) = (size_x - 2) * 2 + 2; - - lines_index +=4; if (V) printf("X=> lines_index = %3ld space 2D X\n", lines_index); - - } - - if (V) printf(" n = %d x 2 côté = [%1.1f]\n", size_x + 1, 2.0f / size_x); - if (V) printf("space_2D lines_origin (%d - size_x(0)) x (%d - size_x(1)) ", 0, 1); - for (int v = 0; v < size_x + 2; v++) - if (V) printf("(%d-%d) ", *(lines_origin + v), *(lines_origin + v + 1)); - if (V) printf(" n = 4 + (%d x 2)\n", size_x + 2); - - return 1; -} - -static int compute_arrow_2D(int size_x, - int size_y, - int size_z, - - int weight, - int site, - int x, - int y, - int z, - - int pref_style_lines_planes, - int pref_style_mix_colors, - - GLfloat *vertex_origin, - GLfloat *colors_origin, - GLuint *lines_origin, - GLuint *plans_origin) -{ - printf("compute_arrow_2D load = %d site = %d x = %d\n", weight, site, x); - float zero = 0.0f; - float center = (1.0f / size_x) * (2 * x - size_x + 2); - float tip = center + (2 * site - 1) * (1.0f / size_x - 0.01f); - float base = center + (2 * site - 1) * (0.1f / size_x); - float lat = 0.4f / size_x; - - /* for (int i = 0; i < size_x; i++) */ - /* for (int j = 0; j < size_y; j++) */ - /* for (int k = 0; k < size_z; k++) */ - /* { */ - - /* x = - ((size_x % 2) * (size_x / 2 - i) */ - /* - (size_x % 2 - 1) * (i + 0.5f - size_x / 2)) / size_x * 2 * EDGE; */ - /* y = - ((size_x % 2) * (size_x / 2 - j) */ - /* - (size_x % 2 - 1) * (j + 0.5f - size_x / 2)) / size_x * 2 * EDGE; */ - /* z = - ((size_x % 2) * (size_x / 2 - k) */ - /* - (size_x % 2 - 1) * (k + 0.5f - size_x / 2)) / size_x * 2 * EDGE; */ - /* } */ - - *(vertex_origin + vertex_index + 0) = tip; - *(vertex_origin + vertex_index + 1) = zero; - *(vertex_origin + vertex_index + 2) = zero; - - *(vertex_origin + vertex_index + 3) = base; - *(vertex_origin + vertex_index + 4) = lat; - *(vertex_origin + vertex_index + 5) = zero; - - *(vertex_origin + vertex_index + 6) = base; - *(vertex_origin + vertex_index + 7) = - lat; - *(vertex_origin + vertex_index + 8) = zero; - - vertex_index += 9; if (V) printf(" => vertex_index = %3ld arrow_2D\n", vertex_index); - - if (pref_style_lines_planes == 0) { - - if (V) printf("0=> lines_index = %ld\n", lines_index); - - *(lines_origin + lines_index + 0) = vertex_index / 3 - 3; - *(lines_origin + lines_index + 1) = vertex_index / 3 - 2; - - *(lines_origin + lines_index + 2) = vertex_index / 3 - 3; - *(lines_origin + lines_index + 3) = vertex_index / 3 - 1; - - *(lines_origin + lines_index + 4) = vertex_index / 3 - 2; - *(lines_origin + lines_index + 5) = vertex_index / 3 - 1; - - lines_index += 6; if (V) printf("|=> lines_index = %3ld arrow_2D\n", lines_index); - } - - - if (pref_style_lines_planes == 1) { - *(plans_origin + plans_index + 0) = vertex_index / 3 + 0; - *(plans_origin + plans_index + 1) = vertex_index / 3 + 1; - *(plans_origin + plans_index + 2) = vertex_index / 3 + 2; - - *(plans_origin + plans_index + 3) = vertex_index / 3 + 0; - *(plans_origin + plans_index + 4) = vertex_index / 3 + 1; - *(plans_origin + plans_index + 5) = vertex_index / 3 + 2; - - plans_index += 6; if (V) printf("|=> plans_index = %ld\n", plans_index); - } - return 0; -} - - /* x = - ((size_x % 2) * (size_x / 2 - i) */ - /* - (size_x % 2 - 1) * (i + 0.5f - size_x / 2)) / size_x * 2 * EDGE; */ - /* y = - ((size_y % 2) * (size_y / 2 - j) */ - /* - (size_y % 2 - 1) * (j + 0.5f - size_y / 2)) / size_y * 2 * EDGE; */ - /* z = - ((size_z % 2) * (size_z / 2 - k) */ - /* - (size_z % 2 - 1) * (k + 0.5f - size_z / 2)) / size_z * 2 * EDGE; */ - -static bool compute_arrow_3D(long size_x, - long size_y, - long size_z, - - int weight, - int site, // North, South, East, West, Zenith, nAdir (NSEWZA) - long x, - long y, - long z, - - int pref_style_lines_planes, - int pref_style_mix_colors, - - GLfloat *vertex_origin, - GLfloat *colors_origin, - GLuint *lines_origin, - GLuint *plans_origin) - -{ - float zero = 0.0f; - float center = (1.0f / size_x) * (2 * x - size_x + 2); - float tip = center + (2 * site - 1) * (1.0f / size_x); - float base = center + (2 * site - 1) * (0.1f / size_x); - - *(vertex_origin + vertex_index + 0) = center + (2 * site - 1) * (1.0f / size_x - 0.01f); - *(vertex_origin + vertex_index + 1) = zero; - *(vertex_origin + vertex_index + 2) = zero; - - *(vertex_origin + vertex_index + 3) = base; - *(vertex_origin + vertex_index + 4) = 0.4f / size_x; - *(vertex_origin + vertex_index + 5) = 0.4f / size_x; - - *(vertex_origin + vertex_index + 6) = base; - *(vertex_origin + vertex_index + 7) = 0.4f / size_x; - *(vertex_origin + vertex_index + 8) = - 0.4f / size_x; - - *(vertex_origin + vertex_index + 9) = base; - *(vertex_origin + vertex_index + 10) = - 0.4f / size_x; - *(vertex_origin + vertex_index + 11) = - 0.4f / size_x; - - *(vertex_origin + vertex_index + 12) = base; - *(vertex_origin + vertex_index + 13) = - 0.4f / size_x; - *(vertex_origin + vertex_index + 14) = 0.4f / size_x; - - vertex_index += 15; if (V) printf(" => vertex_index = %3ld arrow_3D\n", vertex_index); - - if (V) printf("center = %f tip = %f base = %f\n",\ - center, tip, base); // size_x * 12=%d igap=%d - - if (pref_style_lines_planes == 0) { - - *(lines_origin + lines_index + 0) = vertex_index / 3 - 5; - *(lines_origin + lines_index + 1) = vertex_index / 3 - 4; - - *(lines_origin + lines_index + 2) = vertex_index / 3 - 5; - *(lines_origin + lines_index + 3) = vertex_index / 3 - 3; - - *(lines_origin + lines_index + 4) = vertex_index / 3 - 5; - *(lines_origin + lines_index + 5) = vertex_index / 3 - 2; - - *(lines_origin + lines_index + 6) = vertex_index / 3 - 5; - *(lines_origin + lines_index + 7) = vertex_index / 3 - 1; - - *(lines_origin + lines_index + 8) = vertex_index / 3 - 4; - *(lines_origin + lines_index + 9) = vertex_index / 3 - 3; - - *(lines_origin + lines_index + 10) = vertex_index / 3 - 3; - *(lines_origin + lines_index + 11) = vertex_index / 3 - 2; - - *(lines_origin + lines_index + 12) = vertex_index / 3 - 2; - *(lines_origin + lines_index + 13) = vertex_index / 3 - 1; - - *(lines_origin + lines_index + 14) = vertex_index / 3 - 1; - *(lines_origin + lines_index + 15) = vertex_index / 3 - 4; - - lines_index += 16; if (V) printf("|=> lines_index = %3ld arrow_3D\n", lines_index); - } - - if (pref_style_lines_planes == 1) { - - *(plans_origin + plans_index + 0) = size_x * 12 / 3 + 0; - *(plans_origin + plans_index + 1) = size_x * 12 / 3 + 1; - *(plans_origin + plans_index + 2) = size_x * 12 / 3 + 2; - - *(plans_origin + plans_index + 3) = size_x * 12 / 3 + 0; - *(plans_origin + plans_index + 4) = size_x * 12 / 3 + 3; - *(plans_origin + plans_index + 5) = size_x * 12 / 3 + 4; - - *(plans_origin + plans_index + 0) = size_x * 12 / 3 + 0; - *(plans_origin + plans_index + 1) = size_x * 12 / 3 + 1; - *(plans_origin + plans_index + 2) = size_x * 12 / 3 + 3; - - *(plans_origin + plans_index + 3) = size_x * 12 / 3 + 0; - *(plans_origin + plans_index + 4) = size_x * 12 / 3 + 2; - *(plans_origin + plans_index + 5) = size_x * 12 / 3 + 4; - - plans_index += 12; if (V) printf("|=> plans_index = %3ld arrow_3D\n", plans_index); - } - - return 1; -} - -static void old_arrows(int model_arrows_nb, - long model_size_x, long model_size_y, long model_size_z, - int pref_style_lines_planes, int pref_style_mix_colors, - GLuint *arrows, GLfloat *vertex_origin, GLfloat *colors_origin, - GLuint *lines_origin, GLuint *plans_origin) -{ - for (int i = 0; i < model_arrows_nb; i++) - compute_arrow_3D (model_size_x, - model_size_y, - model_size_z, - - *(arrows + i * 5 + 0), // weight (load) - *(arrows + i * 5 + 1), // site - *(arrows + i * 5 + 2), // x - *(arrows + i * 5 + 3), // y - *(arrows + i * 5 + 4), // z - - pref_style_lines_planes, - pref_style_mix_colors, - - vertex_origin, - colors_origin, - lines_origin, - plans_origin); - -} - - - // OLD ARROWS (DEPRECATED) - /* old_arrows (model_arrows_nb, model_size_x, model_size_y, model_size_z, arrows, */ - /* pref_style_lines_planes, pref_style_mix_colors, */ - /* vertex_origin, colors_origin, lines_origin, plans_origin); */ - - - - - From 234d78542f86223eb8e1ee71a2fe4f87014495e8 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 6 Sep 2023 22:19:36 +0200 Subject: [PATCH 2/2] src/graphics/graphics.c: corrected segfault caused by incorrect size for elements --- include/base.h | 1 + include/graphics.h | 2 ++ src/graphics/graphics.c | 10 ++++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/base.h b/include/base.h index 6d3eeaf..24b7329 100644 --- a/include/base.h +++ b/include/base.h @@ -68,6 +68,7 @@ static inline char *read_file(char *filename) filesize = lseek(fd, 0, SEEK_END) + 1 ; contents = g_malloc(filesize * sizeof(char)); + assert (contents); lseek(fd, 0, SEEK_SET); read(fd,contents,filesize); diff --git a/include/graphics.h b/include/graphics.h index 0686350..a066de8 100644 --- a/include/graphics.h +++ b/include/graphics.h @@ -24,6 +24,7 @@ */ #pragma once +#include "base.h" #include #include #include @@ -223,6 +224,7 @@ static inline GLuint create_shader(int type, const char *src) glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len); buffer = g_malloc(log_len + 1); + assert (buffer); glGetShaderInfoLog(shader, log_len, NULL, buffer); g_warning("Compile failure in %s shader:\n%s", diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index 813b8e9..5077374 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -222,7 +222,7 @@ void graphics_write_vertex (GLfloat x, GLfloat y, GLfloat z) buffer_vertex_size += 3; - printf("Created vertex (%f,%f,%f), size is %d\n", x,y,z, buffer_vertex_size); + //printf("Created vertex (%f,%f,%f), size is %d\n", x,y,z, buffer_vertex_size); } /* @@ -293,7 +293,7 @@ void graphics_init_buffers(const void *gl_area) // colors glGenBuffers(1, &color_buffer); glBindBuffer(GL_ARRAY_BUFFER, color_buffer); - glBufferData(GL_ARRAY_BUFFER, buffer_colors_size * sizeof(buffer_colors_origin), buffer_colors_origin, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, buffer_colors_size * sizeof(buffer_colors_origin[0]), buffer_colors_origin, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // We only use one VAO, so we always keep it bound @@ -377,6 +377,7 @@ bool graphics_init_shaders(const void *gl_area) glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len); buffer = g_malloc(log_len + 1); + assert(buffer); glGetProgramInfoLog(program, log_len, NULL, buffer); g_warning("Linking failure:\n%s", buffer); @@ -573,12 +574,14 @@ bool graphics_init(const char *gl_area) // If it does not exist, allocs it if (gl_area_array == NULL) { gl_area_array = g_malloc0(sizeof(struct gl_area_entry *) * 2); + assert(gl_area_array); // If it does exist, g_reallocs it } else { gl_area_array = g_realloc(gl_area_array, (array_size + 1) * sizeof(struct gl_area_entry *)); + assert (gl_area_array); if (gl_area_array == NULL) { perror("Not enough memory to allocate gl_area_array"); return false; @@ -590,6 +593,7 @@ bool graphics_init(const char *gl_area) // Alloc new entry gl_area_array[array_size] = g_malloc0(sizeof(struct gl_area_entry)); + assert(gl_area_array[array_size]); strcpy(gl_area_array[array_size]->name, gl_area); @@ -633,6 +637,7 @@ bool graphics_shutdown(const void *gl_area) gl_area_array = g_realloc(gl_area_array, (gl_area_size()) * sizeof(struct gl_area_entry *)); + assert (gl_area_array || !gl_area_size()); g_free(buffer_vertex_origin); g_free(buffer_colors_origin); @@ -716,6 +721,7 @@ void main_test_graphics (void) arrows, model_arrows_nb, pref_show_grid); + printf("main_test_graphics [ok]\n"); }