From 54585ba35adc6afc46201b746401c5a8a66ab6fa Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 29 May 2011 12:43:00 -0600 Subject: Add transform() to Python Objects. --- libdimension-python/Camera.c | 10 +++++----- libdimension-python/Object.c | 20 ++++++++++++++++++++ libdimension-python/Sphere.c | 14 ++++++++++++++ libdimension-python/tests/demo.py | 10 +++++----- 4 files changed, 44 insertions(+), 10 deletions(-) (limited to 'libdimension-python') diff --git a/libdimension-python/Camera.c b/libdimension-python/Camera.c index 17347d7..286c36d 100644 --- a/libdimension-python/Camera.c +++ b/libdimension-python/Camera.c @@ -46,8 +46,8 @@ dmnsn_py_Camera_initialize(dmnsn_py_Camera *self) static PyObject * dmnsn_py_Camera_transform(dmnsn_py_Camera *self, PyObject *args) { - dmnsn_py_Matrix *matrix; - if (!PyArg_ParseTuple(args, "O!", &dmnsn_py_MatrixType, &matrix)) + dmnsn_py_Matrix *trans; + if (!PyArg_ParseTuple(args, "O!", &dmnsn_py_MatrixType, &trans)) return NULL; if (!self->camera) { @@ -55,9 +55,9 @@ dmnsn_py_Camera_transform(dmnsn_py_Camera *self, PyObject *args) return NULL; } - self->camera->trans = dmnsn_matrix_mul(matrix->m, self->camera->trans); - Py_INCREF(Py_None); - return Py_None; + self->camera->trans = dmnsn_matrix_mul(trans->m, self->camera->trans); + Py_INCREF(self); + return (PyObject *)self; } static PyMethodDef dmnsn_py_Camera_methods[] = { diff --git a/libdimension-python/Object.c b/libdimension-python/Object.c index 5942d3d..b44ff1a 100644 --- a/libdimension-python/Object.c +++ b/libdimension-python/Object.c @@ -43,9 +43,29 @@ dmnsn_py_Object_initialize(dmnsn_py_Object *self) return NULL; } +static PyObject * +dmnsn_py_Object_transform(dmnsn_py_Object *self, PyObject *args) +{ + dmnsn_py_Matrix *trans; + if (!PyArg_ParseTuple(args, "O!", &dmnsn_py_MatrixType, &trans)) + return NULL; + + if (!self->object) { + PyErr_SetString(PyExc_TypeError, "Attempt to transform base Object"); + return NULL; + } + + self->object->trans = dmnsn_matrix_mul(trans->m, self->object->trans); + + Py_INCREF(self); + return (PyObject *)self; +} + static PyMethodDef dmnsn_py_Object_methods[] = { { "initialize", (PyCFunction)dmnsn_py_Object_initialize, METH_NOARGS, "Initialize an object" }, + { "transform", (PyCFunction)dmnsn_py_Object_transform, METH_VARARGS, + "Transform an object" }, { NULL } }; diff --git a/libdimension-python/Sphere.c b/libdimension-python/Sphere.c index c2d5157..c6ec6a6 100644 --- a/libdimension-python/Sphere.c +++ b/libdimension-python/Sphere.c @@ -42,6 +42,20 @@ dmnsn_py_Sphere_init(dmnsn_py_Sphere *self, PyObject *args, PyObject *kwds) static PyObject * dmnsn_py_Sphere_initialize(dmnsn_py_Sphere *self) { + dmnsn_matrix trans = dmnsn_scale_matrix( + dmnsn_new_vector(self->radius, self->radius, self->radius) + ); + trans = dmnsn_matrix_mul(dmnsn_translation_matrix(self->center), trans); + + dmnsn_object *object = self->base.object; + object->trans = dmnsn_matrix_mul(object->trans, trans); + + dmnsn_texture *texture = object->texture; + if (texture) { + dmnsn_matrix trans_inv = dmnsn_matrix_inverse(trans); + texture->trans = dmnsn_matrix_mul(trans_inv, texture->trans); + } + Py_INCREF(Py_None); return Py_None; } diff --git a/libdimension-python/tests/demo.py b/libdimension-python/tests/demo.py index 982fa60..fd4d193 100755 --- a/libdimension-python/tests/demo.py +++ b/libdimension-python/tests/demo.py @@ -39,14 +39,14 @@ camera = PerspectiveCamera(location = (0, 0.25, -4), look_at = Zero) camera.transform(rotate(53*Y)) -objects = [] - -sphere = Sphere(radius = 1, center = Zero) -objects.append(sphere) +objects = [ + Sphere(radius = 1, center = Zero) +] scene = Scene(canvas = canvas, camera = camera, objects = objects) scene.raytrace() -canvas.writePNG('demo.png') +if havePNG: + canvas.writePNG('demo.png') -- cgit v1.2.3