• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

3D Frustum Culling

Hey there,

I'm currently using this method of optimization in my game to speed it up, now we have matrices in Game Maker it's do-able without speed problems, as we can extract the frustum planes from the MVP matrix, doing this though I run into a weird problem where the frustum seems to be locked at 0, 0, 0 as it's position.

Any advice? Code is below.

Code:
///FrustumConstruct(screendepth, matView, matProj);
var matView, matProj, frusMat, frus, matModel;
matView = argument1;
matProj = argument2;

var zMin, r, pA, pB, pC, pD;

//Calculate Minimum Z.
zMin = matProj[14] / matProj[10];
r    = argument0 / (argument0 - zMin);
matProj[10] = r;
matProj[14] =  -r * zMin;

//Create the frustum matrix.
matModel = matrix_build(_cxfrom, _cyfrom, _czfrom, 0, 0, 0, 1, 1, 1);
frusMat = matrix_multiply(matModel, matrix_multiply(matView, matProj));

//Calculate Near Plane
pA = frusMat[3]  + frusMat[2];
pB = frusMat[7]  + frusMat[6];
pC = frusMat[11] + frusMat[10];
pD = frusMat[15] + frusMat[14];
global.frusNear = MAE_PlaneCreate(pA, pB, pC, pD);

//Calculate Far Plane
pA = frusMat[3]  - frusMat[2];
pB = frusMat[7]  - frusMat[6];
pC = frusMat[11] - frusMat[10];
pD = frusMat[15] - frusMat[14];
global.frusFar = MAE_PlaneCreate(pA, pB, pC, pD);

//Calculate Left Plane
pA = frusMat[3]  + frusMat[0];
pB = frusMat[7]  + frusMat[4];
pC = frusMat[11] + frusMat[8];
pD = frusMat[15] + frusMat[12];
global.frusLeft = MAE_PlaneCreate(pA, pB, pC, pD);

//Calculate Right Plane
pA = frusMat[3]  - frusMat[0];
pB = frusMat[7]  - frusMat[4];
pC = frusMat[11] - frusMat[8];
pD = frusMat[15] - frusMat[12];
global.frusRight = MAE_PlaneCreate(pA, pB, pC, pD);

//Calculate Bottom Plane
pA = frusMat[3]  - frusMat[1];
pB = frusMat[7]  - frusMat[5];
pC = frusMat[11] - frusMat[9];
pD = frusMat[15] - frusMat[13];
global.frusBottom = MAE_PlaneCreate(pA, pB, pC, pD);

//Calculate Top Plane
pA = frusMat[3]  + frusMat[1];
pB = frusMat[7]  + frusMat[5];
pC = frusMat[11] + frusMat[9];
pD = frusMat[15] + frusMat[13];
global.frusTop = MAE_PlaneCreate(pA, pB, pC, pD);

frusMat =  -1;
matView =  -1;
matProj =  -1;
matModel = -1;
Code:
///MAE_FrustumCheckPoint(x, y, z);
var pointPos = MAE_VectorCreate(argument0, argument1, argument2);
if(MAE_PlaneDotCoord(global.frusNear  , pointPos) < 0.0) { return false; }
if(MAE_PlaneDotCoord(global.frusFar   , pointPos) < 0.0) { return false; }
if(MAE_PlaneDotCoord(global.frusLeft  , pointPos) < 0.0) { return false; }
if(MAE_PlaneDotCoord(global.frusRight , pointPos) < 0.0) { return false; }
if(MAE_PlaneDotCoord(global.frusTop   , pointPos) < 0.0) { return false; }
if(MAE_PlaneDotCoord(global.frusBottom, pointPos) < 0.0) { return false; }
return true;
Code:
///MAE_PlaneDotCoord(plane, vector);
var pP = argument0;
var pV = argument1;

return pP[0] * pV[0] + pP[1] * pV[1] + pP[2] * pV[2];
Code:
///MAE_PlaneCreate(a, b, c, d);

var p;
    p[3] = argument3;
    p[2] = argument2;
    p[1] = argument1;
    p[0] = argument0;
return p;
 
M

Misty

Guest
Not enough info. Upload the project.

When you make a matrix, you put the first 3 arguments as xyz. Though I assume you already knew that.
 
Not enough info. Upload the project.

When you make a matrix, you put the first 3 arguments as xyz. Though I assume you already knew that.
Yeah I know about building matrices. This method of frustum culling takes the planes from the View_Projection matrix, but making it an MVP with the world position set to the cameras position doesn't actually change anything, it still does everything from the center axis (0, 0, 0)...

I'm sorry for the lack of a project upload, but I don't want to upload the source to this one as theirs a lot of things I have to keep internal, from market place assets to things I've made but don't want to release in their unfinished, bug ridden state.

I know this might be a bit advanced for the average Game Maker user, since I'm used to using C++ and DirectX, but I'd imagine theirs more people like me who'd have a clue about this kind of stuff.
 
Top