medfall

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

commit fa6372cbc2a27fe8dc904813440920f3bbd19c90
parent 7074b22c2820c5d996ed1762f9ce9d9c225a6b91
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Thu Nov  3 21:44:44 +0200

Partially move btt.so to the new renderer

Diffstat:
gpubtt.cc | 74+++++++++++++++++++++++++++++---------------------------------------------
gpubtt.h | 19++++++-------------
mod_btt.cc | 11+++++++----
3 files changed, 42 insertions(+), 62 deletions(-)
diff --git a/gpubtt.cc b/gpubtt.cc @@ -46,8 +46,7 @@ static void gpubtt_build( void gpubtt_init( MemoryArena * arena, GPUBTT * gpubtt, BTTs btts, - const OffsetHeightmap * ohm, const v3 * normals, const float * horizons, - GLuint at_position + const OffsetHeightmap * ohm, const v3 * normals, const float * horizons ) { MEMARENA_SCOPED_CHECKPOINT( arena ); @@ -56,7 +55,7 @@ void gpubtt_init( printf( "%u to %u verts\n", ohm->hm.width * ohm->hm.height * 2, num_leaves ); glm::vec3 * verts = memarena_push_many( arena, glm::vec3, num_leaves * 3 ); - u32 i = 0; + u32 num_vertices = 0; // bottom left, bottom right, top left, top right const glm::ivec2 bl( 0, 0 ); @@ -64,56 +63,41 @@ void gpubtt_init( const glm::ivec2 tl( 0, ohm->hm.height - 1 ); const glm::ivec2 tr( ohm->hm.width - 1, ohm->hm.height - 1 ); - gpubtt_build( verts, &i, ohm, btts.left_root, bl, tl, tr ); - gpubtt_build( verts, &i, ohm, btts.right_root, tr, br, bl ); + gpubtt_build( verts, &num_vertices, ohm, btts.left_root, bl, tl, tr ); + gpubtt_build( verts, &num_vertices, ohm, btts.right_root, tr, br, bl ); - glGenVertexArrays( 1, &gpubtt->vao ); - glBindVertexArray( gpubtt->vao ); + VB positions = renderer_new_vb( verts, num_vertices * sizeof( v3 ) ); - glGenBuffers( 1, &gpubtt->vbo_verts ); - glBindBuffer( GL_ARRAY_BUFFER, gpubtt->vbo_verts ); - glBufferData( GL_ARRAY_BUFFER, i * 3 * sizeof( GLfloat ), verts, GL_STATIC_DRAW ); - glEnableVertexAttribArray( at_position ); - glVertexAttribPointer( at_position, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + TextureConfig normals_config = { }; + normals_config.width = ohm->hm.width; + normals_config.height = ohm->hm.height; + normals_config.format = TEXFMT_RGB_FLOAT; + normals_config.data = normals; + gpubtt->normal_map = renderer_new_texture( normals_config ); - glGenTextures( 1, &gpubtt->tex_normals ); - glBindTexture( GL_TEXTURE_2D, gpubtt->tex_normals ); - 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_RGB32F, ohm->hm.width, ohm->hm.height, 0, GL_RGB, GL_FLOAT, normals ); + TextureConfig horizons_config = { }; + horizons_config.width = ohm->hm.width; + horizons_config.height = ohm->hm.height; + horizons_config.format = TEXFMT_R_FLOAT; + horizons_config.data = horizons; + gpubtt->horizon_map = renderer_new_texture( horizons_config ); - glGenTextures( 1, &gpubtt->tex_horizons ); - glBindTexture( GL_TEXTURE_2D, gpubtt->tex_horizons ); - 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_R32F, ohm->hm.width, ohm->hm.height, 0, GL_RED, GL_FLOAT, horizons ); + MeshConfig mesh_config = { }; + mesh_config.positions = positions; + mesh_config.num_vertices = num_vertices; + gpubtt->mesh = renderer_new_mesh( mesh_config ); - glBindVertexArray( 0 ); - - gpubtt->num_verts = i; + renderer_delete_vb( positions ); } void gpubtt_destroy( GPUBTT * gpubtt ) { - glDeleteTextures( 1, &gpubtt->tex_normals ); - glDeleteBuffers( 1, &gpubtt->vbo_verts ); - glDeleteVertexArrays( 1, &gpubtt->vao ); - - gpubtt->vbo_verts = 0; + renderer_delete_mesh( gpubtt->mesh ); + renderer_delete_texture( gpubtt->normal_map ); + renderer_delete_texture( gpubtt->horizon_map ); } -void gpubtt_render( const GPUBTT * gpubtt, GLuint un_normals, GLuint un_horizons ) { - if( gpubtt->vbo_verts == 0 ) return; - - glBindVertexArray( gpubtt->vao ); - - glActiveTexture( GL_TEXTURE0 ); - glBindTexture( GL_TEXTURE_2D, gpubtt->tex_normals ); - glActiveTexture( GL_TEXTURE3 ); - glBindTexture( GL_TEXTURE_2D, gpubtt->tex_horizons ); - glUniform1i( un_normals, 0 ); - glUniform1i( un_horizons, 3 ); - - glDrawArrays( GL_TRIANGLES, 0, gpubtt->num_verts * 3 ); - - glBindVertexArray( 0 ); +void gpubtt_render( const GPUBTT * gpubtt, RenderState render_state ) { + render_state.textures[ 0 ] = gpubtt->normal_map; + render_state.textures[ 1 ] = gpubtt->horizon_map; + renderer_draw_mesh( gpubtt->mesh, render_state ); } diff --git a/gpubtt.h b/gpubtt.h @@ -1,27 +1,20 @@ #ifndef _GPUBTT_H_ #define _GPUBTT_H_ -#include "glad.h" - +#include "renderer.h" #include "heightmap.h" #include "btt.h" #include "linear_algebra.h" struct GPUBTT { - GLuint vao; - GLuint vbo_verts = 0; - GLuint tex_normals; - GLuint tex_horizons; - u32 num_verts; + Mesh mesh; + Texture normal_map; + Texture horizon_map; }; void gpubtt_init( MemoryArena * arena, GPUBTT * gpubtt, BTTs btts, - const OffsetHeightmap * ohm, const v3 * normals, const float * horizons, - GLuint at_position ); - + const OffsetHeightmap * ohm, const v3 * normals, const float * horizons ); void gpubtt_destroy( GPUBTT * gpubtt ); - -void gpubtt_render( const GPUBTT * gpubtt, GLuint un_normals, - GLuint un_horizons ); +void gpubtt_render( const GPUBTT * gpubtt, RenderState render_state ); #endif // _GPUBTT_H_ diff --git a/mod_btt.cc b/mod_btt.cc @@ -141,7 +141,6 @@ extern "C" GAME_INIT( game_init ) { game->test_sun = 0.3f; game->test_shader = compile_shader( vert_src, frag_src, "screen_colour" ); - game->test_at_position = glGetAttribLocation( game->test_shader, "position" ); game->test_un_VP = glGetUniformLocation( game->test_shader, "vp" ); game->test_un_sun = glGetUniformLocation( game->test_shader, "sun" ); game->test_un_normals = glGetUniformLocation( game->test_shader, "normals" ); @@ -184,7 +183,7 @@ extern "C" GAME_INIT( game_init ) { game->btt = btt_from_heightmap( &game->hm, &mem->persistent_arena ); const OffsetHeightmap ohm = { game->hm, 0, 0 }; - gpubtt_init( &mem->persistent_arena, &game->gpubtt, game->btt, &ohm, normals, horizons, game->test_at_position ); + gpubtt_init( &mem->persistent_arena, &game->gpubtt, game->btt, &ohm, normals, horizons ); glClearColor( 0, 0.5, 0.7, 1 ); @@ -232,8 +231,12 @@ extern "C" GAME_FRAME( game_frame ) { glUniformMatrix4fv( game->test_un_VP, 1, GL_FALSE, glm::value_ptr( VP ) ); glUniform1f( game->test_un_sun, game->test_sun ); glUniform2f( game->test_un_dimensions, game->hm.width, game->hm.height ); - gpubtt_render( &game->gpubtt, game->test_un_normals, game->test_un_horizons ); - glUseProgram( 0 ); + glUniform1i( game->test_un_normals, 0 ); + glUniform1i( game->test_un_horizons, 1 ); + + RenderState render_state = { }; + render_state.shader = game->test_shader; + gpubtt_render( &game->gpubtt, render_state ); immediate_init( &imm, triangles, array_count( triangles ) ); for( size_t i = 0; i < game->hm.width; i++ ) {