From e9e08a1d12594eab0e341049fc252ff8578e9333 Mon Sep 17 00:00:00 2001 From: Daniel Stokes Date: Tue, 17 Dec 2013 14:42:47 -0800 Subject: Game Engine: Level of detail support and tools Levels of detail can be added and modified in the object panel. The object panel also contains new tools for generating levels of detail, setting up levels of detail based on object names (useful for importing), and clearing an object's level of detail settings. This is meant as a game engine feature, though the level of details settings can be previewed in the viewport. Reviewed By: moguri, nexyon, brecht Differential Revision: http://developer.blender.org/D109 --- source/gameengine/Ketsji/KX_GameObject.cpp | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'source/gameengine/Ketsji/KX_GameObject.cpp') diff --git a/source/gameengine/Ketsji/KX_GameObject.cpp b/source/gameengine/Ketsji/KX_GameObject.cpp index d3b5a987138..8b7e6667fae 100644 --- a/source/gameengine/Ketsji/KX_GameObject.cpp +++ b/source/gameengine/Ketsji/KX_GameObject.cpp @@ -71,6 +71,8 @@ typedef unsigned long uint_ptr; #include "NG_NetworkScene.h" //Needed for sendMessage() #include "KX_ObstacleSimulation.h" +#include "BKE_object.h" + #include "BL_ActionManager.h" #include "BL_Action.h" @@ -732,6 +734,43 @@ void KX_GameObject::RemoveMeshes() m_meshes.clear(); } +void KX_GameObject::AddLodMesh(RAS_MeshObject* mesh) +{ + m_lodmeshes.push_back(mesh); +} + +void KX_GameObject::UpdateLod(MT_Vector3 &cam_pos) +{ + // Handle dupligroups + if (this->m_pInstanceObjects) { + KX_GameObject * instob; + int count = this->m_pInstanceObjects->GetCount(); + for (int i = 0; i < count; i++) { + instob = (KX_GameObject*)this->m_pInstanceObjects->GetValue(i); + instob->UpdateLod(cam_pos); + } + } + + if (this->m_lodmeshes.empty()) return; + + MT_Vector3 delta = this->NodeGetWorldPosition() - cam_pos; + float distance2 = delta.length2(); + + int level = 0; + Object *bob = this->GetBlenderObject(); + LodLevel *lod = (LodLevel*) bob->lodlevels.first; + for (; lod; lod = lod->next, level++) { + if (!lod->source) level--; + if (!lod->next || lod->next->distance * lod->next->distance > distance2) break; + } + + RAS_MeshObject *mesh = this->m_lodmeshes[level]; + + if (mesh != this->m_meshes[0]) { + this->GetScene()->ReplaceMesh(this, mesh, true, false); + } +} + void KX_GameObject::UpdateTransform() { // HACK: saves function call for dynamic object, they are handled differently -- cgit v1.2.3