medfall

A super great game engine
Log | Files | Refs

commit 6c879a4bd392fc8e432ce3941f9876ec1399e22f
parent 5147ae8030ffdf21b728afcfdd7d7b2ab3033b0a
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Jul  1 21:10:53 +0300

Try out a Ray3 class

Diffstat:
heightmap.cc | 14+++++++-------
heightmap.h | 2+-
linear_algebra.h | 12++++++++++++
terrain_manager.cc | 6+++---
4 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/heightmap.cc b/heightmap.cc @@ -264,7 +264,7 @@ static void sort4( int * indices, float * elems ) { static bool ray_vs_quadtree_node( const HeightmapQuadTree * qt, size_t node_idx, AABBu32 aabb, - v3 ray_origin, v3 ray_dir, v3 inv_dir, float * t, v3 * xnormal + const Ray3 & ray, float * t, v3 * xnormal ) { if( aabb.maxs.x - aabb.mins.x == 1 || aabb.maxs.y - aabb.mins.y == 1 ) { bool hit = false; @@ -280,7 +280,7 @@ static bool ray_vs_quadtree_node( // bottom right triangle float t0; v3 normal0 = triangle_normal( p0, p1, p3 ); - bool hit0 = ray_vs_triangle( ray_origin, ray_dir, p0, p1, p3, &t0 ); + bool hit0 = ray_vs_triangle( ray.origin, ray.dir, p0, p1, p3, &t0 ); if( hit0 ) { if( !hit || t0 < *t ) { *t = t0; @@ -292,7 +292,7 @@ static bool ray_vs_quadtree_node( // top left triangle float t1; v3 normal1 = triangle_normal( p0, p3, p2 ); - bool hit1 = ray_vs_triangle( ray_origin, ray_dir, p0, p3, p2, &t1 ); + bool hit1 = ray_vs_triangle( ray.origin, ray.dir, p0, p3, p2, &t1 ); if( hit1 ) { if( !hit || t1 < *t ) { *t = t1; @@ -310,7 +310,7 @@ static bool ray_vs_quadtree_node( HeightmapQuadTreeNode 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( AABB( aabbs[ i ] ), ray_origin, inv_dir, &ts[ i ] ); + ray_vs_aabb( AABB( aabbs[ i ] ), ray.origin, ray.inv_dir, &ts[ i ] ); } int sorted[ 4 ]; @@ -321,7 +321,7 @@ static bool ray_vs_quadtree_node( if( ts[ idx ] == FLT_MAX ) break; - if( ray_vs_quadtree_node( qt, child_idx( node_idx, idx ), aabbs[ idx ], ray_origin, ray_dir, inv_dir, t, xnormal ) ) { + if( ray_vs_quadtree_node( qt, child_idx( node_idx, idx ), aabbs[ idx ], ray, t, xnormal ) ) { return true; } } @@ -329,10 +329,10 @@ static bool ray_vs_quadtree_node( return false; } -bool ray_vs_quadtree( const HeightmapQuadTree * qt, v3 ray_origin, v3 ray_dir, v3 inv_dir, float * t, v3 * xnormal ) { +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 ); AABBu32 aabb( mins, maxs ); - return ray_vs_quadtree_node( qt, 0, aabb, ray_origin, ray_dir, inv_dir, t, xnormal ); + return ray_vs_quadtree_node( qt, 0, aabb, ray, t, xnormal ); } diff --git a/heightmap.h b/heightmap.h @@ -61,4 +61,4 @@ HeightmapQuadTree heightmap_build_quadtree( const Heightmap * hm, array< Heightm // return lo0 <= hi1 && lo1 <= hi0; // } -bool ray_vs_quadtree( const HeightmapQuadTree * qt, v3 ray_origin, v3 ray_dir, v3 inv_dir, float * t, v3 * xnormal ); +bool ray_vs_quadtree( const HeightmapQuadTree * qt, const Ray3 & ray, float * t, v3 * xnormal ); diff --git a/linear_algebra.h b/linear_algebra.h @@ -157,6 +157,18 @@ struct m3 { v3 row2() const { return v3( col0.z, col1.z, col2.z ); } }; +struct Ray3 { + v3 origin; + v3 dir; + v3 inv_dir; + + explicit Ray3( v3 o, v3 d ) { + origin = o; + dir = d; + inv_dir = v3( 1.0f / d.x, 1.0f / d.y, 1.0f / d.z ); + } +}; + // AUTOVISITOR struct v4 { float x, y, z, w; diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -382,8 +382,7 @@ bool segment_vs_terrain( const TerrainManager * tm, v3 seg_origin, v3 seg_end, f } float segment_length = length( seg_end - seg_origin ); - v3 ray_dir = normalize( seg_end - seg_origin ); - v3 inv_dir = v3( 1.0f / ray_dir.x, 1.0f / ray_dir.y, 1.0f / ray_dir.z ); + Ray3 ray( seg_origin, normalize( seg_end - seg_origin ) ); bool hit = false; @@ -400,11 +399,12 @@ bool segment_vs_terrain( const TerrainManager * tm, v3 seg_origin, v3 seg_end, f } v3 local_seg_origin = seg_origin - v3( checked_cast< float >( tx * TILE_SIZE ), checked_cast< float >( ty * TILE_SIZE ), 0 ); + ray.origin = local_seg_origin; const HeightmapQuadTree * qt = &tm->decompressed_tiles[ tx ][ ty ].quadtree; // TODO: convert from tile t to global t float tile_t; - bool tile_hit = ray_vs_quadtree( qt, local_seg_origin, ray_dir, inv_dir, &tile_t, xnormal ); + bool tile_hit = ray_vs_quadtree( qt, ray, &tile_t, xnormal ); if( tile_hit && ( !hit || tile_t < *t ) && tile_t <= segment_length ) { hit = true;