medfall

A super great game engine
Log | Files | Refs

commit 5f8da39e52c2123926e38a2df10f3fb5814b0701
parent 7f660d353b08b724b486feb2d5da6e676b690d14
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri, 10 Nov 2017 21:47:26 +0200

Don't descend into quadtree nodes that are too far away in segment_vs_quadtree

Diffstat:
heightmap.cc | 19+++++++++----------
heightmap.h | 4+++-
2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/heightmap.cc b/heightmap.cc @@ -1,5 +1,4 @@ #include <stdio.h> -#include <float.h> #include "intrinsics.h" #include "linear_algebra.h" @@ -175,7 +174,7 @@ static void sort4( int * indices, float * elems ) { static bool ray_vs_quadtree_node( const QuadTree & qt, size_t node_idx, MinMaxu32 aabb, - const Ray3 & ray, float * t, v3 * xnormal + const Ray3 & ray, float * t, v3 * xnormal, float max_dist ) { if( aabb.maxs.x - aabb.mins.x == 2 || aabb.maxs.y - aabb.mins.y == 2 ) { bool hit = false; @@ -233,10 +232,10 @@ static bool ray_vs_quadtree_node( for( int i = 0; i < 4; i++ ) { int idx = sorted[ i ]; - if( ts[ idx ] == FLT_MAX ) + if( ts[ idx ] >= max_dist ) break; - if( ray_vs_quadtree_node( qt, child_idx( node_idx, idx ), aabbs[ idx ], ray, t, xnormal ) ) { + if( ray_vs_quadtree_node( qt, child_idx( node_idx, idx ), aabbs[ idx ], ray, t, xnormal, max_dist ) ) { return true; } } @@ -244,12 +243,12 @@ static bool ray_vs_quadtree_node( return false; } -bool ray_vs_quadtree( const QuadTree & qt, const Ray3 & ray, float * t, v3 * xnormal ) { +bool ray_vs_quadtree( const QuadTree & qt, const Ray3 & ray, float * t, v3 * xnormal, float max_dist ) { 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 ); + return ray_vs_quadtree_node( qt, 0, aabb, ray, t, xnormal, max_dist ); } bool segment_vs_quadtree( const QuadTree & qt, const v3 & start, const v3 & end, float * t, v3 * xnormal ) { @@ -264,15 +263,15 @@ bool segment_vs_quadtree( const QuadTree & qt, const v3 & start, const v3 & end, return false; float nt; + float l = length( end - start ); Ray3 ray( start, normalize( end - start ) ); - bool ok = ray_vs_quadtree( qt, ray, &nt, xnormal ); - if( !ok ) + bool ok = ray_vs_quadtree( qt, ray, &nt, xnormal, l ); + if( !ok || nt >= l ) return false; - float l = length( end - start ); if( t != NULL ) *t = nt / l; - return nt <= l; + return true; } void bc5_to_heightmap( MemoryArena * arena, array2d< u16 > hm, u8 * bc5 ) { diff --git a/heightmap.h b/heightmap.h @@ -1,5 +1,7 @@ #pragma once +#include <float.h> + #include "intrinsics.h" #include "memory_arena.h" #include "array.h" @@ -18,7 +20,7 @@ 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 QuadTree & qt, const Ray3 & ray, float * t, v3 * xnormal ); +bool ray_vs_quadtree( const QuadTree & qt, const Ray3 & ray, float * t, v3 * xnormal, float max_dist = FLT_MAX ); bool segment_vs_quadtree( const QuadTree & qt, const v3 & start, const v3 & end, float * t, v3 * xnormal ); void bc5_to_heightmap( MemoryArena * arena, array2d< u16 > hm, u8 * bc5 );