commit 294397f59dd98804d9321ee128171224cbd1e090
parent 4bdec1aae25211570fe81afcfb35eb144b6b43c5
Author: Michael Savage <mikejsavage@gmail.com>
Date: Fri, 10 Nov 2017 19:47:53 +0200
Lots of cleanup
Diffstat:
aabb.h | | | 3 | ++- |
clipmap.cc | | | 83 | +++++++++++++++++++++++++++++++++++++------------------------------------------ |
game.h | | | 14 | +++++++------- |
heightmap.cc | | | 150 | ++++++++++++++++--------------------------------------------------------------- |
heightmap.h | | | 43 | +++++++++++-------------------------------- |
make.lua | | | 12 | ++++++------ |
pp2.cc | | | 7 | ++++--- |
7 files changed, 99 insertions(+), 213 deletions(-)
diff --git a/aabb.h b/aabb.h
@@ -1,6 +1,7 @@
#pragma once
#include "intrinsics.h"
+#include "log.h"
#include "linear_algebra.h"
#include "ggformat.h"
@@ -52,7 +53,7 @@ struct MinMax {
maxs.z = checked_cast< float >( aabb.maxs.z / 256.0f );
}
- bool contains( v3 p ) {
+ bool contains( v3 p ) const {
return p.x >= mins.x && p.y >= mins.y && p.z >= mins.z &&
p.x <= maxs.x && p.y <= maxs.y && p.z <= maxs.z;
}
diff --git a/clipmap.cc b/clipmap.cc
@@ -1,5 +1,7 @@
#include "intrinsics.h"
#include "int_conversions.h"
+#include "linear_algebra.h"
+#include "aabb.h"
#include "game.h"
#include "renderer.h"
#include "shaders.h"
@@ -18,12 +20,16 @@
#include "libs/lz4/lz4.h"
struct ClipmapTerrain {
- Mesh tile;
- Mesh seam;
- Texture heightmap;
- Texture normalmap;
- Texture horizonmap;
- HeightmapQuadTree quadtree;
+ struct {
+ Mesh tile;
+ Mesh seam;
+ Texture heightmap;
+ Texture normalmap;
+ Texture horizonmap;
+ } gpu;
+
+ array2d< u16 > heightmap;
+ QuadTree quadtree;
};
static const float EYE_HEIGHT = 1.8f;
@@ -109,17 +115,14 @@ struct RGBA {
GAME_INIT( game_init ) {
// load quadtree
{
- Heightmap * hm = alloc< Heightmap >( &mem->persistent_arena );
- hm->width = hm->height = 4096;
- hm->pixels = memarena_push_many( &mem->persistent_arena, u16, 4096 * 4096 );
+ clipmap.heightmap = alloc_array2d< u16 >( &mem->persistent_arena, 4096, 4096 );
- array< HeightmapQuadTreeNode > nodes = alloc_array< HeightmapQuadTreeNode >( &mem->persistent_arena, 4096 * 4096 / 3 );
+ array< QuadTreeNode > nodes = alloc_array< QuadTreeNode >( &mem->persistent_arena, 4096 * 4096 / 3 );
file_get_contents_and_decompress( "terrains/gta16.png.parts/quadtree.lz4", ( u8 * ) nodes.ptr(), nodes.num_bytes() );
- clipmap.quadtree.dim = 4096;
- clipmap.quadtree.hm = hm;
clipmap.quadtree.nodes_memory = nodes.ptr();
clipmap.quadtree.nodes = nodes;
+ clipmap.quadtree.heightmap = clipmap.heightmap;
}
// load heightmap
@@ -136,9 +139,9 @@ GAME_INIT( game_init ) {
texture_config.format = TEXFMT_BC5;
texture_config.wrap = TEXWRAP_BORDER;
texture_config.border_colour = v4( 0 );
- clipmap.heightmap = renderer_new_texture( texture_config );
+ clipmap.gpu.heightmap = renderer_new_texture( texture_config );
- bc5_to_heightmap( &mem->persistent_arena, clipmap.quadtree.hm, heightmap_data );
+ bc5_to_heightmap( &mem->persistent_arena, clipmap.heightmap, heightmap_data );
}
// load normalmap
@@ -155,7 +158,7 @@ GAME_INIT( game_init ) {
texture_config.format = TEXFMT_BC5;
texture_config.wrap = TEXWRAP_BORDER;
texture_config.border_colour = v4( 0.5f );
- clipmap.normalmap = renderer_new_texture( texture_config );
+ clipmap.gpu.normalmap = renderer_new_texture( texture_config );
}
// load horizonmap
@@ -172,7 +175,7 @@ GAME_INIT( game_init ) {
texture_config.format = TEXFMT_BC4;
texture_config.wrap = TEXWRAP_BORDER;
texture_config.border_colour = v4( 0 );
- clipmap.horizonmap = renderer_new_texture( texture_config );
+ clipmap.gpu.horizonmap = renderer_new_texture( texture_config );
}
// generate tile mesh
@@ -212,7 +215,7 @@ GAME_INIT( game_init ) {
mesh_config.positions = renderer_new_vb( vertices );
mesh_config.indices = renderer_new_ib( indices );
mesh_config.num_vertices = indices.n;
- clipmap.tile = renderer_new_mesh( mesh_config );
+ clipmap.gpu.tile = renderer_new_mesh( mesh_config );
}
game->pos = v3( 2048, 2048, 100 );
@@ -232,7 +235,7 @@ GAME_INIT( game_init ) {
server_addr.port = 13337;
}
-static void draw_qt( MinMaxu32 aabb, const array< HeightmapQuadTreeNode > nodes, size_t node_idx ) {
+static void draw_qt( MinMaxu32 aabb, const array< QuadTreeNode > nodes, size_t node_idx ) {
if( aabb.maxs.x - aabb.mins.x < 64 ) {
return;
}
@@ -281,6 +284,7 @@ GAME_FRAME( game_frame ) {
v3 right = normalize( cross( forward, world_up ) );
v3 up = normalize( cross( right, forward ) );
+ // player movement
if( game->noclip ) {
const float speed = 100.0f;
game->pos += forward * dt * fb * speed;
@@ -347,12 +351,12 @@ GAME_FRAME( game_frame ) {
float high_t = -1;
v3 low_normal, high_normal;
- bool low_hit = segment_vs_quadtree( &clipmap.quadtree, low_start, low_end, &low_t, &low_normal );
+ bool low_hit = segment_vs_quadtree( clipmap.quadtree, low_start, low_end, &low_t, &low_normal );
bool high_hit = false;
- bool step_up_hit = segment_vs_quadtree( &clipmap.quadtree, low_start, high_start, NULL, NULL );
+ bool step_up_hit = segment_vs_quadtree( clipmap.quadtree, low_start, high_start, NULL, NULL );
if( !step_up_hit ) {
- high_hit = segment_vs_quadtree( &clipmap.quadtree, high_start, high_end, &high_t, &high_normal );
+ high_hit = segment_vs_quadtree( clipmap.quadtree, high_start, high_end, &high_t, &high_normal );
}
v3 step_down_start;
@@ -388,7 +392,7 @@ GAME_FRAME( game_frame ) {
float step_down_t;
v3 step_down_end = step_down_start - v3( 0, 0, step_down_distance );
- bool step_down_hit = segment_vs_quadtree( &clipmap.quadtree, step_down_start, step_down_end, &step_down_t, NULL );
+ bool step_down_hit = segment_vs_quadtree( clipmap.quadtree, step_down_start, step_down_end, &step_down_t, NULL );
game->pos = lerp( step_down_start, step_down_t, step_down_end );
game->on_ground = step_down_hit;
@@ -405,7 +409,7 @@ GAME_FRAME( game_frame ) {
v3 end = start + game->velocity * remaining_t;
float t;
v3 normal;
- bool hit = segment_vs_quadtree( &clipmap.quadtree, start, end, &t, &normal );
+ bool hit = segment_vs_quadtree( clipmap.quadtree, start, end, &t, &normal );
if( hit ) {
// TODO: check normal is flat enough, otherwise stay in the air
game->on_ground = true;
@@ -442,6 +446,7 @@ GAME_FRAME( game_frame ) {
UniformBinding view_uniforms = renderer_uniforms( V, P, game->pos );
UniformBinding sun_uniforms = renderer_uniforms( sun_dir, game->sun_angle );
+ // draw terrain
for( u32 l = 0; l < NUM_LODS; l++ ) {
float scale = 1.0f;
v2 base = v2( -float( PATCH_RESOLUTION * 2 ), -float( PATCH_RESOLUTION * 2 ) );
@@ -463,16 +468,17 @@ GAME_FRAME( game_frame ) {
render_state.uniforms[ UNIFORMS_VIEW ] = view_uniforms;
render_state.uniforms[ UNIFORMS_SUN ] = sun_uniforms;
render_state.uniforms[ UNIFORMS_CLIPMAP ] = renderer_uniforms( offset, scale );
- render_state.textures[ 0 ] = clipmap.heightmap;
- render_state.textures[ 1 ] = clipmap.normalmap;
- render_state.textures[ 2 ] = clipmap.horizonmap;
+ render_state.textures[ 0 ] = clipmap.gpu.heightmap;
+ render_state.textures[ 1 ] = clipmap.gpu.normalmap;
+ render_state.textures[ 2 ] = clipmap.gpu.horizonmap;
render_state.textures[ 3 ] = renderer_blue_noise();
render_state.wireframe = game->draw_wireframe;
- renderer_draw_mesh( clipmap.tile, render_state );
+ renderer_draw_mesh( clipmap.gpu.tile, render_state );
}
}
}
+ // draw trees
{
RenderState render_state;
render_state.shader = get_shader( SHADER_TREE );
@@ -483,21 +489,7 @@ GAME_FRAME( game_frame ) {
renderer_draw_instances( tree_mesh, render_state, num_trees, trees_instance_data );
}
- {
- float t;
- v3 normal;
- if( ray_vs_quadtree( &clipmap.quadtree, Ray3( game->pos, forward ), &t, &normal ) ) {
- v3 impact = game->pos + forward * t;
- immediate_arrow( impact, normal, 16, v4( 0, 1, 1, 1 ) );
- }
-
- RenderState trace_render_state;
- trace_render_state.shader = get_shader( SHADER_FLAT_VERTEX_COLOURS );
- trace_render_state.uniforms[ UNIFORMS_VIEW ] = view_uniforms;
- trace_render_state.wireframe = break3;
- immediate_render( trace_render_state );
- }
-
+ // draw crosshair
{
const float aspect = get_aspect_ratio();
const float crosshair_thickness = 0.0025f;
@@ -534,6 +526,7 @@ GAME_FRAME( game_frame ) {
immediate_render( render_state );
}
+ // fireballs
if( input->keys[ KEY_F ] ) {
Fireball * fireball = fireballs.acquire();
fireball->pos = game->pos + v3( 0.0f, 0.0f, EYE_HEIGHT );
@@ -550,7 +543,7 @@ GAME_FRAME( game_frame ) {
float t;
v3 normal;
- if( segment_vs_quadtree( &clipmap.quadtree, fireball->pos, end, &t, &normal ) ) {
+ if( segment_vs_quadtree( clipmap.quadtree, fireball->pos, end, &t, &normal ) ) {
Explosion * explosion = explosions.acquire();
explosion->pos = lerp( fireball->pos, t, end );
explosion->created_at = current_time;
@@ -590,6 +583,7 @@ GAME_FRAME( game_frame ) {
immediate_render( explosion_render_state );
}
+ // networking
{
char buf[ 1400 ];
NetAddress recv_addr;
@@ -693,7 +687,7 @@ GAME_FRAME( game_frame ) {
if( game->draw_quadtree ) {
v3u32 mins = v3u32( 0, 0, clipmap.quadtree.nodes[ 0 ].min_z );
- v3u32 maxs = v3u32( clipmap.quadtree.dim, clipmap.quadtree.dim, clipmap.quadtree.nodes[ 0 ].max_z );
+ v3u32 maxs = v3u32( clipmap.heightmap.w, clipmap.heightmap.h, clipmap.quadtree.nodes[ 0 ].max_z );
draw_qt( MinMaxu32( mins, maxs ), clipmap.quadtree.nodes, 0 );
RenderState render_state;
@@ -703,6 +697,7 @@ GAME_FRAME( game_frame ) {
immediate_render( render_state );
}
+ // info text
{
const str< 128 > status( "Frame time: {.1}ms FPS: {.1} {}", dt * 1000.0f, 1.0f / dt,
connected ? "connected!" : "connecting" );
diff --git a/game.h b/game.h
@@ -3,9 +3,9 @@
// TODO: this whole file blows
#include "intrinsics.h"
#include "linear_algebra.h"
-#include "terrain_manager.h"
-#include "btt.h"
-#include "gpubtt.h"
+// #include "terrain_manager.h"
+// #include "btt.h"
+// #include "gpubtt.h"
#include "heightmap.h"
#include "bsp.h"
#include "bsp_renderer.h"
@@ -28,7 +28,7 @@ struct GameState {
bool noclip;
float pitch, yaw;
- TerrainManager tm;
+ // TerrainManager tm;
BSP bsp;
BSPRenderer bspr;
@@ -39,9 +39,9 @@ struct GameState {
float sun_angle;
- BTTs btt;
- GPUBTT gpubtt;
- Heightmap hm;
+ // BTTs btt;
+ // GPUBTT gpubtt;
+ // Heightmap hm;
bool draw_wireframe;
bool draw_quadtree;
diff --git a/heightmap.cc b/heightmap.cc
@@ -3,93 +3,51 @@
#include "intrinsics.h"
#include "linear_algebra.h"
+#include "aabb.h"
#include "heightmap.h"
#include "log.h"
#include "memory_arena.h"
#include "decompress_bc.h"
-static float lerp( float a, float b, float t ) {
- return a * ( 1 - t ) + b * t;
-}
-
static float bilerp( v3 p1, v3 p2, v3 p3, v3 p4, v2 p ) {
const float tx = ( p.x - p1.x ) / ( p2.x - p1.x );
- const float mx1 = lerp( p1.z, p2.z, tx );
- const float mx2 = lerp( p3.z, p4.z, tx );
+ const float mx1 = lerp( p1.z, tx, p2.z );
+ const float mx2 = lerp( p3.z, tx, p4.z );
const float ty = ( p.y - p1.y ) / ( p3.y - p1.y );
- return lerp( mx1, mx2, ty );
-}
-
-void heightmap_init( Heightmap * hm, u16 * pixels, u32 width, u32 height ) {
- hm->pixels = pixels;
- hm->width = width;
- hm->height = height;
-}
-
-void heightmap_destroy( Heightmap * hm ) {
- if( hm->pixels != NULL ) {
- free( hm->pixels );
- }
-}
-
-v3 Heightmap::point( u32 x, u32 y ) const {
- float h = heightmap_height( this, x, y );
- return v3( checked_cast< float >( x ), checked_cast< float >( y ), h );
-}
-
-float heightmap_height( const Heightmap * hm, u32 x, u32 y ) {
- return heightmap_height_u16( hm, x, y ) / 256.0f;
+ return lerp( mx1, ty, mx2 );
}
-u16 heightmap_height_u16( const Heightmap * hm, u32 x, u32 y ) {
- if( x >= hm->width || y >= hm->height )
- return 0;
- return hm->pixels[ y * hm->width + x ];
+float heightmap_height( const array2d< u16 > hm, u32 x, u32 y ) {
+ return hm.try_get( x, y, 0 ) / 256.0f;
}
-float Heightmap::bilerp_height( float x, float y ) const {
- const u32 ix = checked_cast< u32 >( floorf( x ) );
- const u32 iy = checked_cast< u32 >( floorf( y ) );
-
- return bilerp(
- point( ix, iy ),
- point( ix + 1, iy ),
- point( ix, iy + 1 ),
- point( ix + 1, iy + 1 ),
- v2( x, y )
- );
+v3 heightmap_point( const array2d< u16 > hm, u32 x, u32 y ) {
+ return v3( checked_cast< float >( x ), checked_cast< float >( y ), heightmap_height( hm, x, y ) );
}
-//
-//
-//
-//
-//
-//
-
-bool ray_vs_aabb( MinMax aabb, v3 start, v3 inv_dir, float * t ) {
- if( aabb.contains( start ) ) {
+static bool ray_vs_aabb( const MinMax & aabb, const Ray3 & ray, float * t ) {
+ if( aabb.contains( ray.origin ) ) {
*t = 0.0f;
return true;
}
- float tx1 = ( aabb.mins.x - start.x ) * inv_dir.x;
- float tx2 = ( aabb.maxs.x - start.x ) * inv_dir.x;
+ float tx1 = ( aabb.mins.x - ray.origin.x ) * ray.inv_dir.x;
+ float tx2 = ( aabb.maxs.x - ray.origin.x ) * ray.inv_dir.x;
float tmin = min( tx1, tx2 );
float tmax = max( tx1, tx2 );
- float ty1 = ( aabb.mins.y - start.y ) * inv_dir.y;
- float ty2 = ( aabb.maxs.y - start.y ) * inv_dir.y;
+ float ty1 = ( aabb.mins.y - ray.origin.y ) * ray.inv_dir.y;
+ float ty2 = ( aabb.maxs.y - ray.origin.y ) * ray.inv_dir.y;
tmin = max( tmin, min( ty1, ty2 ) );
tmax = min( tmax, max( ty1, ty2 ) );
- float tz1 = ( aabb.mins.z - start.z ) * inv_dir.z;
- float tz2 = ( aabb.maxs.z - start.z ) * inv_dir.z;
+ float tz1 = ( aabb.mins.z - ray.origin.z ) * ray.inv_dir.z;
+ float tz2 = ( aabb.maxs.z - ray.origin.z ) * ray.inv_dir.z;
tmin = max( tmin, min( tz1, tz2 ) );
tmax = min( tmax, max( tz1, tz2 ) );
@@ -188,54 +146,6 @@ static size_t child_idx( size_t node_idx, size_t quadrant ) {
return node_idx * 4 + quadrant + 1;
}
-static void heightmap_build_quadtree_node( HeightmapQuadTree * qt, size_t node_idx, MinMaxu32 aabb ) {
- qt->nodes[ node_idx ].min_z = U16_MAX;
- qt->nodes[ node_idx ].max_z = 0;
-
- if( aabb.maxs.x - aabb.mins.x == 2 || aabb.maxs.y - aabb.mins.y == 2 ) {
- for( size_t y = aabb.mins.y; y <= aabb.maxs.y; y++ ) {
- for( size_t x = aabb.mins.x; x <= aabb.maxs.x; x++ ) {
- u16 h = heightmap_height_u16( qt->hm, x, y );
- qt->nodes[ node_idx ].min_z = min( h, qt->nodes[ node_idx ].min_z );
- qt->nodes[ node_idx ].max_z = max( h, qt->nodes[ node_idx ].max_z );
- }
- }
-
- return;
- }
-
- u16 lo = U16_MAX;
- u16 hi = 0;
- for( int i = 0; i < 4; i++ ) {
- heightmap_build_quadtree_node( qt, child_idx( node_idx, i ), aabb.quadrant( i ) );
- lo = min( lo, qt->nodes[ child_idx( node_idx, i ) ].min_z );
- hi = max( hi, qt->nodes[ child_idx( node_idx, i ) ].max_z );
- }
-
- qt->nodes[ node_idx ].min_z = lo;
- qt->nodes[ node_idx ].max_z = hi;
-}
-
-size_t heightmap_quadtree_max_nodes( const Heightmap * hm ) {
- // with a power of 2 + 1 sized quadtree with a 2x2 bottom level it
- // should be exactly 1/3 (1/4 + 1/16 + ...)
- return ( ( hm->width - 1 ) * ( hm->width - 1 ) ) / 3;
-}
-
-HeightmapQuadTree heightmap_build_quadtree( Heightmap * hm, array< HeightmapQuadTreeNode > & nodes ) {
- ASSERT( nodes.n >= heightmap_quadtree_max_nodes( hm ) );
-
- HeightmapQuadTree qt;
- qt.hm = hm;
- qt.dim = hm->width - 1;
- qt.nodes = nodes;
-
- MinMaxu32 aabb( v3u32( 0, 0, 0 ), v3u32( qt.dim, qt.dim, U16_MAX ) );
- heightmap_build_quadtree_node( &qt, 0, aabb );
-
- return qt;
-}
-
static void swap( int * a, int * b ) {
float t = *a;
*a = *b;
@@ -264,7 +174,7 @@ static void sort4( int * indices, float * elems ) {
}
static bool ray_vs_quadtree_node(
- const HeightmapQuadTree * qt, size_t node_idx, MinMaxu32 aabb,
+ const QuadTree & qt, size_t node_idx, MinMaxu32 aabb,
const Ray3 & ray, float * t, v3 * xnormal
) {
if( aabb.maxs.x - aabb.mins.x == 2 || aabb.maxs.y - aabb.mins.y == 2 ) {
@@ -275,10 +185,10 @@ static bool ray_vs_quadtree_node(
u32 x = aabb.mins.x + j;
u32 y = aabb.mins.y + i;
- v3 p0 = qt->hm->point( x, y );
- v3 p1 = qt->hm->point( x + 1, y );
- v3 p2 = qt->hm->point( x, y + 1 );
- v3 p3 = qt->hm->point( x + 1, y + 1 );
+ v3 p0 = heightmap_point( qt.heightmap, x + 0, y + 0 );
+ v3 p1 = heightmap_point( qt.heightmap, x + 1, y + 0 );
+ v3 p2 = heightmap_point( qt.heightmap, x + 0, y + 1 );
+ v3 p3 = heightmap_point( qt.heightmap, x + 1, y + 1 );
// bottom right triangle
float t0;
@@ -312,10 +222,10 @@ static bool ray_vs_quadtree_node(
MinMaxu32 aabbs[ 4 ];
float ts[ 4 ];
for( int i = 0; i < 4; i++ ) {
- HeightmapQuadTreeNode child = qt->nodes[ child_idx( node_idx, i ) ];
+ QuadTreeNode child = qt.nodes[ child_idx( node_idx, i ) ];
aabbs[ i ] = aabb.quadrant( i ).clamp_z( child.min_z, child.max_z );
ts[ i ] = FLT_MAX;
- ray_vs_aabb( MinMax( aabbs[ i ] ), ray.origin, ray.inv_dir, &ts[ i ] );
+ ray_vs_aabb( MinMax( aabbs[ i ] ), ray, &ts[ i ] );
}
int sorted[ 4 ];
@@ -334,15 +244,15 @@ static bool ray_vs_quadtree_node(
return false;
}
-bool ray_vs_quadtree( const HeightmapQuadTree * qt, const Ray3 & ray, float * t, v3 * xnormal ) {
- v3u32 mins = v3u32( 0, 0, qt->nodes[ 0 ].min_z );
- v3u32 maxs = v3u32( qt->dim, qt->dim, qt->nodes[ 0 ].max_z );
+bool ray_vs_quadtree( const QuadTree & qt, const Ray3 & ray, float * t, v3 * xnormal ) {
+ v3u32 mins = v3u32( 0, 0, qt.nodes[ 0 ].min_z );
+ v3u32 maxs = v3u32( qt.heightmap.w, qt.heightmap.h, qt.nodes[ 0 ].max_z );
MinMaxu32 aabb( mins, maxs );
return ray_vs_quadtree_node( qt, 0, aabb, ray, t, xnormal );
}
-bool segment_vs_quadtree( const HeightmapQuadTree * qt, const v3 & start, const v3 & end, float * t, v3 * xnormal ) {
+bool segment_vs_quadtree( const QuadTree & qt, const v3 & start, const v3 & end, float * t, v3 * xnormal ) {
v3 dont_care_normal;
if( xnormal == NULL )
xnormal = &dont_care_normal;
@@ -365,16 +275,16 @@ bool segment_vs_quadtree( const HeightmapQuadTree * qt, const v3 & start, const
return nt <= l;
}
-void bc5_to_heightmap( MemoryArena * arena, Heightmap * hm, u8 * bc5 ) {
+void bc5_to_heightmap( MemoryArena * arena, array2d< u16 > hm, u8 * bc5 ) {
MEMARENA_SCOPED_CHECKPOINT( arena );
- array2d< v2 > rg = alloc_array2d< v2 >( arena, hm->width, hm->height );
+ array2d< v2 > rg = alloc_array2d< v2 >( arena, hm.w, hm.h );
decompress_bc5( rg, bc5 );
for( u32 y = 0; y < rg.h; y++ ) {
for( u32 x = 0; x < rg.w; x++ ) {
float r = rg( x, y ).x;
float g = rg( x, y ).y;
- hm->pixels[ y * 4096 + x ] = u16( ( r * 256.0f + g ) * 255.0f );
+ hm( x, y ) = u16( ( r * 256.0f + g ) * 255.0f );
}
}
}
diff --git a/heightmap.h b/heightmap.h
@@ -4,43 +4,22 @@
#include "memory_arena.h"
#include "array.h"
#include "linear_algebra.h"
-#include "aabb.h"
-class Heightmap {
-public:
- u16 * pixels = NULL;
- u32 width, height;
-
- v3 point( u32 x, u32 y ) const;
-
- float bilerp_height( float x, float y ) const;
-};
-
-struct OffsetHeightmap {
- Heightmap hm;
- float x_offset, y_offset;
-};
-
-void heightmap_init( Heightmap * hm, u16 * pixels, u32 width, u32 height );
-float heightmap_height( const Heightmap * hm, u32 x, u32 y );
-u16 heightmap_height_u16( const Heightmap * hm, u32 x, u32 y );
-void heightmap_destroy( Heightmap * hm );
-
-struct HeightmapQuadTreeNode {
+struct QuadTreeNode {
u16 min_z, max_z;
};
-struct HeightmapQuadTree {
- u32 dim;
- HeightmapQuadTreeNode * nodes_memory;
- array< HeightmapQuadTreeNode > nodes;
- Heightmap * hm;
+struct QuadTree {
+ QuadTreeNode * nodes_memory;
+ array< QuadTreeNode > nodes;
+ array2d< u16 > heightmap;
};
-size_t heightmap_quadtree_max_nodes( const Heightmap * hm );
-HeightmapQuadTree heightmap_build_quadtree( const Heightmap * hm, array< HeightmapQuadTreeNode > & nodes );
+float heightmap_height( const array2d< u16 > hm, u32 x, u32 y );
+u16 heightmap_height_u16( const array2d< u16 > hm, u32 x, u32 y );
+v3 heightmap_point( const array2d< u16 > hm, u32 x, u32 y );
-bool ray_vs_quadtree( const HeightmapQuadTree * qt, const Ray3 & ray, float * t, v3 * xnormal );
-bool segment_vs_quadtree( const HeightmapQuadTree * qt, const v3 & start, const v3 & end, float * t, v3 * xnormal );
+bool ray_vs_quadtree( const QuadTree & qt, const Ray3 & ray, float * t, v3 * xnormal );
+bool segment_vs_quadtree( const QuadTree & qt, const v3 & start, const v3 & end, float * t, v3 * xnormal );
-void bc5_to_heightmap( MemoryArena * arena, Heightmap * hm, u8 * bc5 );
+void bc5_to_heightmap( MemoryArena * arena, array2d< u16 > hm, u8 * bc5 );
diff --git a/make.lua b/make.lua
@@ -18,9 +18,9 @@ if OS == "macos" then
game_ldflags = "-framework Cocoa -framework CoreVideo -framework IOKit"
end
-bin( "medfall", { "main", "hm", "heightmap", "terrain_manager", "btt", "gpubtt", "skybox", "http", "platform_network", game_objs }, { "lz4", game_libs } )
-msvc_bin_ldflags( "medfall", "opengl32.lib gdi32.lib Ws2_32.lib" )
-gcc_bin_ldflags( "medfall", game_ldflags )
+-- bin( "medfall", { "main", "hm", "heightmap", "terrain_manager", "btt", "gpubtt", "skybox", "http", "platform_network", game_objs }, { "lz4", game_libs } )
+-- msvc_bin_ldflags( "medfall", "opengl32.lib gdi32.lib Ws2_32.lib" )
+-- gcc_bin_ldflags( "medfall", game_ldflags )
bin( "launch", { "launcher/main", "http", "sha2", "platform_network", game_objs }, { "imgui", "monocypher", "whereami", game_libs } )
msvc_bin_ldflags( "launch", "opengl32.lib gdi32.lib Ws2_32.lib" )
@@ -64,9 +64,9 @@ if OS == "macos" then
bin_ldflags( "sound", "-framework AudioUnit" )
end
-bin( "pp", { "pp", "heightmap", common_objs }, { "lodepng", "lz4", "squish", "stb_image", "stb_image_write" } )
-gcc_obj_cxxflags( "pp", "-O2" )
-msvc_obj_cxxflags( "pp", "/O2" )
+-- bin( "pp", { "pp", "heightmap", common_objs }, { "lodepng", "lz4", "squish", "stb_image", "stb_image_write" } )
+-- gcc_obj_cxxflags( "pp", "-O2" )
+-- msvc_obj_cxxflags( "pp", "/O2" )
bin( "pp2", { "pp2", "heightmap", "decompress_bc", common_objs }, { "lz4", "squish", "stb_image", "stb_image_write" } )
gcc_obj_cxxflags( "pp2", "-O2" )
diff --git a/pp2.cc b/pp2.cc
@@ -2,6 +2,7 @@
#include "array.h"
#include "memory_arena.h"
#include "linear_algebra.h"
+#include "aabb.h"
#include "dynstr.h"
#include "int_conversions.h"
#include "heightmap.h"
@@ -124,7 +125,7 @@ static size_t child_idx( size_t node_idx, size_t quadrant ) {
return node_idx * 4 + quadrant + 1;
}
-static void build_quadtree_node( const array2d< u16 > heightmap, array< HeightmapQuadTreeNode > & nodes, size_t node_idx, MinMaxu32 aabb ) {
+static void build_quadtree_node( const array2d< u16 > heightmap, array< QuadTreeNode > & nodes, size_t node_idx, MinMaxu32 aabb ) {
nodes[ node_idx ].min_z = U16_MAX;
nodes[ node_idx ].max_z = 0;
@@ -152,7 +153,7 @@ static void build_quadtree_node( const array2d< u16 > heightmap, array< Heightma
nodes[ node_idx ].max_z = hi;
}
-static void build_quadtree( const array2d< u16 > heightmap, array< HeightmapQuadTreeNode > & nodes ) {
+static void build_quadtree( const array2d< u16 > heightmap, array< QuadTreeNode > & nodes ) {
ASSERT( nodes.n >= quadtree_max_nodes( heightmap.w, heightmap.h ) );
ASSERT( heightmap.w == heightmap.h );
@@ -299,7 +300,7 @@ int main( int argc, char ** argv ) {
}
size_t num_nodes = quadtree_max_nodes( heightmap1.w, heightmap1.h );
- array< HeightmapQuadTreeNode > nodes = alloc_array< HeightmapQuadTreeNode >( &arena, num_nodes );
+ array< QuadTreeNode > nodes = alloc_array< QuadTreeNode >( &arena, num_nodes );
build_quadtree( heightmap1, nodes );