Compare commits

..

2 Commits

6 changed files with 231 additions and 9 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "cglm/cglm"]
path = cglm/cglm
url = https://github.com/recp/cglm.git

View File

@@ -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})

13
cglm/CMakeLists.txt Normal file
View File

@@ -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)

1
cglm/cglm Submodule

Submodule cglm/cglm added at 79c44fdbfa

View File

@@ -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 <glib.h>
#include <glib-object.h>
#include <cglm/cglm.h>
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

View File

@@ -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,8 @@
#include <shimatta-opengl-program.h>
#include <epoxy/gl.h>
#include <stdio.h>
#include <stdlib.h>
struct _ShimattaOpenglProgram {
GObject super;
@@ -149,12 +151,44 @@ gboolean shimatta_opengl_program_is_compiled(ShimattaOpenglProgram *program)
return priv->compiled;
}
static char *load_shader_from_source_file(const char *src_file)
/**
* @brief This function loads the entire content of a file into a zero-terminated string
* @param src_file Path to the source file
* @return Newly created buffer containing file content. NULL in case of an error.
* @note The returend buffer has to be deleted by the caller using g_free()
*/
static gchar *load_shader_from_source_file(const char *src_file)
{
FILE *fil;
long int end_position;
gchar *file_content;
if (!src_file)
return NULL;
fil = fopen(src_file, "rb");
if (!fil) {
g_warning("Shader source file not found: %s", src_file);
return NULL;
}
/* Get length of file */
fseek(fil, 0, SEEK_END);
end_position = ftell(fil);
fseek(fil, 0, SEEK_SET);
/* Allocate memory for file content */
file_content = (gchar *)g_malloc(end_position + 1);
fread(file_content, 1, end_position, fil);
fclose(fil);
/* Append null terminator */
file_content[end_position] = '\0';
return file_content;
}
static int compile_shader(const char *src, char *error_text, size_t error_text_size, gboolean use_error,
static int compile_shader(const gchar *src, char *error_text, size_t error_text_size, gboolean use_error,
GLuint shader_type, GLuint *shader_id_out)
{
GLuint shader_id;
@@ -186,7 +220,7 @@ int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_
{
gboolean use_error = FALSE;
ShimattaOpenglProgramPrivate *priv;
char *shader_source_code;
gchar *shader_source_code;
int ret_val = 0;
int status;
GLint success;
@@ -224,7 +258,7 @@ int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_
status = compile_shader(shader_source_code, error_text, error_text_size, use_error, GL_VERTEX_SHADER,
&vertex_shader);
free(shader_source_code);
g_free(shader_source_code);
if (status) {
ret_val = -2;
@@ -251,7 +285,7 @@ int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_
status = compile_shader(shader_source_code, error_text, error_text_size, use_error, GL_FRAGMENT_SHADER,
&fragment_shader);
free(shader_source_code);
g_free(shader_source_code);
if (status) {
ret_val = -2;
@@ -278,7 +312,7 @@ int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_
status = compile_shader(shader_source_code, error_text, error_text_size, use_error, GL_GEOMETRY_SHADER,
&geometry_shader);
free(shader_source_code);
g_free(shader_source_code);
if (status) {
ret_val = -2;
@@ -309,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]);
}