diff options
author | Daniel Stokes <kupomail@gmail.com> | 2013-08-07 09:24:47 +0400 |
---|---|---|
committer | Daniel Stokes <kupomail@gmail.com> | 2013-08-07 09:24:47 +0400 |
commit | 29c8e4512c2deda8e37f503f30c5a89f20e21a45 (patch) | |
tree | 2971a06ddecf8c831f09dd3499441d3fd75807cf /source/gameengine/Ketsji | |
parent | f5c66a3676da2141b240896ad701689f83f5a143 (diff) |
Adding a fov attribute to KX_Camera. This attribute converts the field of view value and uses it to set lens.
Diffstat (limited to 'source/gameengine/Ketsji')
-rw-r--r-- | source/gameengine/Ketsji/KX_Camera.cpp | 30 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_Camera.h | 2 |
2 files changed, 32 insertions, 0 deletions
diff --git a/source/gameengine/Ketsji/KX_Camera.cpp b/source/gameengine/Ketsji/KX_Camera.cpp index 0cd06a7b6eb..73ebf89bea3 100644 --- a/source/gameengine/Ketsji/KX_Camera.cpp +++ b/source/gameengine/Ketsji/KX_Camera.cpp @@ -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("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), @@ -752,6 +753,35 @@ int KX_Camera::pyattr_set_lens(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, 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); diff --git a/source/gameengine/Ketsji/KX_Camera.h b/source/gameengine/Ketsji/KX_Camera.h index f615fefc223..454c4a54ec1 100644 --- a/source/gameengine/Ketsji/KX_Camera.h +++ b/source/gameengine/Ketsji/KX_Camera.h @@ -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_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); |