Frustum math from raylib extras.

This commit is contained in:
jussi
2025-06-02 20:49:51 +03:00
parent e26bb8603c
commit 5b8af05e96
9 changed files with 462 additions and 2 deletions

212
src/frustum.c Normal file
View File

@@ -0,0 +1,212 @@
/**********************************************************************************************
*
* raylibExtras * Utilities and Shared Components for Raylib
*
* RLAssets * Simple Asset Managment System for Raylib
*
* LICENSE: MIT
*
* Copyright (c) 2020 Jeffery Myers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************/
#include "frustum.h"
#include "rlgl.h"
#include "raymath.h"
#include <math.h>
#include <stddef.h>
void NormalizePlane(Vector4* plane)
{
if (plane == NULL)
return;
float magnitude = sqrtf(plane->x * plane->x + plane->y * plane->y + plane->z * plane->z);
plane->x /= magnitude;
plane->y /= magnitude;
plane->z /= magnitude;
plane->w /= magnitude;
}
// void ExtractFrustum(Frustum* frustum)
void ExtractFrustum(Frustum* frustum, Matrix projection, Matrix modelview)
{
if (frustum == NULL)
return;
// Matrix projection = rlGetMatrixProjection();
// Matrix modelview = rlGetMatrixModelview();
Matrix planes = { 0 };
planes.m0 = modelview.m0 * projection.m0 + modelview.m1 * projection.m4 + modelview.m2 * projection.m8 + modelview.m3 * projection.m12;
planes.m1 = modelview.m0 * projection.m1 + modelview.m1 * projection.m5 + modelview.m2 * projection.m9 + modelview.m3 * projection.m13;
planes.m2 = modelview.m0 * projection.m2 + modelview.m1 * projection.m6 + modelview.m2 * projection.m10 + modelview.m3 * projection.m14;
planes.m3 = modelview.m0 * projection.m3 + modelview.m1 * projection.m7 + modelview.m2 * projection.m11 + modelview.m3 * projection.m15;
planes.m4 = modelview.m4 * projection.m0 + modelview.m5 * projection.m4 + modelview.m6 * projection.m8 + modelview.m7 * projection.m12;
planes.m5 = modelview.m4 * projection.m1 + modelview.m5 * projection.m5 + modelview.m6 * projection.m9 + modelview.m7 * projection.m13;
planes.m6 = modelview.m4 * projection.m2 + modelview.m5 * projection.m6 + modelview.m6 * projection.m10 + modelview.m7 * projection.m14;
planes.m7 = modelview.m4 * projection.m3 + modelview.m5 * projection.m7 + modelview.m6 * projection.m11 + modelview.m7 * projection.m15;
planes.m8 = modelview.m8 * projection.m0 + modelview.m9 * projection.m4 + modelview.m10 * projection.m8 + modelview.m11 * projection.m12;
planes.m9 = modelview.m8 * projection.m1 + modelview.m9 * projection.m5 + modelview.m10 * projection.m9 + modelview.m11 * projection.m13;
planes.m10 = modelview.m8 * projection.m2 + modelview.m9 * projection.m6 + modelview.m10 * projection.m10 + modelview.m11 * projection.m14;
planes.m11 = modelview.m8 * projection.m3 + modelview.m9 * projection.m7 + modelview.m10 * projection.m11 + modelview.m11 * projection.m15;
planes.m12 = modelview.m12 * projection.m0 + modelview.m13 * projection.m4 + modelview.m14 * projection.m8 + modelview.m15 * projection.m12;
planes.m13 = modelview.m12 * projection.m1 + modelview.m13 * projection.m5 + modelview.m14 * projection.m9 + modelview.m15 * projection.m13;
planes.m14 = modelview.m12 * projection.m2 + modelview.m13 * projection.m6 + modelview.m14 * projection.m10 + modelview.m15 * projection.m14;
planes.m15 = modelview.m12 * projection.m3 + modelview.m13 * projection.m7 + modelview.m14 * projection.m11 + modelview.m15 * projection.m15;
frustum->Planes[Right] = (Vector4){ planes.m3 - planes.m0, planes.m7 - planes.m4, planes.m11 - planes.m8, planes.m15 - planes.m12 };
NormalizePlane(&frustum->Planes[Right]);
frustum->Planes[Left] = (Vector4){ planes.m3 + planes.m0, planes.m7 + planes.m4, planes.m11 + planes.m8, planes.m15 + planes.m12 };
NormalizePlane(&frustum->Planes[Left]);
frustum->Planes[Top] = (Vector4){ planes.m3 - planes.m1, planes.m7 - planes.m5, planes.m11 - planes.m9, planes.m15 - planes.m13 };
NormalizePlane(&frustum->Planes[Top]);
frustum->Planes[Bottom] = (Vector4){ planes.m3 + planes.m1, planes.m7 + planes.m5, planes.m11 + planes.m9, planes.m15 + planes.m13 };
NormalizePlane(&frustum->Planes[Bottom]);
frustum->Planes[Back] = (Vector4){ planes.m3 - planes.m2, planes.m7 - planes.m6, planes.m11 - planes.m10, planes.m15 - planes.m14 };
NormalizePlane(&frustum->Planes[Back]);
frustum->Planes[Front] = (Vector4){ planes.m3 + planes.m2, planes.m7 + planes.m6, planes.m11 + planes.m10, planes.m15 + planes.m14 };
NormalizePlane(&frustum->Planes[Front]);
}
float DistanceToPlaneV(const Vector4* plane, const Vector3* position)
{
return (plane->x * position->x + plane->y * position->y + plane->z * position->z + plane->w);
}
float DistanceToPlane(const Vector4* plane, float x, float y, float z)
{
return (plane->x * x + plane->y * y + plane->z * z + plane->w);
}
bool PointInFrustumV(Frustum* frustum, Vector3 position)
{
if (frustum == NULL)
return false;
for (int i = 0; i < 6; i++)
{
if (DistanceToPlaneV(&frustum->Planes[i], &position) <= 0) // point is behind plane
return false;
}
return true;
}
bool PointInFrustum(Frustum* frustum, float x, float y, float z)
{
if (frustum == NULL)
return false;
for (int i = 0; i < 6; i++)
{
if (DistanceToPlane(&frustum->Planes[i], x, y, z) <= 0) // point is behind plane
return false;
}
return true;
}
bool SphereInFrustumV(Frustum* frustum, Vector3 position, float radius)
{
if (frustum == NULL)
return false;
for (int i = 0; i < 6; i++)
{
if (DistanceToPlaneV(&frustum->Planes[i], &position) < -radius) // center is behind plane by more than the radius
return false;
}
return true;
}
bool AABBoxInFrustum(Frustum* frustum, Vector3 min, Vector3 max)
{
// if any point is in and we are good
if (PointInFrustum(frustum, min.x, min.y, min.z))
return true;
if (PointInFrustum(frustum, min.x, max.y, min.z))
return true;
if (PointInFrustum(frustum, max.x, max.y, min.z))
return true;
if (PointInFrustum(frustum, max.x, min.y, min.z))
return true;
if (PointInFrustum(frustum, min.x, min.y, max.z))
return true;
if (PointInFrustum(frustum, min.x, max.y, max.z))
return true;
if (PointInFrustum(frustum, max.x, max.y, max.z))
return true;
if (PointInFrustum(frustum, max.x, min.y, max.z))
return true;
// check to see if all points are outside of any one plane, if so the entire box is outside
for (int i = 0; i < 6; i++)
{
bool oneInside = false;
if (DistanceToPlane(&frustum->Planes[i], min.x, min.y, min.z) >= 0)
oneInside = true;
if (DistanceToPlane(&frustum->Planes[i], max.x, min.y, min.z) >= 0)
oneInside = true;
if (DistanceToPlane(&frustum->Planes[i], max.x, max.y, min.z) >= 0)
oneInside = true;
if (DistanceToPlane(&frustum->Planes[i], min.x, max.y, min.z) >= 0)
oneInside = true;
if (DistanceToPlane(&frustum->Planes[i], min.x, min.y, max.z) >= 0)
oneInside = true;
if (DistanceToPlane(&frustum->Planes[i], max.x, min.y, max.z) >= 0)
oneInside = true;
if (DistanceToPlane(&frustum->Planes[i], max.x, max.y, max.z) >= 0)
oneInside = true;
if (DistanceToPlane(&frustum->Planes[i], min.x, max.y, max.z) >= 0)
oneInside = true;
if (!oneInside)
return false;
}
// the box extends outside the frustum but crosses it
return true;
}

View File

@@ -2470,6 +2470,11 @@ void luaRegister() {
assingGlobalFunction( "QuaternionToEuler", lmathQuaternionToEuler );
assingGlobalFunction( "QuaternionTransform", lmathQuaternionTransform );
assingGlobalFunction( "QuaternionEquals", lmathQuaternionEquals );
/* Frustum. */
assingGlobalFunction( "ExtractFrustum", lmathExtractFrustum );
assingGlobalFunction( "PointInFrustum", lmathPointInFrustum );
assingGlobalFunction( "SphereInFrustum", lmathSphereInFrustum );
assingGlobalFunction( "AABBInFrustum", lmathAABBInFrustum );
/* Gui. */
/* Global gui state control functions. */

View File

@@ -2,6 +2,7 @@
#include "state.h"
#include "rmath.h"
#include "lua_core.h"
#include "frustum.h"
int imin( int a, int b ) {
return a < b ? a : b;
@@ -2343,3 +2344,103 @@ int lmathQuaternionEquals( lua_State* L ) {
return 1;
}
/*
## Math - Frustum
*/
static Frustum getFrustum( lua_State* L, int index ) {
luaL_checktype( L, index, LUA_TTABLE );
Frustum frustum = { 0 };
int t = index, i = 0;
lua_pushnil( L );
while ( lua_next( L, t ) != 0 && i < 6 ) {
frustum.Planes[i] = uluaGetVector4( L, lua_gettop( L ) );
i++;
lua_pop( L, 1 );
}
return frustum;
}
static int pushFrustum( lua_State* L, Frustum* frustum ) {
lua_createtable( L, 6, 0 );
for ( int i = 0; i < 6; i++ ) {
uluaPushVector4( L, frustum->Planes[i] );
lua_rawseti( L, -2, 1 + i );
}
return 1;
}
/*
> frustum = RL.ExtractFrustum( Matrix projection, Matrix modelview )
Extract frustum from projection and modelView matrices.
- Success return Vector4{}
*/
int lmathExtractFrustum( lua_State* L ) {
Matrix projection = uluaGetMatrix( L, 1 );
Matrix modelview = uluaGetMatrix( L, 2 );
Frustum frustum = { 0 };
ExtractFrustum( &frustum, projection, modelview );
pushFrustum( L, &frustum );
return 1;
}
/*
> inFrustum = RL.PointInFrustum( Vector4{} frustum, Vector3 position )
Check if point inside frustum
- Success return bool
*/
int lmathPointInFrustum( lua_State* L ) {
Frustum frustum = getFrustum( L, 1 );
Vector3 position = uluaGetVector3( L, 2 );
lua_pushboolean( L, PointInFrustumV( &frustum, position ) );
return 1;
}
/*
> inFrustum = RL.SphereInFrustum( Vector4{} frustum, Vector3 position )
Check if sphere inside frustum
- Success return bool
*/
int lmathSphereInFrustum( lua_State* L ) {
Frustum frustum = getFrustum( L, 1 );
Vector3 position = uluaGetVector3( L, 2 );
float radius = luaL_checknumber( L, 3 );
lua_pushboolean( L, SphereInFrustumV( &frustum, position, radius ) );
return 1;
}
/*
> inFrustum = RL.AABBInFrustum( Vector4{} frustum, Vector3 min, Vector3 max )
Check if AABB inside frustum
- Success return bool
*/
int lmathAABBInFrustum( lua_State* L ) {
Frustum frustum = getFrustum( L, 1 );
Vector3 min = uluaGetVector3( L, 2 );
Vector3 max = uluaGetVector3( L, 3 );
lua_pushboolean( L, AABBoxInFrustum( &frustum, min, max ) );
return 1;
}