1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-08-01 02:40:43 +02:00

#8 OBB visibility test optimization

This commit is contained in:
XProger
2016-09-15 01:31:49 +03:00
parent c4c6218604
commit 083cd81cbb

View File

@@ -15,16 +15,18 @@ struct Frustum {
}; };
vec3 pos; vec3 pos;
vec4 planes[MAX_CLIP_PLANES]; vec4 planes[MAX_CLIP_PLANES * 2]; // + buffer for OBB visibility test
int count; int start, count;
#ifdef _DEBUG #ifdef _DEBUG
int dbg; int dbg;
Poly debugPoly; Poly debugPoly;
#endif #endif
void calcPlanes(const mat4 &m) { void calcPlanes(const mat4 &m) {
#ifdef _DEBUG #ifdef _DEBUG
dbg = 0; dbg = 0;
#endif #endif
start = 0;
count = 5; count = 5;
planes[0] = vec4(m.e30 - m.e20, m.e31 - m.e21, m.e32 - m.e22, m.e33 - m.e23); // near planes[0] = vec4(m.e30 - m.e20, m.e31 - m.e21, m.e32 - m.e22, m.e33 - m.e23); // near
planes[1] = vec4(m.e30 - m.e10, m.e31 - m.e11, m.e32 - m.e12, m.e33 - m.e13); // top planes[1] = vec4(m.e30 - m.e10, m.e31 - m.e11, m.e32 - m.e12, m.e33 - m.e13); // top
@@ -104,7 +106,7 @@ struct Frustum {
bool isVisible(const vec3 &min, const vec3 &max) const { bool isVisible(const vec3 &min, const vec3 &max) const {
if (count < 4) return false; if (count < 4) return false;
for (int i = 0; i < count; i++) { for (int i = start; i < start + count; i++) {
const vec3 &n = planes[i].xyz; const vec3 &n = planes[i].xyz;
const float d = -planes[i].w; const float d = -planes[i].w;
@@ -123,21 +125,17 @@ struct Frustum {
// OBB visibility check // OBB visibility check
bool isVisible(const mat4 &matrix, const vec3 &min, const vec3 &max) { bool isVisible(const mat4 &matrix, const vec3 &min, const vec3 &max) {
vec4 origPlanes[MAX_CLIP_PLANES]; start = count;
memcpy(origPlanes, planes, count * sizeof(vec4));
// transform clip planes (relative) // transform clip planes (relative)
mat4 m = matrix.inverse(); mat4 m = matrix.inverse();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
vec4 &p = planes[i]; vec4 &p = planes[i];
vec4 o = m * vec4(p.xyz * (-p.w), 1.0f); vec4 o = m * vec4(p.xyz * (-p.w), 1.0f);
vec4 n = m * vec4(p.xyz, 0.0f); vec4 n = m * vec4(p.xyz, 0.0f);
p.xyz = n.xyz; planes[start + i] = vec4(n.xyz, -n.xyz.dot(o.xyz));
p.w = -o.xyz.dot(n.xyz);
} }
bool visible = isVisible(min, max); bool visible = isVisible(min, max);
start = 0;
memcpy(planes, origPlanes, count * sizeof(vec4));
return visible; return visible;
} }