Fix for bug #3858: the game engine mouse focus sensor did not work
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 18 Sep 2008 01:46:28 +0000 (01:46 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 18 Sep 2008 01:46:28 +0000 (01:46 +0000)
correct if there was more than one camera. It shoots rays from the
active camera, but used the viewport from whichever camera was drawn
last, now it uses the correct vieport.

source/gameengine/Converter/BL_BlenderDataConversion.cpp
source/gameengine/Converter/KX_ConvertSensors.cpp
source/gameengine/Converter/KX_ConvertSensors.h
source/gameengine/Ketsji/KX_KetsjiEngine.cpp
source/gameengine/Ketsji/KX_KetsjiEngine.h
source/gameengine/Ketsji/KX_MouseFocusSensor.cpp
source/gameengine/Ketsji/KX_MouseFocusSensor.h

index c3992d02eee9d6414bc9f6b6060d442d200f35b3..425e07a257c8cfc592bf1179fc8e78076c1f2bfb 100644 (file)
@@ -2471,7 +2471,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                struct Object* blenderobj = converter->FindBlenderObject(gameobj);
                int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
                bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
                struct Object* blenderobj = converter->FindBlenderObject(gameobj);
                int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
                bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
-               BL_ConvertSensors(blenderobj,gameobj,logicmgr,kxscene,keydev,executePriority,layerMask,isInActiveLayer,canvas,converter);
+               BL_ConvertSensors(blenderobj,gameobj,logicmgr,kxscene,ketsjiEngine,keydev,executePriority,layerMask,isInActiveLayer,canvas,converter);
                // set the init state to all objects
                gameobj->SetInitState((blenderobj->init_state)?blenderobj->init_state:blenderobj->state);
        }
                // set the init state to all objects
                gameobj->SetInitState((blenderobj->init_state)?blenderobj->init_state:blenderobj->state);
        }
index f16855955ffd566ae1f3168cf611a3647afcc625..be0bb8103c06145079d243a8c9431a8c84da07ba 100644 (file)
@@ -92,6 +92,7 @@ void BL_ConvertSensors(struct Object* blenderobject,
                                           class KX_GameObject* gameobj,
                                           SCA_LogicManager* logicmgr,
                                           KX_Scene* kxscene,
                                           class KX_GameObject* gameobj,
                                           SCA_LogicManager* logicmgr,
                                           KX_Scene* kxscene,
+                                          KX_KetsjiEngine* kxengine,
                                           SCA_IInputDevice* keydev,
                                           int & executePriority,
                                           int activeLayerBitInfo,
                                           SCA_IInputDevice* keydev,
                                           int & executePriority,
                                           int activeLayerBitInfo,
@@ -508,6 +509,7 @@ void BL_ConvertSensors(struct Object* blenderobject,
                                                        trackfocus,
                                                        canvas,
                                                        kxscene,
                                                        trackfocus,
                                                        canvas,
                                                        kxscene,
+                                                       kxengine,
                                                        gameobj); 
                                        }
                                } else {
                                                        gameobj); 
                                        }
                                } else {
index 73da51f47f0f9f44e223ebec1887043e38c506f3..b18ffc10a2ab94555979b9d6553e22763fcec5aa 100644 (file)
@@ -33,6 +33,7 @@ void BL_ConvertSensors(struct Object* blenderobject,
           class KX_GameObject* gameobj,
           class SCA_LogicManager* logicmgr,
           class KX_Scene* kxscene,
           class KX_GameObject* gameobj,
           class SCA_LogicManager* logicmgr,
           class KX_Scene* kxscene,
+          class KX_KetsjiEngine* kxengine,
           class SCA_IInputDevice* keydev,
           int & executePriority ,
           int activeLayerBitInfo,
           class SCA_IInputDevice* keydev,
           int & executePriority ,
           int activeLayerBitInfo,
index 888dcf3d01f58a0f71671d9ed07821c7108640db..c7b0d7b3ea34c7177c56493d8391076a6bf23450 100644 (file)
@@ -299,7 +299,7 @@ void KX_KetsjiEngine::ClearFrame()
                list<KX_Camera*>::iterator it;
                for(it = cameras->begin(); it != cameras->end(); it++)
                {
                list<KX_Camera*>::iterator it;
                for(it = cameras->begin(); it != cameras->end(); it++)
                {
-                       SetupViewport(scene, (*it), area, viewport);
+                       GetSceneViewport(scene, (*it), area, viewport);
 
                        if(!doclear) {
                                clearvp = viewport;
 
                        if(!doclear) {
                                clearvp = viewport;
@@ -894,7 +894,7 @@ void KX_KetsjiEngine::SetCameraOverrideClipping(float near, float far)
        m_overrideCamFar = far;
 }
 
        m_overrideCamFar = far;
 }
 
-void KX_KetsjiEngine::SetupViewport(KX_Scene *scene, KX_Camera* cam, RAS_Rect& area, RAS_Rect& viewport)
+void KX_KetsjiEngine::GetSceneViewport(KX_Scene *scene, KX_Camera* cam, RAS_Rect& area, RAS_Rect& viewport)
 {
        // In this function we make sure the rasterizer settings are upto
        // date. We compute the viewport so that logic
 {
        // In this function we make sure the rasterizer settings are upto
        // date. We compute the viewport so that logic
@@ -1001,7 +1001,7 @@ void KX_KetsjiEngine::RenderFrame(KX_Scene* scene, KX_Camera* cam)
        if (!cam)
                return;
 
        if (!cam)
                return;
 
-       SetupViewport(scene, cam, area, viewport);
+       GetSceneViewport(scene, cam, area, viewport);
 
        // store the computed viewport in the scene
        scene->SetSceneViewport(viewport);      
 
        // store the computed viewport in the scene
        scene->SetSceneViewport(viewport);      
index 8fe8731dc05b162372290efe7d6a24fc245eea17..1aa067a99621dd536bc19aa25e39cfa9e01e5efd 100644 (file)
@@ -179,7 +179,6 @@ private:
        /** Blue component of framing bar color. */
        float                                   m_overrideFrameColorB;
 
        /** Blue component of framing bar color. */
        float                                   m_overrideFrameColorB;
 
-       void                                    SetupViewport(KX_Scene *scene, KX_Camera* cam, RAS_Rect& area, RAS_Rect& viewport);
        void                                    RenderFrame(KX_Scene* scene, KX_Camera* cam);
        void                                    PostRenderFrame();
        void                                    RenderDebugProperties();
        void                                    RenderFrame(KX_Scene* scene, KX_Camera* cam);
        void                                    PostRenderFrame();
        void                                    RenderDebugProperties();
@@ -230,6 +229,8 @@ public:
        void                    SuspendScene(const STR_String& scenename);
        void                    ResumeScene(const STR_String& scenename);
 
        void                    SuspendScene(const STR_String& scenename);
        void                    ResumeScene(const STR_String& scenename);
 
+       void                    GetSceneViewport(KX_Scene* scene, KX_Camera* cam, RAS_Rect& area, RAS_Rect& viewport);
+
        void SetDrawType(int drawingtype);
        void SetCameraZoom(float camzoom);
        
        void SetDrawType(int drawingtype);
        void SetCameraZoom(float camzoom);
        
index 3156f543ed57caf0c2297b7f328a5c0c35251b9d..6f76448707c534f3e72f83ad8d1bc8ecee9da6e2 100644 (file)
@@ -62,12 +62,14 @@ KX_MouseFocusSensor::KX_MouseFocusSensor(SCA_MouseManager* eventmgr,
                                                                                 int focusmode,
                                                                                 RAS_ICanvas* canvas,
                                                                                 KX_Scene* kxscene,
                                                                                 int focusmode,
                                                                                 RAS_ICanvas* canvas,
                                                                                 KX_Scene* kxscene,
+                                                                                KX_KetsjiEngine *kxengine,
                                                                                 SCA_IObject* gameobj, 
                                                                                 PyTypeObject* T)
     : SCA_MouseSensor(eventmgr, startx, starty, mousemode, gameobj, T),
          m_focusmode(focusmode),
          m_gp_canvas(canvas),
                                                                                 SCA_IObject* gameobj, 
                                                                                 PyTypeObject* T)
     : SCA_MouseSensor(eventmgr, startx, starty, mousemode, gameobj, T),
          m_focusmode(focusmode),
          m_gp_canvas(canvas),
-         m_kxscene(kxscene)
+         m_kxscene(kxscene),
+         m_kxengine(kxengine)
 {
        Init();
 }
 {
        Init();
 }
@@ -193,11 +195,14 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
         * calculations don't bomb. Maybe we should explicitly guard for
         * division by 0.0...*/
 
         * calculations don't bomb. Maybe we should explicitly guard for
         * division by 0.0...*/
 
-       /**
-        * Get the scenes current viewport.
-        */
+       KX_Camera* cam = m_kxscene->GetActiveCamera();
+
+       /* get the scenes current viewport. we recompute it because there
+        * may be multiple cameras and m_kxscene->GetSceneViewport() only
+        * has the one that was last drawn */
 
 
-       const RAS_Rect & viewport = m_kxscene->GetSceneViewport();
+       RAS_Rect area, viewport;
+       m_kxengine->GetSceneViewport(m_kxscene, cam, area, viewport);
 
        float height = float(viewport.m_y2 - viewport.m_y1 + 1);
        float width  = float(viewport.m_x2 - viewport.m_x1 + 1);
 
        float height = float(viewport.m_y2 - viewport.m_y1 + 1);
        float width  = float(viewport.m_x2 - viewport.m_x1 + 1);
@@ -205,9 +210,9 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
        float x_lb = float(viewport.m_x1);
        float y_lb = float(viewport.m_y1);
 
        float x_lb = float(viewport.m_x1);
        float y_lb = float(viewport.m_y1);
 
-       KX_Camera* cam = m_kxscene->GetActiveCamera();
        /* There's some strangeness I don't fully get here... These values
         * _should_ be wrong! */
        /* There's some strangeness I don't fully get here... These values
         * _should_ be wrong! */
+       
 
        /* old: */
        float nearclip = 0.0;
 
        /* old: */
        float nearclip = 0.0;
index a6cc39d66ebc41ae15cf8d5d1337810d3699d2d3..6731444699b9bbe9dca2dcfd31b67c1a1ddc10f1 100644 (file)
@@ -56,6 +56,7 @@ class KX_MouseFocusSensor : public SCA_MouseSensor
                                                int focusmode,
                                                RAS_ICanvas* canvas,
                                                KX_Scene* kxscene,
                                                int focusmode,
                                                RAS_ICanvas* canvas,
                                                KX_Scene* kxscene,
+                                               KX_KetsjiEngine* kxengine,
                                                SCA_IObject* gameobj,
                                                PyTypeObject* T=&Type );
 
                                                SCA_IObject* gameobj,
                                                PyTypeObject* T=&Type );
 
@@ -142,12 +143,6 @@ class KX_MouseFocusSensor : public SCA_MouseSensor
        MT_Vector3               m_hitNormal;
 
 
        MT_Vector3               m_hitNormal;
 
 
-       /**
-        * Ref to the engine, for retrieving a reference to the current
-        * scene.  */
-       class KX_KetsjiEngine* m_engine;
-
-
        /**
         * The active canvas. The size of this canvas determines a part of
         * the start position of the picking ray.  */
        /**
         * The active canvas. The size of this canvas determines a part of
         * the start position of the picking ray.  */
@@ -158,6 +153,9 @@ class KX_MouseFocusSensor : public SCA_MouseSensor
         * determines a part of the start location of the picking ray.  */
        KX_Scene* m_kxscene;
 
         * determines a part of the start location of the picking ray.  */
        KX_Scene* m_kxscene;
 
+       /**
+        * The KX engine is needed for computing the viewport */
+       KX_KetsjiEngine* m_kxengine;
 };
 
 #endif //__KX_MOUSESENSOR
 };
 
 #endif //__KX_MOUSESENSOR