aboutsummaryrefslogtreecommitdiff
path: root/src/shaderutils.c
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-06 11:57:49 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-06 11:57:49 +0000
commitacf1eb14767d5d7c5ffa3b2c9402ea459f4ffb42 (patch)
treeee0ed001cd9b9b81b2d883fdceb0094ace6442da /src/shaderutils.c
parent266a3f19778dcf92c5387341192c31979604f968 (diff)
Move shader utilities to a separate file
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@211 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src/shaderutils.c')
-rw-r--r--src/shaderutils.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/shaderutils.c b/src/shaderutils.c
new file mode 100644
index 0000000..74e9bff
--- /dev/null
+++ b/src/shaderutils.c
@@ -0,0 +1,117 @@
+/*
+ * shaderuitils.c
+ *
+ * Shader utilities
+ *
+ * (c) 2008 Thomas White <taw27@cam.ac.uk>
+ *
+ * thrust3d - a silly game
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glew.h>
+#include <stdio.h>
+
+void shaderutils_setunf(GLuint program, const char *name, GLfloat val) {
+ GLint loc;
+ loc = glGetUniformLocation(program, name);
+ glUniform1f(loc, val);
+}
+
+void shaderutils_setun2f(GLuint program, const char *name, GLfloat val1, GLfloat val2) {
+ GLint loc;
+ loc = glGetUniformLocation(program, name);
+ glUniform2f(loc, val1, val2);
+}
+
+void shaderutils_setuni(GLuint program, const char *name, GLint val) {
+ GLint loc;
+ loc = glGetUniformLocation(program, name);
+ glUniform1i(loc, val);
+}
+
+GLuint shaderutils_load_shader(const char *filename, GLenum type) {
+
+ GLuint shader;
+ char text[4096];
+ size_t len;
+ FILE *fh;
+ int l;
+ GLint status;
+
+ fh = fopen(filename, "r");
+ if ( fh == NULL ) {
+ fprintf(stderr, "Couldn't load shader '%s'\n", filename);
+ return 0;
+ }
+ len = fread(text, 1, 4095, fh);
+ fclose(fh);
+ text[len] = '\0';
+ const GLchar *source = text;
+ shader = glCreateShader(type);
+ glShaderSource(shader, 1, &source, NULL);
+ glCompileShader(shader);
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
+ if ( status == GL_FALSE ) {
+ glGetShaderInfoLog(shader, 4095, &l, text);
+ if ( l > 0 ) {
+ printf("%s\n", text); fflush(stdout);
+ } else {
+ printf("Shader compilation failed.\n");
+ }
+ }
+
+ return shader;
+
+}
+
+int shaderutils_link_program(GLuint program) {
+
+ int l;
+ GLint status;
+ char text[4096];
+
+ glLinkProgram(program);
+
+ glGetProgramiv(program, GL_LINK_STATUS, &status);
+ if ( status == GL_FALSE ) {
+ printf("Program linking errors:\n");
+ glGetProgramInfoLog(program, 4095, &l, text);
+ if ( l > 0 ) {
+ printf("%s\n", text); fflush(stdout);
+ } else {
+ printf("Program linking failed.\n");
+ }
+ }
+
+ return status;
+
+}
+
+int shaderutils_validate_program(GLuint program) {
+
+ GLint status;
+ int l;
+ char text[4096];
+
+ glValidateProgram(program);
+ glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
+ if ( status == GL_FALSE ) {
+ printf("Program validation errors:\n");
+ glGetProgramInfoLog(program, 4095, &l, text);
+ if ( l > 0 ) {
+ printf("%s\n", text); fflush(stdout);
+ } else {
+ printf("Program did not validate successfully.\n");
+ }
+ return 0;
+ }
+
+ return 1;
+
+}
+