commit d8d8867c136613e8be91b76894b3e1711f852595 parent 758e00f61b4e288a2a8f7c6e9b9bcf8574b08226 Author: Michael Savage <mikejsavage@gmail.com> Date: Sat Dec 3 22:21:27 +0200 Add alpha blending to the renderer Diffstat:
hm.cc | | | 4 | ---- |
renderer.cc | | | 9 | +++++++++ |
renderer.h | | | 1 | + |
text_renderer.cc | | | 5 | +++-- |
diff --git a/hm.cc b/hm.cc @@ -180,8 +180,6 @@ extern "C" GAME_FRAME( game_frame ) { immediate_render_state.depth_func = DEPTHFUNC_DISABLED; immediate_render( &game->test_immediate, immediate_render_state ); - glEnable( GL_BLEND ); - glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); ImmediateContext imm; ImmediateTriangle asdf[ 32 ]; immediate_init( &imm, asdf, 32 ); @@ -191,6 +189,4 @@ extern "C" GAME_FRAME( game_frame ) { snprintf( buf, sizeof( buf ), "Frame time: %.1fms FPS: %.1f", dt * 1000.0f, 1.0f / dt ); draw_text( buf, 2.0f, 2.0f, 16.0f ); } - - glDisable( GL_BLEND ); } diff --git a/renderer.cc b/renderer.cc @@ -501,6 +501,15 @@ void renderer_draw_mesh( const Mesh & mesh, RenderState state ) { glDepthFunc( depthfunc_to_glenum( state.depth_func ) ); } + // alpha blending + if( state.enable_alpha_blending ) { + glEnable( GL_BLEND ); + glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); + } + else { + glDisable( GL_BLEND ); + } + // TODO: missing a bunch of stuff here GLenum primitive = primitivetype_to_glenum( mesh.primitive_type ); diff --git a/renderer.h b/renderer.h @@ -68,6 +68,7 @@ struct RenderState { CullFace cull_face; bool disable_depth_writes; bool disable_colour_writes; + bool enable_alpha_blending; }; enum PrimitiveType { diff --git a/text_renderer.cc b/text_renderer.cc @@ -33,8 +33,8 @@ static const char * frag_src = GLSL( out vec4 screen_colour; void main() { - float r = texture( atlas, frag_uv ).r; - screen_colour = vec4( frag_colour.rgb, frag_colour.a * r ); + float alpha = texture( atlas, frag_uv ).r; + screen_colour = vec4( frag_colour.rgb, frag_colour.a * alpha ); } ); @@ -152,6 +152,7 @@ void draw_text( const char * str, float x, float y, float pixel_size ) { render_state.shader = shader; render_state.textures[ 0 ] = sizes[ size_idx ].atlas; render_state.depth_func = DEPTHFUNC_DISABLED; + render_state.enable_alpha_blending = true; // render_state.disable_depth_writes = true; immediate_render( &imm, render_state );