Couleurs (premier essai > que du noir et blanc)

This commit is contained in:
Jean Sirmai 2023-06-14 16:01:52 +02:00
parent efe117076f
commit f12c22b73b
Signed by: jean
GPG Key ID: FB3115C340E057E3
6 changed files with 73 additions and 29 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 195 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 259 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

View File

@ -38,17 +38,18 @@
#include "../../include/base.h" #include "../../include/base.h"
#include "../../include/ui.h" #include "../../include/ui.h"
#include "../../include/graphics.h" #include "../../include/graphics.h"
#include "../../include/graphics_cube.h"
#define VERTEX_SHADER_FILE "src/shaders/shader.vert" #define VERTEX_SHADER_FILE "src/shaders/shader.vert"
#define FRAG_SHADER_FILE "src/shaders/shader.frag" #define FRAG_SHADER_FILE "src/shaders/shader.frag"
#define K 0 #define K 0
GLfloat *vertex_base = NULL; GLfloat *vertex_base = NULL;
GLfloat *color_base = NULL;
GLubyte *indices = NULL; GLubyte *indices = NULL;
int dim = 0; int dim = 0;
int state_size = 0; int state_size = 0;
int vertex_nb = 0; int vertex_nb = 0;
int colors_nb = 0;
int segments_nb = 0; int segments_nb = 0;
int arrows_nb = 0; int arrows_nb = 0;
@ -318,6 +319,7 @@ bool graphics_shutdown(const void *gl_area)
* sizeof(struct gl_area_entry *)); * sizeof(struct gl_area_entry *));
g_free(vertex_base); g_free(vertex_base);
g_free(color_base);
g_free(indices); g_free(indices);
return true; return true;
} }
@ -566,6 +568,18 @@ static bool compute_arrow_2D(struct gl_area_entry *entry, int state_size, int vg
if (A) printf("x = %d site = %d center = %f tip = %f base = %f\n",\ if (A) printf("x = %d site = %d center = %f tip = %f base = %f\n",\
x, site, center, tip, base); // vgap=%d igap=%d x, site, center, tip, base); // vgap=%d igap=%d
/* *(color_base + vgap + 0) = 0.2f; // cyan */
/* *(color_base + vgap + 1) = 0.8f; */
/* *(color_base + vgap + 2) = 0.8f; */
/* *(color_base + vgap + 3) = 0.8f; // magenta */
/* *(color_base + vgap + 4) = 0.2f; */
/* *(color_base + vgap + 5) = 0.8f; */
/* *(color_base + vgap + 6) = 0.8f; // jaune */
/* *(color_base + vgap + 7) = 0.8f; */
/* *(color_base + vgap + 8) = 0.2f; */
assert(igap < entry->indices_nb); assert(igap < entry->indices_nb);
assert(igap + 5 < entry->indices_nb); assert(igap + 5 < entry->indices_nb);
@ -722,6 +736,8 @@ static bool compute_space_3D(struct gl_area_entry *entry, int state_size){
assert(k * 12 < vertex_nb); assert(k * 12 < vertex_nb);
assert(k * 12 + 11 < vertex_nb); assert(k * 12 + 11 < vertex_nb);
assert(k * 12 < colors_nb);
assert(k * 12 + 11 < colors_nb);
*(vertex_base + k * 12 + 0) = - x; *(vertex_base + k * 12 + 0) = - x;
*(vertex_base + k * 12 + 1) = 1.0f / state_size; *(vertex_base + k * 12 + 1) = 1.0f / state_size;
@ -738,6 +754,32 @@ static bool compute_space_3D(struct gl_area_entry *entry, int state_size){
*(vertex_base + k * 12 + 9) = - x; *(vertex_base + k * 12 + 9) = - x;
*(vertex_base + k * 12 + 10) = - 1.0f / state_size; *(vertex_base + k * 12 + 10) = - 1.0f / state_size;
*(vertex_base + k * 12 + 11) = 1.0f / state_size; *(vertex_base + k * 12 + 11) = 1.0f / state_size;
*(color_base + k * 12 + 0) = 0.8f; // rouge
*(color_base + k * 12 + 1) = 0.2f;
*(color_base + k * 12 + 2) = 0.2f;
*(color_base + k * 12 + 3) = 0.8f; // jaune
*(color_base + k * 12 + 4) = 0.8f;
*(color_base + k * 12 + 5) = 0.2f;
*(color_base + k * 12 + 6) = 0.2f; // vert
*(color_base + k * 12 + 7) = 0.8f;
*(color_base + k * 12 + 8) = 0.2f;
*(color_base + k * 12 + 9) = 0.8f; // magenta
*(color_base + k * 12 + 10) = 0.2f;
*(color_base + k * 12 + 11) = 0.8f;
/* 0.8, 0.8, 0.8, // blanc */
/* 0.8, 0.8, 0.2, // jaune */
/* 0.8, 0.2, 0.2, // rouge */
/* 0.2, 0.2, 0.2, // noir */
/* 0.2, 0.2, 0.2, // gris */
/* 0.2, 0.8, 0.8, // cyan */
/* 0.2, 0.8, 0.2, // vert */
/* 0.8, 0.2, 0.8, // magenta */
} }
if (V) printf(" n = %d x 2 côté = [%1.1f]\n", state_size + 1, 2.0f / state_size); if (V) printf(" n = %d x 2 côté = [%1.1f]\n", state_size + 1, 2.0f / state_size);
@ -802,46 +844,48 @@ void graphics_init_buffers(const void *gl_area)
{ {
struct gl_area_entry *entry; struct gl_area_entry *entry;
GLuint vao, vertex_buffer, color_buffer; GLuint vao, vertex_buffer, color_buffer;
int vgap = 0, igap = 0; int vgap = 0, igap = 0, cgap = 0;
entry = find_entry_from_ptr(gl_area); entry = find_entry_from_ptr(gl_area);
//{1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21}; //{1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21};
dim = 1; dim = 1;
state_size = 13; // 2 < state_size < 32 state_size = 13; // 2 < state_size < 32
arrows_nb = 6; arrows_nb = 4;
vertex_nb = (state_size + 1) * 6 + arrows_nb * 9; vertex_nb = (state_size + 1) * 6 + arrows_nb * 9;
vertex_nb = (state_size + 1) * 12 + arrows_nb * 15; vertex_nb = (state_size + 1) * 12 + arrows_nb * 15;
colors_nb = (state_size + 1) * 12 + arrows_nb * 0; // TODO
segments_nb = (2 + state_size) + (arrows_nb * 3); segments_nb = (2 + state_size) + (arrows_nb * 3);
segments_nb = (4 + state_size * 4) + (arrows_nb * 8) + 2; // + 2; pour les 2 diagonales segments_nb = (4 + state_size * 4) + (arrows_nb * 8) + 2; // + 2; pour les 2 diagonales
entry->indices_nb = segments_nb * 2; entry->indices_nb = segments_nb * 2;
printf("Initialization of buffers with %u indices and %u vertices & state_size = %d\n", printf("Initialization of buffers with %u indices, %u vertices and %u colors & state_size = %d\n",
entry->indices_nb, vertex_nb, state_size); entry->indices_nb, vertex_nb, colors_nb, state_size);
//XXX g_malloc //XXX g_malloc
vertex_base = g_malloc0(vertex_nb * sizeof(GLfloat) * 2); vertex_base = g_malloc0(vertex_nb * sizeof(GLfloat) * 2);
color_base = g_malloc0(colors_nb * sizeof(GLfloat) * 2);
indices = g_malloc0(segments_nb * 2 * sizeof(GLubyte) * 2); indices = g_malloc0(segments_nb * 2 * sizeof(GLubyte) * 2);
// compute_space_2D(entry, state_size); vgap += (6 * state_size); igap += state_size * 2 + 4; // compute_space_2D(entry, state_size); vgap += (6 * state_size); igap += state_size * 2 + 4; cgap += (6 * state_size);
compute_space_3D(entry, state_size); vgap += (12 * state_size); igap += state_size * 8 + 8; compute_space_3D(entry, state_size); vgap += (12 * state_size); igap += state_size * 8 + 8; cgap += (12 * state_size);
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 9; igap += 6; ++arrows_nb; */ /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 0, 0, 0); vgap += 9; igap += 6; */ /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 0, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 9; igap += 6; */ /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 9; igap += 6; */ /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 9; igap += 6; */ /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 9; igap += 6; */ /* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 15; igap += 16; ++arrows_nb; compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, 0, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 0, 0, 0); vgap += 15; igap += 16; compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 15; igap += 16; compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 15; igap += 16; //compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 15; igap += 16; //compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 15; igap += 16; compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
// We only use one VAO, so we always keep it bound // We only use one VAO, so we always keep it bound
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
@ -853,15 +897,15 @@ void graphics_init_buffers(const void *gl_area)
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
// colors // colors
/* glGenBuffers(1, &color_buffer); */ glGenBuffers(1, &color_buffer);
/* glBindBuffer(GL_ARRAY_BUFFER, color_buffer); */ glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
/* glBufferData(GL_ARRAY_BUFFER, sizeof(color_base), color_base, GL_STATIC_DRAW); */ glBufferData(GL_ARRAY_BUFFER, sizeof(color_base[0]), color_base, GL_STATIC_DRAW); // TODO check : color_base[0] or color_base ?
/* glBindBuffer(GL_ARRAY_BUFFER, 0); */ glBindBuffer(GL_ARRAY_BUFFER, 0);
if(entry != NULL) { if(entry != NULL) {
entry->vao = vao; entry->vao = vao;
entry->position_buffer = vertex_buffer; entry->position_buffer = vertex_buffer;
//entry->color_buffer = color_buffer; entry->color_buffer = color_buffer;
} }
} }
@ -898,7 +942,7 @@ void graphics_draw(const void *gl_area)
compute_i(m); compute_i(m);
compute_i(v); compute_i(v);
glClearColor(0, 0, 0, 0); glClearColor(0.1f, 0.1f, 0.1f, 0.15f); // couleur du 'fond'
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Use our shaders */ /* Use our shaders */
@ -915,9 +959,9 @@ void graphics_draw(const void *gl_area)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0);
// couleurs // couleurs
/* glEnableVertexAttribArray(1); */ glEnableVertexAttribArray(1);
/* glBindBuffer(GL_ARRAY_BUFFER, entry->color_buffer); */ glBindBuffer(GL_ARRAY_BUFFER, entry->color_buffer);
/* glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); */ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);