medfall

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

commit 2b3702573d138f8bd28633646f700b869096db2c
parent 5b9522f26034be2ba803a0916d84e766e5ebc9fa
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Tue Nov 15 20:50:32 +0200

Remove almost all glm usage

Diffstat:
bsp.cc | 65+++++++++++++++++++++++++----------------------------------------
bsp.h | 24+++++++++---------------
bsp_renderer.cc | 2+-
bsp_renderer.h | 2--
btt.cc | 22++++++++--------------
game.h | 7+++----
gpubtt.cc | 35++++++++++++++---------------------
heightmap.cc | 46++++++++++++++--------------------------------
heightmap.h | 4+---
hm.cc | 21+++++++++------------
linear_algebra.h | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
mod_btt.cc | 27++++++++++++---------------
shadow_map.cc | 25+++++++++++--------------
terrain_manager.cc | 8+++-----
terrain_manager.h | 8+++-----
15 files changed, 184 insertions(+), 195 deletions(-)
diff --git a/bsp.cc b/bsp.cc @@ -1,8 +1,5 @@ #include <math.h> -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> - #include "game.h" #include "intrinsics.h" #include "log.h" @@ -14,8 +11,8 @@ static ImmediateTriangle triangles[ 512000 ]; static ImmediateContext imm; static bool fix = false; -static glm::vec3 fix_start; -static glm::vec3 fix_end; +static v3 fix_start; +static v3 fix_end; static const char * vert_src = GLSL( in vec3 position; @@ -82,14 +79,6 @@ float point_plane_distance( v3 point, v3 normal, float d ) { return dot( point, normal ) - d; } -float point_plane_distance( glm::vec3 point, glm::vec3 normal, float d ) { - return point_plane_distance( GLMV3( point ), GLMV3( normal ), d ); -} - -float point_plane_distance( glm::vec3 point, v3 normal, float d ) { - return point_plane_distance( GLMV3( point ), normal, d ); -} - void bsp_init( BSP * bsp, const char * filename ) { bsp->contents = file_get_contents( filename, NULL ); @@ -139,8 +128,8 @@ void BSP::load_vis() { // // from Real-Time Collision Detection by Christer Ericson, published by Morgan // Kaufmann Publishers, (c) 2005 Elsevier Inc -bool BSP::trace_seg_brush( const BSP_Brush & brush, const glm::vec3 start, const glm::vec3 dir, const float tmin, const float tmax, float * tout ) const { - const glm::vec3 end = start + dir * tmax; +bool BSP::trace_seg_brush( const BSP_Brush & brush, v3 start, v3 dir, float tmin, float tmax, float * tout ) const { + const v3 end = start + dir * tmax; float tfar = tmin; float tnear = tmax; bool hit = false; @@ -160,7 +149,7 @@ bool BSP::trace_seg_brush( const BSP_Brush & brush, const glm::vec3 start, const if( start_dist >= 0.0f ) starts_inside = false; - const float denom = -dot( plane.n, GLMV3( dir ) ); + const float denom = -dot( plane.n, dir ); const float t = start_dist / denom; if( t >= tmin && t <= tmax ) { @@ -178,16 +167,16 @@ bool BSP::trace_seg_brush( const BSP_Brush & brush, const glm::vec3 start, const hit = true; colour = v4( 0, 0, 0, 1 ); } - immediate_sphere( &imm, GLMV3( start + t * dir ), 8, colour, 8 ); + immediate_sphere( &imm, start + t * dir, 8, colour, 8 ); } else - immediate_sphere( &imm, GLMV3( start + t * dir ), 8, v4( 0.5, 0.5, 0.5, 1 ), 8 ); + immediate_sphere( &imm, start + t * dir, 8, v4( 0.5, 0.5, 0.5, 1 ), 8 ); } v4 colour = v4( 1, 0, 1, 1 ); if( tnear >= tfar ) colour = v4( 1, 1, 0, 1 ); - if( hit ) immediate_sphere( &imm, GLMV3( start + tfar * dir ), 16, colour ); + if( hit ) immediate_sphere( &imm, start + tfar * dir, 16, colour ); if( hit ) { if( !starts_inside ) { if( tfar <= tnear ) { @@ -204,7 +193,7 @@ bool BSP::trace_seg_brush( const BSP_Brush & brush, const glm::vec3 start, const return false; } -void BSP::trace_seg_leaf( const u32 leaf_idx, const glm::vec3 start, const glm::vec3 dir, const float tmin, const float tmax, BSP_Intersection & bis ) const { +void BSP::trace_seg_leaf( u32 leaf_idx, v3 start, v3 dir, float tmin, float tmax, BSP_Intersection & bis ) const { const BSP_Leaf & leaf = leaves[ leaf_idx ]; for( u32 i = 0; i < leaf.num_brushes; i++ ) { @@ -224,7 +213,7 @@ void BSP::trace_seg_leaf( const u32 leaf_idx, const glm::vec3 start, const glm:: } } -void BSP::trace_seg_tree( const s32 node_idx, const glm::vec3 start, const glm::vec3 dir, const float tmin, const float tmax, BSP_Intersection & bis ) const { +void BSP::trace_seg_tree( s32 node_idx, v3 start, v3 dir, float tmin, float tmax, BSP_Intersection & bis ) const { if( bis.hit ) return; if( node_idx < 0 ) { @@ -239,7 +228,7 @@ void BSP::trace_seg_tree( const s32 node_idx, const glm::vec3 start, const glm:: // start . plane.n + t * dir . plane.n = plane.d // t * dir . plane.n = plane.d - start . plane.n // t * denom = dist - const float denom = dot( plane.n, GLMV3( dir ) ); + const float denom = dot( plane.n, dir ); const float dist = -point_plane_distance( start, plane.n, plane.d ); bool near_child = dist > 0.0f; bool check_both_sides = false; @@ -261,7 +250,7 @@ void BSP::trace_seg_tree( const s32 node_idx, const glm::vec3 start, const glm:: // otherwise we straddle the plane check_both_sides = true; t = unchecked_t; - immediate_sphere( &imm, GLMV3( start + t * dir ), 8, v4( 0, 1, 0, 1 ), 8 ); + immediate_sphere( &imm, start + t * dir, 8, v4( 0, 1, 0, 1 ), 8 ); } } } @@ -274,13 +263,13 @@ void BSP::trace_seg_tree( const s32 node_idx, const glm::vec3 start, const glm:: } } -bool BSP::trace_seg( const glm::vec3 & start, const glm::vec3 & end, Intersection & is ) const { +bool BSP::trace_seg( v3 start, v3 end, Intersection & is ) const { BSP_Intersection bis = { }; bis.is.t = 1.0f; bis.start = start; bis.end = end; - const glm::vec3 dir = end - start; + v3 dir = end - start; trace_seg_tree( 0, start, dir, 0.0f, 1.0f, bis ); @@ -292,7 +281,7 @@ bool BSP::trace_seg( const glm::vec3 & start, const glm::vec3 & end, Intersectio return bis.hit; } -BSP_Leaf & BSP::position_to_leaf( const v3 & pos ) const { +BSP_Leaf & BSP::position_to_leaf( v3 pos ) const { s32 node_idx = 0; do { @@ -307,12 +296,8 @@ BSP_Leaf & BSP::position_to_leaf( const v3 & pos ) const { return leaves[ -( node_idx + 1 ) ]; } -v3 d2r( v3 degrees ) { - return degrees * float( M_PI ) / 180.0f; -} - -glm::vec3 angles_to_vector( const glm::vec3 & angles ) { - return glm::vec3( +static v3 angles_to_vector( v3 angles ) { + return v3( -sin( angles.y ) * sin( angles.x ), -cos( angles.y ) * sin( angles.x ), -cos( angles.x ) @@ -327,8 +312,8 @@ extern "C" GAME_INIT( game_init ) { MemoryArena arena = memarena_push_arena( &mem->persistent_arena, megabytes( 10 ) ); - game->pos = glm::vec3( 0, -100, 450 ); - game->angles = glm::radians( glm::vec3( -90, 135, 0 ) ); + game->pos = v3( 0, -100, 450 ); + game->angles = radians( v3( -90, 135, 0 ) ); game->test_shader = renderer_new_shader( vert_src, frag_src ); @@ -346,10 +331,10 @@ extern "C" GAME_INIT( game_init ) { test_ub = renderer_new_ub(); } -static m4 camera_to_vp( glm::vec3 position, glm::vec3 angles ) { +static m4 camera_to_vp( v3 position, v3 angles ) { const m4 P = m4_perspective( 120.0f, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, 10000.0f ); - return m4_translation( -GLMV3( position ) ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ) * P; + return m4_translation( -position ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ) * P; } extern "C" GAME_FRAME( game_frame ) { @@ -363,9 +348,9 @@ extern "C" GAME_FRAME( game_frame ) { game->angles.x += pitch * dt * 2; game->angles.y += yaw * dt * 2; - const glm::vec3 forward = angles_to_vector( game->angles ); + const v3 forward = angles_to_vector( game->angles ); game->pos += forward * 100.0f * dt * ( float ) fb; - const glm::vec3 sideways = glm::vec3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); + const v3 sideways = v3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); game->pos += sideways * 100.0f * dt * ( float ) lr; game->pos.z += ( float ) dz * 100.0f * dt; m4 VP = camera_to_vp( game->pos, game->angles ); @@ -376,7 +361,7 @@ extern "C" GAME_FRAME( game_frame ) { RenderState render_state = { }; render_state.shader = game->test_shader; render_state.ubs[ UB_VS_HOT ] = test_ub; - bspr_render( &game->bspr, GLMV3( game->pos ), render_state ); + bspr_render( &game->bspr, game->pos, render_state ); immediate_init( &imm, triangles, ARRAY_COUNT( triangles ) ); immediate_sphere( &imm, v3( 0, 0, 0 ), 128, v4( 1, 1, 0, 1 ) ); @@ -392,7 +377,7 @@ extern "C" GAME_FRAME( game_frame ) { bool hit = game->bspr.bsp->trace_seg( fix_start, fix_end, is ); if( hit ) { - immediate_sphere( &imm, GLMV3( is.pos ), 16, v4( 1, 0, 0, 1 ) ); + immediate_sphere( &imm, is.pos, 16, v4( 1, 0, 0, 1 ) ); } } diff --git a/bsp.h b/bsp.h @@ -1,8 +1,6 @@ #ifndef _BSP_H_ #define _BSP_H_ -#include <glm/glm.hpp> - #include "intrinsics.h" #include "linear_algebra.h" @@ -144,14 +142,14 @@ struct BSP_Vis { struct Intersection { const BSP_Plane * plane; - glm::vec3 pos; + v3 pos; float t; }; struct BSP_Intersection { Intersection is; - glm::vec3 start; - glm::vec3 end; + v3 start; + v3 end; bool hit; }; @@ -195,21 +193,17 @@ public: // TODO BSP_Vis * vis; - template< typename T > void load_lump( u32 & num_ts, T *& ts, const BSP_Lump lump ); + template< typename T > void load_lump( u32 & num_ts, T *& ts, BSP_Lump lump ); void load_vis(); - // void trace_seg_brush( const BSP_Brush & brush, BSP_Intersection & bis ) const; - // void trace_seg_leaf( const s32 leaf_idx, BSP_Intersection & bis ) const; - // void trace_seg_tree( const s32 node_idx, const v3 & start, const v3 & end, const float t1, const float t2, BSP_Intersection & bis ) const; - - bool trace_seg_brush( const BSP_Brush & brush, const glm::vec3 start, const glm::vec3 dir, const float tmin, const float tmax, float * tout ) const; - void trace_seg_leaf( const u32 leaf_idx, const glm::vec3 start, const glm::vec3 dir, const float tmin, const float tmax, BSP_Intersection & bis ) const; - void trace_seg_tree( const s32 node_idx, const glm::vec3 start, const glm::vec3 dir, const float tmin, const float tmax, BSP_Intersection & bis ) const; + bool trace_seg_brush( const BSP_Brush & brush, v3 start, v3 dir, float tmin, float tmax, float * tout ) const; + void trace_seg_leaf( u32 leaf_idx, v3 start, v3 dir, float tmin, float tmax, BSP_Intersection & bis ) const; + void trace_seg_tree( s32 node_idx, v3 start, v3 dir, float tmin, float tmax, BSP_Intersection & bis ) const; - BSP_Leaf & position_to_leaf( const v3 & pos ) const; + BSP_Leaf & position_to_leaf( v3 pos ) const; public: - bool trace_seg( const glm::vec3 & start, const glm::vec3 & end, Intersection & is ) const; + bool trace_seg( v3 start, v3 end, Intersection & is ) const; }; void bsp_init( BSP * bsp, const char * filename ); diff --git a/bsp_renderer.cc b/bsp_renderer.cc @@ -85,7 +85,7 @@ void bspr_init( BSPRenderer * bspr, MemoryArena * arena, const BSP * bsp ) { const s32 * face_indices = &bsp->mesh_verts[ face.first_mesh_vert ]; for( s32 v = 0; v < face.num_verts; v++ ) { - v3 pos = GLMV3( vertices[ v ].pos ); + v3 pos = vertices[ v ].pos; positions[ num_positions++ ] = pos; colours[ num_colours++ ] = colour; diff --git a/bsp_renderer.h b/bsp_renderer.h @@ -1,8 +1,6 @@ #ifndef _BSP_RENDERER_H_ #define _BSP_RENDERER_H_ -#include <glm/glm.hpp> - #include "bsp.h" #include "memory_arena.h" #include "renderer.h" diff --git a/btt.cc b/btt.cc @@ -1,5 +1,4 @@ #include "glsl.h" -#include <glm/glm.hpp> #include "intrinsics.h" #include "memory_arena.h" @@ -66,16 +65,12 @@ static void btt_split( MemoryArena * arena, BTT * node ) { } } -static int square_distance( glm::ivec2 u, glm::ivec2 v ) { - glm::ivec2 d = v - u; - return d.x * d.x + d.y * d.y; +static s32 square_distance( v2s32 u, v2s32 v ) { + v2s32 d = v - u; + return dot( d, d ); } -static bool btt_should_split( - const Heightmap * hm, - glm::ivec2 v0, glm::ivec2 v1, glm::ivec2 v2, - glm::ivec2 mid -) { +static bool btt_should_split( const Heightmap * hm, v2s32 v0, v2s32 v1, v2s32 v2, v2s32 mid ) { if( square_distance( v0, v2 ) <= 4 ) return false; const float avg_height = ( hm->point( v0.x, v0.y ).z + hm->point( v2.x, v2.y ).z ) * 0.5f; @@ -88,10 +83,9 @@ static bool btt_should_split( static void btt_build( const Heightmap * hm, MemoryArena * arena, - BTT * node, - glm::ivec2 v0, glm::ivec2 v1, glm::ivec2 v2 + BTT * node, v2s32 v0, v2s32 v1, v2s32 v2 ) { - glm::ivec2 mid = ( v0 + v2 ) / 2; + v2s32 mid = ( v0 + v2 ) / 2; if( !node->left ) { assert( !node->right ); @@ -121,8 +115,8 @@ BTTs btt_from_heightmap( const Heightmap * hm, MemoryArena * 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, hm->height - 1 ), glm::ivec2( hm->width - 1, hm->height - 1 ) ); - btt_build( hm, arena, roots.right_root, glm::ivec2( hm->width - 1, hm->height - 1 ), glm::ivec2( hm->width - 1, 0 ), glm::ivec2( 0, 0 ) ); + btt_build( hm, arena, roots.left_root, v2s32( 0, 0 ), v2s32( 0, hm->height - 1 ), v2s32( hm->width - 1, hm->height - 1 ) ); + btt_build( hm, arena, roots.right_root, v2s32( hm->width - 1, hm->height - 1 ), v2s32( hm->width - 1, 0 ), v2s32( 0, 0 ) ); return roots; } diff --git a/game.h b/game.h @@ -1,10 +1,9 @@ #ifndef _GAME_H_ #define _GAME_H_ -#include <glm/glm.hpp> - // TODO: this whole file blows #include "intrinsics.h" +#include "linear_algebra.h" #include "benchmark.h" #include "assets.h" #include "terrain_manager.h" @@ -27,8 +26,8 @@ const float NEAR_PLANE_DEPTH = 0.1f; const float FAR_PLANE_DEPTH = 10000.0f; struct GameState { - glm::vec3 pos; - glm::vec3 angles; + v3 pos; + v3 angles; TerrainManager tm; BSP bsp; diff --git a/gpubtt.cc b/gpubtt.cc @@ -1,7 +1,4 @@ #include "glad.h" -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> #include "intrinsics.h" #include "memory_arena.h" @@ -20,26 +17,22 @@ static u32 btt_count_leaves( const BTT * btt ) { } static void gpubtt_build( - glm::vec3 * verts, u32 * i, const OffsetHeightmap * ohm, const BTT * btt, - glm::ivec2 iv0, glm::ivec2 iv1, glm::ivec2 iv2 + v3 * verts, u32 * i, const OffsetHeightmap * ohm, const BTT * btt, + v2s32 p0, v2s32 p1, v2s32 p2 ) { - const glm::vec3 offset( ohm->x_offset, ohm->y_offset, 0.0f ); - - const glm::vec3 v0( ohm->hm.point( iv0.x, iv0.y ) + offset ); - const glm::vec3 v1( ohm->hm.point( iv1.x, iv1.y ) + offset ); - const glm::vec3 v2( ohm->hm.point( iv2.x, iv2.y ) + offset ); + v3 offset( ohm->x_offset, ohm->y_offset, 0.0f ); if( btt->left != NULL ) { - const glm::ivec2 mid = ( iv0 + iv2 ) / 2; + v2s32 mid = ( p0 + p2 ) / 2; - gpubtt_build( verts, i, ohm, btt->left, iv1, mid, iv0 ); + gpubtt_build( verts, i, ohm, btt->left, p1, mid, p0 ); assert( btt->right != NULL ); - gpubtt_build( verts, i, ohm, btt->right, iv2, mid, iv1 ); + gpubtt_build( verts, i, ohm, btt->right, p2, mid, p1 ); } else { - verts[ *i + 0 ] = v0; - verts[ *i + 1 ] = v1; - verts[ *i + 2 ] = v2; + verts[ *i + 0 ] = ohm->hm.point( p0.x, p0.y ) + offset; + verts[ *i + 1 ] = ohm->hm.point( p1.x, p1.y ) + offset; + verts[ *i + 2 ] = ohm->hm.point( p2.x, p2.y ) + offset; *i += 3; } } @@ -54,14 +47,14 @@ void gpubtt_init( + btt_count_leaves( btts.right_root ); 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 ); + v3 * verts = memarena_push_many( arena, v3, num_leaves * 3 ); u32 num_vertices = 0; // bottom left, bottom right, top left, top right - const glm::ivec2 bl( 0, 0 ); - const glm::ivec2 br( ohm->hm.width - 1, 0 ); - const glm::ivec2 tl( 0, ohm->hm.height - 1 ); - const glm::ivec2 tr( ohm->hm.width - 1, ohm->hm.height - 1 ); + const v2s32 bl( 0, 0 ); + const v2s32 br( ohm->hm.width - 1, 0 ); + const v2s32 tl( 0, ohm->hm.height - 1 ); + const v2s32 tr( ohm->hm.width - 1, ohm->hm.height - 1 ); gpubtt_build( verts, &num_vertices, ohm, btts.left_root, bl, tl, tr ); gpubtt_build( verts, &num_vertices, ohm, btts.right_root, tr, br, bl ); diff --git a/heightmap.cc b/heightmap.cc @@ -1,8 +1,7 @@ #include <stdio.h> -#include <glm/glm.hpp> - #include "intrinsics.h" +#include "linear_algebra.h" #include "heightmap.h" #include "log.h" #include "memory_arena.h" @@ -13,19 +12,13 @@ static float lerp( float a, float b, float t ) { return a * ( 1 - t ) + b * t; } -static float bilinear_interpolation( - const glm::vec3 & v1, - const glm::vec3 & v2, - const glm::vec3 & v3, - const glm::vec3 & v4, - const glm::vec2 & p -) { - const float tx = ( p.x - v1.x ) / ( v2.x - v1.x ); +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( v1.z, v2.z, tx ); - const float mx2 = lerp( v3.z, v4.z, tx ); + const float mx1 = lerp( p1.z, p2.z, tx ); + const float mx2 = lerp( p3.z, p4.z, tx ); - const float ty = ( p.y - v1.y ) / ( v3.y - v1.y ); + const float ty = ( p.y - p1.y ) / ( p3.y - p1.y ); return lerp( mx1, mx2, ty ); } @@ -42,8 +35,8 @@ void heightmap_destroy( Heightmap * hm ) { } } -glm::vec3 Heightmap::point( u32 x, u32 y ) const { - return glm::vec3( x, y, pixels[ y * width + x ] ); +v3 Heightmap::point( u32 x, u32 y ) const { + return v3( x, y, pixels[ y * width + x ] ); } u8 heightmap_height( const Heightmap * hm, u32 x, u32 y ) { @@ -56,12 +49,12 @@ float Heightmap::bilerp_height( float x, float y ) const { const float ix = floorf( x ); const float iy = floorf( y ); - return bilinear_interpolation( + return bilerp( point( ix, iy ), point( ix + 1, iy ), point( ix, iy + 1 ), point( ix + 1, iy + 1 ), - glm::vec2( x, y ) + v2( x, y ) ); } @@ -177,17 +170,6 @@ bool ray_vs_triangle( v3 ray_origin, v3 ray_dir, v3 p0, v3 p1, v3 p2, float * t return point_in_triangle( xpoint, p0, p1, p2 ); } -u32 mean( u32 a, u32 b ) { - return a / 2 + b / 2 + ( a & b & 1 ); -} - -v3u32 mean( v3u32 mins, v3u32 maxs ) { - ASSERT( mins.x <= maxs.x ); - ASSERT( mins.y <= maxs.y ); - ASSERT( mins.z <= maxs.z ); - return mins + ( maxs - mins ) / 2; -} - AABBu32 AABBu32::quadrant( int q ) const { switch( checked_cast< Quadrant >( q ) ) { case QUADRANT_SW: { @@ -289,10 +271,10 @@ static bool ray_vs_quadtree_node( const HeightmapQuadTree * qt, size_t node_idx, u32 x = aabb.mins.x; u32 y = aabb.mins.y; - v3 p0 = GLMV3( qt->hm->point( x, y ) ); - v3 p1 = GLMV3( qt->hm->point( x + 1, y ) ); - v3 p2 = GLMV3( qt->hm->point( x, y + 1 ) ); - v3 p3 = GLMV3( qt->hm->point( x + 1, y + 1 ) ); + 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 ); // bottom right triangle float t0; diff --git a/heightmap.h b/heightmap.h @@ -1,8 +1,6 @@ #ifndef _HEIGHTMAP_H_ #define _HEIGHTMAP_H_ -#include <glm/glm.hpp> - #include "intrinsics.h" #include "memory_arena.h" #include "linear_algebra.h" @@ -12,7 +10,7 @@ public: u8 * pixels = NULL; u32 width, height; - glm::vec3 point( u32 x, u32 y ) const; + v3 point( u32 x, u32 y ) const; float bilerp_height( float x, float y ) const; }; diff --git a/hm.cc b/hm.cc @@ -3,9 +3,6 @@ #include "glad.h" #include "glsl.h" -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> #include "game.h" #include "intrinsics.h" @@ -94,16 +91,16 @@ static const char * font_frag_src = GLSL( } ); -static glm::vec3 angles_to_vector( const glm::vec3 & angles ) { - return glm::vec3( +static v3 angles_to_vector( v3 angles ) { + return v3( -sin( angles.y ) * sin( angles.x ), -cos( angles.y ) * sin( angles.x ), -cos( angles.x ) ); } -static glm::vec3 angles_to_vector_xy( const glm::vec3 & angles ) { - return glm::vec3( sin( angles.y ), cos( angles.y ), 0 ); +static v3 angles_to_vector_xy( v3 angles ) { + return v3( sin( angles.y ), cos( angles.y ), 0 ); } static WORK_QUEUE_CALLBACK( testwq ) { @@ -120,8 +117,8 @@ extern "C" GAME_INIT( game_init ) { } workqueue_exhaust( &game->background_tasks ); - game->pos = glm::vec3( 1500, 1500, 250 ); - game->angles = glm::radians( glm::vec3( -90, 45, 0 ) ); + game->pos = v3( 15000, 3000, 50 ); + game->angles = radians( v3( -90, 45, 0 ) ); terrain_init( &game->tm, "terrains/gta.png.parts", &mem->persistent_arena, &game->background_tasks ); terrain_teleport( &game->tm, game->pos ); @@ -243,8 +240,8 @@ static void draw_string( const GameState * game, glBindTexture( GL_TEXTURE_2D, 0 ); } -static m4 camera_to_view( glm::vec3 position, glm::vec3 angles ) { - return m4_translation( -GLMV3( position ) ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ); +static m4 camera_to_view( v3 position, v3 angles ) { + return m4_translation( -position ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ); } extern "C" GAME_FRAME( game_frame ) { @@ -267,7 +264,7 @@ extern "C" GAME_FRAME( game_frame ) { // const float speed = 6.0f; const float speed = 100.0f; game->pos += angles_to_vector_xy( game->angles ) * speed * dt * ( float ) fb; - const glm::vec3 sideways = glm::vec3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); + const v3 sideways = v3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); game->pos += sideways * speed * dt * ( float ) lr; // game->pos.z = terrain_height( &game->tm, game->pos ) + 2; game->pos.z += dz * 50.0f * dt; diff --git a/linear_algebra.h b/linear_algebra.h @@ -7,6 +7,7 @@ #include <math.h> +#include "intrinsics.h" #include "platform_inline.h" /* @@ -34,6 +35,17 @@ struct v2 { v2 yy() const { return v2( y, y ); } }; +struct v2s32 { + s32 x, y; + + v2s32() { } + + explicit v2s32( s32 a, s32 b ) { + x = a; + y = b; + } +}; + struct m2 { v2 row0, row1; @@ -90,8 +102,6 @@ struct v3u32 { } }; -#define GLMV3( v ) v3( ( v ).x, ( v ).y, ( v ).z ) - struct m3 { v3 row0, row1, row2; @@ -258,6 +268,26 @@ forceinline v2 normalize( v2 v ) { } /* + * v2s32 + */ + +forceinline v2s32 operator+( v2s32 lhs, v2s32 rhs ) { + return v2s32( lhs.x + rhs.x, lhs.y + rhs.y ); +} + +forceinline v2s32 operator-( v2s32 lhs, v2s32 rhs ) { + return v2s32( lhs.x - rhs.x, lhs.y - rhs.y ); +} + +forceinline v2s32 operator/( v2s32 v, u32 d ) { + return v2s32( v.x / d, v.y / d ); +} + +forceinline s32 dot( v2s32 lhs, v2s32 rhs ) { + return lhs.x * rhs.x + lhs.y * rhs.y; +} + +/* * m2 */ @@ -330,15 +360,15 @@ forceinline bool operator==( v3 lhs, v3 rhs ) { return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z; } -forceinline float dot( v3 u, v3 v ) { - return u.x * v.x + u.y * v.y + u.z * v.z; +forceinline float dot( v3 lhs, v3 rhs ) { + return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; } -forceinline v3 cross( v3 u, v3 v ) { +forceinline v3 cross( v3 lhs, v3 rhs ) { return v3( - u.y * v.z - v.y * u.z, - v.x * u.z - u.x * v.z, - u.x * v.y - v.x * u.y + lhs.y * rhs.z - rhs.y * lhs.z, + rhs.x * lhs.z - lhs.x * rhs.z, + lhs.x * rhs.y - rhs.x * lhs.y ); } @@ -354,18 +384,22 @@ forceinline v3 normalize( v3 v ) { * v3u32 */ -forceinline v3u32 operator+( v3u32 u, v3u32 v ) { - return v3u32( u.x + v.x, u.y + v.y, u.z + v.z ); +forceinline v3u32 operator+( v3u32 lhs, v3u32 rhs ) { + return v3u32( lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z ); } -forceinline v3u32 operator-( v3u32 u, v3u32 v ) { - return v3u32( u.x - v.x, u.y - v.y, u.z - v.z ); +forceinline v3u32 operator-( v3u32 lhs, v3u32 rhs ) { + return v3u32( lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z ); } forceinline v3u32 operator/( v3u32 v, u32 d ) { return v3u32( v.x / d, v.y / d, v.z / d ); } +forceinline u32 dot( v3u32 lhs, v3u32 rhs ) { + return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; +} + /* * m3 */ @@ -677,4 +711,29 @@ forceinline m4 operator-( const m4 & m ) { * quat */ +/* + * misc. TODO: put this stuff somewhere else + */ + +template< typename T > +forceinline T radians( const T & x ) { + return x * M_PI / 180.0; +} + +template< typename T > +forceinline T degrees( const T & x ) { + return x * 180.0 / M_PI; +} + +forceinline u32 mean( u32 a, u32 b ) { + return a / 2 + b / 2 + ( a & b & 1 ); +} + +forceinline v3u32 mean( v3u32 mins, v3u32 maxs ) { + ASSERT( mins.x <= maxs.x ); + ASSERT( mins.y <= maxs.y ); + ASSERT( mins.z <= maxs.z ); + return mins + ( maxs - mins ) / 2; +} + #endif // _LINEAR_ALGEBRA_H_ diff --git a/mod_btt.cc b/mod_btt.cc @@ -1,7 +1,4 @@ #include "glad.h" -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> #include "game.h" #include "intrinsics.h" @@ -97,12 +94,12 @@ static const char * frag_outline_src = GLSL( } ); -static glm::vec3 angles_to_vector_xy( const glm::vec3 & angles ) { - return glm::vec3( sin( angles.y ), cos( angles.y ), 0 ); +static v3 angles_to_vector_xy( v3 angles ) { + return v3( sin( angles.y ), cos( angles.y ), 0 ); } -glm::vec3 angles_to_vector( const glm::vec3 & angles ) { - return glm::vec3( +v3 angles_to_vector( v3 angles ) { + return v3( -sin( angles.y ) * sin( angles.x ), -cos( angles.y ) * sin( angles.x ), -cos( angles.x ) @@ -140,8 +137,8 @@ static HeightmapQuadTree qt; static UB ub_fs, ub_vs; extern "C" GAME_INIT( game_init ) { - game->pos = glm::vec3( 100, 200, 100 ); - game->angles = glm::radians( glm::vec3( -90, 45, 0 ) ); + game->pos = v3( 100, 200, 100 ); + game->angles = radians( v3( -90, 45, 0 ) ); game->test_sun = 0.3f; @@ -199,10 +196,10 @@ extern "C" GAME_INIT( game_init ) { skybox_init( &game->skybox ); } -static m4 camera_to_vp( glm::vec3 position, glm::vec3 angles ) { +static m4 camera_to_vp( v3 position, v3 angles ) { const m4 P = m4_perspective( 120.0f, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, 10000.0f ); - return m4_translation( -GLMV3( position ) ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ) * P; + return m4_translation( -position ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ) * P; } struct FSData { @@ -248,7 +245,7 @@ extern "C" GAME_FRAME( game_frame ) { // const float speed = 6.0f; const float speed = 100.0f; game->pos += angles_to_vector_xy( game->angles ) * speed * dt * ( float ) fb; - const glm::vec3 sideways = glm::vec3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); + const v3 sideways = v3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); game->pos += sideways * speed * dt * ( float ) lr; game->pos.z += dz * 50.0f * dt; @@ -278,10 +275,10 @@ extern "C" GAME_FRAME( game_frame ) { glUniformMatrix4fv( game->test_outline_un_vp, 1, GL_FALSE, ( float * ) &VP ); float t; - if( ray_vs_quadtree( &qt, GLMV3( game->pos ), GLMV3( angles_to_vector( game->angles ) ), &t ) ) { - glm::vec3 impact = game->pos + angles_to_vector( game->angles ) * t; + if( ray_vs_quadtree( &qt, game->pos, angles_to_vector( game->angles ), &t ) ) { + v3 impact = game->pos + angles_to_vector( game->angles ) * t; printf( "impact (%f) %f %f %f\n", t, impact.x, impact.y, impact.z ); - immediate_sphere( &imm, GLMV3( impact ), 4, v4( 1, 0, 0, 1 ) ); + immediate_sphere( &imm, impact, 4, v4( 1, 0, 0, 1 ) ); } else printf( "nope\n" ); immediate_render( &imm, game->test_outline_at_position, game->test_outline_at_colour ); diff --git a/shadow_map.cc b/shadow_map.cc @@ -1,8 +1,5 @@ #include "glad.h" #include "glsl.h" -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> #include "game.h" #include "intrinsics.h" @@ -92,8 +89,8 @@ static void draw_scene( GLint at_position, GLint at_colour ) { immediate_render( &imm, at_position, at_colour ); } -static glm::vec3 angles_to_vector( const glm::vec3 & angles ) { - return glm::vec3( +static v3 angles_to_vector( v3 angles ) { + return v3( -sin( angles.y ) * sin( angles.x ), -cos( angles.y ) * sin( angles.x ), -cos( angles.x ) @@ -102,8 +99,8 @@ static glm::vec3 angles_to_vector( const glm::vec3 & angles ) { static void update_camera( const GameInput * input, float dt, - glm::vec3 position, glm::vec3 angles, - glm::vec3 * out_position, glm::vec3 * out_angles + v3 position, v3 angles, + v3 * out_position, v3 * out_angles ) { float speed = 6.0f; float angular_speed = 2.0f; @@ -112,8 +109,8 @@ static void update_camera( float dy = ( float ) input->keys[ 'w' ] - ( float ) input->keys[ 's' ]; float dz = ( float ) input->keys[ KEY_SPACE ] - ( float ) input->keys[ KEY_LEFTSHIFT ]; - glm::vec3 forward = angles_to_vector( angles ); - glm::vec3 sideways = glm::vec3( -cosf( angles.y ), sinf( angles.y ), 0 ); + v3 forward = angles_to_vector( angles ); + v3 sideways = v3( -cosf( angles.y ), sinf( angles.y ), 0 ); *out_position = position + forward * dt * dy * speed @@ -123,13 +120,13 @@ static void update_camera( float pitch = ( float ) input->keys[ KEY_UPARROW ] - ( float ) input->keys[ KEY_DOWNARROW ]; float yaw = ( float ) input->keys[ KEY_RIGHTARROW ] - ( float ) input->keys[ KEY_LEFTARROW ]; - *out_angles = angles + glm::vec3( dt * pitch * angular_speed, dt * yaw * angular_speed, 0 ); + *out_angles = angles + v3( dt * pitch * angular_speed, dt * yaw * angular_speed, 0 ); } -static m4 camera_to_vp( glm::vec3 position, glm::vec3 angles ) { +static m4 camera_to_vp( v3 position, v3 angles ) { const m4 P = m4_perspective( 120.0f, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, 25.0f ); - return m4_translation( -GLMV3( position ) ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ) * P; + return m4_translation( -position ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ) * P; } const u32 SHADOW_WIDTH = 1024; @@ -163,8 +160,8 @@ extern "C" GAME_INIT( game_init ) { glBindFramebuffer( GL_FRAMEBUFFER, 0 ); - game->pos = glm::vec3( -10, -10, 5 ); - game->angles = glm::radians( ( glm::vec3( -90, 45, 0 ) ) ); + game->pos = v3( -10, -10, 5 ); + game->angles = radians( ( v3( -90, 45, 0 ) ) ); float verts[] = { -1, -1, -1, 1, 1, -1, 1, 1 }; glGenVertexArrays( 1, &screen_vao ); diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -1,7 +1,5 @@ #include <stdio.h> -#include <glm/glm.hpp> - #include "intrinsics.h" #include "log.h" #include "heightmap.h" @@ -273,7 +271,7 @@ void terrain_init( tm->first_teleport = true; } -void terrain_teleport( TerrainManager * tm, glm::vec3 position ) { +void terrain_teleport( TerrainManager * tm, v3 position ) { assert( tm->first_teleport ); tm->first_teleport = false; @@ -311,7 +309,7 @@ static void terrain_update_lods( TerrainManager * tm ) { } } -void terrain_update( TerrainManager * tm, glm::vec3 position ) { +void terrain_update( TerrainManager * tm, v3 position ) { s32 player_tile_x = position.x / TILE_SIZE; s32 player_tile_y = position.y / TILE_SIZE; @@ -464,7 +462,7 @@ void terrain_render( TerrainManager * tm, m4 V, m4 VP, float sun, double current } } -float terrain_height( const TerrainManager * tm, glm::vec3 position ) { +float terrain_height( const TerrainManager * tm, v3 position ) { assert( position.x >= 0 ); assert( position.y >= 0 ); assert( position.x < tm->width ); diff --git a/terrain_manager.h b/terrain_manager.h @@ -1,8 +1,6 @@ #ifndef _TERRAIN_MANAGER_H_ #define _TERRAIN_MANAGER_H_ -#include <glm/glm.hpp> - #include "intrinsics.h" #include "array.h" #include "memory_arena.h" @@ -79,9 +77,9 @@ struct TerrainManager { }; void terrain_init( TerrainManager * tm, const char * tiles_dir, MemoryArena * arena, WorkQueue * background_tasks ); -void terrain_teleport( TerrainManager * tm, glm::vec3 position ); -void terrain_update( TerrainManager * tm, glm::vec3 position ); +void terrain_teleport( TerrainManager * tm, v3 position ); +void terrain_update( TerrainManager * tm, v3 position ); void terrain_render( TerrainManager * tm, m4 V, m4 VP, float sun, double current_time ); -float terrain_height( const TerrainManager * tm, glm::vec3 position ); +float terrain_height( const TerrainManager * tm, v3 position ); #endif // _TERRAIN_MANAGER_H_