Skip to content

Commit c8bd92c

Browse files
authored
xScene and xGrid (#683)
* Completed and Linked xUpdateCull, and xPtankPool * Review Fixes * Ported all xCollide code from seils pc port repo Thanks seil. * xScene and xGrid code port
1 parent cc41942 commit c8bd92c

File tree

8 files changed

+1439
-401
lines changed

8 files changed

+1439
-401
lines changed

configure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def MatchingFor(*versions):
453453
Object(NonMatching, "SB/Core/x/xpkrsvc.cpp"),
454454
Object(Matching, "SB/Core/x/xQuickCull.cpp"),
455455
Object(Matching, "SB/Core/x/xsavegame.cpp"),
456-
Object(NonMatching, "SB/Core/x/xScene.cpp"),
456+
Object(NonMatching, "SB/Core/x/xScene.cpp", extra_cflags=["-sym on"]),
457457
Object(NonMatching, "SB/Core/x/xScrFx.cpp"),
458458
Object(NonMatching, "SB/Core/x/xserializer.cpp"),
459459
Object(NonMatching, "SB/Core/x/xSFX.cpp"),

src/SB/Core/gc/iCollide.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ RpCollisionTriangle* sphereHitsEnvCB(RpIntersection* isx, RpWorldSector* sector,
1313
S32 iSphereHitsEnv3(const xSphere* b, const xEnv* env, xCollis* colls, U8 ncolls, F32 sth);
1414
S32 iSphereHitsModel3(const xSphere* b, const xModelInstance* m, xCollis* colls, U8 ncolls,
1515
F32 sth);
16+
U32 iRayHitsEnv(const xRay3* r, const xEnv* env, xCollis* coll);
1617
U32 iRayHitsModel(const xRay3* r, const xModelInstance* m, xCollis* coll);
1718
void iSphereForModel(xSphere* o, const xModelInstance* m);
1819
void iCollideInit(xScene* sc);

src/SB/Core/x/xGrid.cpp

Lines changed: 232 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
#include "xMemMgr.h"
77
#include "xEnt.h"
88

9-
extern float xGrid_float_0p001;
10-
extern float xGrid_float_one;
11-
extern float xGrid_float_one_quarter;
12-
139
volatile S32 gGridIterActive = 0;
1410

1511
void xGridBoundInit(xGridBound* bound, void* data)
@@ -43,25 +39,25 @@ void xGridInit(xGrid* grid, const xBox* bounds, U16 nx, U16 nz, U8 ingrid_id)
4339
grid->csizex = gsizex / nx;
4440
grid->csizez = gsizex / nz;
4541

46-
if (__fabs(gsizex) <= xGrid_float_0p001)
42+
if (__fabs(gsizex) <= 0.001f)
4743
{
48-
grid->inv_csizex = xGrid_float_one;
44+
grid->inv_csizex = 1.0f;
4945
}
5046
else
5147
{
5248
grid->inv_csizex = nx / gsizex;
5349
}
5450

55-
if (__fabs(gsizez) <= xGrid_float_0p001)
51+
if (__fabs(gsizez) <= 0.001f)
5652
{
57-
grid->inv_csizez = xGrid_float_one;
53+
grid->inv_csizez = 1.0f;
5854
}
5955
else
6056
{
6157
grid->inv_csizez = nz / gsizez;
6258
}
6359

64-
grid->maxr = xGrid_float_one_quarter * MAX(grid->csizex, grid->csizez);
60+
grid->maxr = 0.25f * MAX(grid->csizex, grid->csizez);
6561
grid->cells = (xGridBound**)xMemAllocSize(nx * nz * sizeof(xGridBound*));
6662
memset(grid->cells, 0, sizeof(xGridBound*) * (nz * nx));
6763
}
@@ -128,6 +124,94 @@ void xGridAdd(xGrid* grid, xGridBound* bound, S32 x, S32 z)
128124
xGridAddToCell(&grid->cells[z * grid->nx] + x, bound);
129125
}
130126

127+
S32 xGridAdd(xGrid* grid, xEnt* ent)
128+
//NONMATCH("https://decomp.me/scratch/5R7FZ")
129+
{
130+
xBound* bound;
131+
xVec3* center;
132+
F32 maxr;
133+
134+
bound = &ent->bound;
135+
maxr = grid->maxr;
136+
137+
if (bound->type == XBOUND_TYPE_SPHERE)
138+
{
139+
xSphere* sph = &bound->sph;
140+
center = &sph->center;
141+
if (bound->sph.r >= maxr)
142+
{
143+
S32 r = xGridAddToCell(&grid->other, &ent->gridb);
144+
if (r)
145+
{
146+
ent->gridb.ingrid = grid->ingrid_id;
147+
}
148+
return r;
149+
}
150+
}
151+
else if (bound->type == XBOUND_TYPE_OBB)
152+
{
153+
xBBox* bbox = &bound->box;
154+
center = &bbox->center;
155+
F32 rx = bbox->box.upper.x - bbox->box.lower.x;
156+
F32 ry = bbox->box.upper.y - bbox->box.lower.y;
157+
F32 rz = bbox->box.upper.z - bbox->box.lower.z;
158+
F32 len2 =
159+
SQR(rx) *
160+
(SQR(bound->mat->right.x) + SQR(bound->mat->right.y) + SQR(bound->mat->right.z)) +
161+
SQR(ry) * (SQR(bound->mat->up.x) + SQR(bound->mat->up.y) + SQR(bound->mat->up.z)) +
162+
SQR(rz) * (SQR(bound->mat->at.x) + SQR(bound->mat->at.y) + SQR(bound->mat->at.z));
163+
if (len2 >= 4.0f * maxr * maxr)
164+
{
165+
S32 r = xGridAddToCell(&grid->other, &ent->gridb);
166+
if (r)
167+
{
168+
ent->gridb.ingrid = grid->ingrid_id;
169+
}
170+
return r;
171+
}
172+
}
173+
else if (bound->type == XBOUND_TYPE_BOX)
174+
{
175+
xBBox* bbox = &bound->box;
176+
center = &bbox->center;
177+
F32 rx = bound->box.box.upper.x - bound->box.box.lower.x;
178+
F32 rz = bound->box.box.upper.z - bound->box.box.lower.z;
179+
F32 len2 = SQR(rx) + SQR(rz);
180+
if (len2 >= 4.0f * maxr * maxr)
181+
{
182+
S32 r = xGridAddToCell(&grid->other, &ent->gridb);
183+
if (r)
184+
{
185+
ent->gridb.ingrid = grid->ingrid_id;
186+
}
187+
return r;
188+
}
189+
}
190+
else
191+
{
192+
return 0;
193+
}
194+
195+
F32 cgridx = center->x - grid->minx;
196+
cgridx *= grid->inv_csizex;
197+
198+
F32 cgridz = center->z - grid->minz;
199+
cgridz *= grid->inv_csizez;
200+
201+
S32 x = (S32)MIN(grid->nx - 1, MAX(0.0f, cgridx));
202+
S32 z = (S32)MIN(grid->nz - 1, MAX(0.0f, cgridz));
203+
204+
if (1)
205+
{
206+
ent->gridb.gx = x;
207+
ent->gridb.gz = z;
208+
ent->gridb.ingrid = grid->ingrid_id;
209+
return 1;
210+
}
211+
212+
return 0;
213+
}
214+
131215
S32 xGridRemove(xGridBound* bound)
132216
{
133217
if (bound->head)
@@ -214,3 +298,142 @@ xGridBound* xGridIterFirstCell(xGrid* grid, F32 posx, F32 posy, F32 posz, S32& g
214298
xGridGetCell(grid, posx, posy, posz, grx, grz);
215299
return xGridIterFirstCell(grid, grx, grz, iter);
216300
}
301+
302+
S32 xGridEntIsTooBig(xGrid* grid, const xEnt* ent)
303+
{
304+
const xBound* bound = &ent->bound;
305+
F32 maxr = grid->maxr;
306+
307+
if (bound->type == XBOUND_TYPE_SPHERE)
308+
{
309+
const xSphere* sph = &bound->sph;
310+
if (sph->r >= maxr)
311+
{
312+
return 1;
313+
}
314+
}
315+
else if (bound->type == XBOUND_TYPE_OBB)
316+
{
317+
const xBBox* bbox = &bound->box;
318+
F32 rx = bbox->box.upper.x - bbox->box.lower.x;
319+
F32 ry = bbox->box.upper.y - bbox->box.lower.y;
320+
F32 rz = bbox->box.upper.z - bbox->box.lower.z;
321+
F32 len2 =
322+
SQR(rx) *
323+
(SQR(bound->mat->right.x) + SQR(bound->mat->right.y) + SQR(bound->mat->right.z)) +
324+
SQR(ry) * (SQR(bound->mat->up.x) + SQR(bound->mat->up.y) + SQR(bound->mat->up.z)) +
325+
SQR(rz) * (SQR(bound->mat->at.x) + SQR(bound->mat->at.y) + SQR(bound->mat->at.z));
326+
if (len2 >= 4.0f * maxr * maxr)
327+
{
328+
return 1;
329+
}
330+
}
331+
else if (bound->type == XBOUND_TYPE_BOX)
332+
{
333+
const xBBox* bbox = &bound->box;
334+
F32 rx = bbox->box.upper.x - bbox->box.lower.x;
335+
F32 rz = bbox->box.upper.z - bbox->box.lower.z;
336+
F32 len2 = SQR(rx) + SQR(rz);
337+
if (len2 >= 4.0f * maxr * maxr)
338+
{
339+
return 1;
340+
}
341+
}
342+
343+
return 0;
344+
}
345+
346+
void xGridCheckPosition(xGrid* grid, xVec3* pos, xQCData* qcd, xGridCheckPositionCallback hitCB,
347+
void* cbdata)
348+
{
349+
xGridIterator it;
350+
S32 px, pz;
351+
xGridBound* cell;
352+
353+
cell = xGridIterFirstCell(grid, pos->x, pos->y, pos->z, px, pz, it);
354+
while (cell)
355+
{
356+
xBound* cellbound = (xBound*)(cell + 1);
357+
if (xQuickCullIsects(qcd, &cellbound->qcd) && !hitCB((xEnt*)cell->data, cbdata))
358+
{
359+
xGridIterClose(it);
360+
return;
361+
}
362+
cell = xGridIterNextCell(it);
363+
}
364+
365+
xBox clbox;
366+
clbox.lower.x = grid->csizex * px;
367+
clbox.lower.z = grid->csizez * pz;
368+
clbox.lower.x += grid->minx;
369+
clbox.lower.z += grid->minz;
370+
371+
F32 clcenterx = 0.5f * grid->csizex;
372+
clcenterx += clbox.lower.x;
373+
374+
F32 clcenterz = 0.5f * grid->csizez;
375+
clcenterz += clbox.lower.z;
376+
377+
static S32 offs[4][3][2] = { -1, 0, -1, -1, 0, -1, 0, -1, 1, -1, 1, 0,
378+
1, 0, 1, 1, 0, 1, 0, 1, -1, 1, -1, 0 };
379+
380+
static S32 k;
381+
382+
if (pos->x < clcenterx)
383+
{
384+
if (pos->z < clcenterz)
385+
{
386+
k = 0;
387+
}
388+
else
389+
{
390+
k = 1;
391+
}
392+
}
393+
else
394+
{
395+
if (pos->z < clcenterz)
396+
{
397+
k = 3;
398+
}
399+
else
400+
{
401+
k = 2;
402+
}
403+
}
404+
405+
for (S32 i = 0; i < 3; i++)
406+
{
407+
S32 _x = px + offs[k][i][1];
408+
if (_x >= 0 && _x < grid->nx)
409+
{
410+
S32 _z = pz + offs[k][i][0];
411+
if (_z >= 0 && _z < grid->nz)
412+
{
413+
cell = xGridIterFirstCell(grid, _x, _z, it);
414+
while (cell)
415+
{
416+
xBound* cellbound = (xBound*)(cell + 1);
417+
if (xQuickCullIsects(qcd, &cellbound->qcd) && !hitCB((xEnt*)cell->data, cbdata))
418+
{
419+
xGridIterClose(it);
420+
return;
421+
}
422+
cell = xGridIterNextCell(it);
423+
}
424+
}
425+
}
426+
}
427+
428+
cell = xGridIterFirstCell(&grid->other, it);
429+
while (cell)
430+
{
431+
xBound* cellbound = (xBound*)(cell + 1);
432+
if (xQuickCullIsects(qcd, &cellbound->qcd) && !hitCB((xEnt*)cell->data, cbdata))
433+
{
434+
xGridIterClose(it);
435+
return;
436+
}
437+
cell = xGridIterNextCell(it);
438+
}
439+
}

src/SB/Core/x/xGrid.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55

66
#include "xMath3.h"
77

8+
#include "xBound.h"
9+
810
struct xEnt;
911
struct xQCData;
1012

13+
typedef S32 (*xGridCheckPositionCallback)(xEnt*, void*);
14+
1115
struct xGridBound
1216
{
1317
void* data;
@@ -174,4 +178,74 @@ inline void xGridIterClose(xGridIterator& it)
174178
}
175179
}
176180

181+
struct grid_index
182+
{
183+
U16 x;
184+
U16 z;
185+
};
186+
187+
inline grid_index get_grid_index(const xGrid& grid, F32 x, F32 z)
188+
189+
//NONMATCH("https://decomp.me/scratch/pMa66")
190+
{
191+
grid_index index = { range_limit<U16>((U16)((x - grid.minx) * grid.inv_csizex), 0, grid.nx - 1),
192+
range_limit<U16>((U16)((z - grid.minz) * grid.inv_csizez), 0,
193+
grid.nz - 1) };
194+
return index;
195+
}
196+
197+
template <class T>
198+
inline void xGridCheckBound(xGrid& grid, const xBound& bound, const xQCData& qcd, T cb)
199+
{
200+
xGridIterator it;
201+
202+
xBox box;
203+
xBoundGetBox(box, bound);
204+
205+
F32 ex = 0.25f * grid.csizex;
206+
F32 ez = 0.25f * grid.csizez;
207+
box.lower.x -= ex;
208+
box.lower.z -= ez;
209+
box.upper.x += ex;
210+
box.upper.z += ez;
211+
212+
grid_index var_4C, var_50;
213+
var_50 = get_grid_index(grid, box.lower.x, box.lower.z);
214+
var_4C = get_grid_index(grid, box.upper.x, box.upper.z);
215+
216+
xGridBound* cell = xGridIterFirstCell(&grid.other, it);
217+
while (cell)
218+
{
219+
if (xQuickCullIsects(&qcd, &((xBound*)(cell + 1))->qcd))
220+
{
221+
if (!cb(*(xEnt*)cell->data, *cell))
222+
{
223+
xGridIterClose(it);
224+
return;
225+
}
226+
}
227+
cell = xGridIterNextCell(it);
228+
}
229+
230+
for (U16 gx = var_50.x; gx <= var_4C.x; gx++)
231+
{
232+
for (U16 gz = var_50.z; gz <= var_4C.z; gz++)
233+
{
234+
xGridBound* cell = xGridIterFirstCell(&grid, gx, gz, it);
235+
while (cell)
236+
{
237+
if (xQuickCullIsects(&qcd, &((xBound*)(cell + 1))->qcd))
238+
{
239+
if (!cb(*(xEnt*)cell->data, *cell))
240+
{
241+
xGridIterClose(it);
242+
return;
243+
}
244+
}
245+
cell = xGridIterNextCell(it);
246+
}
247+
}
248+
}
249+
}
250+
177251
#endif

src/SB/Core/x/xQuickCull.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static void xQuickCullCellForVec(xQCControl* ctrl, xQCData* c, const xVec3* v);
5454
static void xQuickCullCellMerge(xQCData* dest, const xQCData* a, const xQCData* b);
5555
void xQuickCullForLine(xQCControl* ctrl, xQCData* q, const xLine3* ln);
5656
void xQuickCullForRay(xQCControl* ctrl, xQCData* q, const xRay3* r);
57+
void xQuickCullForRay(xQCData* q, const xRay3* r);
5758
void xQuickCullForSphere(xQCControl* ctrl, xQCData* q, const xSphere* s);
5859
void xQuickCullForBox(xQCControl* ctrl, xQCData* q, const xBox* box);
5960
void xQuickCullForBox(xQCData* q, const xBox* box);

0 commit comments

Comments
 (0)