diff options
-rw-r--r-- | libdimension-python/Camera.c | 10 | ||||
-rw-r--r-- | libdimension-python/Object.c | 20 | ||||
-rw-r--r-- | libdimension-python/Sphere.c | 14 | ||||
-rwxr-xr-x | libdimension-python/tests/demo.py | 10 |
4 files changed, 44 insertions, 10 deletions
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') |