add guMakeShaderFromFile() and guMakeShader()

This commit is contained in:
Josh Holtrop 2011-05-09 17:28:33 -04:00
parent 01f9c2e467
commit 956f249d54
2 changed files with 51 additions and 0 deletions

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
@ -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;
}

View File

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