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 c3992d0..425e07a 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;
-               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);
        }
index f168559..be0bb81 100644 (file)
@@ -92,6 +92,7 @@ void BL_ConvertSensors(struct Object* blenderobject,
                                           class KX_GameObject* gameobj,
                                           SCA_LogicManager* logicmgr,
                                           KX_Scene* kxscene,
+                                          KX_KetsjiEngine* kxengine,
                                           SCA_IInputDevice* keydev,
                                           int & executePriority,
                                           int activeLayerBitInfo,
@@ -508,6 +509,7 @@ void BL_ConvertSensors(struct Object* blenderobject,
                                                        trackfocus,
                                                        canvas,
                                                        kxscene,
+                                                       kxengine,
                                                        gameobj); 
                                        }
                                } else {
index 73da51f..b18ffc1 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_KetsjiEngine* kxengine,
           class SCA_IInputDevice* keydev,
           int & executePriority ,
           int activeLayerBitInfo,
index 888dcf3..c7b0d7b 100644 (file)
@@ -299,7 +299,7 @@ void KX_KetsjiEngine::ClearFrame()
                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;
@@ -894,7 +894,7 @@ void KX_KetsjiEngine::SetCameraOverrideClipping(float near, float 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
@@ -1001,7 +1001,7 @@ void KX_KetsjiEngine::RenderFrame(KX_Scene* scene, KX_Camera* cam)
        if (!cam)
                return;
 
-       SetupViewport(scene, cam, area, viewport);
+       GetSceneViewport(scene, cam, area, viewport);
 
        // store the computed viewport in the scene
        scene->SetSceneViewport(viewport);      
index 8fe8731..1aa067a 100644 (file)
@@ -179,7 +179,6 @@ private:
        /** 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();
@@ -230,6 +229,8 @@ public:
        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);
        
index 3156f54..6f76448 100644 (file)
@@ -62,12 +62,14 @@ KX_MouseFocusSensor::KX_MouseFocusSensor(SCA_MouseManager* eventmgr,
                                                                                 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),
-         m_kxscene(kxscene)
+         m_kxscene(kxscene),
+         m_kxengine(kxengine)
 {
        Init();
 }
@@ -193,11 +195,14 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
         * 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);
@@ -205,9 +210,9 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
        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! */
+       
 
        /* old: */
        float nearclip = 0.0;
index a6cc39d..6731444 100644 (file)
@@ -56,6 +56,7 @@ class KX_MouseFocusSensor : public SCA_MouseSensor
                                                int focusmode,
                                                RAS_ICanvas* canvas,
                                                KX_Scene* kxscene,
+                                               KX_KetsjiEngine* kxengine,
                                                SCA_IObject* gameobj,
                                                PyTypeObject* T=&Type );
 
@@ -142,12 +143,6 @@ class KX_MouseFocusSensor : public SCA_MouseSensor
        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.  */
@@ -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;
 
+       /**
+        * The KX engine is needed for computing the viewport */
+       KX_KetsjiEngine* m_kxengine;
 };
 
 #endif //__KX_MOUSESENSOR