medfall

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

commit 2ea9a2a3c2f58c8578c15c9c71d167d7d1486212
parent b825d9fab38b6e43e54beff7822ea1ce57456044
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Sep  2 15:43:12 -0700

Better font shader

Diffstat:
game.h | 6++++++
hm.cc | 42+++++++++++++++++++++++++++++++++++++++---
2 files changed, 45 insertions(+), 3 deletions(-)
diff --git a/game.h b/game.h @@ -114,6 +114,12 @@ struct GameState { GLint test_at_colour; GLint test_un_VP; + GLuint font_shader; + GLint font_at_position; + GLint font_at_colour; + GLint font_at_uv; + GLint font_un_atlas; + GLuint test_tex_shader; GLint test_tex_at_pos; GLint test_tex_at_colour; diff --git a/hm.cc b/hm.cc @@ -65,6 +65,35 @@ static const GLchar * textured_frag_src = GLSL( } ); +static const GLchar * const font_vert_src = GLSL( + in vec3 position; + in vec4 colour; + in vec2 uv; + + out vec4 frag_colour; + out vec2 frag_uv; + + void main() { + gl_Position = vec4( position, 1.0 ); + frag_colour = colour; + frag_uv = uv; + } +); + +static const GLchar * font_frag_src = GLSL( + in vec4 frag_colour; + in vec2 frag_uv; + + uniform sampler2D atlas; + + out vec4 screen_colour; + + void main() { + float r = texture( atlas, frag_uv ).r; + screen_colour = vec4( frag_colour.rgb, frag_colour.a * r ); + } +); + glm::vec3 angles_to_vector( const glm::vec3 & angles ) { return glm::vec3( -sin( angles.y ) * sin( angles.x ), @@ -170,12 +199,18 @@ extern "C" GAME_INIT( game_init ) { state->test_tex_at_colour = glGetAttribLocation( state->test_tex_shader, "colour" ); state->test_tex_at_uv = glGetAttribLocation( state->test_tex_shader, "uv" ); state->test_tex_un_tex = glGetUniformLocation( state->test_tex_shader, "tex" ); + + state->font_shader = compile_shader( font_vert_src, font_frag_src, "screen_colour" ); + state->font_at_position = glGetAttribLocation( state->font_shader, "position" ); + state->font_at_colour = glGetAttribLocation( state->font_shader, "colour" ); + state->font_at_uv = glGetAttribLocation( state->font_shader, "uv" ); + state->font_un_atlas = glGetUniformLocation( state->font_shader, "atlas" ); } static void draw_string( const GameState * state, float x, float y, const char * str, - const GLint at_pos, const GLint at_colour, const GLint at_uv, const GLint un_tex + const GLint at_pos, const GLint at_colour, const GLint at_uv, const GLint un_atlas ) { const float scale = 1.0f / 32.0f / 5.0f; const glm::vec4 white( 1, 1, 1, 1 ); @@ -201,7 +236,7 @@ static void draw_string( const GameState * state, str++; } - immediate_render( &imm, at_pos, at_colour, true, at_uv, un_tex ); + immediate_render( &imm, at_pos, at_colour, true, at_uv, un_atlas ); glBindTexture( GL_TEXTURE_2D, 0 ); } @@ -293,8 +328,9 @@ extern "C" GAME_FRAME( game_frame ) { glDeleteTextures( 1, &tex ); glBindTexture( GL_TEXTURE_2D, 0 ); + glUseProgram( state->font_shader ); // TODO: the positions don't make sense - draw_string( state, -140, -100, "hello", state->test_tex_at_pos, state->test_tex_at_colour, state->test_tex_at_uv, state->test_tex_un_tex ); + draw_string( state, -140, -100, "hello", state->font_at_position, state->font_at_colour, state->font_at_uv, state->font_un_atlas ); glUseProgram( 0 ); glDisable( GL_BLEND );