From 9734c9b2af9cf1fc8dfdbe5788aa03ab0203fc2a Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Thu, 6 Jun 2024 10:52:41 +0200 Subject: [PATCH] =?UTF-8?q?WIP:=20je=20comprends=20pas=20que=20=C3=A7a=20m?= =?UTF-8?q?arche,=20l=C3=A0=20=3F=20(draw=5Fspace=5F...=20draw=5Fgrids=5F.?= =?UTF-8?q?..=20in=20graph=5Fstack=20(288))?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- (notes) | 20 ++++-- graph_stack.c | 8 +-- init.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 8 deletions(-) create mode 100644 init.c diff --git a/(notes) b/(notes) index cc3c095..78b10ee 100644 --- a/(notes) +++ b/(notes) @@ -1,27 +1,39 @@ -jean@Project:~/01/Gtk4/Getting_Started_with_GTK [env] $ grep -r "////...////" contain.c: (136) gtk_box_append (middle_box, GTK_WIDGET (get_GLArea())); graph_area.h: (177) static inline GLuint create_shader(const int stack_id, int type, const char *src) {...} graph_area.c: (153) graphics_draw (stack_index[i].stack_id); + graph_area.c: (233) g_signal_connect (adj, "value-changed", G_CALLBACK(on_axis_value_change), (gpointer) label); graph_area.c: (289) g_signal_connect(GTK_GL_AREA(gl_area), "render", G_CALLBACK(on_glarea_render), NULL); graph_area.c: g_signal_connect(gl_area, "realize", G_CALLBACK(on_glarea_realize), NULL); graph_area.c: g_signal_connect(gl_area, "unrealize", G_CALLBACK(on_glarea_unrealize), NULL); -init.c.todo: (98) vertex = create_shader(stack_id, GL_VERTEX_SHADER, vertex_shader); -init.c.todo: (110) fragment = create_shader(stack_id, GL_FRAGMENT_SHADER, fragment_shader); - graph_stack.c: (149) if (!graphics_init_shaders(cur_id)) return -1; graph_stack.c: (153) graphics_init_buffers(cur_id); + graph_stack.c: (288) draw_space_ridges_vertex (stack_id, stack->buffer_vertex_size, space_X, space_Y, space_Z); graph_stack.c: draw_space_ridges_lines (stack_id); graph_stack.c: draw_grids_on_space_faces_vertex (stack_id, space_X, space_Y, space_Z); graph_stack.c: draw_grids_on_space_faces_lines (stack_id, stack->buffer_lines_size, space_X, space_Y, space_Z); + graph_stack.c: (316) stack->arrows_nb = set_arrow (stack_id, stack->arrows_nb, space_X, space_Y, space_Z, arrow.load, arrow.site, arrow.x, arrow.y, arrow.z); -------------------------------------------------------------------------------- + + + + + + + + + + + + + https://docs.gtk.org/gtk4/visual_index.html widgets gallery https://forge.a-lec.org/gem-graph/gem-graph-client/src/branch/devel/Makefile https://docs.gtk.org/gtk4/class.Widget.html#height-for-width-geometry-management diff --git a/graph_stack.c b/graph_stack.c index 1ddb4dc..32d9690 100644 --- a/graph_stack.c +++ b/graph_stack.c @@ -285,10 +285,10 @@ void graphics_model_setup (const int stack_id) /*------------------------------------------------------------------------*/ -////////////////////////// draw_space_ridges_vertex (stack_id, stack->buffer_vertex_size, space_X, space_Y, space_Z); -////////////////////////// draw_space_ridges_lines (stack_id); -////////////////////////// draw_grids_on_space_faces_vertex (stack_id, space_X, space_Y, space_Z); -////////////////////////// draw_grids_on_space_faces_lines (stack_id, stack->buffer_lines_size, space_X, space_Y, space_Z); + draw_space_ridges_vertex (stack_id, stack->buffer_vertex_size, space_X, space_Y, space_Z); + draw_space_ridges_lines (stack_id); + draw_grids_on_space_faces_vertex (stack_id, space_X, space_Y, space_Z); + draw_grids_on_space_faces_lines (stack_id, stack->buffer_lines_size, space_X, space_Y, space_Z); stack->buffer_vertex_0_arrow = stack->buffer_vertex_size; stack->buffer_colors_0_arrow = stack->buffer_colors_size; diff --git a/init.c b/init.c new file mode 100644 index 0000000..7789ec0 --- /dev/null +++ b/init.c @@ -0,0 +1,175 @@ +/* + * Gem-graph OpenGL experiments + * + * Desc: GL functions + * + * Copyright (C) 2023 Arthur Menges + * 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 + * along with this program. If not, see . + */ + +/* -------------------------------------------------------------------------- */ + +#include +#include +#include "base.h" +#include "contain.h" // instead of "ui.h" +#include "graph_area.h" + +/* 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 int stack_id) +{ + struct graphic_stack_t *stack = &graphic_stack[stack_id]; + + //XXX + graphics_model_setup(stack_id); + + GLuint vao, vertex_buffer, color_buffer; + + glGenBuffers(1, &vertex_buffer); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, + stack->buffer_vertex_size * + sizeof(stack->buffer_vertex_origin[0]), + stack->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, stack->buffer_colors_size * + sizeof(stack->buffer_colors_origin[0]), + stack->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); + + stack->vao = vao; + stack->position_buffer = vertex_buffer; + stack->color_buffer = color_buffer; +} + +/* + * 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 int stack_id) +{ + struct graphic_stack_t *stack = &graphic_stack[stack_id]; + char *vertex_shader; + char *fragment_shader; + int status; + GLuint vertex = 0, fragment; // 2024-06-05 j'initialise vertex à 0 (au pif !) + GLuint program = 0; + GLuint m = 0; + GLuint v = 0; + GLuint p = 0; + + // Load vertex shader file + vertex_shader = read_file(VERTEX_SHADER_FILE); + if (vertex_shader == NULL) + return false; + vertex = create_shader(stack_id, GL_VERTEX_SHADER, vertex_shader); + + if(vertex == 0) { + stack->program = 0; + g_free(vertex_shader); + return false; + } + + // Load fragment shader file + fragment_shader = read_file(FRAG_SHADER_FILE); + if (fragment_shader == NULL) + return false; + fragment = create_shader(stack_id, GL_FRAGMENT_SHADER, fragment_shader); + + if(fragment == 0) { + glDeleteShader(vertex); + stack->program = 0; + g_free(vertex_shader); + g_free(fragment_shader); + return false; + } + + // Link shaders to program + program = glCreateProgram(); + glAttachShader(program, vertex); + glAttachShader(program, fragment); + + glLinkProgram(program); + + glGetProgramiv(program, GL_LINK_STATUS, &status); + + if(status == GL_FALSE) { + int log_len; + char *buffer; + + 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); + + g_free(buffer); + + glDeleteProgram(program); + program = 0; + + glDeleteShader(vertex); + glDeleteShader(fragment); + + g_free(vertex_shader); + g_free(fragment_shader); + + return false; + } + + /* Get the location of the "mvp" uniform */ + m = glGetUniformLocation(program, "model_matrix"); + v = glGetUniformLocation(program, "view_matrix"); + p = glGetUniformLocation(program, "projection_matrix"); + + glDetachShader(program, vertex); + glDetachShader(program, fragment); + + glDeleteShader(vertex); + glDeleteShader(fragment); + + stack->program = program; + stack->m = m; + stack->v = v; + stack->p = p; + + g_free(vertex_shader); + g_free(fragment_shader); + + return true; +}