medfall

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

commit 2c49de0dca8ef930bd4a7c0d77926c729fe6fc44
parent 5755e41c42aaf7f1e33f171c03411dfe1e95aa60
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Aug 16 19:34:58 +0200

Move GLSL related things into a temporary header until I do a better job

Diffstat:
heightmap.cc | 74+++-----------------------------------------------------------------------
shitty_glsl.h | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 79 insertions(+), 71 deletions(-)
diff --git a/heightmap.cc b/heightmap.cc @@ -11,12 +11,11 @@ #include "heightmap.h" #include "stb_image.h" #include "stb_perlin.h" - -#define GLSL( shader ) "#version 150\n" #shader +#include "shitty_glsl.h" const float SLOPE = 0.3; -const GLchar * const vert_src = GLSL( +static const GLchar * const vert_src = GLSL( in vec3 position; in vec3 normal; in float lit; @@ -35,7 +34,7 @@ const GLchar * const vert_src = GLSL( } ); -const GLchar * frag_src = GLSL( +static const GLchar * frag_src = GLSL( in vec3 n; in float depth; in float l; @@ -64,73 +63,6 @@ const GLchar * frag_src = GLSL( } ); -// this is taken from somewhere online -static 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, nullptr, log); - fprintf(stderr, "%s", log); - free(log); -} - -// this is taken from somewhere online -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 -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); - } -} - -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; -} - float lerp( const float a, const float b, const float t ) { return a * ( 1 - t ) + b * t; } diff --git a/shitty_glsl.h b/shitty_glsl.h @@ -0,0 +1,76 @@ +#ifndef _SHITTY_H_ +#define _SHITTY_H_ + +#include <stdio.h> +#include "platform_opengl.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, nullptr, 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; +} + +#endif // _SHITTY_H_