commit 793a8e05eb894cdd3c7c1804e309319c4e34c3d1 parent 7a8f9d85ed1ae6248cec4dd72725851470ab1d7d Author: Michael Savage <mikejsavage@gmail.com> Date: Sun Sep 6 10:46:24 +0200 Fix BTT segfaults and draw a black screen. Don't know if it actually works yet Diffstat:
btt.cc | | | 128 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- |
game.h | | | 2 | ++ |
diff --git a/btt.cc b/btt.cc @@ -1,6 +1,7 @@ #include "platform_opengl.h" #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> +#include <glm/gtc/type_ptr.hpp> #include "game.h" #include "intrinsics.h" @@ -11,25 +12,50 @@ static const GLchar * const vert_src = GLSL( in vec3 position; - in vec3 colour; + in vec3 normal; + in float lit; - out vec3 frag_colour; + out vec3 n; + out float depth; + out float l; - uniform mat4 VP; + uniform mat4 vp; void main() { - gl_Position = VP * vec4( position, 1.0 ); - frag_colour = colour; + n = normal; + l = lit; + gl_Position = vp * vec4( position, 1.0 ); + depth = gl_Position.z; } ); static const GLchar * frag_src = GLSL( - in vec3 frag_colour; + in vec3 n; + in float depth; + in float l; - out vec4 screen_colour; + out vec4 colour; + + uniform vec3 sun; void main() { - screen_colour = vec4( frag_colour, 1.0 ); + vec3 ground; + if( n.z > 0.9 ) { + ground = vec3( 0.4, 1.0, 0.4 ); + } + else { + ground = vec3( 0.7, 0.7, 0.5 ); + } + + float d = max( 0, -dot( n, sun ) ); + float light = max( 0.2, l * d ); + + vec3 fog = vec3( 0.6, 0.6, 0.6 ); + + float t = smoothstep( 400, 600, depth ); + + colour = vec4( ( 1.0 - t ) * ground * light + t * fog, 1.0 ); + colour = vec4( 1, 0, 0, 1 ); } ); @@ -44,26 +70,26 @@ static const GLchar * frag_src = GLSL( // static void btt_link_diamond( MemoryArena * const arena, BTT * const node ) { - printf( "linking. left %p right %p bottom %p left_sibling %p right_sibling %p\n", node->left, node->right, node->bottom, node->left_sibling, node->right_sibling ); + // printf( "linking. left %p right %p bottom %p left_sibling %p right_sibling %p\n", node->left, node->right, node->bottom, node->left_sibling, node->right_sibling ); - printf( "alloc left/right\n" ); + // printf( "alloc left/right\n" ); node->left = memarena_push_type( arena, BTT ); node->right = memarena_push_type( arena, BTT ); - printf( "zero left/right\n" ); + // printf( "zero left/right\n" ); *node->left = { }; *node->right = { }; - printf( "link left and right\n" ); + // printf( "link left and right\n" ); node->left->right_sibling = node->right; node->right->left_sibling = node->left; - printf( "link left and right bottoms\n" ); + // printf( "link left and right bottoms\n" ); node->left->bottom = node->left_sibling; node->right->bottom = node->right_sibling; if( node->left_sibling ) { - printf( "point left sibling at us\n" ); + // printf( "point left sibling at us\n" ); BTT * const ls = node->left_sibling; if( ls->left_sibling == node ) ls->left_sibling = node->left; if( ls->right_sibling == node ) ls->right_sibling = node->left; @@ -71,7 +97,7 @@ static void btt_link_diamond( MemoryArena * const arena, BTT * const node ) { } if( node->right_sibling ) { - printf( "point right sibling at us\n" ); + // printf( "point right sibling at us\n" ); BTT * const rs = node->right_sibling; if( rs->left_sibling == node ) rs->left_sibling = node->right; if( rs->right_sibling == node ) rs->right_sibling = node->right; @@ -80,28 +106,28 @@ static void btt_link_diamond( MemoryArena * const arena, BTT * const node ) { } static void btt_split( MemoryArena * const arena, BTT * const node ) { - printf( "btt_split\n" ); + // printf( "btt_split\n" ); assert( node ); // assert( !node->left && !node->right ); - printf( "do we split bottom? %p\n", node->bottom ); - if( node->bottom ) printf( "%p\n", node->bottom->bottom ); + // printf( "do we split bottom? %p\n", node->bottom ); + // if( node->bottom ) printf( "%p\n", node->bottom->bottom ); if( node->bottom && node->bottom->bottom != node ) { assert( !node->bottom->left && !node->bottom->right ); // assert( node->bottom->level == node->level - 1 ); - printf( "yes\n" ); + // printf( "yes\n" ); btt_split( arena, node->bottom ); } - printf( "link node\n" ); + // printf( "link node\n" ); btt_link_diamond( arena, node ); if( node->bottom ) { - printf( "link bottom\n" ); + // printf( "link bottom\n" ); btt_link_diamond( arena, node->bottom ); - printf( "touch up bottom\n" ); + // printf( "touch up bottom\n" ); node->left->left_sibling = node->bottom->right; node->right->left_sibling = node->bottom->left; @@ -110,6 +136,10 @@ static void btt_split( MemoryArena * const arena, BTT * const node ) { } } +static int iabs( const int x ) { + return x < 0 ? -x : x; +} + // TODO: this should be recursive static bool btt_should_split( const Heightmap * const hm, @@ -126,6 +156,8 @@ static void btt_build( BTT * const node, const glm::ivec2 v0, const glm::ivec2 v1, const glm::ivec2 v2 ) { + if( iabs( v0.x - v2.x ) < 4 ) return; + const glm::ivec2 mid = ( v0 + v2 ) / 2; if( !node->left ) { @@ -157,8 +189,8 @@ BTTs btt_from_heightmap( const Heightmap * const hm, MemoryArena * const arena ) roots.left_root->bottom = roots.right_root; roots.right_root->bottom = roots.left_root; - btt_build( hm, arena, roots.left_root, glm::ivec2( 0, 0 ), glm::ivec2( 0, 1 ), glm::ivec2( 1, 1 ) ); - btt_build( hm, arena, roots.right_root, glm::ivec2( 1, 1 ), glm::ivec2( 1, 0 ), glm::ivec2( 0, 0 ) ); + btt_build( hm, arena, roots.left_root, glm::ivec2( 0, 0 ), glm::ivec2( 0, hm->h ), glm::ivec2( hm->w, hm->h ) ); + btt_build( hm, arena, roots.right_root, glm::ivec2( hm->w, hm->h ), glm::ivec2( hm->w, 0 ), glm::ivec2( 0, 0 ) ); return roots; } @@ -166,6 +198,9 @@ BTTs btt_from_heightmap( const Heightmap * const hm, MemoryArena * const arena ) static const glm::mat4 P( glm::perspective( glm::radians( 120.0f ), 640.0f / 480.0f, 0.1f, 10000.0f ) ); extern "C" GAME_INIT( game_init ) { + state->pos = glm::vec3( 100, 100, 50 ); + state->angles = glm::radians( glm::vec3( -90, 45, 0 ) ); + 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" ); @@ -173,11 +208,50 @@ extern "C" GAME_INIT( game_init ) { state->test_at_lit = glGetAttribLocation( state->test_shader, "lit" ); state->test_un_VP = glGetUniformLocation( state->test_shader, "VP" ); - Heightmap hm; - hm.load( "mountains512.png", 0, 0, state->test_at_position, state->test_at_normal, state->test_at_lit ); - state->btt = btt_from_heightmap( &hm, &mem->persistent_arena ); + 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 ); +} + +static glm::vec3 angles_to_vector_xy( const glm::vec3 & angles ) { + return glm::vec3( sin( angles.y ), cos( angles.y ), 0 ); } 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 ]; + + const int pitch = input->keys[ KEY_UPARROW ] - input->keys[ KEY_DOWNARROW ]; + const int yaw = input->keys[ KEY_RIGHTARROW ] - input->keys[ KEY_LEFTARROW ]; + + state->angles.x += pitch * dt * 2; + state->angles.y += yaw * dt * 2; + + // const float speed = 6.0f; + const float speed = 100.0f; + state->pos += angles_to_vector_xy( state->angles ) * speed * dt * ( float ) fb; + const glm::vec3 sideways = glm::vec3( -cosf( state->angles.y ), sinf( state->angles.y ), 0 ); + state->pos += sideways * speed * dt * ( float ) lr; + state->pos.z += dz * 50.0f * dt; + + const glm::mat4 VP = glm::translate( + glm::rotate( + glm::rotate( + P, + state->angles.x, + glm::vec3( 1, 0, 0 ) + ), + state->angles.y, + glm::vec3( 0, 0, 1 ) + ), + -state->pos + ); + + glUseProgram( state->test_shader ); + glUniformMatrix4fv( state->test_un_VP, 1, GL_FALSE, glm::value_ptr( VP ) ); + state->hm.render(); + glUseProgram( 0 ); } diff --git a/game.h b/game.h @@ -67,6 +67,7 @@ #include "assets.h" #include "terrain_manager.h" #include "btt.h" +#include "heightmap.h" #include "bsp.h" #include "bsp_renderer.h" #include "immediate.h" @@ -100,6 +101,7 @@ struct GameState { GLint test_at_lit; BTTs btt; + Heightmap hm; ImmediateContext test_immediate;