medfall

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

commit c4f0d20b3e2bbfcaff5271eecdceeb265b677539
parent 84fe415c8713d41cf2086dc635a13e5ab5b3e96f
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Nov 27 14:18:33 +0200

Make the immediate renderer use the new renderer API

Diffstat:
bsp.cc | 29++++++++++++++++++++---------
game.h | 10----------
hm.cc | 47+++++++++++++++--------------------------------
immediate.cc | 54+++++++++++++++++++++++-------------------------------
immediate.h | 7+++----
mod_btt.cc | 6++----
shadow_map.cc | 10+++-------
7 files changed, 66 insertions(+), 97 deletions(-)
diff --git a/bsp.cc b/bsp.cc @@ -21,11 +21,12 @@ static const char * vert_src = GLSL( out vec3 frag_colour; layout( std140 ) uniform v_hot { - mat4 VP; + mat4 V; + mat4 P; }; void main() { - gl_Position = VP * vec4( position, 1.0 ); + gl_Position = P * V * vec4( position, 1.0 ); frag_colour = colour; } ); @@ -331,12 +332,15 @@ extern "C" GAME_INIT( game_init ) { test_ub = renderer_new_ub(); } -static m4 camera_to_vp( v3 position, v3 angles ) { - const m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); - - return m4_translation( -position ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ) * P; +static m4 camera_to_view( v3 position, v3 angles ) { + return m4_translation( -position ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ); } +struct FSData { + m4 V; + m4 P; +}; + extern "C" GAME_FRAME( game_frame ) { const int fb = input->keys[ 'w' ] - input->keys[ 's' ]; const int lr = input->keys[ 'a' ] - input->keys[ 'd' ]; @@ -353,10 +357,14 @@ extern "C" GAME_FRAME( game_frame ) { const v3 sideways = v3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); game->pos += sideways * 100.0f * dt * ( float ) lr; game->pos.z += ( float ) dz * 100.0f * dt; - m4 VP = camera_to_vp( game->pos, game->angles ); + + m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); + m4 V = camera_to_view( game->pos, game->angles ); + + FSData fsdata = { V, P }; renderer_begin_frame(); - renderer_ub_data( test_ub, &VP, sizeof( VP ) ); + renderer_ub_data( test_ub, &fsdata, sizeof( fsdata ) ); RenderState render_state = { }; render_state.shader = game->test_shader; @@ -381,7 +389,10 @@ extern "C" GAME_FRAME( game_frame ) { } } - immediate_render( &imm, game->test_at_position, game->test_at_colour ); + RenderState immediate_render_state = { }; + immediate_render_state.shader = game->test_shader; + immediate_render_state.ubs[ UB_VS_HOT ] = test_ub; + immediate_render( &imm, immediate_render_state ); const v4 checkerboard[] = { v4( 0, 0, 0, 0.5 ), diff --git a/game.h b/game.h @@ -41,25 +41,15 @@ struct GameState { WorkQueue background_tasks; Shader test_shader; - GLint test_at_position; - GLint test_at_colour; GLint test_un_VP; Shader font_shader; - GLint font_at_position; - GLint font_at_colour; - GLint font_at_uv; GLint font_un_atlas; Shader test_tex_shader; - GLint test_tex_at_pos; - GLint test_tex_at_colour; - GLint test_tex_at_uv; GLint test_tex_un_tex; Shader test_outline_shader; - GLint test_outline_at_position; - GLint test_outline_at_colour; GLint test_outline_un_vp; Skybox skybox; diff --git a/hm.cc b/hm.cc @@ -65,7 +65,7 @@ static const char * textured_frag_src = GLSL( static const char * font_vert_src = GLSL( in vec3 position; in vec4 colour; - in vec2 uv; + in vec2 tex_coord0; out vec4 frag_colour; out vec2 frag_uv; @@ -73,7 +73,7 @@ static const char * font_vert_src = GLSL( void main() { gl_Position = vec4( position, 1.0 ); frag_colour = colour; - frag_uv = uv; + frag_uv = tex_coord0; } ); @@ -123,8 +123,6 @@ extern "C" GAME_INIT( game_init ) { terrain_teleport( &game->tm, game->pos ); game->test_shader = renderer_new_shader( vert_src, frag_src ); - game->test_at_position = glGetAttribLocation( game->test_shader, "position" ); - game->test_at_colour = glGetAttribLocation( game->test_shader, "colour" ); game->test_sun = 0.3f; @@ -164,51 +162,41 @@ extern "C" GAME_INIT( game_init ) { // TODO // u8 * arial = file_get_contents( "Arial.ttf" ); - // + // // u8 * font_memory = memarena_push_size( &mem->persistent_arena, 512 * 256 ); // const int offset = stbtt_GetFontOffsetForIndex( arial, 0 ); // const int ok = stbtt_BakeFontBitmap( arial, offset, 48, font_memory, 512, 512, ' ', 127 - ' ', game->test_chars ); // assert( ok > 0 ); - // + // // free( arial ); - + // // BitmapData font_bitmap = { }; // font_bitmap.width = 512; // font_bitmap.height = 256; // font_bitmap.data = font_memory; - // + // // Asset font_asset = { }; // font_asset.type = AT_BITMAP; // font_asset.bitmap = font_bitmap; - // + // // glGenTextures( 1, &font_asset.texture ); // glBindTexture( GL_TEXTURE_2D, font_asset.texture ); // glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, font_bitmap.width, font_bitmap.height, 0, GL_RED, GL_UNSIGNED_BYTE, font_memory ); - // + // // game->assets[ ASSET_FONT ] = font_asset; game->test_tex_shader = renderer_new_shader( textured_vert_src, textured_frag_src ); - game->test_tex_at_pos = glGetAttribLocation( game->test_tex_shader, "position" ); - game->test_tex_at_colour = glGetAttribLocation( game->test_tex_shader, "colour" ); - game->test_tex_at_uv = glGetAttribLocation( game->test_tex_shader, "uv" ); game->test_tex_un_tex = glGetUniformLocation( game->test_tex_shader, "tex" ); game->font_shader = renderer_new_shader( font_vert_src, font_frag_src ); - game->font_at_position = glGetAttribLocation( game->font_shader, "position" ); - game->font_at_colour = glGetAttribLocation( game->font_shader, "colour" ); - game->font_at_uv = glGetAttribLocation( game->font_shader, "uv" ); game->font_un_atlas = glGetUniformLocation( game->font_shader, "atlas" ); skybox_init( &game->skybox ); } -static void draw_string( const GameState * game, - float x, float y, - const char * str, - const GLint at_pos, const GLint at_colour, const GLint at_uv, const GLint un_atlas -) { +static void draw_string( const GameState * game, float x, float y, const char * str ) { const float scale = 1.0f / 32.0f / 15.0f; const v4 white( 1, 1, 1, 1 ); @@ -216,8 +204,6 @@ static void draw_string( const GameState * game, ImmediateTriangle triangles[ 256 ]; immediate_init( &imm, triangles, ARRAY_COUNT( triangles ) ); - glBindTexture( GL_TEXTURE_2D, game->assets[ ASSET_FONT ].texture ); - while( *str != '\0' ) { stbtt_aligned_quad q; stbtt_GetBakedQuad( game->test_chars, 512, 256, *str - ' ', &x, &y, &q, 1 ); @@ -233,9 +219,7 @@ static void draw_string( const GameState * game, str++; } - immediate_render( &imm, at_pos, at_colour, true, at_uv, un_atlas ); - - glBindTexture( GL_TEXTURE_2D, 0 ); + immediate_render( &imm, game->font_shader, game->assets[ ASSET_FONT ].texture ); } static m4 camera_to_view( v3 position, v3 angles ) { @@ -275,10 +259,10 @@ extern "C" GAME_FRAME( game_frame ) { skybox_render( &game->skybox, game->angles, game->test_sun ); terrain_render( &game->tm, V, P, game->test_sun, current_time ); - glDisable( GL_DEPTH_TEST ); - glUseProgram( game->test_shader ); - immediate_render( &game->test_immediate, game->test_at_position, game->test_at_colour ); - glEnable( GL_DEPTH_TEST ); + RenderState immediate_render_state = { }; + immediate_render_state.shader = game->test_shader; + immediate_render_state.depth_func = DEPTHFUNC_DISABLED; + immediate_render( &game->test_immediate, immediate_render_state ); glDisable( GL_DEPTH_TEST ); glEnable( GL_BLEND ); @@ -288,9 +272,8 @@ extern "C" GAME_FRAME( game_frame ) { ImmediateTriangle asdf[ 32 ]; immediate_init( &imm, asdf, 32 ); - glUseProgram( game->font_shader ); // TODO: the positions don't make sense - // draw_string( game, -140, -100, "hello", game->font_at_position, game->font_at_colour, game->font_at_uv, game->font_un_atlas ); + // draw_string( game, -140, -100, "hello" ); glUseProgram( 0 ); glDisable( GL_BLEND ); diff --git a/immediate.cc b/immediate.cc @@ -1,10 +1,10 @@ +#include <stddef.h> #include <math.h> -#include "glad.h" - #include "intrinsics.h" #include "linear_algebra.h" #include "immediate.h" +#include "renderer.h" void immediate_init( ImmediateContext * ctx, ImmediateTriangle * memory, size_t max_triangles ) { ctx->triangles = memory; @@ -215,41 +215,33 @@ void immediate_arrow( } } -void immediate_render( - const ImmediateContext * ctx, - GLint at_position, GLint at_colour, - bool textured, GLint at_uv, GLint un_texture -) { - GLuint vao; - glGenVertexArrays( 1, &vao ); - glBindVertexArray( vao ); - - GLuint vbo; - glGenBuffers( 1, &vbo ); - glBindBuffer( GL_ARRAY_BUFFER, vbo ); - - glBufferData( GL_ARRAY_BUFFER, ctx->num_triangles * sizeof( ImmediateTriangle ), ctx->triangles, GL_STREAM_DRAW ); +void immediate_render( const ImmediateContext * ctx, Shader shader, Texture texture ) { + RenderState render_state = { }; + render_state.shader = shader; + render_state.textures[ 0 ] = texture; - glEnableVertexAttribArray( at_position ); - glVertexAttribPointer( at_position, 3, GL_FLOAT, GL_FALSE, sizeof( ImmediateVertex ), 0 ); - - glEnableVertexAttribArray( at_colour ); - glVertexAttribPointer( at_colour, 3, GL_FLOAT, GL_FALSE, sizeof( ImmediateVertex ), ( GLvoid * ) offsetof( ImmediateVertex, colour ) ); - - if( textured ) { - glEnableVertexAttribArray( at_uv ); - glVertexAttribPointer( at_uv, 2, GL_FLOAT, GL_FALSE, sizeof( ImmediateVertex ), ( GLvoid * ) offsetof( ImmediateVertex, uv ) ); + immediate_render( ctx, render_state ); +} - glUniform1i( un_texture, 0 ); +void immediate_render( const ImmediateContext * ctx, const RenderState & render_state ) { + VB vb = renderer_new_vb( ctx->triangles, ctx->num_triangles * sizeof( ImmediateTriangle ), BUFFERUSAGE_STREAM ); + + MeshConfig mesh_config = { }; + mesh_config.unified_buffer = vb; + mesh_config.stride = sizeof( ImmediateVertex ); + mesh_config.positions_offset = checked_cast< u32 >( offsetof( ImmediateVertex, pos ) ); + mesh_config.colours_offset = checked_cast< u32 >( offsetof( ImmediateVertex, colour ) ); + mesh_config.num_vertices = ctx->num_triangles * 3; + if( render_state.textures[ 0 ] != 0 ) { + mesh_config.tex_coords0_offset = checked_cast< u32 >( offsetof( ImmediateVertex, uv ) ); } - glDrawArrays( GL_TRIANGLES, 0, ctx->num_triangles * 3 ); + Mesh mesh = renderer_new_mesh( mesh_config ); - glBindVertexArray( 0 ); - glBindBuffer( GL_ARRAY_BUFFER, 0 ); + renderer_draw_mesh( mesh, render_state ); - glDeleteBuffers( 1, &vbo ); - glDeleteVertexArrays( 1, &vao ); + renderer_delete_mesh( mesh ); + renderer_delete_vb( vb ); } void immediate_clear( ImmediateContext * ctx ) { diff --git a/immediate.h b/immediate.h @@ -1,8 +1,8 @@ #ifndef _IMMEDIATE_H_ #define _IMMEDIATE_H_ -#include "glad.h" #include "linear_algebra.h" +#include "renderer.h" struct ImmediateVertex { v3 pos; @@ -36,9 +36,8 @@ void immediate_aabb( ImmediateContext * ctx, void immediate_arrow( ImmediateContext * ctx, v3 origin, v3 direction, float length, v4 colour ); -void immediate_render( const ImmediateContext * ctx, - GLint at_position, GLint at_colour, - bool textured = false, GLint at_uv = 0, GLint un_texture = 0 ); +void immediate_render( const ImmediateContext * ctx, Shader shader, Texture texture = 0 ); +void immediate_render( const ImmediateContext * ctx, const RenderState & render_state ); void immediate_clear( ImmediateContext * ctx ); diff --git a/mod_btt.cc b/mod_btt.cc @@ -150,8 +150,6 @@ extern "C" GAME_INIT( game_init ) { game->test_shader = renderer_new_shader( terrain_config ); game->test_outline_shader = renderer_new_shader( vert_outline_src, frag_outline_src ); - game->test_outline_at_position = glGetAttribLocation( game->test_outline_shader, "position" ); - game->test_outline_at_colour = glGetAttribLocation( game->test_outline_shader, "colour" ); game->test_outline_un_vp = glGetUniformLocation( game->test_outline_shader, "vp" ); ub_vs = renderer_new_ub(); @@ -280,14 +278,14 @@ extern "C" GAME_FRAME( game_frame ) { immediate_sphere( &imm, impact, 4, v4( 1, 0, 0, 1 ) ); } else printf( "nope\n" ); - immediate_render( &imm, game->test_outline_at_position, game->test_outline_at_colour ); + immediate_render( &imm, game->test_outline_shader ); immediate_init( &imm, triangles, ARRAY_COUNT( triangles ) ); v3u32 mins = v3u32( 0, 0, qt.nodes[ 0 ].min_z ); v3u32 maxs = v3u32( qt.dim, qt.dim, qt.nodes[ 0 ].max_z ); draw_qt( &imm, AABBu32( mins, maxs ), qt.nodes, 0 ); glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); - immediate_render( &imm, game->test_outline_at_position, game->test_outline_at_colour ); + immediate_render( &imm, game->test_outline_shader ); glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); glUseProgram( 0 ); diff --git a/shadow_map.cc b/shadow_map.cc @@ -12,8 +12,6 @@ static ImmediateContext imm; static GLuint screen_vao, screen_vbo; static Shader prog; -static GLint at_position; -static GLint at_colour; static GLint un_vp; static Shader depth_prog; @@ -73,7 +71,7 @@ static const char * depth_frag_src = GLSL( } ); -static void draw_scene( GLint at_position, GLint at_colour ) { +static void draw_scene( Shader shader ) { immediate_init( &imm, triangles, ARRAY_COUNT( triangles ) ); immediate_sphere( &imm, v3( 0, 0, 5 ), 3, v4( 1, 0, 0, 1 ) ); @@ -86,7 +84,7 @@ static void draw_scene( GLint at_position, GLint at_colour ) { immediate_triangle( &imm, tl, tr, bl, v4( 0.5, 0.5, 0.5, 1.0 ) ); immediate_triangle( &imm, bl, tr, br, v4( 0.5, 0.5, 0.5, 1.0 ) ); - immediate_render( &imm, at_position, at_colour ); + immediate_render( &imm, shader ); } static v3 angles_to_vector( v3 angles ) { @@ -134,8 +132,6 @@ const u32 SHADOW_HEIGHT = 1024; extern "C" GAME_INIT( game_init ) { prog = renderer_new_shader( vert_src, frag_src ); - at_position = glGetAttribLocation( prog, "position" ); - at_colour = glGetAttribLocation( prog, "colour" ); un_vp = glGetUniformLocation( prog, "VP" ); depth_prog = renderer_new_shader( depth_vert_src, depth_frag_src ); @@ -185,7 +181,7 @@ extern "C" GAME_FRAME( game_frame ) { glViewport( 0, 0, SHADOW_WIDTH, SHADOW_HEIGHT ); glBindFramebuffer( GL_DRAW_FRAMEBUFFER, fbo ); glClear( GL_DEPTH_BUFFER_BIT ); - draw_scene( at_position, at_colour ); + draw_scene( prog ); glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 ); glViewport( 0, 0, WIDTH, HEIGHT ); glUseProgram( 0 );