medfall

A super great game engine
Log | Files | Refs

commit 5426a00d1456d43492ef28b30295a165c635d84d
parent 4ae2bf48c8baa4af5d4d345e97bff7a3602de861
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat,  4 Nov 2017 16:32:36 +0200

Remove mod_btt

Diffstat:
make.lua | 4----
mod_btt.cc | 431-------------------------------------------------------------------------------
2 files changed, 0 insertions(+), 435 deletions(-)

diff --git a/make.lua b/make.lua @@ -48,15 +48,11 @@ end require( "libs/squish" ) bin( "bsp", { "main", "bsp", "bsp_renderer", game_objs }, { game_libs } ) --- TODO: fix btt build --- bin( "btt", { "main", "mod_btt", "btt", "heightmap", "skybox", "lz4", game_objs }, { "stb_image", game_libs } ) bin( "sm", { "main", "shadow_map", game_objs }, { game_libs } ) bin( "clipmap", { "main", "clipmap", "heightmap", "skybox", "decompress_bc", game_objs }, { "lz4", game_libs } ) msvc_bin_ldflags( "bsp", "opengl32.lib gdi32.lib" ) gcc_bin_ldflags( "bsp", game_ldflags ) -msvc_bin_ldflags( "btt", "opengl32.lib gdi32.lib" ) -gcc_bin_ldflags( "btt", game_ldflags ) msvc_bin_ldflags( "sm", "opengl32.lib gdi32.lib" ) gcc_bin_ldflags( "sm", game_ldflags ) msvc_bin_ldflags( "clipmap", "opengl32.lib gdi32.lib" ) diff --git a/mod_btt.cc b/mod_btt.cc @@ -1,431 +0,0 @@ -#include "game.h" -#include "intrinsics.h" -#include "int_conversions.h" -#include "immediate.h" -#include "skybox.h" -#include "profiler.h" -#include "linear_algebra.h" - -#include "libs/lz4/lz4.h" - -#include "libs/stb/stb_image.h" - -// #define TERRAIN_NAME "mountains512.png" -#define TERRAIN_NAME "grad.png" - -static const char * vert_src = GLSL( - in vec3 position; - - out vec3 smooth_position; - out float depth; - - layout( std140 ) uniform v_hot { - mat4 vp; - }; - - void main() { - gl_Position = vp * vec4( position, 1.0 ); - smooth_position = position; - depth = gl_Position.z; - } -); - -static const char * frag_src = GLSL( - in vec3 smooth_position; - in float depth; - - out vec4 colour; - - layout( std140 ) uniform f_hot { - vec2 dimensions; - float sun; - }; - - uniform sampler2D normals; - uniform sampler2D horizons; - - void main() { - vec3 normal = normalize( texture( normals, smooth_position.xy / dimensions ).xyz * 2.0 - 1.0 ); - float horizon = texture( horizons, smooth_position.xy / dimensions ).r; - - // ground colour - vec3 ground; - if( smooth_position.z > 175 ) { - // snow/rocks - if( normal.z > 0.5 ) { - ground = vec3( 0.8, 0.8, 0.8 ); - } - else { - ground = vec3( 0.6, 0.6, 0.6 ); - } - } - else if( smooth_position.z < 5 ) { - ground = vec3( 0.0, 0.25, 1.0 ); - } - else { - if( normal.z > 0.8 ) { - ground = vec3( 0.4, 1.0, 0.4 ); - } - else { - ground = vec3( 0.7, 0.7, 0.5 ); - } - } - - // sunlight + sky ambient lighting - // TODO: use formula for area of a chord? - float sun_visible_fraction = smoothstep( 0, 0.2, sun - horizon ); - sun_visible_fraction += 5.0; - sun_visible_fraction = min( 1.0, sun_visible_fraction ); - vec3 sun_direction = normalize( vec3( -1, 0, sun ) ); - float sunlight_lambert = max( 0, dot( normal, sun_direction ) ); - vec3 sunlight = sun_visible_fraction * sunlight_lambert * vec3( 0.9, 0.9, 0.5 ); - vec3 ambient = vec3( 0.02, 0.02, 0.15 ); - - colour = vec4( ( sunlight + ambient ) * ground, 1.0 ); - colour = vec4( ( normal + 1.0 ) / 2.0, 1.0 ); - } -); - -static const char * vert_outline_src = GLSL( - in vec3 position; - in vec3 colour; - - out vec3 frag_colour; - - layout( std140 ) uniform v_hot { - mat4 vp; - }; - - void main() { - frag_colour = colour; - gl_Position = vp * vec4( position, 1.0 ); - } -); - -static const char * frag_outline_src = GLSL( - in vec3 frag_colour; - - out vec4 screen_colour; - - void main() { - screen_colour = vec4( frag_colour, 1.0 ); - } -); - -static v3 angles_to_vector_xy( v3 angles ) { - return v3( sin( angles.y ), cos( angles.y ), 0 ); -} - -v3 angles_to_vector( v3 angles ) { - return v3( - -sin( angles.y ) * sin( angles.x ), - -cos( angles.y ) * sin( angles.x ), - -cos( angles.x ) - ); -} - -// static void draw_btt( -// const BTT * btt, const Heightmap * hm, -// ImmediateContext * imm, -// glm::ivec2 iv0, glm::ivec2 iv1, glm::ivec2 iv2 -// ) { -// const glm::vec4 white( 1, 1, 1, 1 ); -// const glm::vec3 offset( 0.0f, 0.0f, 0.5f ); -// -// glm::vec3 v0( hm->point( iv0.x, iv0.y ) + offset ); -// glm::vec3 v1( hm->point( iv1.x, iv1.y ) + offset ); -// glm::vec3 v2( hm->point( iv2.x, iv2.y ) + offset ); -// -// if( btt->left ) { -// assert( btt->right ); -// -// glm::ivec2 mid = ( iv0 + iv2 ) / 2; -// -// draw_btt( btt->left, hm, imm, iv1, mid, iv0 ); -// draw_btt( btt->right, hm, imm, iv2, mid, iv1 ); -// } -// else { -// immediate_triangle( imm, v0, v1, v2, white ); -// } -// } - -static void compute_normals( const array2d< u8 > heightmap, array2d< v3 > normals ) { - ASSERT( heightmap.w == normals.w && heightmap.h == normals.h ); - - // estimate the gradient at each point by doing central differences on - // each axis, then cross the tangent and bitangent to find the normal - for( size_t y = 0; y < heightmap.h; y++ ) { - for( size_t x = 0; x < heightmap.w; x++ ) { - size_t x_plus_one = x + 1; - size_t x_minus_one = x - 1; - size_t y_plus_one = y + 1; - size_t y_minus_one = y - 1; - - if( x == 0 ) x_minus_one = x; - if( x == heightmap.w - 1 ) x_plus_one = x; - if( y == 0 ) y_minus_one = y; - if( y == heightmap.h - 1 ) y_plus_one = y; - - v3 tangent( - checked_cast< float >( x_plus_one - x_minus_one ), - 0, - checked_cast< float >( heightmap( x_plus_one, y ) - heightmap( x_minus_one, y ) ) ); - v3 bitangent( - 0, - checked_cast< float >( y_plus_one - y_minus_one ), - checked_cast< float >( heightmap( x, y_plus_one ) - heightmap( x, y_minus_one ) ) ); - - v3 cp = cross( tangent, bitangent ); - v3 n = normalize( cp ); - normals( x, y ) = ( n + v3( 1, 1, 1 ) ) / 2; - } - } -} - -static array2d< float > horizons; -static array2d< v3 > normals; -static HeightmapQuadTree qt; - -static UB ub_fs, ub_vs; - -extern "C" GAME_INIT( game_init ) { - PROFILE_FUNCTION(); - - game->pos = v3( 100, 200, 100 ); - game->pitch = 0; - game->yaw = 45; - - game->test_sun = 0.3f; - - ShaderConfig terrain_config; - terrain_config.vertex_src = vert_src; - terrain_config.fragment_src = frag_src; - terrain_config.texture_uniform_names[ 0 ] = "normals"; - terrain_config.texture_uniform_names[ 1 ] = "horizons"; - game->test_shader = renderer_new_shader( terrain_config ); - - game->test_outline_shader = renderer_new_shader( vert_outline_src, frag_outline_src ); - - ub_vs = renderer_new_ub(); - ub_fs = renderer_new_ub(); - - int w, h, channels; - u8 * pixels = stbi_load( "terrains/" TERRAIN_NAME, &w, &h, &channels, 1 ); - ASSERT( channels == 1 ); - heightmap_init( &game->hm, pixels, w, h ); - - { - PROFILE_BLOCK( "Build quadtree" ); - size_t num_nodes = heightmap_quadtree_max_nodes( &game->hm ); - array< HeightmapQuadTreeNode > nodes = memarena_push_array( &mem->persistent_arena, HeightmapQuadTreeNode, num_nodes ); - qt = heightmap_build_quadtree( &game->hm, nodes ); - } - - horizons = memarena_push_array2d( &mem->persistent_arena, float, w, h ); - - size_t compressed_horizons_size; - u8 * compressed_horizons = file_get_contents( "terrains/" TERRAIN_NAME ".parts/0_0_horizons.lz4", &compressed_horizons_size ); - int ok_horizons = LZ4_decompress_safe( - ( const char * ) compressed_horizons, - ( char * ) horizons.ptr(), - compressed_horizons_size, horizons.num_bytes() ); - ASSERT( ok_horizons > 0 && to_unsigned( ok_horizons ) == horizons.num_bytes() ); - - normals = memarena_push_array2d( &mem->persistent_arena, v3, w, h ); - - // size_t compressed_normals_size; - // u8 * compressed_normals = file_get_contents( "terrains/" TERRAIN_NAME ".parts/0_0_normals.lz4", &compressed_normals_size ); - // int ok_normals = LZ4_decompress_safe( - // ( const char * ) compressed_normals, - // ( char * ) normals.ptr(), - // compressed_normals_size, normals.num_bytes() ); - // ASSERT( ok_normals > 0 && to_unsigned( ok_normals ) == normals.num_bytes() ); - - array2d< u8 > heightmap( pixels, w, h ); - compute_normals( heightmap, normals ); - - { - PROFILE_BLOCK( "Build BTT" ); - 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.ptr(), horizons.ptr() ); - - skybox_init( &game->skybox ); -} - -static m4 camera_to_vp( v3 position, v3 angles ) { - const m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); - - return m4_translation( -position ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ) * P; -} - -struct FSData { - v2 dimensions; - float sun; -}; - -static void draw_qt( ImmediateContext * imm, AABBu32 aabb, const array< HeightmapQuadTreeNode > nodes, size_t node_idx ) { - if( aabb.maxs.x - aabb.mins.x < 32 ) { - return; - } - - v3 mins( aabb.mins.x, aabb.mins.y, aabb.mins.z ); - v3 maxs( aabb.maxs.x, aabb.maxs.y, aabb.maxs.z ); - immediate_aabb( imm, mins, maxs, v4( 0, 0, 1, 1 ) ); - - for( size_t i = 0; i < 4; i++ ) { - AABBu32 child_aabb = aabb.quadrant( i ).clamp_z( nodes[ node_idx * 4 + i + 1 ].min_z, nodes[ node_idx * 4 + i + 1 ].max_z ); - draw_qt( imm, child_aabb, nodes, node_idx * 4 + i + 1 ); - } -} - -template< typename T > -static T lerp( T a, float t, T b ) { - ASSERT( t >= 0.0f && t <= 1.0f ); - return a * ( 1.0f - t ) + b * t; -} - -template< typename T > -static T bilerp( const array2d< T > arr, float x, float y ) { - size_t xi = ( size_t ) x; - size_t yi = ( size_t ) y; - size_t xi1 = min( xi + 1, arr.w - 1 ); - size_t yi1 = min( yi + 1, arr.h - 1 ); - - float xf = x - xi; - float yf = y - yi; - - T a = arr( xi, yi ); - T b = arr( xi1, yi ); - T c = arr( xi, yi1 ); - T d = arr( xi1, yi1 ); - - T ab = lerp( a, xf, b ); - T cd = lerp( c, xf, d ); - - return lerp( ab, yf, cd ); -} - -extern "C" GAME_FRAME( game_frame ) { - renderer_begin_frame( CLEARCOLOUR_DONT ); - - int fb = input->keys[ KEY_W ] - input->keys[ KEY_S ]; - int lr = input->keys[ KEY_D ] - input->keys[ KEY_A ]; - int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; - - int dpitch = input->keys[ KEY_DOWNARROW ] - input->keys[ KEY_UPARROW ]; - int dyaw = input->keys[ KEY_LEFTARROW ] - input->keys[ KEY_RIGHTARROW ]; - - dpitch += input->keys[ KEY_K ] - input->keys[ KEY_I ]; - dyaw += input->keys[ KEY_J ] - input->keys[ KEY_L ]; - - game->pitch += dpitch * dt * 100; - game->yaw += dyaw * dt * 100; - - game->pitch -= float( input->mouse_dy * 0.1 ); - game->yaw -= float( input->mouse_dx * 0.1 ); - - const float speed = 50.0f; - - const v3 world_up = v3( 0, 0, 1 ); - v3 forward = v3_forward( game->pitch, game->yaw ); - v3 right = normalize( cross( forward, world_up ) ); - v3 up = normalize( cross( right, forward ) ); - - game->pos += forward * dt * fb * speed; - game->pos += right * dt * lr * speed; - game->pos.z += dt * dz * speed; - - const float dsun = ( input->keys[ KEY_EQUALS ] - input->keys[ KEY_MINUS ] ) * dt; - game->test_sun += dsun; - if( dsun != 0 ) printf( "sun: %.4f\n", game->test_sun ); - - m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); - m4 V = m4_view( forward, right, up, game->pos ); - m4 Vsky = m4_view( forward, right, up, v3( 0 ) ); - m4 VP = P * V; - - skybox_render( &game->skybox, Vsky, P, game->test_sun ); - - FSData fs_data = { }; - fs_data.dimensions = v2( game->hm.width, game->hm.height ); - fs_data.sun = game->test_sun; - - renderer_ub_data( ub_vs, &VP, sizeof( VP ) ); - renderer_ub_data( ub_fs, &fs_data, sizeof( fs_data ) ); - - RenderState render_state; - render_state.shader = game->test_shader; - render_state.ubs[ UB_VS_HOT ] = ub_vs; - render_state.ubs[ UB_FS_HOT ] = ub_fs; - gpubtt_render( &game->gpubtt, render_state ); - - static ImmediateTriangle triangles[ megabytes( 16 ) ]; - ImmediateContext imm; - - { - immediate_init( &imm, triangles, ARRAY_COUNT( triangles ) ); - // for( size_t i = 0; i < game->hm.width; i++ ) { - // v3 origin = v3( i, 1, game->hm.point( i, 1 ).z ); - // v3 direction = normalize( v3( -1, 0, horizons( i, 1 ) ) ); - // v4 white( 1, 1, 1, 1 ); - // immediate_arrow( &imm, origin, direction, 2, white ); - // } - - for( u32 y = 4; y < game->hm.height; y += 8 ) { - for( u32 x = 4; x < game->hm.width; x += 8 ) { - v3 origin = game->hm.point( x, y ); - v3 direction = normals( x, y ) * 2 - v3( 1, 1, 1 ); - v4 white( 1, 1, 1, 1 ); - immediate_sphere( &imm, origin, 2, white, 4 ); - immediate_sphere( &imm, origin + 3 * direction, 2, white, 4 ); - } - } - - float t; - v3 ray_dir = forward; - v3 inv_dir = v3( 1.0f / ray_dir.x, 1.0f / ray_dir.y, 1.0f / ray_dir.z ); - v3 xnormal; - if( ray_vs_quadtree( &qt, game->pos, ray_dir, inv_dir, &t, &xnormal ) ) { - v3 impact = game->pos + forward * t; - immediate_sphere( &imm, impact, 2, v4( 1, 0, 0, 1 ) ); - v3 smooth_normal = normalize( bilerp( normals, impact.x, impact.y ) ); - printf( "%.2f %.2f %.2f\n", smooth_normal.x, smooth_normal.y, smooth_normal.z ); - immediate_arrow( &imm, impact, smooth_normal, 8, v4( 1, 0, 0, 1 ) ); - } - else printf( "nope\n" ); - - RenderState impact_render_state; - impact_render_state.shader = game->test_outline_shader; - impact_render_state.ubs[ UB_VS_HOT ] = ub_vs; - immediate_render( &imm, impact_render_state ); - } - - // { - // immediate_init( &imm, triangles, ARRAY_COUNT( triangles ) ); - // v3u32 mins = v3u32( 0, 0, qt.nodes[ 0 ].min_z ); - // v3u32 maxs = v3u32( qt.dim, qt.dim, qt.nodes[ 0 ].max_z ); - // draw_qt( &imm, AABBu32( mins, maxs ), qt.nodes, 0 ); - // - // RenderState qt_render_state; - // qt_render_state.shader = game->test_outline_shader; - // qt_render_state.ubs[ UB_VS_HOT ] = ub_vs; - // qt_render_state.wireframe = true; - // immediate_render( &imm, qt_render_state ); - // } - - // immediate_init( &imm, triangles, ARRAY_COUNT( triangles ) ); - // draw_btt( game->btt.left_root, &game->hm, &imm, glm::ivec2( 0, 0 ), glm::ivec2( 0, game->hm.height - 1 ), glm::ivec2( game->hm.width - 1, game->hm.height - 1 ) ); - // draw_btt( game->btt.right_root, &game->hm, &imm, glm::ivec2( game->hm.width - 1, game->hm.height - 1 ), glm::ivec2( game->hm.width - 1, 0 ), glm::ivec2( 0, 0 ) ); - - // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); - // glUseProgram( game->test_outline_shader ); - // glUniformMatrix4fv( game->test_outline_un_vp, 1, GL_FALSE, ( float * ) &VP ); - // immediate_render( &imm, game->test_outline_at_position, game->test_outline_at_colour ); - // glUseProgram( 0 ); - // glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); - - // benchmark_print_timers(); -}