diff --git a/glslUtil/glslUtil.c b/glslUtil/glslUtil.c index 1b8dc2e..caba5ff 100644 --- a/glslUtil/glslUtil.c +++ b/glslUtil/glslUtil.c @@ -1,4 +1,5 @@ +#include #include #include #include @@ -215,3 +216,51 @@ char *guGetProgramLog(GLuint id) } return NULL; } + +GLuint guMakeShaderFromFile(GLenum shaderType, const char *fname) +{ + char *source; + source = loadFile(fname); + if (source == NULL) + { + fprintf(stderr, "Error reading file '%s'\n", fname); + return 0; + } + GLuint id = guMakeShader(shaderType, source); + free(source); + return id; +} + +GLuint guMakeShader(GLenum shaderType, const char *source) +{ + GLuint id; + GLint status; + + id = glCreateShader(shaderType); + if (id <= 0) + { + fprintf(stderr, "Error creating shader object\n"); + goto out; + } + + glShaderSource(id, 1, &source, NULL); + + glCompileShader(id); + + glGetShaderiv(id, GL_COMPILE_STATUS, &status); + if (status != GL_TRUE) + { + fprintf(stderr, "Error compiling shader\n"); + char *log = guGetShaderLog(id); + fprintf(stderr, "Shader Log:\n%s\n", log); + free(log); + goto cleanup_shader; + } + + return id; + +cleanup_shader: + glDeleteShader(id); +out: + return 0; +} diff --git a/glslUtil/glslUtil.h b/glslUtil/glslUtil.h index d05498e..90390fd 100644 --- a/glslUtil/glslUtil.h +++ b/glslUtil/glslUtil.h @@ -32,6 +32,8 @@ void guOrtho(guMatrix4x4 *m, GLfloat near, GLfloat far); char *guGetShaderLog(GLuint id); char *guGetProgramLog(GLuint id); +GLuint guMakeShaderFromFile(GLenum shaderType, const char *fname); +GLuint guMakeShader(GLenum shaderType, const char *source); #ifdef __cplusplus }