[Bf-blender-cvs] [687f6a9] master: BGE: Code Cleanup: LOD hysteresis calculation

Thomas Szepe noreply at git.blender.org
Thu May 14 09:40:51 CEST 2015


Commit: 687f6a9752fd2009f67dd0b66208777cd203bde1
Author: Thomas Szepe
Date:   Thu May 14 09:40:03 2015 +0200
Branches: master
https://developer.blender.org/rB687f6a9752fd2009f67dd0b66208777cd203bde1

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

===================================================================

M	source/gameengine/Ketsji/KX_GameObject.cpp

===================================================================

diff --git a/source/gameengine/Ketsji/KX_GameObject.cpp b/source/gameengine/Ketsji/KX_GameObject.cpp
index 44d9bfa..14a7324 100644
--- a/source/gameengine/Ketsji/KX_GameObject.cpp
+++ b/source/gameengine/Ketsji/KX_GameObject.cpp
@@ -806,68 +806,74 @@ void KX_GameObject::AddLodMesh(RAS_MeshObject* 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)
 {
 	// Handle dupligroups
-	if (this->m_pInstanceObjects) {
-		KX_GameObject * instob;
-		int count = this->m_pInstanceObjects->GetCount();
+	if (m_pInstanceObjects) {
+		KX_GameObject *instob;
+		int count = m_pInstanceObjects->GetCount();
 		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);
 		}
 	}
 
-	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();
 
 	int level = 0;
-	Object *bob = this->GetBlenderObject();
-	LodLevel *lod = (LodLevel*) bob->lodlevels.first;
-	KX_Scene *kxscene = this->GetScene();
+	float hystvariance = 0.0f;
+	Object *bob = GetBlenderObject();
+	LodLevel *lod = (LodLevel *)bob->lodlevels.first;
+	KX_Scene *kxscene = GetScene();
 
 	for (; lod; lod = lod->next, level++) {
-		if (!lod->source || lod->source->type != OB_MESH) level--;
-		if (!lod->next) break;
-		if (level == (this->m_previousLodLevel) || (level == (this->m_previousLodLevel + 1))) {
-			short hysteresis = 0;
-			if (kxscene->IsActivedLodHysteresis()) {
-				// 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();
-				}
-			}
-			float hystvariance = MT_abs(lod->next->distance - lod->distance) * hysteresis / 100;
-			if ((lod->next->distance + hystvariance) * (lod->next->distance + hystvariance) > distance2)
+		if (!lod->source || lod->source->type != OB_MESH)
+			level--;
+
+		if (!lod->next)
+			break;
+
+		if (level == m_previousLodLevel || level == (m_previousLodLevel + 1)) {
+			hystvariance = calcHysteresis(kxscene, lod);
+			float newdistance = lod->next->distance + hystvariance;
+			if (newdistance * newdistance > distance2)
 				break;
 		}
-		else if (level == (this->m_previousLodLevel - 1)) {
-			short hysteresis = 0;
-			if (kxscene->IsActivedLodHysteresis()) {
-				// 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();
-				}
-			}
-			float hystvariance = MT_abs(lod->next->distance - lod->distance) * hysteresis / 100;
-			if ((lod->next->distance - hystvariance) * (lod->next->distance - hystvariance) > distance2)
+		else if (level == (m_previousLodLevel - 1)) {
+			hystvariance = calcHysteresis(kxscene, lod);
+			float newdistance = lod->next->distance - hystvariance;
+			if (newdistance * newdistance > distance2)
 				break;
 		}
 	}
 
-	RAS_MeshObject *mesh = this->m_lodmeshes[level];
-	this->m_currentLodLevel = level;
-	if (mesh != this->m_meshes[0]) {
-		this->m_previousLodLevel = level;
-		this->GetScene()->ReplaceMesh(this, mesh, true, false);
+	RAS_MeshObject *mesh = m_lodmeshes[level];
+	m_currentLodLevel = level;
+	if (mesh != m_meshes[0]) {
+		m_previousLodLevel = level;
+		GetScene()->ReplaceMesh(this, mesh, true, false);
 	}
 }




More information about the Bf-blender-cvs mailing list