gem-graph-client/include/graphics.h

414 lines
13 KiB
C

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Gem-graph client *
* Graphics header *
* *
* Copyright © 2021 Libre en Communs <contact@a-lec.org> *
* Copyright © 2023-2024 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 publishedby 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 "base.h"
#include <epoxy/gl.h>
#include <GL/glu.h>
#define VERTEX_SHADER_FILE "src/shader.vert"
#define FRAG_SHADER_FILE "src/shader.frag"
#define GL_TARGET_MAJOR_VERSION 0
#define GL_TARGET_MINOR_VERSION 4
/******************************************************************************/
/* G L A R E A */
/******************************************************************************/
/*
* Structure describing a gl_area and its parameters, used to create a table
* of Gem-graph client current gl_areas
*/
struct graphics_stack_t {
int id;
int mode;
float rotation_angles[N_AXIS]; // Rotation angles on each axis
GLuint vao; // init_buffers
GLuint position_buffer; // shutdown, draw
GLuint color_buffer; // shutdown, draw
GLuint program; // shutdown, init_shaders, draw
GLuint m; // init_shaders, draw
GLuint v; // init_shaders, draw
GLuint p; // init_shaders, draw
struct arrow_t *arrows_ptr;
long arrows_nb;
GLfloat *buffer_vertex_origin;
GLfloat *buffer_colors_origin;
GLuint *buffer_lines_origin;
GLuint *buffer_plans_origin;
long buffer_vertex_size;
long buffer_colors_size;
long buffer_lines_size;
long buffer_plans_size;
long buffer_vertex_0_arrow;
long buffer_colors_0_arrow;
long buffer_lines_0_arrow;
long buffer_plans_0_arrow;
};
/*
* Dynamic array of ptrs to dynamically allocated gl_area_entry
*/
extern struct graphics_stack_t *graphics_stack;
/*
* Structure describing a gl_area and its parameters, used to create a table
* of Gem-graph client current gl_areas
*/
bool graphics_render_stack(GtkWidget *container_widget);
bool graphics_setup_glarea(int target_mode, GtkWidget *target_widget);
/*
* Initializes a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return true if initialized
*/
int graphics_init(void *error_buffer);
/*
* Draws the current buffer to a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return void
*/
void graphics_draw(const int graphics_stack_id);
/*
* Shutdowns a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return true if success
*/
bool graphics_shutdown(const int graphics_stack_id, void *error_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 graphics_stack_id);
bool graphics_init_graphics_stack(void *container_widget, GError *error_buffer);
bool graphics_shutdown_graphics_stack(void *container_widget, GError *error_buffer);
bool graphics_update_axis_stack(GtkWidget *container_widget, int axis, int value);
/* 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 graphics_stack_id);
/*
* Draws a vertex (x, y, z)
* if (console) prints (x, y, z) values to console
*
* @param GLfloat x, GLfloat y, GLfloat z
*
* @return void
*/
void graphics_draw_vertex (const int graphics_stack_id,
GLfloat x,
GLfloat y,
GLfloat z);
/*
* Draws the color (r, g, b) associated to a vertex
* if (console) prints (r, g, b) values to console
*
* @param GLfloat r, GLfloat g, GLfloat b
*
* @return void
*/
//void graphics_draw_color (const int graphics_stack_id, GLfloat r, GLfloat g, GLfloat b);
/*
* Writes values to describe a line from a to b into the line buffer
*
* @use liste de fonctions GTK
*
* @param coords GLuint (a,b)
*
* @return void
*/
void graphics_draw_line (const int graphics_stack_id, GLuint a, GLuint 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_draw_plan (const int graphics_stack_id, GLuint a, GLuint b, GLuint c);
/*
* Created and compile a shader
*
* @param type, shader type
* src, source code of shader
*
* @return shader id
*/
static inline GLuint graphics_create_shader(const int graphics_stack_id, 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;
}
static inline void graphics_print_stack(int graphics_stack_id)
{
static int n = 0;
printf("\n[n=%d]***************", n);
printf("id = %d\tmode = %d\n",
graphics_stack[graphics_stack_id].id,
graphics_stack[graphics_stack_id].mode);
printf("rotation_angles = ");
for (int i = 0; i < N_AXIS; i++) {
printf("%f\t", graphics_stack[graphics_stack_id].rotation_angles[i]);
// Rotation angles on each axis
}
printf("\n");
printf("vao = %d\n", graphics_stack[graphics_stack_id].vao);
printf("position_buffer = %d\n", graphics_stack[graphics_stack_id].position_buffer);
printf("color_buffer = %d\n", graphics_stack[graphics_stack_id].color_buffer);
printf("program = %d\n", graphics_stack[graphics_stack_id].program);
printf("m = %d\n", graphics_stack[graphics_stack_id].m);
printf("v = %d\n", graphics_stack[graphics_stack_id].v);
printf("p = %d\n", graphics_stack[graphics_stack_id].p);
printf("arrows_ptr = %p\n", graphics_stack[graphics_stack_id].arrows_ptr);
printf("arrows_nb = %ld\n", graphics_stack[graphics_stack_id].arrows_nb);
printf("buffer_vertex_origin = %p\n", graphics_stack[graphics_stack_id].buffer_vertex_origin);
printf("buffer_colors_origin = %p\n", graphics_stack[graphics_stack_id].buffer_colors_origin);
printf("buffer_lines_origin = %p\n", graphics_stack[graphics_stack_id].buffer_lines_origin);
printf("buffer_plans_origin = %p\n", graphics_stack[graphics_stack_id].buffer_plans_origin);
printf("buffer_vertex_size = %ld\n", graphics_stack[graphics_stack_id].buffer_vertex_size);
printf("buffer_colors_size = %ld\n", graphics_stack[graphics_stack_id].buffer_colors_size);
printf("buffer_lines_size = %ld\n", graphics_stack[graphics_stack_id].buffer_lines_size);
printf("buffer_plans_size = %ld\n", graphics_stack[graphics_stack_id].buffer_plans_size);
printf("buffer_vertex_0_arrow = %ld\n", graphics_stack[graphics_stack_id].buffer_vertex_0_arrow);
printf("buffer_colors_0_arrow = %ld\n", graphics_stack[graphics_stack_id].buffer_colors_0_arrow);
printf("buffer_lines_0_arrow = %ld\n", graphics_stack[graphics_stack_id].buffer_lines_0_arrow);
printf("buffer_plans_0_arrow = %ld\n", graphics_stack[graphics_stack_id].buffer_plans_0_arrow);
printf("********************\n");
n++;
}
/*
* Initializes a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return true if initialized
*/
int graphics_init(void *error_buffer);
/*
* Draws the current buffer to a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return void
*/
//void graphics_draw(const int graphics_stack_id);
/*
* Shutdowns a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return true if success
*/
bool graphics_shutdown(const int graphics_stack_id, void *error_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 graphics_stack_id);
/* 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 graphics_stack_id);
/*
* Draws a vertex (x, y, z)
* if (console) prints (x, y, z) values to console
*
* @param GLfloat x, GLfloat y, GLfloat z
*
* @return void
*/
void graphics_draw_vertex (const int graphics_stack_id,
GLfloat x,
GLfloat y,
GLfloat z);
/*
* Draws the color (r, g, b) associated to a vertex
* if (console) prints (r, g, b) values to console
*
* @param GLfloat r, GLfloat g, GLfloat b
*
* @return void
*/
void graphics_draw_color (const int graphics_stack_id, GLfloat r, GLfloat g, GLfloat b);
/*
* Writes values to describe a line from a to b into the line buffer
*
* @param coords GLuint (a,b)
*
* @return void
*/
void graphics_draw_line (const int graphics_stack_id, GLuint a, GLuint 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_draw_plan (const int graphics_stack_id, GLuint a, GLuint b, GLuint c);
void graphics_model_setup (const int graphics_stack_id);
int graphics_draw_one_arrow_vertex (const int graphics_stack_id,
int space_X,
int space_Y,
int space_Z,
int weight,
int site,
int x,
int y,
int z);
int graphics_draw_one_arrow_line(const int graphics_stack_id,
int offset_vertex);
/*
* Writes grid ridges to vertex and color buffers
*
* @param coords long (x,y,z), step_x, step_y, step_z
*
* @return void
*/
int graphics_draw_space_ridges_vertex (const int graphics_stack_id,
long offset_vertex,
long x,
long y,
long z);
int graphics_draw_space_ridges_lines (const int graphics_stack_id);
/*
* Writes grid lines on space faces
*
* @param coords long (x,y,z)
*
* @return void
*/
long graphics_draw_grids_on_space_faces_vertex (const int graphics_stack_id,
long x,
long y,
long z);
long graphics_draw_grids_on_space_faces_lines (const int graphics_stack_id,
long offset_vertex,
long x,
long y,
long z);
int graphics_set_arrow (int graphics_stack_id,
int arrows_nb,
int space_X,
int space_Y,
int space_Z,
int requested_weight,
int site,
int arrow_x,
int arrow_y,
int arrow_z);