diff options
Diffstat (limited to 'src/shader')
-rw-r--r-- | src/shader/shader.c | 198 | ||||
-rw-r--r-- | src/shader/shader.h | 100 |
2 files changed, 0 insertions, 298 deletions
diff --git a/src/shader/shader.c b/src/shader/shader.c deleted file mode 100644 index 122d59e..0000000 --- a/src/shader/shader.c +++ /dev/null @@ -1,198 +0,0 @@ -#include "shader.h" - -void honey_setup_shader(lua_State* L) -{ - honey_lua_element shader_elements[] = { - { "new", HONEY_FUNCTION, { .function = honey_shader_new } }, - { "use", HONEY_FUNCTION, { .function = honey_shader_use } }, - { "set_int", HONEY_FUNCTION, { .function = honey_shader_set_int } }, - { "set_float", HONEY_FUNCTION, { .function = honey_shader_set_float } }, - { "set_vec3", HONEY_FUNCTION, { .function = honey_shader_set_vec3 } }, - { "set_vec4", HONEY_FUNCTION, { .function = honey_shader_set_vec4 } }, - { "set_mat3", HONEY_FUNCTION, { .function = honey_shader_set_mat3 } }, - { "set_mat4", HONEY_FUNCTION, { .function = honey_shader_set_mat4 } }, - { "delete", HONEY_FUNCTION, { .function = honey_shader_delete } }, - }; - - honey_lua_create_table(L, shader_elements, 9); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_shader_new(lua_State* L) -{ - if (!honey_lua_validate_types(L, 2, HONEY_STRING, HONEY_STRING)) - lua_error(L); - - const char* vertex_shader_source = lua_tostring(L, 1); - const char* fragment_shader_source = lua_tostring(L, 2); - - int success; - char error[1024]; - - int vertex_shader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex_shader, 1, - &vertex_shader_source, NULL); - glCompileShader(vertex_shader); - glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(vertex_shader, 1024, NULL, error); - lua_pushstring(L, error); - lua_error(L); - } - - int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment_shader, 1, - &fragment_shader_source, NULL); - glCompileShader(fragment_shader); - glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(fragment_shader, 1024, NULL, error); - lua_pushstring(L, error); - lua_error(L); - } - - int shader = glCreateProgram(); - glAttachShader(shader, vertex_shader); - glAttachShader(shader, fragment_shader); - glLinkProgram(shader); - - glGetShaderiv(shader, GL_LINK_STATUS, &success); - if (!success) { - glGetShaderInfoLog(shader, 1024, NULL, error); - lua_pushstring(L, error); - lua_error(L); - } - - glDeleteShader(vertex_shader); - glDeleteShader(fragment_shader); - - lua_pushinteger(L, shader); - return 1; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_shader_use(lua_State* L) -{ - if (!honey_lua_validate_types(L, 1, HONEY_INTEGER)) - lua_error(L); - - int shader = lua_tointeger(L, 1); - glUseProgram(shader); - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_shader_set_int(lua_State* L) -{ - if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_STRING, HONEY_INTEGER)) - lua_error(L); - - int shader = lua_tointeger(L, 1); - const char* name = lua_tostring(L, 2); - int value = lua_tointeger(L, 3); - - glUseProgram(shader); - unsigned int location = glGetUniformLocation(shader, name); - glUniform1i(location, value); - - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_shader_set_float(lua_State* L) -{ - if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_STRING, HONEY_NUMBER)) - lua_error(L); - - int shader = lua_tointeger(L, 1); - const char* name = lua_tostring(L, 2); - float value = lua_tonumber(L, 3); - - glUseProgram(shader); - unsigned int location = glGetUniformLocation(shader, name); - glUniform1f(location, value); - - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -static void get_array(lua_State* L, - unsigned int* location, - float** value) -{ - if (!honey_lua_validate_types(L, 2, HONEY_INTEGER, HONEY_STRING, HONEY_USERDATA)) - lua_error(L); - - int shader = lua_tointeger(L, 1); - const char* name = lua_tostring(L, 2); - *value = lua_touserdata(L, 3); - - glUseProgram(shader); - *location = glGetUniformLocation(shader, name); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_shader_set_vec3(lua_State* L) -{ - unsigned int location; - float* array; - get_array(L, &location, &array); - - glUniform3fv(location, 1, array); - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_shader_set_vec4(lua_State* L) -{ - unsigned int location; - float* array; - get_array(L, &location, &array); - - glUniform4fv(location, 1, array); - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_shader_set_mat3(lua_State* L) -{ - unsigned int location; - float* array; - get_array(L, &location, &array); - - glUniformMatrix3fv(location, 1, GL_FALSE, array); - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_shader_set_mat4(lua_State* L) -{ - unsigned int location; - float* array; - get_array(L, &location, &array); - - glUniformMatrix4fv(location, 1, GL_FALSE, array); - return 0; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -int honey_shader_delete(lua_State* L) -{ - if (!honey_lua_validate_types(L, 1, HONEY_INTEGER)) - lua_error(L); - - int shader = lua_tointeger(L, 1); - - glDeleteProgram(shader); - return 0; -} diff --git a/src/shader/shader.h b/src/shader/shader.h deleted file mode 100644 index 95956ea..0000000 --- a/src/shader/shader.h +++ /dev/null @@ -1,100 +0,0 @@ -/** @file shader.h - * - * @brief Functions to create, manipulate, and destroy GLSL shaders. - */ - -#ifndef HONEY_SHADER_H -#define HONEY_SHADER_H - -#include "../common.h" - -/** @brief Push the shader table to the lua stack. - */ -void honey_setup_shader(lua_State* L); - -/** @brief Create a new shader from source. - * - * @param[in] vertex_source The GLSL code for the vertex shader. - * @param[in] fragment_source The GLSL code for the fragment shader. - * - * @returns OpenGL handle for the compiled shader. - */ -int honey_shader_new(lua_State* L); - -/** @brief Set a shader as the current OpenGL shader. - * - * @param[in] shader The OpenGL handle to a shader, as generated by honey.shader.new - * - * @returns Nothing. - */ -int honey_shader_use(lua_State* L); - -/** @brief Set an integer uniform on a shader. - * - * @param[in] shader The OpenGL shader handle. - * @param[in] name The name of the shader uniform as a string. - * @param[in] value The value to set the uniform to. - * - * @returns Nothing. - */ -int honey_shader_set_int(lua_State* L); - -/** @brief Set a float uniform on a shader. - * - * @param[in] shader The OpenGL shader handle. - * @param[in] name The name of the shader uniform as a string. - * @param[in] value The value to set the uniform to. - * - * @returns Nothing. - */ -int honey_shader_set_float(lua_State* L); - -/** @brief Set a vec3 uniform on a shader. - * - * @param[in] shader The OpenGL shader handle. - * @param[in] name The name of the shader uniform as a string. - * @param[in] value The value to set the uniform to. - * - * @returns Nothing. - */ -int honey_shader_set_vec3(lua_State* L); - -/** @brief Set a vec4 uniform on a shader. - * - * @param[in] shader The OpenGL shader handle. - * @param[in] name The name of the shader uniform as a string. - * @param[in] value The value to set the uniform to. - * - * @returns Nothing. - */ -int honey_shader_set_vec4(lua_State* L); - -/** @brief Set a mat3 uniform on a shader. - * - * @param[in] shader The OpenGL shader handle. - * @param[in] name The name of the shader uniform as a string. - * @param[in] value The value to set the uniform to. - * - * @returns Nothing. - */ -int honey_shader_set_mat3(lua_State* L); - -/** @brief Set a mat4 uniform on a shader. - * - * @param[in] shader The OpenGL shader handle. - * @param[in] name The name of the shader uniform as a string. - * @param[in] value The value to set the uniform to. - * - * @returns Nothing. - */ -int honey_shader_set_mat4(lua_State* L); - -/** @brief Delete a shader. - * - * @param[in] shader An OpenGL shader handle. - * - * @returns Nothing. - */ -int honey_shader_delete(lua_State* L); - -#endif |