-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from phpgl/feature/frustum
Frustum system
- Loading branch information
Showing
8 changed files
with
305 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace VISU\Geo; | ||
|
||
use GL\Math\Mat4; | ||
use GL\Math\Vec3; | ||
|
||
class Frustum | ||
{ | ||
public static function fromMat4(Mat4 $mat) : self | ||
{ | ||
// Gribb/Hartmann method | ||
return new self( | ||
left: new Plane( | ||
new Vec3( | ||
$mat[3] + $mat[0], // a | ||
Check failure on line 16 in src/Geo/Frustum.php GitHub Actions / ubuntu (ubuntu-latest, 8.2, 9.6)
|
||
$mat[7] + $mat[4], // b | ||
Check failure on line 17 in src/Geo/Frustum.php GitHub Actions / ubuntu (ubuntu-latest, 8.2, 9.6)
|
||
$mat[11] + $mat[8] // c | ||
Check failure on line 18 in src/Geo/Frustum.php GitHub Actions / ubuntu (ubuntu-latest, 8.2, 9.6)
|
||
), | ||
$mat[15] + $mat[12] // d | ||
Check failure on line 20 in src/Geo/Frustum.php GitHub Actions / ubuntu (ubuntu-latest, 8.2, 9.6)
|
||
), | ||
right: new Plane( | ||
new Vec3( | ||
$mat[3] - $mat[0], // a | ||
$mat[7] - $mat[4], // b | ||
$mat[11] - $mat[8] // c | ||
), | ||
$mat[15] - $mat[12] // d | ||
), | ||
top: new Plane( | ||
new Vec3( | ||
$mat[3] - $mat[1], // a | ||
$mat[7] - $mat[5], // b | ||
$mat[11] - $mat[9] // c | ||
), | ||
$mat[15] - $mat[13] // d | ||
), | ||
bottom: new Plane( | ||
new Vec3( | ||
$mat[3] + $mat[1], // a | ||
$mat[7] + $mat[5], // b | ||
$mat[11] + $mat[9] // c | ||
), | ||
$mat[15] + $mat[13] // d | ||
), | ||
near: new Plane( | ||
new Vec3( | ||
$mat[3] + $mat[2], // a | ||
$mat[7] + $mat[6], // b | ||
$mat[11] + $mat[10] // c | ||
), | ||
$mat[15] + $mat[14] // d | ||
), | ||
far: new Plane( | ||
new Vec3( | ||
$mat[3] - $mat[2], // a | ||
$mat[7] - $mat[6], // b | ||
$mat[11] - $mat[10] // c | ||
), | ||
$mat[15] - $mat[14] // d | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Holds an additional reference to the planes that can be accessed by index | ||
*/ | ||
private array $planes = []; | ||
|
||
/** | ||
* Frustum is represented by 6 planes | ||
*/ | ||
public function __construct( | ||
public Plane $left, | ||
public Plane $right, | ||
public Plane $top, | ||
public Plane $bottom, | ||
public Plane $near, | ||
public Plane $far | ||
) | ||
{ | ||
foreach ([$left, $right, $top, $bottom, $near, $far] as $plane) { | ||
$this->planes[] = $plane; | ||
} | ||
} | ||
|
||
/** | ||
* Returns boolean if a given sphere is visible in the frustum | ||
*/ | ||
public function isSphereInView(Vec3 $center, float $radius): bool | ||
{ | ||
foreach ($this->planes as $plane) { | ||
$distance = Vec3::dot($plane->normal, $center) + $plane->distance; | ||
if ($distance < -$radius) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace VISU\Geo; | ||
|
||
use GL\Math\Vec3; | ||
|
||
class Plane | ||
{ | ||
public function __construct( | ||
public Vec3 $normal, | ||
public float $distance | ||
) | ||
{ | ||
$mag = $this->normal->length(); | ||
$this->normal = $this->normal / $mag; | ||
$this->distance /= $mag; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.