src/graphics/*: Reworked modularity, buffer allocation and reallocation of lines buffer

Acked-by: Adrien 'neox' Bourmault <neox@a-lec.org>
This commit is contained in:
Jean Sirmai 2023-09-05 15:00:48 +02:00 committed by Adrien 'neox' Bourmault
parent 5549f334b8
commit 2732ec3289
Signed by: neox
GPG Key ID: 2974E1D5F25DFCC8
10 changed files with 1306 additions and 1537 deletions

View File

@ -0,0 +1,143 @@
static GLuint *buffer_lines_origin = NULL;
---
long get_buffer_lines_size() {return buffer_lines_size / 2;}
void inc_buffer_lines_size(long inc) {buffer_lines_size += inc * 2;}
void dec_buffer_lines_size(long dec) {buffer_lines_size -= dec * 2;}
void xxx_draw_line (GLuint *lines_origin, int a, int b, long lines_index)
{
// https://docs.gtk.org/glib/func.realloc.html
printf("xxx_draw_line %ld\n", lines_index);
buffer_lines_origin = g_realloc(lines_origin, sizeof(GLuint) * 2);
*(lines_origin + lines_index + 0) = a; // lines_index ++;
*(lines_origin + lines_index + 1) = b; // lines_index ++;
}
/* (gdb) help
List of classes of commands:
aliases -- User-defined aliases of other commands.
breakpoints -- Making program stop at certain points.
data -- Examining data.
files -- Specifying and examining files.
internals -- Maintenance commands.
obscure -- Obscure features.
running -- Running the program.
stack -- Examining the stack.
status -- Status inquiries.
support -- Support facilities.
text-user-interface -- TUI is the GDB text based interface.
tracepoints -- Tracing of program execution without stopping the program.
user-defined -- User-defined commands.
*/
-------------------------------------------------------------------------------------------
GLuint arrows[] = {
1, 0, 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); // 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_mark_unit_space = 0; // 0 = no marks, 1 = 1st, 2 = last, 3 = both
// pref_style_lines_planes = 0; // 0 = arrows as lines, 1 = as planes, 2 = mix
// pref_style_mix_colors = 0; // TODO
pref_show_grid = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc
// xyz, 0, x, y, z, xy, xz, yz, xyz
pref_test_diagonal = 1;
}
-------------------------------------------------------------------------------------------
in buffers.c (line 283)
static void draw_line (GLuint *lines_origin, int a, int b)
{
xxx_draw_line (lines_origin, a, b, lines_index);
lines_index += 2;
// *(lines_origin + lines_index) = a; lines_index ++;
// *(lines_origin + lines_index) = b; lines_index ++;
}
---------------------------------------------------------------------------
[ n] load site x y z ---- < arrows array > ------------------------------------------------------------
[ 0] = 1, 0, 0, 0, 0
[ 1] = 0, 0, 0, 217808480, 32708
[ 2] = 217809344, 32708, 217807968, 32708, 217810080
[ 3] = 32708, -69297984, 32707, 229965808, 32708
[ 4] = 229944176, 32708, 217808608, 32708, 229970384
[ 5] = 32708, 217808576, 32708, 217809024, 32708
[ 6] = 217808992, 32708, -69297888, 32707, 229954976
[ 7] = 32708, 217808512, 32708, 229949456, 32708
[ 8] = 229942736, 32708, 0, 0, 0
[ 9] = 0, 0, 0, 226977440, 32708
[10] = 217810048, 32708, 217808352, 32708, 217808704
[11] = 32708, -69297600, 32707, 217808448, 32708
[12] = 217807808, 32708, 217808896, 32708, 217809312
[13] = 32708, 217807872, 32708, 225561296, 32708
[14] = 217814880, 32708, 217814944, 32708, 217807776
[15] = 32708, 229974752, 32708, 217811040, 32708
[16] = 229947200, 32708, 0, 0, 33506144
[17] = 0, 0, 0, 0, 0
[18] = 33204048, 0, 0, 0, 33587952
[19] = 0, 30217696, 0, 30245088, 0
[ n] load site x y z ---- < arrows array > ------------------------------------------------------------
[ 0] = 1, 0, 0, 0, 0
[ 1] = 0, 0, 0, 2105314912, 32683
[ 2] = 2105315776, 32683, 2105314400, 32683, 2105316512
[ 3] = 32683, 2019551424, 32683, 2117545968, 32683
[ 4] = 2117524336, 32683, 2105315040, 32683, 2117550544
[ 5] = 32683, 2105315008, 32683, 2105315456, 32683
[ 6] = 2105315424, 32683, 2019551520, 32683, 2117535136
[ 7] = 32683, 2105314944, 32683, 2117529616, 32683
[ 8] = 2117522896, 32683, 0, 0, 0
[ 9] = 0, 0, 0, 2114557600, 32683
[10] = 2105316480, 32683, 2105314784, 32683, 2105315136
[11] = 32683, 2019551808, 32683, 2105314880, 32683
[12] = 2105314240, 32683, 2105315328, 32683, 2105315744
[13] = 32683, 2105314304, 32683, 2113141456, 32683
[14] = 2105321312, 32683, 2105321376, 32683, 2105314208
[15] = 32683, 2117554912, 32683, 2105317472, 32683
[16] = 2117527360, 32683, 0, 0, 16032608
[17] = 0, 0, 0, 0, 0
[18] = 15766240, 0, 0, 0, 15950576
[19] = 0, 12654048, 0, 12681440, 0

114
include/arrows.h Normal file
View File

@ -0,0 +1,114 @@
/*
* Gem-graph OpenGL experiments
*
* Desc: OpenGL utils header
*
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <unistd.h>
#include <stdbool.h>
#include <GL/glu.h>
/*
* 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);
/*
* 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);
/*
* 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);
/*
* 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);
/*
* 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);

View File

@ -23,13 +23,18 @@
*/
#pragma once
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <glib.h>
#include <glib-2.0/glib.h>
#define G_APPLICATION_DEFAULT_FLAGS 0
enum
@ -63,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);
@ -73,3 +79,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

View File

@ -24,9 +24,12 @@
*/
#pragma once
#include "base.h"
#include <unistd.h>
#include <stdbool.h>
#include <epoxy/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
/*
* Structure describing a gl_area and its parameters, used to create a table
@ -46,6 +49,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,18 +91,181 @@ 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);
/*
* 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);
/*
* 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);
assert (buffer);
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);

View File

@ -3,6 +3,7 @@
*
* Desc: OpenGL utils header
*
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
*
* This file is part of Gem-graph.
@ -26,23 +27,48 @@
#include <stdbool.h>
#include <GL/glu.h>
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_mark_unit_space,
// int pref_style_lines_planes,
// int pref_style_mix_colors,
int pref_show_grid,
int pref_test_diagonal,
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);
/*
* 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);

218
src/graphics/arrows.c Normal file
View File

@ -0,0 +1,218 @@
/*
* Gem-graph OpenGL experiments
*
* Desc: OpenGL utils header
*
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
* Copyright (C) 2023 Arien Bourmault <neox@a-lec.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "../../include/base.h"
#include "../../include/graphics.h"
/* 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;
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),
// 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 + (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 (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);
// 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);
// 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);
// 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
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);
};
}
/*
* 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)
{
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);
}
/*
* 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)
{
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);
}
/*
* 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)
{
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);
}
/*
* 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)
{
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);
}
/*
* 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)
{
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);
}
/*
* 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)
{
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);
}
/*
* Writes lines for arrow basis into lines buffer
*
* @param n,
* weight,
* site
*
* @return void
*/
void arrows_write_basis(long n)
{
graphics_write_line (n + 0, n + 1);
graphics_write_line (n + 2, n + 3);
graphics_write_line (n + 4, n + 5);
}

View File

@ -1,607 +0,0 @@
/*
* Gem-graph OpenGL experiments
*
* Desc: OpenGL utils header
*
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
//#pragma once
#include <unistd.h>
#include <stdbool.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <epoxy/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <glib-2.0/glib.h>
#include <math.h>
#include <errno.h>
#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;
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)
{
/* beaucoup de ^c ^v ici (peu d'info) (mais un peu)
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
if (0) printf("arrows_anchors 1 / max = %5.2f max / 2 = %5.2f\n", ad, max / 2);
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 draw_line (GLuint *lines_origin, int a, int b)
{
*(lines_origin + lines_index) = a; lines_index ++;
*(lines_origin + lines_index) = b; 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 (lines_origin, 0,21); draw_line (lines_origin, 21,42);
// ce qui suit ne vaut que dans un espace 3 x 3 x 3 (le reste est à corriger - ou à oublier ?)
draw_line (lines_origin, 21,22); draw_line (lines_origin, 21,25);
draw_line (lines_origin, 25,26); draw_line (lines_origin, 22,26);
draw_line (lines_origin, 37,41); draw_line (lines_origin, 41,42);
draw_line (lines_origin, 38,42); draw_line (lines_origin, 37,38);
draw_line (lines_origin, 25,41); draw_line (lines_origin, 21,37);
draw_line (lines_origin, 26,42); draw_line (lines_origin, 22,38);
/* draw_line (lines_origin, step_z * w + step_y * v + step_x * u, step_z * (w + 0) + step_y * (v + 0) + step_x * (u + 1)); */
/* draw_line (lines_origin, step_z * w + step_y * v + step_x * u, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 0)); */
/* draw_line (lines_origin, step_z * w + step_y * v + step_x * u, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 0)); */
/* draw_line (lines_origin, step_z * z + step_y * y + step_x * x, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 0)); */
/* draw_line (lines_origin, step_z * z + step_y * y + step_x * x, step_z * (w + 1) + step_y * (v + 0) + step_x * (u + 1)); */
/* draw_line (lines_origin, step_z * z + step_y * y + step_x * x, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 1)); */
/* draw_line (lines_origin, step_z * w + step_y * v + step_x * x, step_z * (w + 1) + step_y * (v + 0) + step_x * (u + 1)); */
/* draw_line (lines_origin, step_z * w + step_y * v + step_x * x, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 1)); */
/* draw_line (lines_origin, step_z * w + step_y * y + step_x * u, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 1)); */
/* draw_line (lines_origin, step_z * z + step_y * v + step_x * u, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 0)); */
/* draw_line (lines_origin, step_z * z + step_y * v + step_x * u, step_z * (w + 1) + step_y * (v + 0) + step_x * (u + 1)); */
/* draw_line (lines_origin, 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 (lines_origin, step_z * w + step_y * v + step_x * u, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 1));
draw_line (lines_origin, step_z * w + step_y * v + step_x * x, step_z * (w + 1) + step_y * (v + 1) + step_x * (u + 0));
draw_line (lines_origin, step_z * z + step_y * v + step_x * u, step_z * (w + 0) + step_y * (v + 1) + step_x * (u + 1));
draw_line (lines_origin, 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 (lines_origin, step_z * 0 + step_y * 0 + step_x * 0, step_z * 0 + step_y * 0 + step_x * x);
draw_line (lines_origin, step_z * 0 + step_y * 0 + step_x * 0, step_z * 0 + step_y * y + step_x * 0);
draw_line (lines_origin, step_z * 0 + step_y * 0 + step_x * 0, step_z * z + step_y * 0 + step_x * 0);
draw_line (lines_origin, step_z * z + step_y * y + step_x * x, step_z * 0 + step_y * y + step_x * x);
draw_line (lines_origin, step_z * z + step_y * y + step_x * x, step_z * z + step_y * y + step_x * 0);
draw_line (lines_origin, step_z * z + step_y * y + step_x * x, step_z * z + step_y * 0 + step_x * x);
draw_line (lines_origin, step_z * 0 + step_y * 0 + step_x * x, step_z * 0 + step_y * y + step_x * x);
draw_line (lines_origin, step_z * 0 + step_y * 0 + step_x * x, step_z * z + step_y * 0 + step_x * x);
draw_line (lines_origin, step_z * z + step_y * 0 + step_x * 0, step_z * z + step_y * y + step_x * 0);
draw_line (lines_origin, step_z * 0 + step_y * y + step_x * 0, step_z * 0 + step_y * y + step_x * x);
draw_line (lines_origin, step_z * 0 + step_y * y + step_x * 0, step_z * z + step_y * y + step_x * 0);
draw_line (lines_origin, 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;
// écriture plus concise en utilisant la fonction "draw_line(...){...}"
// mais probablement plus lente.
// Je commente donc la ligne "draw_line" et laisse les deux lignes suivantes actives
// draw_line (lines_origin, calcul, calcul + step_y - step_z);
*(lines_origin + lines_index) = calcul; lines_index ++;
*(lines_origin + lines_index) = calcul + step_y - step_z; lines_index ++;
}
}
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;
*(lines_origin + lines_index) = calcul; lines_index ++;
*(lines_origin + lines_index) = calcul + step_x - step_y; lines_index ++;
}
}
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;
*(lines_origin + lines_index) = calcul; lines_index ++;
*(lines_origin + lines_index) = calcul + step_x * (x - 1); lines_index ++;
}
}
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 (lines_origin, 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 (lines_origin, s + 2, s + 6 + site % 2);
draw_line (lines_origin, s + 3, s + 6 + site % 2);
draw_line (lines_origin, s + 4, s + 6 + site % 2);
draw_line (lines_origin, 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 (lines_origin, s + 0, s + 8 + site % 2);
draw_line (lines_origin, s + 1, s + 8 + site % 2);
draw_line (lines_origin, s + 4, s + 8 + site % 2);
draw_line (lines_origin, 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 (lines_origin, s + 0, s + 10 + site % 2);
draw_line (lines_origin, s + 1, s + 10 + site % 2);
draw_line (lines_origin, s + 2, s + 10 + site % 2);
draw_line (lines_origin, s + 3, s + 10 + site % 2);
}
static void draw_a_central_star(GLuint *lines_origin, long n)
{
draw_line (lines_origin, n + 0, n + 1);
draw_line (lines_origin, n + 2, n + 3);
draw_line (lines_origin, 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_mark_unit_space,
// int pref_style_lines_planes,
// int pref_style_mix_colors,
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_mark_unit_space == 0) printf("no unit_space marks ");
// if (pref_mark_unit_space == 1) printf("first unit space marked ");
// if (pref_mark_unit_space == 2) printf("last unit_space marked ");
// if (pref_mark_unit_space == 3) printf("first and last units space marked ");
// if (! pref_style_lines_planes) printf("no style_lines_planes ");
// if (pref_style_lines_planes > 0) printf("style_lines_planes = %d ", pref_style_lines_planes);
// if (! pref_style_mix_colors) printf("no style_mix_colors ");
// if (pref_style_mix_colors > 0) printf("style_mix_colors = %d ", pref_style_mix_colors);
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[ n] load site x y z ---- < arrows array >\
------------------------------------------------------------\n");
// (DEPRECATED) arrows = { 1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21 }
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));
printf("NB If you play : 'draw_some_arrows_demo(...)',\
don't forget to set model_arrows_nb to 6 (line 555 in graphics.c). \
The 'nombre_de_cases_contenant_des_fleches' is automatically set to 6 (line 560) ( ;- ))\n");
printf("NB The same is true if you play : 'draw_some_arrows(...)',\
and modify the number of arrows described above the line 570.\n");
printf("NB If you play : 'draw_some_arrows(...)' and add some more arrows (above the line 570),\
you must also increase the number of arrows (line 555) \n\
The 'nombre_de_cases_contenant_des_fleches' is automatically set equal to the number of arrows (line 560).\n");
}
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_mark_unit_space, int pref_style_lines_planes, int pref_style_mix_colors,
int pref_show_grid, int pref_test_diagonal,
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_mark_unit_space, pref_style_lines_planes, pref_style_mix_colors,
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);
if (pref_test_diagonal) diagonal_test (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;
if (0) draw_some_arrows_demo (lines_origin, s, model_size_x, model_size_y, model_size_z, arrows, model_arrows_nb);
if (1) draw_some_arrows (lines_origin, s, model_size_z * model_size_y, model_size_z, arrows, model_arrows_nb);
// draw_a_cube_at(1, 1, 1, lines_origin, model_size_x, model_size_y, model_size_z, 0);
return 0;
}
// X - X = EAST - WEST = rouge - cyan
// Y - Y = ZENITH - NADIR = vert - magenta
// Z - Z = NORTH - SOUTH = bleu - jaune

File diff suppressed because it is too large Load Diff

169
src/graphics/grid.c Normal file
View File

@ -0,0 +1,169 @@
/*
* Gem-graph
*
* Desc: OpenGL grid functions
*
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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));
}
}
}

View File

@ -1,394 +0,0 @@
/*
* Gem-graph OpenGL experiments
*
* Desc: OpenGL utils header
*
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
//#pragma once
#include <unistd.h>
#include <stdbool.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <epoxy/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <glib-2.0/glib.h>
#include <math.h>
#include <errno.h>
#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); */