Point de départ pour l'étude des shaders

This commit is contained in:
Jean Sirmai 2023-09-28 11:36:15 +02:00
parent 2a66537af4
commit 61fe121d2d
Signed by: jean
GPG Key ID: FB3115C340E057E3
4 changed files with 374 additions and 109 deletions

View File

@ -207,39 +207,39 @@ static inline void compute_mvp(float *res, float phi, float theta, float psi)
*
* @return shader id
*/
static inline GLuint create_shader(int type, const char *src)
{
GLuint shader;
int status;
// static inline GLuint create_shader(int type, const char *src)
// {
// GLuint shader;
// int status;
shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
// 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_COMPILE_STATUS, &status);
// if(status == GL_FALSE) {
// int log_len;
// char *buffer;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
// glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
buffer = g_malloc(log_len + 1);
assert (buffer);
glGetShaderInfoLog(shader, log_len, NULL, buffer);
// 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_warning("Compile failure in %s shader:\n%s",
// type == GL_VERTEX_SHADER ? "vertex" : "fragment",
// buffer);
g_free(buffer);
// g_free(buffer);
glDeleteShader(shader);
// glDeleteShader(shader);
return 0;
}
// return 0;
// }
return shader;
}
// return shader;
// }
/*
* Find the gl_area_entry size
@ -254,7 +254,11 @@ static inline short gl_area_size(void)
struct gl_area_entry **cur;
short size;
// Check uninitialized and quit
// 28-9-2023 JE COMMENTE LA LIGNE SUIVANTE SANS SAVOIR CE QUE JE FAIS
// (J'ESSAIE D'ISOLER CE QUI CONCERNE LES SHADERS HORS DE "graphics.c")
// Check uninitialized and quit <<< WARNING ! TODO
if (gl_area_array == NULL) return 0;
cur = gl_area_array;

110
include/shaders_study.h Normal file
View File

@ -0,0 +1,110 @@
/*
* 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 "base.h"
#include <unistd.h>
#include <stdbool.h>
#include <epoxy/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
/*
* 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);
/*
* 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

@ -28,6 +28,7 @@
#include "../../include/graphics.h"
#include "../../include/arrows.h"
#include "../../include/grid.h"
#include "../../include/shaders_study.h"
#define VERTEX_SHADER_FILE "src/shaders/shader.vert"
#define FRAG_SHADER_FILE "src/shaders/shader.frag"
@ -244,109 +245,109 @@ void graphics_init_buffers(const void *gl_area)
*
* @return true if initialized
*/
bool graphics_init_shaders(const void *gl_area)
{
char *vertex_shader;
char *fragment_shader;
struct gl_area_entry *entry;
int status;
GLuint vertex, fragment;
GLuint program = 0;
GLuint m = 0;
GLuint v = 0;
GLuint p = 0;
/* bool graphics_init_shaders(const void *gl_area) */
/* { */
/* char *vertex_shader; */
/* char *fragment_shader; */
/* struct gl_area_entry *entry; */
/* int status; */
/* GLuint vertex, fragment; */
/* GLuint program = 0; */
/* GLuint m = 0; */
/* GLuint v = 0; */
/* GLuint p = 0; */
entry = graphics_find_glarea_from_ptr(gl_area);
/* entry = graphics_find_glarea_from_ptr(gl_area); */
if (entry == NULL) {
errno = EFAULT;
perror("gl_area_array entry not found");
return false;
}
/* if (entry == NULL) { */
/* errno = EFAULT; */
/* perror("gl_area_array entry not found"); */
/* return false; */
/* } */
// Load vertex shader file
vertex_shader = read_file(VERTEX_SHADER_FILE);
if (vertex_shader == NULL)
return false;
vertex = create_shader(GL_VERTEX_SHADER, vertex_shader);
/* // Load vertex shader file */
/* vertex_shader = read_file(VERTEX_SHADER_FILE); */
/* if (vertex_shader == NULL) */
/* return false; */
/* vertex = create_shader(GL_VERTEX_SHADER, vertex_shader); */
// ! WARNING ! l'instruction suivante (ou un autre 'free') a pu provoquer un 'double free' (Abandon)
if(vertex == 0) {
entry->program = 0;
g_free(vertex_shader);
return false;
}
/* // ! WARNING ! l'instruction suivante (ou un autre 'free') a pu provoquer un 'double free' (Abandon) */
/* if(vertex == 0) { */
/* entry->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(GL_FRAGMENT_SHADER, fragment_shader);
/* // Load fragment shader file */
/* fragment_shader = read_file(FRAG_SHADER_FILE); */
/* if (fragment_shader == NULL) */
/* return false; */
/* fragment = create_shader(GL_FRAGMENT_SHADER, fragment_shader); */
if(fragment == 0) {
glDeleteShader(vertex);
entry->program = 0;
g_free(vertex_shader);
g_free(fragment_shader);
return false;
}
/* if(fragment == 0) { */
/* glDeleteShader(vertex); */
/* entry->program = 0; */
/* g_free(vertex_shader); */
/* g_free(fragment_shader); */
/* return false; */
/* } */
// Link shaders to program
program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
/* // Link shaders to program */
/* program = glCreateProgram(); */
/* glAttachShader(program, vertex); */
/* glAttachShader(program, fragment); */
glLinkProgram(program);
/* glLinkProgram(program); */
glGetProgramiv(program, GL_LINK_STATUS, &status);
/* glGetProgramiv(program, GL_LINK_STATUS, &status); */
if(status == GL_FALSE) {
int log_len;
char *buffer;
/* if(status == GL_FALSE) { */
/* int log_len; */
/* char *buffer; */
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
/* glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len); */
buffer = g_malloc(log_len + 1);
assert(buffer);
glGetProgramInfoLog(program, log_len, NULL, buffer);
/* buffer = g_malloc(log_len + 1); */
/* assert(buffer); */
/* glGetProgramInfoLog(program, log_len, NULL, buffer); */
g_warning("Linking failure:\n%s", buffer);
/* g_warning("Linking failure:\n%s", buffer); */
g_free(buffer);
/* g_free(buffer); */
glDeleteProgram(program);
program = 0;
/* glDeleteProgram(program); */
/* program = 0; */
glDeleteShader(vertex);
glDeleteShader(fragment);
/* glDeleteShader(vertex); */
/* glDeleteShader(fragment); */
g_free(vertex_shader);
g_free(fragment_shader);
/* g_free(vertex_shader); */
/* g_free(fragment_shader); */
return false;
}
/* return false; */
/* } */
/* Get the location of the "mvp" uniform */
m = glGetUniformLocation(program, "model_matrix");
v = glGetUniformLocation(program, "view_matrix");
p = glGetUniformLocation(program, "projection_matrix");
/* m = glGetUniformLocation(program, "model_matrix"); */
/* v = glGetUniformLocation(program, "view_matrix"); */
/* p = glGetUniformLocation(program, "projection_matrix"); */
glDetachShader(program, vertex);
glDetachShader(program, fragment);
/* glDetachShader(program, vertex); */
/* glDetachShader(program, fragment); */
glDeleteShader(vertex);
glDeleteShader(fragment);
/* glDeleteShader(vertex); */
/* glDeleteShader(fragment); */
entry->program = program;
entry->m = m;
entry->v = v;
entry->p = p;
/* entry->program = program; */
/* entry->m = m; */
/* entry->v = v; */
/* entry->p = p; */
g_free(vertex_shader);
g_free(fragment_shader);
/* g_free(vertex_shader); */
/* g_free(fragment_shader); */
return true;
}
/* return true; */
/* } */
/*
* Draws the current buffer to a gl_area
@ -788,12 +789,6 @@ void main_test_graphics (void)
arrows_nb = set_arrow (arrows, arrows_nb, 0, 0, 2, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 0, 1, 1, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 0, 1, 3, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 1, 0, 1, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 1, 1, 2, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 8, 1, 3, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 8, 1, 3, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 0, 1, 3, 0, 0);
arrows_nb = set_arrow (arrows, arrows_nb, 0, 1, 1, 0, 0);
int weight = 0, site = 0,
stx = space_Z * space_Y, sty = space_Z, stz = 1, arrow_offset = 0;

View File

@ -0,0 +1,156 @@
/*
* Gem-graph OpenGL experiments
*
* Desc: GL functions
*
* 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/>.
*/
#include "../../include/base.h"
#include "../../include/ui.h"
#include "../../include/graphics.h"
#include "../../include/shaders_study.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"
static volatile int buffer_vertex_size = 0;
static volatile int buffer_lines_size = 0;
static volatile int buffer_colors_size = 0;
static volatile int buffer_plans_size = 0;
/*
* Dynamic array of ptrs to dynamically allocated gl_area_entry
*/
// struct gl_area_entry **gl_area_array = NULL;
/*
* 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)
{
char *vertex_shader;
char *fragment_shader;
struct gl_area_entry *entry;
int status;
GLuint vertex, fragment;
GLuint program = 0;
GLuint m = 0;
GLuint v = 0;
GLuint p = 0;
entry = graphics_find_glarea_from_ptr(gl_area);
if (entry == NULL) {
errno = EFAULT;
perror("gl_area_array entry not found");
return false;
}
// Load vertex shader file
vertex_shader = read_file(VERTEX_SHADER_FILE);
if (vertex_shader == NULL)
return false;
vertex = create_shader(GL_VERTEX_SHADER, vertex_shader);
// ! WARNING ! l'instruction suivante (ou un autre 'free') a pu provoquer un 'double free' (Abandon)
if(vertex == 0) {
entry->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(GL_FRAGMENT_SHADER, fragment_shader);
if(fragment == 0) {
glDeleteShader(vertex);
entry->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);
entry->program = program;
entry->m = m;
entry->v = v;
entry->p = p;
g_free(vertex_shader);
g_free(fragment_shader);
return true;
}