medfall

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

commit a635f92785b519d0bf94ba5e341dc0b0b0a319a0
parent 2320f250622ca4bbb286288dd902efc651a64e4b
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon Sep  7 21:01:32 +0200

Draw BTT outline

Diffstat:
Makefile | 2+-
btt.cc | 72+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
game.h | 5+++++
3 files changed, 77 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile @@ -32,7 +32,7 @@ bsp.so: $(BSPOBJS) pp: $(PPOBJS) $(CXX) $^ $(LDFLAGS) -o $@ -btt.so: btt.o heightmap.o memory_arena.o work_queue.o stb_image.o +btt.so: btt.o heightmap.o memory_arena.o work_queue.o stb_image.o immediate.o $(CXX) $^ $(LDFLAGS) -o $@ -shared clean: diff --git a/btt.cc b/btt.cc @@ -10,6 +10,9 @@ #include "gl.h" #include "shitty_glsl.h" +static ImmediateTriangle triangles[ 512000 ]; +static ImmediateContext imm; + static const GLchar * const vert_src = GLSL( in vec3 position; in vec3 normal; @@ -58,6 +61,30 @@ static const GLchar * frag_src = GLSL( } ); +static const GLchar * const vert_outline_src = GLSL( + in vec3 position; + in vec3 colour; + + out vec3 frag_colour; + + uniform mat4 vp; + + void main() { + frag_colour = colour; + gl_Position = vp * vec4( position, 1.0 ); + } +); + +static const GLchar * frag_outline_src = GLSL( + in vec3 frag_colour; + + out vec4 screen_colour; + + void main() { + screen_colour = vec4( frag_colour, 1.0 ); + } +); + // triangles should look like this: // // v1 @@ -202,12 +229,16 @@ extern "C" GAME_INIT( game_init ) { state->test_shader = compile_shader( vert_src, frag_src, "screen_colour" ); state->test_at_position = glGetAttribLocation( state->test_shader, "position" ); - state->test_at_colour = glGetAttribLocation( state->test_shader, "colour" ); state->test_at_normal = glGetAttribLocation( state->test_shader, "normal" ); state->test_at_lit = glGetAttribLocation( state->test_shader, "lit" ); state->test_un_VP = glGetUniformLocation( state->test_shader, "vp" ); state->test_un_sun = glGetUniformLocation( state->test_shader, "sun" ); + state->test_outline_shader = compile_shader( vert_outline_src, frag_outline_src, "screen_colour" ); + state->test_outline_at_position = glGetAttribLocation( state->test_outline_shader, "position" ); + state->test_outline_at_colour = glGetAttribLocation( state->test_outline_shader, "colour" ); + state->test_outline_un_vp = glGetUniformLocation( state->test_outline_shader, "vp" ); + state->hm.load( "mountains512.png", 0, 0, state->test_at_position, state->test_at_normal, state->test_at_lit ); state->btt = btt_from_heightmap( &state->hm, &mem->persistent_arena ); @@ -217,6 +248,30 @@ static glm::vec3 angles_to_vector_xy( const glm::vec3 & angles ) { return glm::vec3( sin( angles.y ), cos( angles.y ), 0 ); } +static void draw_btt( + const BTT * const btt, const Heightmap * const hm, + ImmediateContext * const imm, + const glm::ivec2 v0, const glm::ivec2 v1, const glm::ivec2 v2 +) { + const glm::vec4 white( 1, 1, 1, 1 ); + + const glm::vec3 v0_( v0, hm->point( v0.x, v0.y ).z ); + const glm::vec3 v1_( v1, hm->point( v1.x, v1.y ).z ); + const glm::vec3 v2_( v2, hm->point( v2.x, v2.y ).z ); + + if( btt->left ) { + assert( btt->right ); + + const glm::ivec2 mid = ( v0 + v2 ) / 2; + + draw_btt( btt->left, hm, imm, v1, mid, v0 ); + draw_btt( btt->right, hm, imm, v2, mid, v1 ); + } + else { + immediate_triangle( imm, v0_, v1_, v2_, white ); + } +} + extern "C" GAME_FRAME( game_frame ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); @@ -251,9 +306,24 @@ extern "C" GAME_FRAME( game_frame ) { ); const glm::vec3 sun = glm::normalize( glm::vec3( 1, 0, -0.3 ) ); + glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); glUseProgram( state->test_shader ); glUniformMatrix4fv( state->test_un_VP, 1, GL_FALSE, glm::value_ptr( VP ) ); glUniform3fv( state->test_un_sun, 1, glm::value_ptr( sun ) ); state->hm.render(); glUseProgram( 0 ); + + immediate_init( &imm, triangles, array_count( triangles ) ); + draw_btt( state->btt.left_root, &state->hm, &imm, glm::ivec2( 0, 0 ), glm::ivec2( 0, state->hm.h ), glm::ivec2( state->hm.w, state->hm.h ) ); + draw_btt( state->btt.right_root, &state->hm, &imm, glm::ivec2( state->hm.w, state->hm.h ), glm::ivec2( state->hm.w, 0 ), glm::ivec2( 0, 0 ) ); + + glDisable( GL_DEPTH_TEST ); + glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); + + glUseProgram( state->test_outline_shader ); + glUniformMatrix4fv( state->test_outline_un_vp, 1, GL_FALSE, glm::value_ptr( VP ) ); + immediate_render( &imm, state->test_outline_at_position, state->test_outline_at_colour ); + glUseProgram( 0 ); + + glEnable( GL_DEPTH_TEST ); } diff --git a/game.h b/game.h @@ -101,6 +101,11 @@ struct GameState { GLint test_at_lit; GLint test_un_sun; + GLuint test_outline_shader; + GLint test_outline_at_position; + GLint test_outline_at_colour; + GLint test_outline_un_vp; + BTTs btt; Heightmap hm;