mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-24 05:10:42 -04:00
Port frustum culling to C
This commit is contained in:
parent
32f6d4c783
commit
ad760bcc96
@ -178,6 +178,7 @@
|
||||
<ClInclude Include="Bitmap.h" />
|
||||
<ClInclude Include="Drawer.h" />
|
||||
<ClInclude Include="ErrorHandler.h" />
|
||||
<ClInclude Include="FrustumCulling.h" />
|
||||
<ClInclude Include="TerrainAtlas1D.h" />
|
||||
<ClInclude Include="TerrainAtlas2D.h" />
|
||||
<ClInclude Include="WorldEvents.h" />
|
||||
@ -213,6 +214,7 @@
|
||||
<ClCompile Include="Drawer.c" />
|
||||
<ClCompile Include="EventHandler.c" />
|
||||
<ClCompile Include="ExtMath.c" />
|
||||
<ClCompile Include="FrustumCulling.c" />
|
||||
<ClCompile Include="PackedCol.c" />
|
||||
<ClCompile Include="Funcs.c" />
|
||||
<ClCompile Include="GraphicsCommon.c" />
|
||||
|
@ -88,6 +88,12 @@
|
||||
<Filter Include="Header Files\MeshBuilder">
|
||||
<UniqueIdentifier>{1012ed27-a9f2-4b01-a9a4-be75e2a1e2f0}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Rendering">
|
||||
<UniqueIdentifier>{d746c4e4-411e-4e25-9e79-a2fceabe7cda}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Rendering">
|
||||
<UniqueIdentifier>{2134e4ed-c57e-40b2-b329-5f5b7b71a799}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="NotchyGenerator.h">
|
||||
@ -192,6 +198,9 @@
|
||||
<ClInclude Include="Drawer.h">
|
||||
<Filter>Header Files\MeshBuilder</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="FrustumCulling.h">
|
||||
<Filter>Header Files\Rendering</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="NotchyGenerator.c">
|
||||
@ -275,5 +284,8 @@
|
||||
<ClCompile Include="Drawer.c">
|
||||
<Filter>Source Files\MeshBuilder</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="FrustumCulling.c">
|
||||
<Filter>Source Files\Rendering</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
81
src/Client/FrustumCulling.c
Normal file
81
src/Client/FrustumCulling.c
Normal file
@ -0,0 +1,81 @@
|
||||
#include "FrustumCulling.h"
|
||||
#include "ExtMath.h"
|
||||
|
||||
Real32
|
||||
frustum00, frustum01, frustum02, frustum03,
|
||||
frustum10, frustum11, frustum12, frustum13,
|
||||
frustum20, frustum21, frustum22, frustum23,
|
||||
frustum30, frustum31, frustum32, frustum33,
|
||||
frustum40, frustum41, frustum42, frustum43;
|
||||
|
||||
bool FrustumCulling_SphereInFrustum(Real32 x, Real32 y, Real32 z, Real32 radius) {
|
||||
float d = frustum00 * x + frustum01 * y + frustum02 * z + frustum03;
|
||||
if (d <= -radius) return false;
|
||||
|
||||
d = frustum10 * x + frustum11 * y + frustum12 * z + frustum13;
|
||||
if (d <= -radius) return false;
|
||||
|
||||
d = frustum20 * x + frustum21 * y + frustum22 * z + frustum23;
|
||||
if (d <= -radius) return false;
|
||||
|
||||
d = frustum30 * x + frustum31 * y + frustum32 * z + frustum33;
|
||||
if (d <= -radius) return false;
|
||||
|
||||
d = frustum40 * x + frustum41 * y + frustum42 * z + frustum43;
|
||||
if (d <= -radius) return false;
|
||||
// Don't test NEAR plane, it's pointless
|
||||
return true;
|
||||
}
|
||||
|
||||
void FrustumCulling_CalcFrustumEquations(Matrix* projection, Matrix* modelView) {
|
||||
Matrix clipMatrix;
|
||||
Matrix_Mul(modelView, projection, &clipMatrix);
|
||||
|
||||
Real32* clip = (Real32*)&clipMatrix;
|
||||
// Extract the numbers for the RIGHT plane
|
||||
frustum00 = clip[3] - clip[0];
|
||||
frustum01 = clip[7] - clip[4];
|
||||
frustum02 = clip[11] - clip[8];
|
||||
frustum03 = clip[15] - clip[12];
|
||||
FrustumCulling_Normalise(&frustum00, &frustum01, &frustum02, &frustum03);
|
||||
|
||||
// Extract the numbers for the LEFT plane
|
||||
frustum10 = clip[3] + clip[0];
|
||||
frustum11 = clip[7] + clip[4];
|
||||
frustum12 = clip[11] + clip[8];
|
||||
frustum13 = clip[15] + clip[12];
|
||||
FrustumCulling_Normalise(&frustum10, &frustum11, &frustum12, &frustum13);
|
||||
|
||||
// Extract the BOTTOM plane
|
||||
frustum20 = clip[3] + clip[1];
|
||||
frustum21 = clip[7] + clip[5];
|
||||
frustum22 = clip[11] + clip[9];
|
||||
frustum23 = clip[15] + clip[13];
|
||||
FrustumCulling_Normalise(&frustum20, &frustum21, &frustum22, &frustum23);
|
||||
|
||||
// Extract the TOP plane
|
||||
frustum30 = clip[3] - clip[1];
|
||||
frustum31 = clip[7] - clip[5];
|
||||
frustum32 = clip[11] - clip[9];
|
||||
frustum33 = clip[15] - clip[13];
|
||||
FrustumCulling_Normalise(&frustum30, &frustum31, &frustum32, &frustum33);
|
||||
|
||||
// Extract the FAR plane
|
||||
frustum40 = clip[3] - clip[2];
|
||||
frustum41 = clip[7] - clip[6];
|
||||
frustum42 = clip[11] - clip[10];
|
||||
frustum43 = clip[15] - clip[14];
|
||||
FrustumCulling_Normalise(&frustum40, &frustum41, &frustum42, &frustum43);
|
||||
}
|
||||
|
||||
void FrustumCulling_Normalise(Real32* plane0, Real32* plane1, Real32* plane2, Real32* plane3) {
|
||||
Real32 val1 = *plane0;
|
||||
Real32 val2 = *plane1;
|
||||
Real32 val3 = *plane2;
|
||||
Real32 t = (Real32)Math_Sqrt(val1 * val1 + val2 * val2 + val3 * val3);
|
||||
|
||||
*plane0 /= t;
|
||||
*plane1 /= t;
|
||||
*plane2 /= t;
|
||||
*plane3 /= t;
|
||||
}
|
17
src/Client/FrustumCulling.h
Normal file
17
src/Client/FrustumCulling.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef CS_FRUSTUMCULLING_H
|
||||
#define CS_FRUSTUMCULLING_H
|
||||
#include "Matrix.h"
|
||||
#include "Typedefs.h"
|
||||
/* Implements frustum culling of bounding spheres.
|
||||
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
|
||||
Sourced from http://www.crownandcutlass.com/features/technicaldetails/frustum.html
|
||||
*/
|
||||
|
||||
/* Returns whether the given bounding sphere is in the view frustum. */
|
||||
bool FrustumCulling_SphereInFrustum(Real32 x, Real32 y, Real32 z, Real32 radius);
|
||||
|
||||
/* Calculates the planes that constitute view frustum. */
|
||||
void FrustumCulling_CalcFrustumEquations(Matrix* projection, Matrix* modelView);
|
||||
|
||||
static void FrustumCulling_Normalise(Real32* plane0, Real32* plane1, Real32* plane2, Real32* plane3);
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user