commit 284ebbf15202e84b911450b69a42ad6605162d2c parent aed9d05d33dcbdd406eafdd96f6481c45b9e38c9 Author: Michael Savage <mikejsavage@gmail.com> Date: Mon Aug 31 21:27:56 +0200 Use VAOs properly in the BSP renderer Diffstat:
bsp.cc | | | 6 | +++--- |
bsp_renderer.cc | | | 25 | +++++++++++-------------- |
bsp_renderer.h | | | 5 | +++-- |
diff --git a/bsp.cc b/bsp.cc @@ -274,8 +274,6 @@ extern "C" GAME_INIT( game_init ) { MemoryArena arena; memarena_init( &arena, memory, megabytes( 10 ) ); - bspr_init( &state->bspr, &arena, &state->bsp ); - state->pos = glm::vec3( 0, -100, 450 ); state->angles = glm::radians( ( glm::vec3( -90, 135, 0 ) ) ); @@ -284,6 +282,8 @@ extern "C" GAME_INIT( game_init ) { state->test_at_colour = glGetAttribLocation( state->test_shader, "colour" ); state->test_un_VP = glGetUniformLocation( state->test_shader, "VP" ); + bspr_init( &state->bspr, &arena, &state->bsp, state->test_at_position, state->test_at_colour ); + immediate_init( &imm, triangles, array_count( triangles ) ); } @@ -323,7 +323,7 @@ extern "C" GAME_FRAME( game_frame ) { glUseProgram( state->test_shader ); glUniformMatrix4fv( state->test_un_VP, 1, GL_FALSE, glm::value_ptr( VP ) ); - bspr_render( &state->bspr, state->pos, state->test_at_position, state->test_at_colour ); + bspr_render( &state->bspr, state->pos ); immediate_clear( &imm ); immediate_sphere( &imm, glm::vec3( 0, 0, 0 ), 128, glm::vec4( 1, 1, 0, 1 ) ); diff --git a/bsp_renderer.cc b/bsp_renderer.cc @@ -59,7 +59,9 @@ glm::vec3 bspr_face_colour( const BSP * const bsp, const BSP_Face & face ) { } } -void bspr_init( BSPRenderer * const bspr, MemoryArena * const arena, const BSP * const bsp ) { +void bspr_init( BSPRenderer * const bspr, MemoryArena * const arena, const BSP * const bsp, + const GLint at_position, const GLint at_colour +) { bspr->arena = arena; bspr->bsp = bsp; @@ -118,9 +120,13 @@ void bspr_init( BSPRenderer * const bspr, MemoryArena * const arena, const BSP * 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 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, bspr->ebos[ l ] ); glBufferData( GL_ELEMENT_ARRAY_BUFFER, ebo_scratch_used * sizeof( GLuint ), ebo_scratch, GL_STATIC_DRAW ); @@ -132,18 +138,9 @@ void bspr_init( BSPRenderer * const bspr, MemoryArena * const arena, const BSP * } -static void bspr_render_leaf( const BSPRenderer * const bspr, const u32 leaf, const GLint at_position, const GLint at_colour ) { +static void bspr_render_leaf( const BSPRenderer * const bspr, const u32 leaf ) { glBindVertexArray( bspr->vaos[ leaf ] ); - glBindBuffer( GL_ARRAY_BUFFER, bspr->vbos[ leaf * 2 ] ); - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, bspr->ebos[ leaf ] ); - glEnableVertexAttribArray( at_position ); - glVertexAttribPointer( at_position, 3, GL_FLOAT, GL_FALSE, 0, 0 ); - - glBindBuffer( GL_ARRAY_BUFFER, bspr->vbos[ leaf * 2 + 1 ] ); - glEnableVertexAttribArray( at_colour ); - glVertexAttribPointer( at_colour, 3, GL_FLOAT, GL_FALSE, 0, 0 ); - glDrawElements( GL_TRIANGLES, bspr->vertex_counts[ leaf ], GL_UNSIGNED_INT, 0 ); glBindVertexArray( 0 ); @@ -151,12 +148,12 @@ static void bspr_render_leaf( const BSPRenderer * const bspr, const u32 leaf, co static const glm::mat4 P( glm::perspective( glm::radians( 120.0f ), 640.0f / 480.0f, 0.1f, 10000.0f ) ); -void bspr_render( const BSPRenderer * const bspr, const glm::vec3 & pos, const GLint at_position, const GLint at_colour ) { +void bspr_render( const BSPRenderer * const bspr, const glm::vec3 & pos ) { const 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, at_position, at_colour ); + bspr_render_leaf( bspr, i ); } return; @@ -169,7 +166,7 @@ void bspr_render( const BSPRenderer * const bspr, const glm::vec3 & pos, const G const 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, at_position, at_colour ); + bspr_render_leaf( bspr, i ); } } } diff --git a/bsp_renderer.h b/bsp_renderer.h @@ -16,8 +16,9 @@ struct BSPRenderer { u32 * vertex_counts; }; -void bspr_init( BSPRenderer * const bspr, MemoryArena * const arena, const BSP * const bsp ); -void bspr_render( const BSPRenderer * const bspr, const glm::vec3 & pos, const GLint at_position, const GLint at_colour ); +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 ); #endif // _BSP_RENDERER_H_