[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19733] trunk/blender/source/gameengine: BGE MouseFocusSensor
Campbell Barton
ideasman42 at gmail.com
Wed Apr 15 15:50:56 CEST 2009
Revision: 19733
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19733
Author: campbellbarton
Date: 2009-04-15 15:50:56 +0200 (Wed, 15 Apr 2009)
Log Message:
-----------
BGE MouseFocusSensor
- fix for multiple viewpors broke single viewport (both work now)
- python could get uninitialized values from m_prevTargetPoint and m_prevSourcePoint
- getting the RayDirection for python could crash blender trying to normalize a zero length vector.
- added python attributes
- removed unused canvas from the MouseFocusSensor class
Modified Paths:
--------------
trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.cpp
trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.h
trunk/blender/source/gameengine/PyDoc/KX_MouseFocusSensor.py
Modified: trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp 2009-04-15 13:11:08 UTC (rev 19732)
+++ trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp 2009-04-15 13:50:56 UTC (rev 19733)
@@ -509,7 +509,6 @@
starty,
keytype,
trackfocus,
- canvas,
kxscene,
kxengine,
gameobj);
Modified: trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.cpp 2009-04-15 13:11:08 UTC (rev 19732)
+++ trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.cpp 2009-04-15 13:50:56 UTC (rev 19733)
@@ -61,14 +61,12 @@
int starty,
short int mousemode,
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_kxengine(kxengine)
{
@@ -81,6 +79,11 @@
m_positive_event = false;
m_hitObject = 0;
m_reset = true;
+
+ m_hitPosition.setValue(0,0,0);
+ m_prevTargetPoint.setValue(0,0,0);
+ m_prevSourcePoint.setValue(0,0,0);
+ m_hitNormal.setValue(0,0,1);
}
bool KX_MouseFocusSensor::Evaluate(CValue* event)
@@ -150,12 +153,8 @@
-bool KX_MouseFocusSensor::ParentObjectHasFocus()
+bool KX_MouseFocusSensor::ParentObjectHasFocusCamera(KX_Camera *cam)
{
- m_hitObject = 0;
- m_hitPosition.setValue(0,0,0);
- m_hitNormal.setValue(1,0,0);
-
/* All screen handling in the gameengine is done by GL,
* specifically the model/view and projection parts. The viewport
* part is in the creator.
@@ -187,12 +186,8 @@
* = 1.0 - 2(y_blender - y_lb)/height
*
* */
+
- MT_Vector4 frompoint;
- MT_Vector4 topoint;
-
- bool result = false;
-
/* Because we don't want to worry about resize events, camera
* changes and all that crap, we just determine this over and
* over. Stop whining. We have lots of other calculations to do
@@ -200,107 +195,140 @@
* canvas, the test is irrelevant. The 1.0 makes sure the
* calculations don't bomb. Maybe we should explicitly guard for
* division by 0.0...*/
- list<class KX_Camera*>* cameras = m_kxscene->GetCameras();
- // Draw the scene once for each camera with an enabled viewport
- list<KX_Camera*>::iterator it = cameras->begin();
- while(it != cameras->end())
+ RAS_Rect area, viewport;
+ m_kxengine->GetSceneViewport(m_kxscene, cam, area, viewport);
+
+ /* Check if the mouse is in the viewport */
+ if (( m_x < viewport.m_x2 && // less then right
+ m_x > viewport.m_x1 && // more then then left
+ m_y < viewport.m_y2 && // below top
+ m_y > viewport.m_y1) == 0) // above bottom
{
- if((*it)->GetViewport())
- {
- KX_Camera* cam= (*it);
-
- /* 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 */
+ return false;
+ }
- RAS_Rect area, viewport;
- m_kxengine->GetSceneViewport(m_kxscene, cam, area, viewport);
-
- /* Check if the mouse is in the viewport */
- if ( m_x < viewport.m_x2 && // less then right
- m_x > viewport.m_x1 && // more then then left
- m_y < viewport.m_y2 && // below top
- m_y > viewport.m_y1) // above bottom
- {
- float height = float(viewport.m_y2 - viewport.m_y1 + 1);
- float width = float(viewport.m_x2 - viewport.m_x1 + 1);
-
- float x_lb = float(viewport.m_x1);
- float y_lb = float(viewport.m_y1);
+ float height = float(viewport.m_y2 - viewport.m_y1 + 1);
+ float width = float(viewport.m_x2 - viewport.m_x1 + 1);
+
+ float x_lb = float(viewport.m_x1);
+ float y_lb = float(viewport.m_y1);
- /* There's some strangeness I don't fully get here... These values
- * _should_ be wrong! - see from point Z values */
-
-
- /* build the from and to point in normalized device coordinates
- * Looks like normailized device coordinates are [-1,1] in x [-1,1] in y
- * [0,-1] in z
- *
- * The actual z coordinates used don't have to be exact just infront and
- * behind of the near and far clip planes.
- */
- frompoint.setValue( (2 * (m_x-x_lb) / width) - 1.0,
- 1.0 - (2 * (m_y - y_lb) / height),
- 0.0, /* nearclip, see above comments */
- 1.0 );
-
- topoint.setValue( (2 * (m_x-x_lb) / width) - 1.0,
- 1.0 - (2 * (m_y-y_lb) / height),
- 1.0, /* farclip, see above comments */
- 1.0 );
+ MT_Vector4 frompoint;
+ MT_Vector4 topoint;
+
+ /* There's some strangeness I don't fully get here... These values
+ * _should_ be wrong! - see from point Z values */
+
+
+ /* build the from and to point in normalized device coordinates
+ * Looks like normailized device coordinates are [-1,1] in x [-1,1] in y
+ * [0,-1] in z
+ *
+ * The actual z coordinates used don't have to be exact just infront and
+ * behind of the near and far clip planes.
+ */
+ frompoint.setValue( (2 * (m_x-x_lb) / width) - 1.0,
+ 1.0 - (2 * (m_y - y_lb) / height),
+ 0.0, /* nearclip, see above comments */
+ 1.0 );
+
+ topoint.setValue( (2 * (m_x-x_lb) / width) - 1.0,
+ 1.0 - (2 * (m_y-y_lb) / height),
+ 1.0, /* farclip, see above comments */
+ 1.0 );
- /* camera to world */
- MT_Transform wcs_camcs_tranform = cam->GetWorldToCamera();
- if (!cam->GetCameraData()->m_perspective)
- wcs_camcs_tranform.getOrigin()[2] *= 100.0;
- MT_Transform cams_wcs_transform;
- cams_wcs_transform.invert(wcs_camcs_tranform);
-
- MT_Matrix4x4 camcs_wcs_matrix = MT_Matrix4x4(cams_wcs_transform);
+ /* camera to world */
+ MT_Transform wcs_camcs_tranform = cam->GetWorldToCamera();
+ if (!cam->GetCameraData()->m_perspective)
+ wcs_camcs_tranform.getOrigin()[2] *= 100.0;
+ MT_Transform cams_wcs_transform;
+ cams_wcs_transform.invert(wcs_camcs_tranform);
+
+ MT_Matrix4x4 camcs_wcs_matrix = MT_Matrix4x4(cams_wcs_transform);
- /* badly defined, the first time round.... I wonder why... I might
- * want to guard against floating point errors here.*/
- MT_Matrix4x4 clip_camcs_matrix = MT_Matrix4x4(cam->GetProjectionMatrix());
- clip_camcs_matrix.invert();
+ /* badly defined, the first time round.... I wonder why... I might
+ * want to guard against floating point errors here.*/
+ MT_Matrix4x4 clip_camcs_matrix = MT_Matrix4x4(cam->GetProjectionMatrix());
+ clip_camcs_matrix.invert();
- /* shoot-points: clip to cam to wcs . win to clip was already done.*/
- frompoint = clip_camcs_matrix * frompoint;
- topoint = clip_camcs_matrix * topoint;
- frompoint = camcs_wcs_matrix * frompoint;
- topoint = camcs_wcs_matrix * topoint;
-
- /* from hom wcs to 3d wcs: */
- m_prevSourcePoint.setValue( frompoint[0]/frompoint[3],
- frompoint[1]/frompoint[3],
- frompoint[2]/frompoint[3]);
-
- m_prevTargetPoint.setValue( topoint[0]/topoint[3],
- topoint[1]/topoint[3],
- topoint[2]/topoint[3]);
-
- /* 2. Get the object from PhysicsEnvironment */
- /* Shoot! Beware that the first argument here is an
- * ignore-object. We don't ignore anything... */
- KX_IPhysicsController* physics_controller = cam->GetPhysicsController();
- PHY_IPhysicsEnvironment* physics_environment = m_kxscene->GetPhysicsEnvironment();
+ /* shoot-points: clip to cam to wcs . win to clip was already done.*/
+ frompoint = clip_camcs_matrix * frompoint;
+ topoint = clip_camcs_matrix * topoint;
+ frompoint = camcs_wcs_matrix * frompoint;
+ topoint = camcs_wcs_matrix * topoint;
+
+ /* from hom wcs to 3d wcs: */
+ m_prevSourcePoint.setValue( frompoint[0]/frompoint[3],
+ frompoint[1]/frompoint[3],
+ frompoint[2]/frompoint[3]);
+
+ m_prevTargetPoint.setValue( topoint[0]/topoint[3],
+ topoint[1]/topoint[3],
+ topoint[2]/topoint[3]);
+
+ /* 2. Get the object from PhysicsEnvironment */
+ /* Shoot! Beware that the first argument here is an
+ * ignore-object. We don't ignore anything... */
+ KX_IPhysicsController* physics_controller = cam->GetPhysicsController();
+ PHY_IPhysicsEnvironment* physics_environment = m_kxscene->GetPhysicsEnvironment();
- KX_RayCast::Callback<KX_MouseFocusSensor> callback(this,physics_controller);
-
- KX_RayCast::RayTest(physics_environment, m_prevSourcePoint, m_prevTargetPoint, callback);
-
- if (m_hitObject) {
- result= true;
- break;
- }
- }
- }
+ KX_RayCast::Callback<KX_MouseFocusSensor> callback(this,physics_controller);
+
+ KX_RayCast::RayTest(physics_environment, m_prevSourcePoint, m_prevTargetPoint, callback);
+
+ if (m_hitObject)
+ return true;
+
+ return false;
+}
+
+bool KX_MouseFocusSensor::ParentObjectHasFocus()
+{
+ m_hitObject = 0;
+ m_hitPosition.setValue(0,0,0);
+ m_hitNormal.setValue(1,0,0);
+
+ KX_Camera *cam= m_kxscene->GetActiveCamera();
+
+ if(ParentObjectHasFocusCamera(cam))
+ return true;
+
+ list<class KX_Camera*>* cameras = m_kxscene->GetCameras();
+ list<KX_Camera*>::iterator it = cameras->begin();
+
+ while(it != cameras->end())
+ {
+ if(((*it) != cam) && (*it)->GetViewport())
+ if (ParentObjectHasFocusCamera(*it))
+ return true;
+
it++;
}
- return result;
+ return false;
}
+const MT_Point3& KX_MouseFocusSensor::RaySource() const
+{
+ return m_prevSourcePoint;
+}
+
+const MT_Point3& KX_MouseFocusSensor::RayTarget() const
+{
+ return m_prevTargetPoint;
+}
+
+const MT_Point3& KX_MouseFocusSensor::HitPosition() const
+{
+ return m_hitPosition;
+}
+
+const MT_Vector3& KX_MouseFocusSensor::HitNormal() const
+{
+ return m_hitNormal;
+}
+
/* ------------------------------------------------------------------------- */
/* Python functions */
/* ------------------------------------------------------------------------- */
@@ -342,11 +370,16 @@
{"getHitNormal",(PyCFunction) KX_MouseFocusSensor::sPyGetHitNormal,METH_NOARGS, (PY_METHODCHAR)GetHitNormal_doc},
{"getRayDirection",(PyCFunction) KX_MouseFocusSensor::sPyGetRayDirection,METH_NOARGS, (PY_METHODCHAR)GetRayDirection_doc},
-
{NULL,NULL} //Sentinel
};
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list