4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 * Ketsji scene. Holds references to all scene data.
39 #pragma warning (disable : 4786)
42 #include "MT_assert.h"
44 #include "KX_KetsjiEngine.h"
45 #include "RAS_IPolygonMaterial.h"
47 #include "ListValue.h"
48 #include "SCA_LogicManager.h"
49 #include "SCA_TimeEventManager.h"
50 #include "SCA_AlwaysEventManager.h"
51 #include "SCA_RandomEventManager.h"
52 #include "KX_RayEventManager.h"
53 #include "KX_TouchEventManager.h"
54 #include "SCA_KeyboardManager.h"
55 #include "SCA_MouseManager.h"
56 #include "SCA_PropertyEventManager.h"
57 #include "KX_Camera.h"
58 #include "SCA_JoystickManager.h"
60 #include "RAS_MeshObject.h"
61 #include "RAS_IRasterizer.h"
62 #include "RAS_BucketManager.h"
64 #include "FloatValue.h"
65 #include "SCA_IController.h"
66 #include "SCA_IActuator.h"
68 #include "SYS_System.h"
69 #include "SG_Controller.h"
70 #include "SG_IObject.h"
73 #include "KX_SG_NodeRelationships.h"
75 #include "KX_NetworkEventManager.h"
76 #include "NG_NetworkScene.h"
77 #include "PHY_IPhysicsEnvironment.h"
78 #include "KX_IPhysicsController.h"
81 #include "SumoPhysicsEnvironment.h"
83 void* KX_SceneReplicationFunc(SG_IObject* node,void* gameobj,void* scene)
85 KX_GameObject* replica = ((KX_Scene*)scene)->AddNodeReplicaObject(node,(KX_GameObject*)gameobj);
87 return (void*)replica;
90 void* KX_SceneDestructionFunc(SG_IObject* node,void* gameobj,void* scene)
92 ((KX_Scene*)scene)->RemoveNodeDestructObject(node,(KX_GameObject*)gameobj);
97 SG_Callbacks KX_Scene::m_callbacks = SG_Callbacks(KX_SceneReplicationFunc,KX_SceneDestructionFunc,KX_GameObject::UpdateTransformFunc);
99 // temporarily var until there is a button in the userinterface
100 // (defined in KX_PythonInit.cpp)
101 extern bool gUseVisibilityTemp;
103 KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
104 class SCA_IInputDevice* mousedevice,
105 class NG_NetworkDeviceInterface *ndi,
106 class SND_IAudioDevice* adi,
107 const STR_String& sceneName):
108 PyObjectPlus(&KX_Scene::Type),
111 m_physicsEnvironment(0),
112 m_sceneName(sceneName),
114 m_networkDeviceInterface(ndi),
115 m_active_camera(NULL),
116 m_ueberExecutionPriority(0)
120 m_activity_culling = false;
122 m_isclearingZbuffer = true;
123 m_tempObjectList = new CListValue();
124 m_objectlist = new CListValue();
125 m_parentlist = new CListValue();
126 m_lightlist= new CListValue();
127 m_euthanasyobjects = new CListValue();
129 m_logicmgr = new SCA_LogicManager();
131 m_timemgr = new SCA_TimeEventManager(m_logicmgr);
132 m_keyboardmgr = new SCA_KeyboardManager(m_logicmgr,keyboarddevice);
133 m_mousemgr = new SCA_MouseManager(m_logicmgr,mousedevice);
135 SCA_AlwaysEventManager* alwaysmgr = new SCA_AlwaysEventManager(m_logicmgr);
136 SCA_PropertyEventManager* propmgr = new SCA_PropertyEventManager(m_logicmgr);
137 SCA_RandomEventManager* rndmgr = new SCA_RandomEventManager(m_logicmgr);
138 KX_RayEventManager* raymgr = new KX_RayEventManager(m_logicmgr);
140 KX_NetworkEventManager* netmgr = new KX_NetworkEventManager(m_logicmgr, ndi);
142 SCA_JoystickManager *joymgr = new SCA_JoystickManager(m_logicmgr);
144 m_logicmgr->RegisterEventManager(alwaysmgr);
145 m_logicmgr->RegisterEventManager(propmgr);
146 m_logicmgr->RegisterEventManager(m_keyboardmgr);
147 m_logicmgr->RegisterEventManager(m_mousemgr);
148 m_logicmgr->RegisterEventManager(m_timemgr);
149 m_logicmgr->RegisterEventManager(rndmgr);
150 m_logicmgr->RegisterEventManager(raymgr);
151 m_logicmgr->RegisterEventManager(netmgr);
152 m_logicmgr->RegisterEventManager(joymgr);
154 m_soundScene = new SND_Scene(adi);
155 MT_assert (m_networkDeviceInterface != NULL);
156 m_networkScene = new NG_NetworkScene(m_networkDeviceInterface);
160 m_bucketmanager=new RAS_BucketManager();
162 m_canvasDesignWidth = 0;
163 m_canvasDesignHeight = 0;
165 m_attrlist = PyDict_New(); /* new ref */
170 KX_Scene::~KX_Scene()
173 // int numobj = m_objectlist->GetCount();
175 //int numrootobjects = GetRootParentList()->GetCount();
176 for (int i = 0; i < GetRootParentList()->GetCount(); i++)
178 KX_GameObject* parentobj = (KX_GameObject*) GetRootParentList()->GetValue(i);
179 this->RemoveObject(parentobj);
183 m_objectlist->Release();
186 m_parentlist->Release();
189 m_lightlist->Release();
191 if (m_tempObjectList)
192 m_tempObjectList->Release();
194 if (m_euthanasyobjects)
195 m_euthanasyobjects->Release();
200 if (m_physicsEnvironment)
201 delete m_physicsEnvironment;
207 delete m_networkScene;
211 delete m_bucketmanager;
213 Py_DECREF(m_attrlist);
219 void KX_Scene::SetProjectionMatrix(MT_CmMatrix4x4& pmat)
221 m_projectionmat = pmat;
226 RAS_BucketManager* KX_Scene::GetBucketManager()
228 return m_bucketmanager;
233 CListValue* KX_Scene::GetObjectList()
240 CListValue* KX_Scene::GetRootParentList()
247 CListValue* KX_Scene::GetLightList()
252 SCA_LogicManager* KX_Scene::GetLogicManager()
257 SCA_TimeEventManager* KX_Scene::GetTimeEventManager()
265 void KX_Scene::SetFramingType(RAS_FrameSettings & frame_settings)
267 m_frame_settings = frame_settings;
271 * Return a const reference to the framing
272 * type set by the above call.
273 * The contents are not guarenteed to be sensible
274 * if you don't call the above function.
276 const RAS_FrameSettings& KX_Scene::GetFramingType() const
278 return m_frame_settings;
284 * Store the current scene's viewport on the
285 * game engine canvas.
287 void KX_Scene::SetSceneViewport(const RAS_Rect &viewport)
289 m_viewport = viewport;
294 const RAS_Rect& KX_Scene::GetSceneViewport() const
301 void KX_Scene::SetWorldInfo(class KX_WorldInfo* worldinfo)
303 m_worldinfo = worldinfo;
308 class KX_WorldInfo* KX_Scene::GetWorldInfo()
315 SND_Scene* KX_Scene::GetSoundScene()
320 const STR_String& KX_Scene::GetName()
326 void KX_Scene::Suspend()
331 void KX_Scene::Resume()
336 void KX_Scene::SetActivityCulling(bool b)
338 m_activity_culling = b;
341 bool KX_Scene::IsSuspended()
346 bool KX_Scene::IsClearingZBuffer()
348 return m_isclearingZbuffer;
351 void KX_Scene::EnableZBufferClearing(bool isclearingZbuffer)
353 m_isclearingZbuffer = isclearingZbuffer;
356 void KX_Scene::RemoveNodeDestructObject(class SG_IObject* node,class CValue* gameobj)
358 KX_GameObject* orgobj = (KX_GameObject*)gameobj;
359 NewRemoveObject(orgobj);
365 KX_GameObject* KX_Scene::AddNodeReplicaObject(class SG_IObject* node, class CValue* gameobj)
367 KX_GameObject* orgobj = (KX_GameObject*)gameobj;
368 KX_GameObject* newobj = (KX_GameObject*)orgobj->GetReplica();
369 m_map_gameobject_to_replica.insert(orgobj, newobj);
371 // also register 'timers' (time properties) of the replica
372 int numprops = newobj->GetPropertyCount();
374 for (int i = 0; i < numprops; i++)
376 CValue* prop = newobj->GetProperty(i);
378 if (prop->GetProperty("timer"))
379 this->m_timemgr->AddTimeProperty(prop);
384 newobj->SetSGNode((SG_Node*)node);
388 m_rootnode = new SG_Node(newobj,this,KX_Scene::m_callbacks);
390 // this fixes part of the scaling-added object bug
391 SG_Node* orgnode = orgobj->GetSGNode();
392 m_rootnode->SetLocalScale(orgnode->GetLocalScale());
393 m_rootnode->SetLocalPosition(orgnode->GetLocalPosition());
394 m_rootnode->SetLocalOrientation(orgnode->GetLocalOrientation());
396 // define the relationship between this node and it's parent.
397 KX_NormalParentRelation * parent_relation =
398 KX_NormalParentRelation::New();
399 m_rootnode->SetParentRelation(parent_relation);
401 newobj->SetSGNode(m_rootnode);
404 SG_IObject* replicanode = newobj->GetSGNode();
405 SG_Node* rootnode = (replicanode == m_rootnode ? NULL : m_rootnode);
407 replicanode->SetSGClientObject(newobj);
409 // this is the list of object that are send to the graphics pipeline
410 m_objectlist->Add(newobj);
413 // logic cannot be replicated, until the whole hierarchy is replicated.
414 m_logicHierarchicalGameObjects.push_back(newobj);
415 //replicate controllers of this node
416 SGControllerList scenegraphcontrollers = orgobj->GetSGNode()->GetSGControllerList();
417 replicanode->RemoveAllControllers();
418 SGControllerList::iterator cit;
419 //int numcont = scenegraphcontrollers.size();
421 for (cit = scenegraphcontrollers.begin();!(cit==scenegraphcontrollers.end());++cit)
423 // controller replication is quite complicated
424 // only replicate ipo and physics controller for now
426 SG_Controller* replicacontroller = (*cit)->GetReplica((SG_Node*) replicanode);
427 if (replicacontroller)
429 replicacontroller->SetObject(replicanode);
430 replicanode->AddSGController(replicacontroller);
439 // before calling this method KX_Scene::ReplicateLogic(), make sure to
440 // have called 'GameObject::ReParentLogic' for each object this
441 // hierarchy that's because first ALL bricks must exist in the new
442 // replica of the hierarchy in order to make cross-links work properly
444 void KX_Scene::ReplicateLogic(KX_GameObject* newobj)
446 // also relink the controller to sensors/actuators
447 SCA_ControllerList& controllers = newobj->GetControllers();
448 //SCA_SensorList& sensors = newobj->GetSensors();
449 //SCA_ActuatorList& actuators = newobj->GetActuators();
451 for (SCA_ControllerList::iterator itc = controllers.begin(); !(itc==controllers.end());itc++)
453 SCA_IController* cont = (*itc);
454 cont->SetUeberExecutePriority(m_ueberExecutionPriority);
455 vector<SCA_ISensor*> linkedsensors = cont->GetLinkedSensors();
456 vector<SCA_IActuator*> linkedactuators = cont->GetLinkedActuators();
458 // disconnect the sensors and actuators
459 cont->UnlinkAllSensors();
460 cont->UnlinkAllActuators();
462 // now relink each sensor
463 for (vector<SCA_ISensor*>::iterator its = linkedsensors.begin();!(its==linkedsensors.end());its++)
465 SCA_ISensor* oldsensor = (*its);
466 STR_String name = oldsensor->GetName();
467 //find this name in the list
468 SCA_ISensor* newsensor = newobj->FindSensor(name);
472 // relink this newsensor to the controller
473 m_logicmgr->RegisterToSensor(cont,newsensor);
477 // it can be linked somewhere in the hierarchy or...
478 for (vector<KX_GameObject*>::iterator git = m_logicHierarchicalGameObjects.begin();
479 !(git==m_logicHierarchicalGameObjects.end());++git)
481 newsensor = (*git)->FindSensor(name);
488 // relink this newsensor to the controller somewhere else within this
490 m_logicmgr->RegisterToSensor(cont,newsensor);
494 // must be an external sensor, so...
495 m_logicmgr->RegisterToSensor(cont,oldsensor);
500 // now relink each actuator
501 for (vector<SCA_IActuator*>::iterator ita = linkedactuators.begin();!(ita==linkedactuators.end());ita++)
503 SCA_IActuator* oldactuator = (*ita);
504 STR_String name = oldactuator->GetName();
505 //find this name in the list
506 SCA_IActuator* newactuator = newobj->FindActuator(name);
509 // relink this newsensor to the controller
510 m_logicmgr->RegisterToActuator(cont,newactuator);
511 newactuator->SetUeberExecutePriority(m_ueberExecutionPriority);
515 // it can be linked somewhere in the hierarchy or...
516 for (vector<KX_GameObject*>::iterator git = m_logicHierarchicalGameObjects.begin();
517 !(git==m_logicHierarchicalGameObjects.end());++git)
519 newactuator= (*git)->FindActuator(name);
526 // relink this actuator to the controller somewhere else within this
528 m_logicmgr->RegisterToActuator(cont,newactuator);
529 newactuator->SetUeberExecutePriority(m_ueberExecutionPriority);
533 // must be an external actuator, so...
534 m_logicmgr->RegisterToActuator(cont,oldactuator);
543 SCA_IObject* KX_Scene::AddReplicaObject(class CValue* originalobject,
544 class CValue* parentobject,
548 m_logicHierarchicalGameObjects.clear();
549 m_map_gameobject_to_replica.clear();
551 // todo: place a timebomb in the object, for temporarily objects :)
552 // lifespan of zero means 'this object lives forever'
553 KX_GameObject* originalobj = (KX_GameObject*) originalobject;
554 KX_GameObject* parentobj = (KX_GameObject*) parentobject;
556 m_ueberExecutionPriority++;
558 // lets create a replica
559 KX_GameObject* replica = (KX_GameObject*) AddNodeReplicaObject(NULL,originalobj);
563 // add a timebomb to this object
564 // for now, convert between so called frames and realtime
565 m_tempObjectList->Add(replica->AddRef());
566 replica->SetProperty("::timebomb",new CFloatValue(lifespan*0.02));
569 // add to 'rootparent' list (this is the list of top hierarchy objects, updated each frame)
570 m_parentlist->Add(replica->AddRef());
572 // recurse replication into children nodes
574 NodeList& children = originalobj->GetSGNode()->GetSGChildren();
576 replica->GetSGNode()->ClearSGChildren();
577 for (NodeList::iterator childit = children.begin();!(childit==children.end());++childit)
579 SG_Node* orgnode = (*childit);
580 SG_Node* childreplicanode = orgnode->GetSGReplica();
581 replica->GetSGNode()->AddChild(childreplicanode);
584 // relink any pointers as necessary, sort of a temporary solution
585 vector<KX_GameObject*>::iterator git;
586 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
588 (*git)->Relink(&m_map_gameobject_to_replica);
591 // now replicate logic
592 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
594 (*git)->ReParentLogic();
597 // replicate crosslinks etc. between logic bricks
598 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
600 ReplicateLogic((*git));
603 MT_Point3 newpos = ((KX_GameObject*) parentobject)->NodeGetWorldPosition();
604 replica->NodeSetLocalPosition(newpos);
606 MT_Matrix3x3 newori = ((KX_GameObject*) parentobject)->NodeGetWorldOrientation();
607 replica->NodeSetLocalOrientation(newori);
609 // get the rootnode's scale
610 MT_Vector3 newscale = parentobj->GetSGNode()->GetRootSGParent()->GetLocalScale();
612 // set the replica's relative scale with the rootnode's scale
613 replica->NodeSetRelativeScale(newscale);
615 if (replica->GetPhysicsController())
617 replica->GetPhysicsController()->setPosition(newpos);
618 replica->GetPhysicsController()->setOrientation(newori.getRotation());
619 replica->GetPhysicsController()->setScaling(newscale);
622 // here we want to set the relative scale: the rootnode's scale will override all other
623 // scalings, so lets better prepare for it
626 replica->GetSGNode()->UpdateWorldData(0);
627 replica->GetSGNode()->SetBBox(originalobj->GetSGNode()->BBox());
628 replica->GetSGNode()->SetRadius(originalobj->GetSGNode()->Radius());
635 void KX_Scene::RemoveObject(class CValue* gameobj)
637 KX_GameObject* newobj = (KX_GameObject*) gameobj;
639 // first disconnect child from parent
640 SG_Node* node = newobj->GetSGNode();
644 node->DisconnectFromParent();
646 // recursively destruct
653 void KX_Scene::DelayedRemoveObject(class CValue* gameobj)
655 //KX_GameObject* newobj = (KX_GameObject*) gameobj;
656 if (!m_euthanasyobjects->SearchValue(gameobj))
658 m_euthanasyobjects->Add(gameobj->AddRef());
664 void KX_Scene::NewRemoveObject(class CValue* gameobj)
666 KX_GameObject* newobj = (KX_GameObject*) gameobj;
667 //SM_Object* sumoObj = newobj->GetSumoObject();
670 // this->GetSumoScene()->remove(*sumoObj);
672 // remove all sensors/controllers/actuators from logicsystem...
674 SCA_SensorList& sensors = newobj->GetSensors();
675 for (SCA_SensorList::iterator its = sensors.begin();
676 !(its==sensors.end());its++)
678 m_logicmgr->RemoveSensor(*its);
681 SCA_ControllerList& controllers = newobj->GetControllers();
682 for (SCA_ControllerList::iterator itc = controllers.begin();
683 !(itc==controllers.end());itc++)
685 (*itc)->UnlinkAllSensors();
686 (*itc)->UnlinkAllActuators();
689 SCA_ActuatorList& actuators = newobj->GetActuators();
690 for (SCA_ActuatorList::iterator ita = actuators.begin();
691 !(ita==actuators.end());ita++)
693 m_logicmgr->RemoveDestroyedActuator(*ita);
696 // now remove the timer properties from the time manager
697 int numprops = newobj->GetPropertyCount();
699 for (int i = 0; i < numprops; i++)
701 CValue* propval = newobj->GetProperty(i);
702 if (propval->GetProperty("timer"))
704 m_timemgr->RemoveTimeProperty(propval);
708 newobj->RemoveMeshes();
709 if (m_objectlist->RemoveValue(newobj))
711 if (m_tempObjectList->RemoveValue(newobj))
713 if (m_parentlist->RemoveValue(newobj))
715 if (m_euthanasyobjects->RemoveValue(newobj))
718 if (newobj == m_active_camera)
720 m_active_camera = NULL;
726 void KX_Scene::ReplaceMesh(class CValue* gameobj,void* meshobj)
728 KX_GameObject* newobj = (KX_GameObject*) gameobj;
729 newobj->RemoveMeshes();
730 newobj->AddMesh((RAS_MeshObject*)meshobj);
736 MT_CmMatrix4x4& KX_Scene::GetViewMatrix()
738 MT_Scalar cammat[16];
739 m_active_camera->GetWorldToCamera().getValue(cammat);
746 MT_CmMatrix4x4& KX_Scene::GetProjectionMatrix()
748 return m_projectionmat;
752 KX_Camera* KX_Scene::FindCamera(KX_Camera* cam)
754 set<KX_Camera*>::iterator it = m_cameras.begin();
756 while ( (it != m_cameras.end())
757 && ((*it) != cam) ) {
761 return ((it == m_cameras.end()) ? NULL : (*it));
765 KX_Camera* KX_Scene::FindCamera(STR_String& name)
767 set<KX_Camera*>::iterator it = m_cameras.begin();
769 while ( (it != m_cameras.end())
770 && ((*it)->GetName() != name) ) {
774 return ((it == m_cameras.end()) ? NULL : (*it));
777 void KX_Scene::AddCamera(KX_Camera* cam)
779 m_cameras.insert(cam);
782 KX_Camera* KX_Scene::GetActiveCamera()
784 // NULL if not defined
785 return m_active_camera;
789 void KX_Scene::SetActiveCamera(KX_Camera* cam)
791 // only set if the cam is in the active list? Or add it otherwise?
792 if (!FindCamera(cam)){
794 if (cam) std::cout << "Added cam " << cam->GetName() << std::endl;
797 m_active_camera = cam;
802 void KX_Scene::UpdateMeshTransformations()
804 // do this incrementally in the future
805 for (int i = 0; i < m_objectlist->GetCount(); i++)
807 KX_GameObject* gameobj = (KX_GameObject*)m_objectlist->GetValue(i);
808 gameobj->GetOpenGLMatrix();
809 // gameobj->UpdateNonDynas();
813 void KX_Scene::MarkVisible(SG_Tree *node, RAS_IRasterizer* rasty)
815 int intersect = KX_Camera::INTERSECT;
816 KX_GameObject *gameobj = node->Client()?(KX_GameObject*) node->Client()->GetSGClientObject():NULL;
817 bool dotest = (gameobj && gameobj->GetVisible()) || node->Left() || node->Right();
819 /* If the camera is inside the box, assume intersect. */
820 if (dotest && !node->inside(GetActiveCamera()->NodeGetWorldPosition()))
822 MT_Scalar radius = node->Radius();
823 MT_Point3 centre = node->Centre();
825 intersect = GetActiveCamera()->SphereInsideFrustum(centre, radius);
827 if (intersect == KX_Camera::INTERSECT)
831 intersect = GetActiveCamera()->BoxInsideFrustum(box);
837 case KX_Camera::OUTSIDE:
838 MarkSubTreeVisible(node, rasty, false);
840 case KX_Camera::INTERSECT:
842 MarkVisible(rasty, gameobj);
844 MarkVisible(node->Left(), rasty);
846 MarkVisible(node->Right(), rasty);
848 case KX_Camera::INSIDE:
849 MarkSubTreeVisible(node, rasty, true);
854 void KX_Scene::MarkSubTreeVisible(SG_Tree *node, RAS_IRasterizer* rasty, bool visible)
858 KX_GameObject *gameobj = (KX_GameObject*) node->Client()->GetSGClientObject();
859 if (gameobj->GetVisible())
863 int nummeshes = gameobj->GetMeshCount();
864 MT_Transform t( GetActiveCamera()->GetWorldToCamera() * gameobj->GetSGNode()->GetWorldTransform());
867 for (int m=0;m<nummeshes;m++)
869 // this adds the vertices to the display list
870 (gameobj->GetMesh(m))->SchedulePolygons(t, rasty->GetDrawingMode());
873 gameobj->MarkVisible(visible);
877 MarkSubTreeVisible(node->Left(), rasty, visible);
879 MarkSubTreeVisible(node->Right(), rasty, visible);
882 void KX_Scene::MarkVisible(RAS_IRasterizer* rasty, KX_GameObject* gameobj)
884 // User (Python/Actuator) has forced object invisible...
885 if (!gameobj->GetVisible())
887 // If Frustum culling is off, the object is always visible.
888 bool vis = !GetActiveCamera()->GetFrustumCulling();
890 // If the camera is inside this node, then the object is visible.
893 vis = gameobj->GetSGNode()->inside( GetActiveCamera()->GetCameraLocation() );
896 // Test the object's bound sphere against the view frustum.
899 MT_Vector3 scale = gameobj->GetSGNode()->GetWorldScaling();
900 MT_Scalar radius = fabs(scale[scale.closestAxis()] * gameobj->GetSGNode()->Radius());
901 switch (GetActiveCamera()->SphereInsideFrustum(gameobj->NodeGetWorldPosition(), radius))
903 case KX_Camera::INSIDE:
906 case KX_Camera::OUTSIDE:
909 case KX_Camera::INTERSECT:
910 // Test the object's bound box against the view frustum.
912 gameobj->GetSGNode()->getBBox(box);
913 vis = GetActiveCamera()->BoxInsideFrustum(box) != KX_Camera::OUTSIDE;
920 int nummeshes = gameobj->GetMeshCount();
921 MT_Transform t(GetActiveCamera()->GetWorldToCamera() * gameobj->GetSGNode()->GetWorldTransform());
923 for (int m=0;m<nummeshes;m++)
925 // this adds the vertices to the display list
926 (gameobj->GetMesh(m))->SchedulePolygons(t, rasty->GetDrawingMode());
928 // Visibility/ non-visibility are marked
930 gameobj->MarkVisible();
932 gameobj->MarkVisible(false);
936 void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty)
938 // FIXME: When tree is operational
940 // do this incrementally in the future
941 for (int i = 0; i < m_objectlist->GetCount(); i++)
943 MarkVisible(rasty, static_cast<KX_GameObject*>(m_objectlist->GetValue(i)));
946 if (GetActiveCamera()->GetFrustumCulling())
947 MarkVisible(m_objecttree, rasty);
949 MarkSubTreeVisible(m_objecttree, rasty, true);
954 void KX_Scene::LogicBeginFrame(double curtime)
956 // have a look at temp objects ...
957 int lastobj = m_tempObjectList->GetCount() - 1;
959 for (int i = lastobj; i >= 0; i--)
961 CValue* objval = m_tempObjectList->GetValue(i);
962 CFloatValue* propval = (CFloatValue*) objval->GetProperty("::timebomb");
966 float timeleft = propval->GetNumber() - 1.0/KX_KetsjiEngine::GetTicRate();
970 propval->SetFloat(timeleft);
974 DelayedRemoveObject(objval);
980 // all object is the tempObjectList should have a clock
983 m_logicmgr->BeginFrame(curtime, 1.0/KX_KetsjiEngine::GetTicRate());
988 void KX_Scene::LogicUpdateFrame(double curtime, bool frame)
990 m_logicmgr->UpdateFrame(curtime, frame);
995 void KX_Scene::LogicEndFrame()
997 m_logicmgr->EndFrame();
998 int numobj = m_euthanasyobjects->GetCount();
1000 for (int i = numobj - 1; i >= 0; i--)
1002 KX_GameObject* gameobj = (KX_GameObject*)m_euthanasyobjects->GetValue(i);
1003 this->RemoveObject(gameobj);
1010 * UpdateParents: SceneGraph transformation update.
1012 void KX_Scene::UpdateParents(double curtime)
1014 // int numrootobjects = GetRootParentList()->GetCount();
1016 for (int i=0; i<GetRootParentList()->GetCount(); i++)
1018 KX_GameObject* parentobj = (KX_GameObject*)GetRootParentList()->GetValue(i);
1019 parentobj->NodeUpdateGS(curtime,true);
1025 RAS_MaterialBucket* KX_Scene::FindBucket(class RAS_IPolyMaterial* polymat)
1027 return m_bucketmanager->RAS_BucketManagerFindBucket(polymat);
1032 void KX_Scene::RenderBuckets(const MT_Transform & cameratransform,
1033 class RAS_IRasterizer* rasty,
1034 class RAS_IRenderTools* rendertools)
1036 m_bucketmanager->Renderbuckets(cameratransform,rasty,rendertools);
1041 void KX_Scene::UpdateObjectActivity(void)
1043 if (m_activity_culling) {
1044 /* determine the activity criterium and set objects accordingly */
1047 MT_Point3 camloc = GetActiveCamera()->NodeGetWorldPosition(); //GetCameraLocation();
1049 for (i=0;i<GetObjectList()->GetCount();i++)
1051 KX_GameObject* ob = (KX_GameObject*) GetObjectList()->GetValue(i);
1053 if (!ob->GetIgnoreActivityCulling()) {
1054 /* Simple test: more than 10 away from the camera, count
1055 * Manhattan distance. */
1056 MT_Point3 obpos = ob->NodeGetWorldPosition();
1058 if ( (fabs(camloc[0] - obpos[0]) > m_activity_box_radius)
1059 || (fabs(camloc[1] - obpos[1]) > m_activity_box_radius)
1060 || (fabs(camloc[2] - obpos[2]) > m_activity_box_radius) )
1071 void KX_Scene::SetActivityCullingRadius(float f)
1075 m_activity_box_radius = f;
1078 NG_NetworkDeviceInterface* KX_Scene::GetNetworkDeviceInterface()
1080 return m_networkDeviceInterface;
1083 NG_NetworkScene* KX_Scene::GetNetworkScene()
1085 return m_networkScene;
1088 void KX_Scene::SetNetworkDeviceInterface(NG_NetworkDeviceInterface* newInterface)
1090 m_networkDeviceInterface = newInterface;
1093 void KX_Scene::SetNetworkScene(NG_NetworkScene *newScene)
1095 m_networkScene = newScene;
1099 void KX_Scene::SetGravity(const MT_Vector3& gravity)
1101 GetPhysicsEnvironment()->setGravity(gravity[0],gravity[1],gravity[2]);
1104 void KX_Scene::SetNodeTree(SG_Tree* root)
1106 m_objecttree = root;
1109 void KX_Scene::SetPhysicsEnvironment(class PHY_IPhysicsEnvironment* physEnv)
1111 SumoPhysicsEnvironment *sme = dynamic_cast<SumoPhysicsEnvironment *>(physEnv);
1112 m_physicsEnvironment = physEnv;
1115 KX_TouchEventManager* touchmgr = new KX_TouchEventManager(m_logicmgr, sme->GetSumoScene());
1116 m_logicmgr->RegisterEventManager(touchmgr);
1121 //----------------------------------------------------------------------------
1124 PyMethodDef KX_Scene::Methods[] = {
1125 KX_PYMETHODTABLE(KX_Scene, getLightList),
1126 KX_PYMETHODTABLE(KX_Scene, getObjectList),
1127 KX_PYMETHODTABLE(KX_Scene, getName),
1129 {NULL,NULL} //Sentinel
1132 PyTypeObject KX_Scene::Type = {
1133 PyObject_HEAD_INIT(&PyType_Type)
1144 0, //&cvalue_as_number,
1151 PyParentObject KX_Scene::Parents[] = {
1157 PyObject* KX_Scene::_getattr(const STR_String& attr)
1160 return PyString_FromString(GetName());
1162 if (attr == "active_camera")
1164 KX_Camera *camera = GetActiveCamera();
1166 return (PyObject*) camera;
1169 if (attr == "suspended")
1170 return PyInt_FromLong(m_suspend);
1172 if (attr == "activity_culling")
1173 return PyInt_FromLong(m_activity_culling);
1175 if (attr == "activity_culling_radius")
1176 return PyFloat_FromDouble(m_activity_box_radius);
1178 PyObject* value = PyDict_GetItemString(m_attrlist, const_cast<char *>(attr.ReadPtr()));
1185 _getattr_up(PyObjectPlus);
1188 int KX_Scene::_delattr(const STR_String &attr)
1190 PyDict_DelItemString(m_attrlist, const_cast<char *>(attr.ReadPtr()));
1194 int KX_Scene::_setattr(const STR_String &attr, PyObject *pyvalue)
1197 if (!PyDict_SetItemString(m_attrlist, const_cast<char *>(attr.ReadPtr()), pyvalue))
1200 return PyObjectPlus::_setattr(attr, pyvalue);
1203 KX_PYMETHODDEF_DOC(KX_Scene, getLightList,
1204 "getLightList() -> list [KX_Light]\n"
1205 "Returns a list of all lights in the scene.\n"
1208 m_lightlist->AddRef();
1209 return (PyObject*) m_lightlist;
1212 KX_PYMETHODDEF_DOC(KX_Scene, getObjectList,
1213 "getObjectList() -> list [KX_GameObject]\n"
1214 "Returns a list of all game objects in the scene.\n"
1217 m_objectlist->AddRef();
1218 return (PyObject*) m_objectlist;
1221 KX_PYMETHODDEF_DOC(KX_Scene, getName,
1222 "getName() -> string\n"
1223 "Returns the name of the scene.\n"
1226 return PyString_FromString(GetName());