Adding a fov attribute to KX_Camera. This attribute converts the field of view value...
authorDaniel Stokes <kupomail@gmail.com>
Wed, 7 Aug 2013 05:24:47 +0000 (05:24 +0000)
committerDaniel Stokes <kupomail@gmail.com>
Wed, 7 Aug 2013 05:24:47 +0000 (05:24 +0000)
doc/python_api/rst/bge_types/bge.types.KX_Camera.rst
source/gameengine/Ketsji/KX_Camera.cpp
source/gameengine/Ketsji/KX_Camera.h

index baf60ec0c970580d0c9543f47bd5e5340266fcb6..0baba5c33591475a05358ce3add5bf112049776f 100644 (file)
@@ -27,6 +27,12 @@ base class --- :class:`KX_GameObject`
 
       :type: float
 
 
       :type: float
 
+   .. attribute:: fov
+
+      The camera's field of view value.
+
+      :type: float
+
    .. attribute:: ortho_scale
 
       The camera's view scale when in orthographic mode.
    .. attribute:: ortho_scale
 
       The camera's view scale when in orthographic mode.
index 0cd06a7b6ebb6d71fc10228fa429c1f04415d517..73ebf89bea316eebd3371e177a088ae7f59dcaef 100644 (file)
@@ -521,6 +521,7 @@ PyAttributeDef KX_Camera::Attributes[] = {
        KX_PYATTRIBUTE_RW_FUNCTION("perspective", KX_Camera, pyattr_get_perspective, pyattr_set_perspective),
        
        KX_PYATTRIBUTE_RW_FUNCTION("lens",      KX_Camera,      pyattr_get_lens, pyattr_set_lens),
        KX_PYATTRIBUTE_RW_FUNCTION("perspective", KX_Camera, pyattr_get_perspective, pyattr_set_perspective),
        
        KX_PYATTRIBUTE_RW_FUNCTION("lens",      KX_Camera,      pyattr_get_lens, pyattr_set_lens),
+       KX_PYATTRIBUTE_RW_FUNCTION("fov",       KX_Camera,      pyattr_get_fov,  pyattr_set_fov),
        KX_PYATTRIBUTE_RW_FUNCTION("ortho_scale",       KX_Camera,      pyattr_get_ortho_scale, pyattr_set_ortho_scale),
        KX_PYATTRIBUTE_RW_FUNCTION("near",      KX_Camera,      pyattr_get_near, pyattr_set_near),
        KX_PYATTRIBUTE_RW_FUNCTION("far",       KX_Camera,      pyattr_get_far,  pyattr_set_far),
        KX_PYATTRIBUTE_RW_FUNCTION("ortho_scale",       KX_Camera,      pyattr_get_ortho_scale, pyattr_set_ortho_scale),
        KX_PYATTRIBUTE_RW_FUNCTION("near",      KX_Camera,      pyattr_get_near, pyattr_set_near),
        KX_PYATTRIBUTE_RW_FUNCTION("far",       KX_Camera,      pyattr_get_far,  pyattr_set_far),
@@ -752,6 +753,35 @@ int KX_Camera::pyattr_set_lens(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef,
        return PY_SET_ATTR_SUCCESS;
 }
 
        return PY_SET_ATTR_SUCCESS;
 }
 
+PyObject *KX_Camera::pyattr_get_fov(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_Camera* self = static_cast<KX_Camera*>(self_v);
+
+       float lens = self->m_camdata.m_lens;
+       float width = self->m_camdata.m_sensor_x;
+       float fov = 2.0 * atan(0.5 * width / lens);
+
+       return PyFloat_FromDouble(fov * MT_DEGS_PER_RAD);
+}
+
+int KX_Camera::pyattr_set_fov(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_Camera* self = static_cast<KX_Camera*>(self_v);
+       float fov = PyFloat_AsDouble(value);
+       if (fov <= 0.0) {
+               PyErr_SetString(PyExc_AttributeError, "camera.fov = float: KX_Camera, expected a float greater then zero");
+               return PY_SET_ATTR_FAIL;
+       }
+
+       fov *= MT_RADS_PER_DEG;
+       float width = self->m_camdata.m_sensor_x;
+       float lens = width / (2.0 * tan(0.5 * fov));
+       
+       self->m_camdata.m_lens= lens;
+       self->m_set_projection_matrix = false;
+       return PY_SET_ATTR_SUCCESS;
+}
+
 PyObject *KX_Camera::pyattr_get_ortho_scale(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_Camera* self = static_cast<KX_Camera*>(self_v);
 PyObject *KX_Camera::pyattr_get_ortho_scale(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_Camera* self = static_cast<KX_Camera*>(self_v);
index f615fefc22367b27b68773f4bc5560fac7346bde..454c4a54ec1f2018d08a750884e0ecfda0a72e8b 100644 (file)
@@ -298,6 +298,8 @@ public:
 
        static PyObject*        pyattr_get_lens(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_lens(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
 
        static PyObject*        pyattr_get_lens(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_lens(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+       static PyObject*        pyattr_get_fov(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_fov(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_ortho_scale(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_ortho_scale(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_near(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static PyObject*        pyattr_get_ortho_scale(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_ortho_scale(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_near(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);