From c5610b444bd4fba68851c82708983b1b3142572c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Wed, 24 Jun 2020 23:43:13 +0200 Subject: [PATCH] Add opengl shader program class --- lib/CMakeLists.txt | 2 +- .../include/shimatta-opengl-program.h | 75 +++++++++++++++++++ shimatta-opengl/src/shimatta-opengl-program.c | 75 +++++++++++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index c208d70..abfc3f9 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -12,6 +12,6 @@ aux_source_directory("src" SRC_DIR) add_library(${PROJECT_NAME} SHARED ${SRC_DIR}) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) target_include_directories(${PROJECT_NAME} PRIVATE ${EPOXY_INCLUDE_DIRS}) -target_link_libraries(${PROJECT_NAME} PUBLIC ${GLIB_LDFLAGS} ${GTK3_LDFLAGS} ${EPOXY_LDFLAGS}) +target_link_libraries(${PROJECT_NAME} PUBLIC ${GLIB_LDFLAGS} ${GTK3_LDFLAGS} ${EPOXY_LDFLAGS} shimatta-opengl) target_link_directories(${PROJECT_NAME} PUBLIC ${GLIB_LINK_DIRS} ${GTK3_LINK_DIRS} ${EPOXY_LINK_DIRS}) target_include_directories(${PROJECT_NAME} PUBLIC ${GLIB_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS}) diff --git a/shimatta-opengl/include/shimatta-opengl-program.h b/shimatta-opengl/include/shimatta-opengl-program.h index e69de29..6ba1bca 100644 --- a/shimatta-opengl/include/shimatta-opengl-program.h +++ b/shimatta-opengl/include/shimatta-opengl-program.h @@ -0,0 +1,75 @@ +/* + * This file is part of Shimatta OpenGL. + * +* Foobar is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Shimatta OpenGL 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GtkGraphView. If not, see . +*/ + +#include +#include + +G_BEGIN_DECLS + +#define SHIMATTA_TYPE_OPENGL_PROGRAM (shimatta_opengl_program_get_type()) + +G_DECLARE_FINAL_TYPE(ShimattaOpenglProgram, shimatta_opengl_program, SHIMATTA, OPENGL_PROGRAM, GObject); + +/** + * @brief generate a new shader program using the source code for the shaders + * @param vertex_shader Vertex shader source code + * @param geometry_shader Geometry shader source code + * @param fragment_shader Fragement shader source code + * @return New shader program + */ +ShimattaOpenglProgram *shimatta_opengl_program_new_from_data(const char *vertex_shader, + const char *geometry_shader, + const char *fragment_shader); + +/** + * @brief shimatta_opengl_program_new_from_file + * @param vertex_shader Source file for the vertex shader + * @param geometry_shader Source file for the geometry shader + * @param fragment_shader source file for the fragment shader + * @return New shader program + * @note The files have tobe accessible when calling @ref shimatta_opengl_program_compile + */ +ShimattaOpenglProgram *shimatta_opengl_program_new_from_file(const char *vertex_shader, + const char *geometry_shader, + const char *fragment_shader); + +/** + * @brief Compile the OpenGL Program + * @param program Shader Program + * @param error_text Error message from compilation. May be NULL. + * @param error_text_size Size in bytes of the error_text buffer. May be 0. + * @return 0 if successful. If Error error text will hold the error + */ +int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_text, size_t error_text_size); + +/** + * @brief Active shader program. + * + * The activation only works, if the program is compiled successfully + * + * @param program Shader program + * @return 0 if successful + */ +int shimatta_opengl_program_use(ShimattaOpenglProgram *program); + +/** + * @brief Check if shader program is compiled + * @param program Shader program + * @return true if successfully built + */ +gboolean shimatta_opengl_program_is_compiled(ShimattaOpenglProgram *program); + +G_END_DECLS diff --git a/shimatta-opengl/src/shimatta-opengl-program.c b/shimatta-opengl/src/shimatta-opengl-program.c index e69de29..c7af5a9 100644 --- a/shimatta-opengl/src/shimatta-opengl-program.c +++ b/shimatta-opengl/src/shimatta-opengl-program.c @@ -0,0 +1,75 @@ +/* + * This file is part of Shimatta OpenGL. + * +* Foobar is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Shimatta OpenGL 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GtkGraphView. If not, see . +*/ + +#include +#include + +struct _ShimattaOpenglProgram { + GObject super; +}; + +typedef struct { + GLint shader_id; + gboolean compiled; + char *fragment_file; + char *vertex_file; + char *geometry_file; + char *fragment_src; + char *vertex_src; + char *geometry_src; + +} ShimattaOpenglProgramPrivate; + +G_DEFINE_TYPE_WITH_PRIVATE(ShimattaOpenglProgram, shimatta_opengl_program, G_TYPE_OBJECT); + +static void shimatta_opengl_program_class_init(ShimattaOpenglProgramClass *klass) +{ + (void)klass; + + return; +} + +static void shimatta_opengl_program_init(ShimattaOpenglProgram *self) +{ + ShimattaOpenglProgramPrivate *priv; + + priv = shimatta_opengl_program_get_instance_private(self); + + priv->shader_id = 0; + priv->vertex_src = NULL; + priv->vertex_file = NULL; + priv->fragment_src = NULL; + priv->geometry_src = NULL; + priv->fragment_file = NULL; + priv->geometry_file = NULL; + priv->compiled = false; +} + +ShimattaOpenglProgram *shimatta_opengl_program_new_from_data(const char *vertex_shader, const char *geometry_shader, + const char *fragment_shader) +{ + ShimattaOpenglProgram *ret; + ShimattaOpenglProgramPrivate *priv; + + ret = g_object_new(SHIMATTA_TYPE_OPENGL_PROGRAM, NULL); + priv = shimatta_opengl_program_get_instance_private(ret); + + priv->fragment_src = g_strdup(fragment_shader); + priv->geometry_src = g_strdup(geometry_shader); + priv->vertex_src= g_strdup(vertex_shader); + + return ret; +}