Skip to content

Commit a65557c

Browse files
committed
UPBGE: Simplify bullet occlusion.
The bullet occlusion and culling call was simplified by avoiding passing extra arguments. The number of planes is statically set to 6, the modelview and projection are replaced by the matrix from the camera frustum as this matrix is the result of the same operation made in CullingTest. In the same time the plane reordering is removed in favor of a reorder in SG_Frustum.
1 parent 3341047 commit a65557c

File tree

7 files changed

+44
-35
lines changed

7 files changed

+44
-35
lines changed

source/gameengine/Ketsji/KX_Scene.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,8 @@ void KX_Scene::CalculateVisibleMeshes(KX_CullingNodeList& nodes, KX_Camera *cam,
13211321
return;
13221322
}
13231323

1324+
const SG_Frustum& frustum = cam->GetFrustum();
1325+
13241326
bool dbvt_culling = false;
13251327
if (m_dbvt_culling) {
13261328
/* Reset KX_GameObject m_bCulled to true before doing culling
@@ -1333,23 +1335,15 @@ void KX_Scene::CalculateVisibleMeshes(KX_CullingNodeList& nodes, KX_Camera *cam,
13331335

13341336
// test culling through Bullet
13351337
// get the clip planes
1336-
const std::array<MT_Vector4, 6>& cplanes = cam->GetFrustum().GetPlanes();
1337-
// and convert
1338-
MT_Vector4 planes[6] = {cplanes[4], cplanes[5], cplanes[0], cplanes[1], cplanes[2], cplanes[3]};
1339-
1338+
const std::array<MT_Vector4, 6>& planes = frustum.GetPlanes();
1339+
const MT_Matrix4x4& matrix = frustum.GetMatrix();
1340+
const int *viewport = KX_GetActiveEngine()->GetCanvas()->GetViewPort();
13401341
CullingInfo info(layer, nodes);
13411342

1342-
float mvmat[16] = {0.0f};
1343-
cam->GetModelviewMatrix().getValue(mvmat);
1344-
float pmat[16] = {0.0f};
1345-
cam->GetProjectionMatrix().getValue(pmat);
1346-
1347-
dbvt_culling = m_physicsEnvironment->CullingTest(PhysicsCullingCallback,&info,planes,6,m_dbvt_occlusion_res,
1348-
KX_GetActiveEngine()->GetCanvas()->GetViewPort(),
1349-
mvmat, pmat);
1343+
dbvt_culling = m_physicsEnvironment->CullingTest(PhysicsCullingCallback, &info, planes, m_dbvt_occlusion_res, viewport, matrix);
13501344
}
13511345
if (!dbvt_culling) {
1352-
KX_CullingHandler handler(nodes, cam->GetFrustum());
1346+
KX_CullingHandler handler(nodes, frustum);
13531347
for (KX_GameObject *gameobj : m_objectlist) {
13541348
if (gameobj->UseCulling() && gameobj->GetVisible() && (layer == 0 || gameobj->GetLayer() & layer)) {
13551349
handler.Process(gameobj->GetCullingNode());

source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ struct OcclusionBuffer {
13621362
m[15] = btScalar(m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15]);
13631363
}
13641364

1365-
void setup(int size, const int *view, float modelview[16], float projection[16])
1365+
void setup(int size, const int *view, float mat[16])
13661366
{
13671367
m_initialized = false;
13681368
m_occlusion = false;
@@ -1381,7 +1381,9 @@ struct OcclusionBuffer {
13811381
// at this time of the rendering, the modelview matrix is the
13821382
// world to camera transformation and the projection matrix is
13831383
// camera to clip transformation. combine both so that
1384-
CMmat4mul(m_wtc, projection, modelview);
1384+
for (unsigned short i = 0; i < 16; i++) {
1385+
m_wtc[i] = btScalar(mat[i]);
1386+
}
13851387
}
13861388

13871389
void initialize()
@@ -1897,30 +1899,31 @@ struct DbvtCullingCallback : btDbvt::ICollide {
18971899
};
18981900

18991901
static OcclusionBuffer gOcb;
1900-
bool CcdPhysicsEnvironment::CullingTest(PHY_CullingCallback callback, void *userData, MT_Vector4 *planes, int nplanes, int occlusionRes, const int *viewport, float modelview[16], float projection[16])
1902+
bool CcdPhysicsEnvironment::CullingTest(PHY_CullingCallback callback, void *userData, const std::array<MT_Vector4, 6>& planes,
1903+
int occlusionRes, const int *viewport, const MT_Matrix4x4& matrix)
19011904
{
19021905
if (!m_cullingTree)
19031906
return false;
19041907
DbvtCullingCallback dispatcher(callback, userData);
19051908
btVector3 planes_n[6];
19061909
btScalar planes_o[6];
1907-
if (nplanes > 6)
1908-
nplanes = 6;
1909-
for (int i = 0; i < nplanes; i++) {
1910+
for (int i = 0; i < 6; i++) {
19101911
planes_n[i] = ToBullet(planes[i]);
19111912
planes_o[i] = planes[i][3];
19121913
}
19131914
// if occlusionRes != 0 => occlusion culling
19141915
if (occlusionRes) {
1915-
gOcb.setup(occlusionRes, viewport, modelview, projection);
1916+
float mat[16];
1917+
matrix.getValue(mat);
1918+
gOcb.setup(occlusionRes, viewport, mat);
19161919
dispatcher.m_ocb = &gOcb;
19171920
// occlusion culling, the direction of the view is taken from the first plan which MUST be the near plane
1918-
btDbvt::collideOCL(m_cullingTree->m_sets[1].m_root, planes_n, planes_o, planes_n[0], nplanes, dispatcher);
1919-
btDbvt::collideOCL(m_cullingTree->m_sets[0].m_root, planes_n, planes_o, planes_n[0], nplanes, dispatcher);
1921+
btDbvt::collideOCL(m_cullingTree->m_sets[1].m_root, planes_n, planes_o, planes_n[0], 6, dispatcher);
1922+
btDbvt::collideOCL(m_cullingTree->m_sets[0].m_root, planes_n, planes_o, planes_n[0], 6, dispatcher);
19201923
}
19211924
else {
1922-
btDbvt::collideKDOP(m_cullingTree->m_sets[1].m_root, planes_n, planes_o, nplanes, dispatcher);
1923-
btDbvt::collideKDOP(m_cullingTree->m_sets[0].m_root, planes_n, planes_o, nplanes, dispatcher);
1925+
btDbvt::collideKDOP(m_cullingTree->m_sets[1].m_root, planes_n, planes_o, 6, dispatcher);
1926+
btDbvt::collideKDOP(m_cullingTree->m_sets[0].m_root, planes_n, planes_o, 6, dispatcher);
19241927
}
19251928
return true;
19261929
}

source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment
187187
btTypedConstraint *GetConstraintById(int constraintId);
188188

189189
virtual PHY_IPhysicsController *RayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX, float fromY, float fromZ, float toX, float toY, float toZ);
190-
virtual bool CullingTest(PHY_CullingCallback callback, void *userData, MT_Vector4 * planes, int nplanes, int occlusionRes, const int *viewport, float modelview[16], float projection[16]);
190+
virtual bool CullingTest(PHY_CullingCallback callback, void *userData, const std::array<MT_Vector4, 6>& planes,
191+
int occlusionRes, const int *viewport, const MT_Matrix4x4& matrix);
191192

192193

193194
//Methods for gamelogic collision/physics callbacks

source/gameengine/Physics/Common/PHY_IPhysicsEnvironment.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@
3333
#define __PHY_IPHYSICSENVIRONMENT_H__
3434

3535
#include "PHY_DynamicTypes.h"
36+
#include "MT_Matrix4x4.h"
3637
#include "MT_Vector2.h"
3738
#include "MT_Vector3.h"
3839
#include "MT_Vector4.h"
3940

41+
#include <array>
42+
4043
class PHY_IConstraint;
4144
class PHY_IVehicle;
4245
class PHY_ICharacter;
@@ -209,7 +212,8 @@ class PHY_IPhysicsEnvironment
209212
// culling based on physical broad phase
210213
// the plane number must be set as follow: near, far, left, right, top, botton
211214
// the near plane must be the first one and must always be present, it is used to get the direction of the view
212-
virtual bool CullingTest(PHY_CullingCallback callback, void *userData, MT_Vector4 * planeNormals, int planeNumber, int occlusionRes, const int *viewport, float modelview[16], float projection[16]) = 0;
215+
virtual bool CullingTest(PHY_CullingCallback callback, void *userData, const std::array<MT_Vector4, 6>& planes,
216+
int occlusionRes, const int *viewport, const MT_Matrix4x4& matrix) = 0;
213217

214218
// Methods for gamelogic collision/physics callbacks
215219
virtual void AddSensor(PHY_IPhysicsController *ctrl) = 0;

source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ class DummyPhysicsEnvironment : public PHY_IPhysicsEnvironment
8282
}
8383

8484
virtual PHY_IPhysicsController *RayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX, float fromY, float fromZ, float toX, float toY, float toZ);
85-
virtual bool CullingTest(PHY_CullingCallback callback, void *userData, class MT_Vector4 *planes, int nplanes, int occlusionRes, const int *viewport, float modelview[16], float projection[16])
85+
virtual bool CullingTest(PHY_CullingCallback callback, void *userData, const std::array<MT_Vector4, 6>& planes,
86+
int occlusionRes, const int *viewport, const MT_Matrix4x4& matrix)
8687
{
8788
return false;
8889
}

source/gameengine/SceneGraph/SG_Frustum.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
SG_Frustum::SG_Frustum(const MT_Matrix4x4& matrix)
66
:m_matrix(matrix)
77
{
8+
// Near clip plane
9+
m_planes[0] = m_matrix[3] + m_matrix[2];
10+
// Far clip plane
11+
m_planes[1] = m_matrix[3] - m_matrix[2];
812
// Left clip plane
9-
m_planes[0] = m_matrix[3] + m_matrix[0];
13+
m_planes[2] = m_matrix[3] + m_matrix[0];
1014
// Right clip plane
11-
m_planes[1] = m_matrix[3] - m_matrix[0];
15+
m_planes[3] = m_matrix[3] - m_matrix[0];
1216
// Top clip plane
13-
m_planes[2] = m_matrix[3] - m_matrix[1];
17+
m_planes[4] = m_matrix[3] - m_matrix[1];
1418
// Bottom clip plane
15-
m_planes[3] = m_matrix[3] + m_matrix[1];
16-
// Near clip plane
17-
m_planes[4] = m_matrix[3] + m_matrix[2];
18-
// Far clip plane
19-
m_planes[5] = m_matrix[3] - m_matrix[2];
19+
m_planes[5] = m_matrix[3] + m_matrix[1];
2020

2121
// Normalize clip planes.
2222
for (MT_Vector4& plane : m_planes) {
@@ -32,6 +32,11 @@ const std::array<MT_Vector4, 6>& SG_Frustum::GetPlanes() const
3232
return m_planes;
3333
}
3434

35+
const MT_Matrix4x4& SG_Frustum::GetMatrix() const
36+
{
37+
return m_matrix;
38+
}
39+
3540
SG_Frustum::TestType SG_Frustum::PointInsideFrustum(const MT_Vector3& point) const
3641
{
3742
for (const MT_Vector4& plane : m_planes) {

source/gameengine/SceneGraph/SG_Frustum.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class SG_Frustum
2727
~SG_Frustum() = default;
2828

2929
const std::array<MT_Vector4, 6>& GetPlanes() const;
30+
const MT_Matrix4x4& GetMatrix() const;
3031

3132
TestType PointInsideFrustum(const MT_Vector3& point) const;
3233
TestType SphereInsideFrustum(const MT_Vector3& center, float radius) const;

0 commit comments

Comments
 (0)