medfall

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

commit 3dcef9750ed00938cdada9cd94d4db6915fca628
parent 50223ff960d63d667227467230788d1d1f580f85
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Oct 29 13:43:18 +0300

Use the new renderer API in BSPRenderer, start removing GLM from BSP code, const madness

Diffstat:
Makefile | 2+-
bsp.cc | 51++++++++++++++++++++++++++++++++++++---------------
bsp.h | 21++++++++++++---------
bsp_renderer.cc | 159+++++++++++++++++++++++++++++++------------------------------------------------
bsp_renderer.h | 14+++++---------
5 files changed, 116 insertions(+), 131 deletions(-)
diff --git a/Makefile b/Makefile @@ -11,7 +11,7 @@ sound: audio.o mixer.o log.o memory_arena.o wave.o platform_audio_output.o srv: server/main.o rng/well512.o # Module dependencies -bsp.so: bsp.o bsp_renderer.o +bsp.so: bsp.o bsp_renderer.o renderer.o hm.so: hm.o heightmap.o terrain_manager.o lz4.o btt.o gpubtt.o skybox.o btt.so: mod_btt.o btt.o gpubtt.o heightmap.o skybox.o stb_image.o lz4.o sm.so: shadow_map.o diff --git a/bsp.cc b/bsp.cc @@ -1,7 +1,5 @@ #include <math.h> -#include "glad.h" -#include "glsl.h" #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> @@ -9,6 +7,7 @@ #include "game.h" #include "intrinsics.h" #include "log.h" +#include "renderer.h" #include "bsp.h" #include "bsp_renderer.h" @@ -25,6 +24,9 @@ static const GLchar * const vert_src = GLSL( out vec3 frag_colour; + // layout( std140 ) uniform uniforms { + // mat4 VP; + // }; uniform mat4 VP; void main() { @@ -50,8 +52,16 @@ bool same_sign( const float a, const float b ) { return a * b >= 0; } -float point_plane_distance( const glm::vec3 point, const glm::vec3 normal, const float d ) { - return glm::dot( point, normal ) - d; +float point_plane_distance( v3 point, v3 normal, float d ) { + return dot( point, normal ) - d; +} + +float point_plane_distance( glm::vec3 point, glm::vec3 normal, float d ) { + return point_plane_distance( GLMV3( point ), GLMV3( normal ), d ); +} + +float point_plane_distance( glm::vec3 point, v3 normal, float d ) { + return point_plane_distance( GLMV3( point ), normal, d ); } void bsp_init( BSP * bsp, const char * filename ) { @@ -124,7 +134,7 @@ bool BSP::trace_seg_brush( const BSP_Brush & brush, const glm::vec3 start, const if( start_dist >= 0.0f ) starts_inside = false; - const float denom = -glm::dot( plane.n, dir ); + const float denom = -dot( plane.n, GLMV3( dir ) ); const float t = start_dist / denom; if( t >= tmin && t <= tmax ) { @@ -203,7 +213,7 @@ void BSP::trace_seg_tree( const s32 node_idx, const glm::vec3 start, const glm:: // start . plane.n + t * dir . plane.n = plane.d // t * dir . plane.n = plane.d - start . plane.n // t * denom = dist - const float denom = glm::dot( plane.n, dir ); + const float denom = dot( plane.n, GLMV3( dir ) ); const float dist = -point_plane_distance( start, plane.n, plane.d ); bool near_child = dist > 0.0f; bool check_both_sides = false; @@ -256,7 +266,7 @@ bool BSP::trace_seg( const glm::vec3 & start, const glm::vec3 & end, Intersectio return bis.hit; } -BSP_Leaf & BSP::position_to_leaf( const glm::vec3 & pos ) const { +BSP_Leaf & BSP::position_to_leaf( const v3 & pos ) const { s32 node_idx = 0; do { @@ -271,8 +281,8 @@ BSP_Leaf & BSP::position_to_leaf( const glm::vec3 & pos ) const { return leaves[ -( node_idx + 1 ) ]; } -glm::vec3 d2r( const glm::vec3 & degrees ) { - return degrees * static_cast< float >( M_PI / 180 ); +v3 d2r( v3 degrees ) { + return degrees * float( M_PI ) / 180.0f; } glm::vec3 angles_to_vector( const glm::vec3 & angles ) { @@ -285,25 +295,27 @@ glm::vec3 angles_to_vector( const glm::vec3 & angles ) { static const glm::mat4 P( glm::perspective( glm::radians( 120.0f ), 640.0f / 480.0f, 0.1f, 10000.0f ) ); +static UB test_ub; + extern "C" GAME_INIT( game_init ) { bsp_init( &game->bsp, "acidwdm2.bsp" ); MemoryArena arena = memarena_push_arena( &mem->persistent_arena, megabytes( 10 ) ); game->pos = glm::vec3( 0, -100, 450 ); - game->angles = glm::radians( ( glm::vec3( -90, 135, 0 ) ) ); + game->angles = glm::radians( glm::vec3( -90, 135, 0 ) ); - game->test_shader = compile_shader( vert_src, frag_src, "screen_colour" ); + 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_un_VP = glGetUniformLocation( game->test_shader, "VP" ); - bspr_init( &game->bspr, &arena, &game->bsp, game->test_at_position, game->test_at_colour ); + bspr_init( &game->bspr, &arena, &game->bsp ); + + test_ub = renderer_new_ub(); } extern "C" GAME_FRAME( game_frame ) { - glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); - const int fb = input->keys[ 'w' ] - input->keys[ 's' ]; const int lr = input->keys[ 'a' ] - input->keys[ 'd' ]; const int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; @@ -332,10 +344,17 @@ extern "C" GAME_FRAME( game_frame ) { -game->pos ); + // TODO: remove this when we switch to UBs glUseProgram( game->test_shader ); glUniformMatrix4fv( game->test_un_VP, 1, GL_FALSE, glm::value_ptr( VP ) ); - bspr_render( &game->bspr, game->pos ); + renderer_begin_frame(); + renderer_ub_data( test_ub, glm::value_ptr( VP ), sizeof( VP ) ); + + RenderState render_state = { }; + render_state.shader = game->test_shader; + render_state.ubs[ 0 ] = test_ub; + bspr_render( &game->bspr, GLMV3( game->pos ), render_state ); immediate_init( &imm, triangles, array_count( triangles ) ); immediate_sphere( &imm, glm::vec3( 0, 0, 0 ), 128, glm::vec4( 1, 1, 0, 1 ) ); @@ -356,4 +375,6 @@ extern "C" GAME_FRAME( game_frame ) { } immediate_render( &imm, game->test_at_position, game->test_at_colour ); + + renderer_end_frame(); } diff --git a/bsp.h b/bsp.h @@ -2,7 +2,10 @@ #define _BSP_H_ #include <string> +#include <glm/glm.hpp> + #include "intrinsics.h" +#include "linear_algebra.h" enum BSP_Lump { LUMP_ENTITIES = 0, @@ -45,7 +48,7 @@ struct BSP_Texture { }; struct BSP_Plane { - glm::vec3 n; + v3 n; float d; }; @@ -96,9 +99,9 @@ struct BSP_BrushSide { }; struct BSP_Vertex { - glm::vec3 pos; + v3 pos; float uv[ 2 ][ 2 ]; - glm::vec3 normal; + v3 normal; u8 rgba[ 4 ]; }; @@ -119,11 +122,11 @@ struct BSP_Face { s32 lightmap; s32 lmpos[ 2 ]; s32 lmsize[ 2 ]; - glm::vec3 lmorigin; - glm::vec3 a; - glm::vec3 b; + v3 lmorigin; + v3 a; + v3 b; - glm::vec3 normal; + v3 normal; s32 c; s32 d; }; @@ -198,13 +201,13 @@ public: // TODO // void trace_seg_brush( const BSP_Brush & brush, BSP_Intersection & bis ) const; // void trace_seg_leaf( const s32 leaf_idx, BSP_Intersection & bis ) const; - // void trace_seg_tree( const s32 node_idx, const glm::vec3 & start, const glm::vec3 & end, const float t1, const float t2, BSP_Intersection & bis ) const; + // void trace_seg_tree( const s32 node_idx, const v3 & start, const v3 & end, const float t1, const float t2, BSP_Intersection & bis ) const; bool trace_seg_brush( const BSP_Brush & brush, const glm::vec3 start, const glm::vec3 dir, const float tmin, const float tmax, float * tout ) const; void trace_seg_leaf( const u32 leaf_idx, const glm::vec3 start, const glm::vec3 dir, const float tmin, const float tmax, BSP_Intersection & bis ) const; void trace_seg_tree( const s32 node_idx, const glm::vec3 start, const glm::vec3 dir, const float tmin, const float tmax, BSP_Intersection & bis ) const; - BSP_Leaf & position_to_leaf( const glm::vec3 & pos ) const; + BSP_Leaf & position_to_leaf( const v3 & pos ) const; public: bool trace_seg( const glm::vec3 & start, const glm::vec3 & end, Intersection & is ) const; diff --git a/bsp_renderer.cc b/bsp_renderer.cc @@ -1,154 +1,121 @@ #include <string.h> -#include "glad.h" -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> - #include "intrinsics.h" +#include "linear_algebra.h" #include "bsp.h" #include "bsp_renderer.h" #include "memory_arena.h" +#include "renderer.h" -glm::vec3 bspr_face_colour( const BSP * const bsp, const BSP_Face & face ) { +v3 bspr_face_colour( const BSP * bsp, const BSP_Face & face ) { if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_olive033") == 0) { - return glm::vec3(0, 0.33, 0); + return v3(0, 0.33, 0); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_olive066") == 0) { - return glm::vec3(0, 0.66, 0); + return v3(0, 0.66, 0); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_olive100") == 0) { - return glm::vec3(0, 1, 0); + return v3(0, 1, 0); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_olive100_fade") == 0) { - return glm::vec3(0.25, 1, 0.25); + return v3(0.25, 1, 0.25); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_red100_fade") == 0) { - return glm::vec3(1, 0.25, 0.25); + return v3(1, 0.25, 0.25); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_red100") == 0) { - return glm::vec3(1, 0, 0); + return v3(1, 0, 0); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_red066") == 0) { - return glm::vec3(0.66, 0, 0); + return v3(0.66, 0, 0); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_red033") == 0) { - return glm::vec3(0.33, 0, 0); + return v3(0.33, 0, 0); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_purple033") == 0) { - return glm::vec3(0.33, 0, 0.33); + return v3(0.33, 0, 0.33); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_purple066") == 0) { - return glm::vec3(0.66, 0, 0.66); + return v3(0.66, 0, 0.66); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_purple100") == 0) { - return glm::vec3(1, 0, 1); + return v3(1, 0, 1); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_purple100_fade") == 0) { - return glm::vec3(1, 0.25, 1); + return v3(1, 0.25, 1); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_cyan033") == 0) { - return glm::vec3(0, 0.33, 0.33); + return v3(0, 0.33, 0.33); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_cyan066") == 0) { - return glm::vec3(0, 0.66, 0.66); + return v3(0, 0.66, 0.66); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_cyan100") == 0) { - return glm::vec3(0, 1, 1); + return v3(0, 1, 1); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_cyan100_fade") == 0) { - return glm::vec3(0.25, 1, 1); + return v3(0.25, 1, 1); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/struc_lightgrey") == 0) { - //return glm::vec3(0.85, 0.85, 0.85); - return glm::vec3(0.15, 0.15, 0.15); + //return v3(0.85, 0.85, 0.85); + return v3(0.15, 0.15, 0.15); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/struc_darkgrey") == 0) { - return glm::vec3(0.5, 0.5, 0.5); + return v3(0.5, 0.5, 0.5); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/sky_black") == 0) { - return glm::vec3(0, 0, 0); + return v3(0, 0, 0); } else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/ink") == 0) { - //return glm::vec3(0, 0, 0); - return glm::vec3( 0, 0, 0 ); + //return v3(0, 0, 0); + return v3( 0, 0, 0 ); } else { - //return glm::vec3( 1, 1, 1 ); - return glm::vec3( 0, 0, 0 ); + //return v3( 1, 1, 1 ); + return v3( 0, 0, 0 ); } } -void bspr_init( BSPRenderer * const bspr, MemoryArena * const arena, const BSP * const bsp, - const GLint at_position, const GLint at_colour -) { +void bspr_init( BSPRenderer * bspr, MemoryArena * arena, const BSP * bsp ) { bspr->arena = arena; bspr->bsp = bsp; + bspr->meshes = memarena_push_many( arena, Mesh, bsp->num_leaves ); - bspr->vaos = memarena_push_many( arena, GLuint, bsp->num_leaves ); - bspr->vbos = memarena_push_many( arena, GLuint, bsp->num_leaves * 2 ); - bspr->ebos = memarena_push_many( arena, GLuint, bsp->num_leaves ); - bspr->vertex_counts = memarena_push_many( arena, u32, bsp->num_leaves ); - - glGenVertexArrays( bsp->num_leaves, bspr->vaos ); - glGenBuffers( bsp->num_leaves * 2, bspr->vbos ); - glGenBuffers( bsp->num_leaves, bspr->ebos ); - - glm::vec3 * pos_scratch = memarena_push_many( arena, glm::vec3, 2048 ); - u32 pos_scratch_used; - - glm::vec3 * colour_scratch = memarena_push_many( arena, glm::vec3, 2048 ); - u32 colour_scratch_used; - - GLuint * ebo_scratch = memarena_push_many( arena, GLuint, 2048 ); - u32 ebo_scratch_used; + MEMARENA_SCOPED_CHECKPOINT( arena ); + array< v3 > positions = memarena_push_array( arena, v3, 2048 ); + array< v3 > colours = memarena_push_array( arena, v3, 2048 ); + array< u32 > indices = memarena_push_array( arena, u32, 2048 ); + size_t num_positions, num_colours, num_indices; for( u32 l = 0; l < bsp->num_leaves; l++ ) { - pos_scratch_used = 0; - colour_scratch_used = 0; - ebo_scratch_used = 0; + num_positions = 0; + num_colours = 0; + num_indices = 0; const BSP_Leaf & leaf = bsp->leaves[ l ]; for( u32 f = 0; f < leaf.num_faces; f++ ) { - const u32 offset = pos_scratch_used; + u32 offset = num_positions; const BSP_LeafFace & leaf_face = bsp->leaf_faces[ f + leaf.first_face ]; const BSP_Face & face = bsp->faces[ leaf_face ]; - const glm::vec3 colour = bspr_face_colour( bsp, face ); + const v3 colour = bspr_face_colour( bsp, face ); - const BSP_Vertex * const vertices = &bsp->vertices[ face.first_vert ]; - const s32 * const indices = &bsp->mesh_verts[ face.first_mesh_vert ]; + const BSP_Vertex * vertices = &bsp->vertices[ face.first_vert ]; + const s32 * face_indices = &bsp->mesh_verts[ face.first_mesh_vert ]; for( s32 v = 0; v < face.num_verts; v++ ) { - const glm::vec3 & pos = vertices[ v ].pos; - - pos_scratch[ pos_scratch_used++ ] = pos; - colour_scratch[ colour_scratch_used++ ] = colour; + v3 pos = GLMV3( vertices[ v ].pos ); - assert( pos_scratch_used < 2048 && colour_scratch_used < 2048 ); + positions[ num_positions++ ] = pos; + colours[ num_colours++ ] = colour; } for( s32 m = 0; m < face.num_mesh_verts; m++ ) { - ebo_scratch[ ebo_scratch_used++ ] = indices[ m ] + offset; - - assert( ebo_scratch_used < 2048 ); + indices[ num_indices++ ] = face_indices[ m ] + offset; } } - glBindVertexArray( bspr->vaos[ l ] ); - - glBindBuffer( GL_ARRAY_BUFFER, bspr->vbos[ l * 2 ] ); - glBufferData( GL_ARRAY_BUFFER, pos_scratch_used * sizeof( glm::vec3 ), pos_scratch, GL_STATIC_DRAW ); - glEnableVertexAttribArray( at_position ); - glVertexAttribPointer( at_position, 3, GL_FLOAT, GL_FALSE, 0, 0 ); - - glBindBuffer( GL_ARRAY_BUFFER, bspr->vbos[ l * 2 + 1 ] ); - glBufferData( GL_ARRAY_BUFFER, colour_scratch_used * sizeof( glm::vec3 ), colour_scratch, GL_STATIC_DRAW ); - glEnableVertexAttribArray( at_colour ); - glVertexAttribPointer( at_colour, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + MeshConfig config = { }; + config.positions = renderer_new_vb( positions.ptr(), num_positions * sizeof( v3 ) ); + config.colours = renderer_new_vb( colours.ptr(), num_colours * sizeof( v3 ) ); + config.indices = renderer_new_ib( indices.ptr(), num_indices * sizeof( u32 ) ); + config.num_vertices = num_indices; - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, bspr->ebos[ l ] ); - glBufferData( GL_ELEMENT_ARRAY_BUFFER, ebo_scratch_used * sizeof( GLuint ), ebo_scratch, GL_STATIC_DRAW ); + bspr->meshes[ l ] = renderer_new_mesh( config ); - glBindVertexArray( 0 ); - - bspr->vertex_counts[ l ] = ebo_scratch_used; + renderer_delete_vb( config.positions ); + renderer_delete_vb( config.colours ); + renderer_delete_ib( config.indices ); } } -static void bspr_render_leaf( const BSPRenderer * const bspr, const u32 leaf ) { - glBindVertexArray( bspr->vaos[ leaf ] ); - glDrawElements( GL_TRIANGLES, bspr->vertex_counts[ leaf ], GL_UNSIGNED_INT, 0 ); - glBindVertexArray( 0 ); -} - -void bspr_render( const BSPRenderer * const bspr, const glm::vec3 & pos ) { - const s32 cluster = bspr->bsp->position_to_leaf( pos ).cluster; +void bspr_render( const BSPRenderer * bspr, v3 pos, const RenderState & render_state ) { + s32 cluster = bspr->bsp->position_to_leaf( pos ).cluster; if( cluster == -1 ) { for( u32 i = 0; i < bspr->bsp->num_leaves; i++ ) { - bspr_render_leaf( bspr, i ); + renderer_draw_mesh( bspr->meshes[ i ], render_state ); } return; @@ -157,19 +124,17 @@ void bspr_render( const BSPRenderer * const bspr, const glm::vec3 & pos ) { for( u32 i = 0; i < bspr->bsp->num_leaves; i++ ) { const BSP_Leaf & leaf = bspr->bsp->leaves[ i ]; - const s32 other_cluster = leaf.cluster; - const s32 pvs_idx = cluster * bspr->bsp->vis->cluster_size + other_cluster / 8; + s32 other_cluster = leaf.cluster; + s32 pvs_idx = cluster * bspr->bsp->vis->cluster_size + other_cluster / 8; if( bspr->bsp->vis->pvs[ pvs_idx ] & ( 1 << other_cluster % 8 ) ) { - bspr_render_leaf( bspr, i ); + renderer_draw_mesh( bspr->meshes[ i ], render_state ); } } } -void bspr_destroy( BSPRenderer * const bspr ) { +void bspr_destroy( BSPRenderer * bspr ) { memarena_clear( bspr->arena ); - glDeleteBuffers( bspr->bsp->num_leaves, bspr->ebos ); - glDeleteBuffers( bspr->bsp->num_leaves * 2, bspr->vbos ); - glDeleteVertexArrays( bspr->bsp->num_leaves, bspr->vaos ); + // TODO } diff --git a/bsp_renderer.h b/bsp_renderer.h @@ -5,20 +5,16 @@ #include "bsp.h" #include "memory_arena.h" +#include "renderer.h" struct BSPRenderer { MemoryArena * arena; const BSP * bsp; - - GLuint * vaos; - GLuint * vbos; - GLuint * ebos; - u32 * vertex_counts; + Mesh * meshes; }; -void bspr_init( BSPRenderer * const bspr, MemoryArena * const arena, const BSP * const bsp, - const GLint at_position, const GLint at_colour ); -void bspr_render( const BSPRenderer * const bspr, const glm::vec3 & pos ); -void bspr_destroy( BSPRenderer * const bspr ); +void bspr_init( BSPRenderer * bspr, MemoryArena * arena, const BSP * bsp ); +void bspr_render( const BSPRenderer * bspr, v3 pos, const RenderState & render_state ); +void bspr_destroy( BSPRenderer * bspr ); #endif // _BSP_RENDERER_H_