BGE: Code Cleanup: LOD hysteresis calculation

* Cleanup duplicated code.
* Remove unnecessary "this->"

Reviewers: kupoman, lordloki

Reviewed By: kupoman, lordloki

Differential Revision: https://developer.blender.org/D1293
This commit is contained in:
Thomas Szepe
2015-05-14 09:40:03 +02:00
parent 434086dc53
commit 687f6a9752

View File

@@ -806,68 +806,74 @@ void KX_GameObject::AddLodMesh(RAS_MeshObject* mesh)
m_lodmeshes.push_back(mesh); m_lodmeshes.push_back(mesh);
} }
static float calcHysteresis(KX_Scene *kxscene, LodLevel *lod)
{
float hystvariance = 0.0f;
if (!kxscene->IsActivedLodHysteresis())
return hystvariance;
short hysteresis = 0;
// if exists, LoD level hysteresis will override scene hysteresis
if (lod->next->flags & OB_LOD_USE_HYST)
hysteresis = lod->next->obhysteresis;
else
hysteresis = kxscene->GetLodHysteresisValue();
return hystvariance = MT_abs(lod->next->distance - lod->distance) * hysteresis / 100;
}
void KX_GameObject::UpdateLod(MT_Vector3 &cam_pos) void KX_GameObject::UpdateLod(MT_Vector3 &cam_pos)
{ {
// Handle dupligroups // Handle dupligroups
if (this->m_pInstanceObjects) { if (m_pInstanceObjects) {
KX_GameObject * instob; KX_GameObject *instob;
int count = this->m_pInstanceObjects->GetCount(); int count = m_pInstanceObjects->GetCount();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
instob = (KX_GameObject*)this->m_pInstanceObjects->GetValue(i); instob = (KX_GameObject*)m_pInstanceObjects->GetValue(i);
instob->UpdateLod(cam_pos); instob->UpdateLod(cam_pos);
} }
} }
if (this->m_lodmeshes.empty()) return; if (m_lodmeshes.empty())
return;
MT_Vector3 delta = this->NodeGetWorldPosition() - cam_pos; MT_Vector3 delta = NodeGetWorldPosition() - cam_pos;
float distance2 = delta.length2(); float distance2 = delta.length2();
int level = 0; int level = 0;
Object *bob = this->GetBlenderObject(); float hystvariance = 0.0f;
LodLevel *lod = (LodLevel*) bob->lodlevels.first; Object *bob = GetBlenderObject();
KX_Scene *kxscene = this->GetScene(); LodLevel *lod = (LodLevel *)bob->lodlevels.first;
KX_Scene *kxscene = GetScene();
for (; lod; lod = lod->next, level++) { for (; lod; lod = lod->next, level++) {
if (!lod->source || lod->source->type != OB_MESH) level--; if (!lod->source || lod->source->type != OB_MESH)
if (!lod->next) break; level--;
if (level == (this->m_previousLodLevel) || (level == (this->m_previousLodLevel + 1))) {
short hysteresis = 0; if (!lod->next)
if (kxscene->IsActivedLodHysteresis()) { break;
// if exists, LoD level hysteresis will override scene hysteresis
if (lod->next->flags & OB_LOD_USE_HYST) { if (level == m_previousLodLevel || level == (m_previousLodLevel + 1)) {
hysteresis = lod->next->obhysteresis; hystvariance = calcHysteresis(kxscene, lod);
} float newdistance = lod->next->distance + hystvariance;
else { if (newdistance * newdistance > distance2)
hysteresis = kxscene->GetLodHysteresisValue();
}
}
float hystvariance = MT_abs(lod->next->distance - lod->distance) * hysteresis / 100;
if ((lod->next->distance + hystvariance) * (lod->next->distance + hystvariance) > distance2)
break; break;
} }
else if (level == (this->m_previousLodLevel - 1)) { else if (level == (m_previousLodLevel - 1)) {
short hysteresis = 0; hystvariance = calcHysteresis(kxscene, lod);
if (kxscene->IsActivedLodHysteresis()) { float newdistance = lod->next->distance - hystvariance;
// if exists, LoD level hysteresis will override scene hysteresis if (newdistance * newdistance > distance2)
if (lod->next->flags & OB_LOD_USE_HYST) {
hysteresis = lod->next->obhysteresis;
}
else {
hysteresis = kxscene->GetLodHysteresisValue();
}
}
float hystvariance = MT_abs(lod->next->distance - lod->distance) * hysteresis / 100;
if ((lod->next->distance - hystvariance) * (lod->next->distance - hystvariance) > distance2)
break; break;
} }
} }
RAS_MeshObject *mesh = this->m_lodmeshes[level]; RAS_MeshObject *mesh = m_lodmeshes[level];
this->m_currentLodLevel = level; m_currentLodLevel = level;
if (mesh != this->m_meshes[0]) { if (mesh != m_meshes[0]) {
this->m_previousLodLevel = level; m_previousLodLevel = level;
this->GetScene()->ReplaceMesh(this, mesh, true, false); GetScene()->ReplaceMesh(this, mesh, true, false);
} }
} }