medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f6e452b5fa8dc2520b55c43ff8434fb5d716154b
parent 255d211769e7ebbbe8a5b409583215b23b2b81bb
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Mar 25 21:58:29 +0000

Move stuff to glsl.cc

Diffstat:
glsl.cc | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
glsl.h | 72++++--------------------------------------------------------------------
2 files changed, 81 insertions(+), 68 deletions(-)
diff --git a/glsl.cc b/glsl.cc @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "opengl33.h" +#include "glsl.h" + +#include "log.h" + +// TODO: this is kinda crap because it calls delete +void check_compile_status( GLuint shader ) { + GLint ok; + glGetShaderiv( shader, GL_COMPILE_STATUS, &ok ); + + if( ok == GL_FALSE ) { + GLint len; + glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &len ); + + char * buf = ( char * ) malloc( len ); + if( buf == NULL ) { + ERROR( "malloc" ); + } + glGetShaderInfoLog( shader, len, NULL, buf ); + + fprintf( stderr, "compiling the shader failed:\n%s", buf ); + free( buf ); + + glDeleteShader( shader ); + } +} + +void check_link_status( GLuint program ) { + GLint ok; + glGetProgramiv( program, GL_LINK_STATUS, &ok ); + + if( ok == GL_FALSE ) { + GLint len; + glGetProgramiv( program, GL_INFO_LOG_LENGTH, &len ); + + char * buf = ( char * ) malloc( len ); + if( buf == NULL ) { + ERROR( "malloc" ); + } + glGetProgramInfoLog( program, len, NULL, buf ); + + fprintf( stderr, "linking the shader failed:\n%s", buf ); + free( buf ); + + glDeleteProgram( program ); + } +} + +GLuint compile_shader( const char * vert, const char * frag, const char * out ) { + GLuint vs = glCreateShader( GL_VERTEX_SHADER ); + GLuint fs = glCreateShader( GL_FRAGMENT_SHADER ); + + glShaderSource( vs, 1, &vert, NULL ); + glShaderSource( fs, 1, &frag, NULL ); + + glCompileShader( vs ); + check_compile_status( vs ); + glCompileShader( fs ); + check_compile_status( fs ); + + GLuint prog = glCreateProgram(); + + glAttachShader( prog, vs ); + glAttachShader( prog, fs ); + glBindFragDataLocation( prog, 0, out ); + glLinkProgram( prog ); + + glDeleteShader( vs ); + glDeleteShader( fs ); + + check_link_status( prog ); + + return prog; +} diff --git a/glsl.h b/glsl.h @@ -1,76 +1,12 @@ #ifndef _GLSL_H_ #define _GLSL_H_ -#include <stdio.h> -#include <stdlib.h> +#include "opengl33.h" #define GLSL( shader ) "#version 150\n" #shader -// this is taken from somewhere online -// inline void show_info_log( -// GLuint object, -// PFNGLGETSHADERIVPROC glGet__iv, -// PFNGLGETSHADERINFOLOGPROC glGet__InfoLog -// ) -// { -// GLint log_length; -// char *log; -// -// glGet__iv(object, GL_INFO_LOG_LENGTH, &log_length); -// log = ( char * ) malloc(log_length); -// glGet__InfoLog(object, log_length, NULL, log); -// fprintf(stderr, "%s", log); -// free(log); -// } - -// this is taken from somewhere online -inline void check_compile_status( const GLuint shader ) { - GLint ok; - glGetShaderiv( shader, GL_COMPILE_STATUS, &ok ); - - if( !ok ) { - fprintf( stderr, "shit bruh:\n" ); - // show_info_log( shader, glGetShaderiv, glGetShaderInfoLog ); - glDeleteShader( shader ); - } -} - -// this is taken from somewhere online -inline void check_link_status( const GLuint prog ) { - GLint ok; - glGetProgramiv(prog, GL_LINK_STATUS, &ok); - if (!ok) { - fprintf(stderr, "Failed to link shader program:\n"); - // show_info_log(prog, glGetProgramiv, glGetProgramInfoLog); - glDeleteProgram(prog); - } -} - -inline GLuint compile_shader( const char * const vert, const char * const frag, const char * const out ) { - const GLuint vs = glCreateShader( GL_VERTEX_SHADER ); - const GLuint fs = glCreateShader( GL_FRAGMENT_SHADER ); - - glShaderSource( vs, 1, &vert, NULL ); - glShaderSource( fs, 1, &frag, NULL ); - - glCompileShader( vs ); - check_compile_status( vs ); - glCompileShader( fs ); - check_compile_status( fs ); - - const GLuint prog = glCreateProgram(); - - glAttachShader( prog, vs ); - glAttachShader( prog, fs ); - glBindFragDataLocation( prog, 0, out ); - glLinkProgram( prog ); - - glDeleteShader( vs ); - glDeleteShader( fs ); - - check_link_status( prog ); - - return prog; -} +void check_compile_status( GLuint shader ); +void check_link_status( GLuint prog ); +GLuint compile_shader( const char * vert, const char * frag, const char * out ); #endif // _GLSL_H_