From 97d11cf2b000cadb5e5e2434296e1e3d4d0fe7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sat, 27 Jun 2020 20:10:33 +0200 Subject: [PATCH] Add cglm library and add uniform setters for shader programs --- .gitmodules | 3 + CMakeLists.txt | 4 +- cglm/CMakeLists.txt | 13 +++ cglm/cglm | 1 + include/shimatta-opengl-program.h | 35 +++++++- src/shimatta-opengl-program.c | 138 +++++++++++++++++++++++++++++- 6 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 .gitmodules create mode 100644 cglm/CMakeLists.txt create mode 160000 cglm/cglm diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f89b525 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "cglm/cglm"] + path = cglm/cglm + url = https://github.com/recp/cglm.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 67e388a..f5dd07c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.5) project(shimatta-opengl LANGUAGES C) +add_subdirectory(cglm) + find_package(PkgConfig REQUIRED) pkg_search_module(GLIB REQUIRED glib-2.0) pkg_check_modules(EPOXY REQUIRED epoxy) @@ -11,7 +13,7 @@ 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} ${EPOXY_LDFLAGS}) +target_link_libraries(${PROJECT_NAME} PUBLIC ${GLIB_LDFLAGS} ${EPOXY_LDFLAGS} cglm) target_link_directories(${PROJECT_NAME} PUBLIC ${GLIB_LINK_DIRS} ${EPOXY_LINK_DIRS}) target_include_directories(${PROJECT_NAME} PUBLIC ${GLIB_INCLUDE_DIRS}) diff --git a/cglm/CMakeLists.txt b/cglm/CMakeLists.txt new file mode 100644 index 0000000..aaeb690 --- /dev/null +++ b/cglm/CMakeLists.txt @@ -0,0 +1,13 @@ +project(cglm) +cmake_minimum_required(VERSION 2.8) + +include_directories(${GLIB_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS} ${CAIRO_INCLUDE_DIRS} ${OPENCL_INCLUDE_DIRS}) +link_directories(${GLIB_LINK_DIRS} ${GTK3_LINK_DIRS} ${CAIRO_LINK_DIRS}) +add_definitions(${GLIB2_CFLAGS_OTHER}) + +aux_source_directory("cglm/src" SOURCES) +add_library(${PROJECT_NAME} STATIC ${SOURCES}) +add_compile_options(-Wall -std=gnu99 -O3 -Wstrict-aliasing=2 -fstrict-aliasing -pedantic) +target_include_directories(${PROJECT_NAME} PUBLIC "cglm/include") + +target_link_libraries(${PROJECT_NAME} m) diff --git a/cglm/cglm b/cglm/cglm new file mode 160000 index 0000000..79c44fd --- /dev/null +++ b/cglm/cglm @@ -0,0 +1 @@ +Subproject commit 79c44fdbfa35289f5346f0c6cf560be75f015357 diff --git a/include/shimatta-opengl-program.h b/include/shimatta-opengl-program.h index b10e902..8d7cb40 100644 --- a/include/shimatta-opengl-program.h +++ b/include/shimatta-opengl-program.h @@ -1,7 +1,7 @@ /* * This file is part of Shimatta OpenGL. * -* Foobar is free software: you can redistribute it and/or modify +* Shimatta OpenGL 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. * @@ -16,6 +16,7 @@ #include #include +#include G_BEGIN_DECLS @@ -72,4 +73,36 @@ int shimatta_opengl_program_use(ShimattaOpenglProgram *program); */ gboolean shimatta_opengl_program_is_compiled(ShimattaOpenglProgram *program); +/** + * @brief Get the location of a uniform of a given shader program + * @param program Shader program + * @param uniform_name Name of the uniform parameter + * @return Location. -1 if error. + */ +int shimatta_opengl_program_get_uniform_location(ShimattaOpenglProgram *program, const char *uniform_name); + +/** + * @brief Get the location of an Attribute of a given shader program + * @param program Shader program + * @param attrib_name Name of the attribute + * @return Location of attribute. -1 if error. + */ +int shimatta_opengl_program_get_attrib_location(ShimattaOpenglProgram *program, const char *attrib_name); + +void shimatta_opengl_program_set_uniform_float(ShimattaOpenglProgram *program, const char *uniform, float value); + +void shimatta_opengl_program_set_uniform_int(ShimattaOpenglProgram *program, const char *uniform, int value); + +void shimatta_opengl_program_set_uniform_vec2(ShimattaOpenglProgram *program, const char *uniform, vec2 value); + +void shimatta_opengl_program_set_uniform_vec3(ShimattaOpenglProgram *program, const char *uniform, vec3 value); + +void shimatta_opengl_program_set_uniform_vec4(ShimattaOpenglProgram *program, const char *uniform, vec4 value); + +void shimatta_opengl_program_set_uniform_mat2(ShimattaOpenglProgram *program, const char *uniform, mat2 value); + +void shimatta_opengl_program_set_uniform_mat3(ShimattaOpenglProgram *program, const char *uniform, mat3 value); + +void shimatta_opengl_program_set_uniform_mat4(ShimattaOpenglProgram *program, const char *uniform, mat4 value); + G_END_DECLS diff --git a/src/shimatta-opengl-program.c b/src/shimatta-opengl-program.c index 5cab47a..0c26ba7 100644 --- a/src/shimatta-opengl-program.c +++ b/src/shimatta-opengl-program.c @@ -1,7 +1,7 @@ /* * This file is part of Shimatta OpenGL. * -* Foobar is free software: you can redistribute it and/or modify +* Shimatta OpenGL 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. * @@ -343,3 +343,139 @@ return_value: return ret_val; } + +int shimatta_opengl_program_get_uniform_location(ShimattaOpenglProgram *program, const char *uniform_name) +{ + int ret; + ShimattaOpenglProgramPrivate *priv; + + g_return_val_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program), -1001); + if (!uniform_name) + return -1002; + + if (!shimatta_opengl_program_is_compiled(program)) { + g_warning("Trying to get Uniform location of %s for not loaded shader program", uniform_name); + return -1; + } + + priv = shimatta_opengl_program_get_instance_private(program); + ret = glGetUniformLocation(priv->shader_id, (const GLchar *)uniform_name); + + return ret; +} + +int shimatta_opengl_program_get_attrib_location(ShimattaOpenglProgram *program, const char *attrib_name) +{ + int ret; + ShimattaOpenglProgramPrivate *priv; + + g_return_val_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program), -1001); + if (!attrib_name) + return -1002; + + if (!shimatta_opengl_program_is_compiled(program)) { + g_warning("Trying to get Attribute location of %s for not loaded shader program", attrib_name); + return -1; + } + + priv = shimatta_opengl_program_get_instance_private(program); + ret = glGetAttribLocation(priv->shader_id, (const GLchar *)attrib_name); + + return ret; +} + +void shimatta_opengl_program_set_uniform_float(ShimattaOpenglProgram *program, const char *uniform, float value) +{ + int loc; + + g_return_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program)); + g_return_if_fail(uniform != NULL); + + loc = shimatta_opengl_program_get_uniform_location(program, uniform); + if (loc >= 0) + glUniform1f(loc, value); +} + +void shimatta_opengl_program_set_uniform_int(ShimattaOpenglProgram *program, const char *uniform, int value) +{ + int loc; + + g_return_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program)); + g_return_if_fail(uniform != NULL); + + loc = shimatta_opengl_program_get_uniform_location(program, uniform); + if (loc >= 0) + glUniform1i(loc, value); +} + +void shimatta_opengl_program_set_uniform_vec2(ShimattaOpenglProgram *program, const char *uniform, vec2 value) +{ + int loc; + + g_return_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program)); + g_return_if_fail(uniform != NULL); + + loc = shimatta_opengl_program_get_uniform_location(program, uniform); + if (loc >= 0) + glUniform2fv(loc, 1, value); +} + +void shimatta_opengl_program_set_uniform_vec3(ShimattaOpenglProgram *program, const char *uniform, vec3 value) +{ + int loc; + + g_return_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program)); + g_return_if_fail(uniform != NULL); + + loc = shimatta_opengl_program_get_uniform_location(program, uniform); + if (loc >= 0) + glUniform3fv(loc, 1, value); +} + +void shimatta_opengl_program_set_uniform_vec4(ShimattaOpenglProgram *program, const char *uniform, vec4 value) +{ + int loc; + + g_return_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program)); + g_return_if_fail(uniform != NULL); + + loc = shimatta_opengl_program_get_uniform_location(program, uniform); + if (loc >= 0) + glUniform4fv(loc, 1, value); +} + +void shimatta_opengl_program_set_uniform_mat2(ShimattaOpenglProgram *program, const char *uniform, mat2 value) +{ + int loc; + + g_return_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program)); + g_return_if_fail(uniform != NULL); + + loc = shimatta_opengl_program_get_uniform_location(program, uniform); + if (loc >= 0) + glUniformMatrix2fv(loc, 1, GL_FALSE, &value[0][0]); +} + +void shimatta_opengl_program_set_uniform_mat3(ShimattaOpenglProgram *program, const char *uniform, mat3 value) +{ + int loc; + + g_return_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program)); + g_return_if_fail(uniform != NULL); + + loc = shimatta_opengl_program_get_uniform_location(program, uniform); + if (loc >= 0) + glUniformMatrix3fv(loc, 1, GL_FALSE, &value[0][0]); +} + +void shimatta_opengl_program_set_uniform_mat4(ShimattaOpenglProgram *program, const char *uniform, mat4 value) +{ + int loc; + + g_return_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program)); + g_return_if_fail(uniform != NULL); + + loc = shimatta_opengl_program_get_uniform_location(program, uniform); + if (loc >= 0) + glUniformMatrix4fv(loc, 1, GL_FALSE, &value[0][0]); +}