Skip to content

Commit

Permalink
Quickstart system + few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Nov 13, 2023
1 parent 9265554 commit 6910648
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 20 deletions.
86 changes: 72 additions & 14 deletions src/Geo/AABB.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,78 @@ public function getCenter() : Vec3
return ($this->min + $this->max) * 0.5;

Check failure on line 63 in src/Geo/AABB.php

View workflow job for this annotation

GitHub Actions / ubuntu (ubuntu-latest, 8.2, 9.6)

Method VISU\Geo\AABB::getCenter() should return GL\Math\Vec3 but returns float.
}

/**
* Extends the current AABB to include the given AABB
*/
public function extend(AABB $aabb) : void
{
$this->min->x = min($this->min->x, $aabb->min->x);
$this->min->y = min($this->min->y, $aabb->min->y);
$this->min->z = min($this->min->z, $aabb->min->z);

$this->max->x = max($this->max->x, $aabb->max->x);
$this->max->y = max($this->max->y, $aabb->max->y);
$this->max->z = max($this->max->z, $aabb->max->z);
}

/**
* Returns true if the given AABB intersects with the current AABB
*/
public function intersects(AABB $aabb) : bool
{
return !(
$this->min->x > $aabb->max->x ||
$this->max->x < $aabb->min->x ||
$this->min->y > $aabb->max->y ||
$this->max->y < $aabb->min->y ||
$this->min->z > $aabb->max->z ||
$this->max->z < $aabb->min->z
);
}

/**
* Returns true if the given point is inside the current AABB
*/
public function contains(Vec3 $point) : bool
{
return !(
$point->x < $this->min->x ||
$point->x > $this->max->x ||
$point->y < $this->min->y ||
$point->y > $this->max->y ||
$point->z < $this->min->z ||
$point->z > $this->max->z
);
}

/**
* Returns a Vec3 representing a translation that could be applied to the current AABB to make it not intersect with the given AABB
*/
public function getTranslationToAvoidIntersection(AABB $aabb) : Vec3
{
$translation = new Vec3(0, 0, 0);

if ($this->max->x > $aabb->min->x && $this->min->x < $aabb->min->x) {
$translation->x = $aabb->min->x - $this->max->x;
} else if ($this->min->x < $aabb->max->x && $this->max->x > $aabb->max->x) {
$translation->x = $aabb->max->x - $this->min->x;
}

if ($this->max->y > $aabb->min->y && $this->min->y < $aabb->min->y) {
$translation->y = $aabb->min->y - $this->max->y;
} else if ($this->min->y < $aabb->max->y && $this->max->y > $aabb->max->y) {
$translation->y = $aabb->max->y - $this->min->y;
}

if ($this->max->z > $aabb->min->z && $this->min->z < $aabb->min->z) {
$translation->z = $aabb->min->z - $this->max->z;
} else if ($this->min->z < $aabb->max->z && $this->max->z > $aabb->max->z) {
$translation->z = $aabb->max->z - $this->min->z;
}

return $translation;
}

/**
* Returns the intersection point of the current AABB and the given Ray
*
Expand All @@ -80,20 +152,6 @@ public function intersectRay(Ray $ray) : ?Vec3
return $ray->origin + $ray->direction * $t;
}

/**
* Extends the current AABB to include the given AABB
*/
public function extend(AABB $aabb) : void
{
$this->min->x = min($this->min->x, $aabb->min->x);
$this->min->y = min($this->min->y, $aabb->min->y);
$this->min->z = min($this->min->z, $aabb->min->z);

$this->max->x = max($this->max->x, $aabb->max->x);
$this->max->y = max($this->max->y, $aabb->max->y);
$this->max->z = max($this->max->z, $aabb->max->z);
}

/**
* Retuns the intersection distance of this AABB and Ray
*
Expand Down
8 changes: 4 additions & 4 deletions src/Graphics/BasicVertexArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public function bind() : void
/**
* Draws all vertices
*/
public function drawAll() : void
public function drawAll(int $renderMode = GL_TRIANGLES) : void
{
$this->bind();
glDrawArrays(GL_TRIANGLES, 0, $this->vertexCount);
glDrawArrays($renderMode, 0, $this->vertexCount);
}

/**
Expand All @@ -123,9 +123,9 @@ public function drawAll() : void
* @param int $offset The offset
* @param int $count The count
*/
public function draw(int $offset, int $count) : void
public function draw(int $offset, int $count, int $renderMode = GL_TRIANGLES) : void
{
$this->bind();
glDrawArrays(GL_TRIANGLES, $offset, $count);
glDrawArrays($renderMode, $offset, $count);
}
}
4 changes: 2 additions & 2 deletions src/Graphics/Rendering/Pass/BackbufferData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace VISU\Graphics\Rendering\Pass;

use VISU\Graphics\Rendering\RenderResource;
use VISU\Graphics\Rendering\Resource\RenderTargetResource;

class BackbufferData
{
public RenderResource $target;
public RenderTargetResource $target;
}
69 changes: 69 additions & 0 deletions src/Quickstart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace VISU;

use ClanCats\Container\Container;
use VISU\Exception\VISUException;
use VISU\Quickstart\QuickstartApp;
use VISU\Quickstart\QuickstartOptions;
use VISU\Runtime\GameLoop;

/**
* Quickstart prepares a basic and simple VISU runtime environemnt to give you,
* you guessed it, a quick start. It is not intended to be used in large and complex apps
* but rather for quick prototyping, testing and learning.
*
* VISU Quickstart will cut a lot of corners and make a lot of assumptions for you.
*
* Additionally quickstart can expose global aliases for the runtime alla Laravel's Facades.
* This is purely optional and again just for quick prototyping, testing and learning.
*/
class Quickstart
{
/**
* Instance of the app we are building and running.
*/
private QuickstartApp $app;

/**
* Create a new Quickstart instance.
*
* @param callable(QuickstartOptions): void $appBuilder
*/
public function __construct(callable $appBuilder)
{
// check that the glfw extension is loaded
if (!extension_loaded('glfw')) {
throw new VISUException("The glfw extension is not loaded, please check your installation.");
}

if (!glfwInit()) {
throw new VISUException("Could not initalize glfw, please report this issue to php-glfw.");
}

// create an app container to store all our services
$container = new Container();

// create the quickstart app options and let the user configure it
$options = new QuickstartOptions();
$appBuilder($options);

// construct the quickstart app
$this->app = new QuickstartApp($container, $options);

// register a game loop in the container
$container->set('loop', new GameLoop($this->app, $options->gameLoopTickRate, $options->gameLoopMaxUpdatesPerFrame));
}

/**
* Run the quickstart app.
*/
public function run() : void
{
// run ready callback
$this->app->options->ready?->call($this->app, $this->app);

// start the game loop
$this->app->container->getTyped(GameLoop::class, 'loop')->start();
}
}
Loading

0 comments on commit 6910648

Please sign in to comment.