src/raytracermodule.cc
branchpyrit
changeset 59 64e456ab823d
parent 48 a4913301c626
child 60 a23b5089b9c3
--- a/src/raytracermodule.cc	Fri Apr 04 01:55:25 2008 +0200
+++ b/src/raytracermodule.cc	Fri Apr 04 13:54:47 2008 +0200
@@ -113,6 +113,128 @@
 	return Py_None;
 }
 
+//=========================== Camera Object ===========================
+
+typedef struct {
+	PyObject_HEAD
+	Camera *camera;
+} CameraObject;
+
+static PyObject *Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
+static void Camera_Destructor(PyObject* self);
+static PyObject *Camera_Getattr(PyObject *self, char *name);
+static PyObject *Camera_setEye(PyObject* self, PyObject* args);
+static PyObject *Camera_rotate(PyObject* self, PyObject* args);
+
+static PyTypeObject CameraType = {
+	PyObject_HEAD_INIT(NULL)
+	0,				/*ob_size*/
+	"Camera",			/*tp_name*/
+	sizeof(CameraObject),		/*tp_basicsize*/
+	0,				/*tp_itemsize*/
+	/* methods */
+	Camera_Destructor,		/*tp_dealloc*/
+	0,				/*tp_print*/
+	Camera_Getattr,			/*tp_getattr*/
+	0,				/*tp_setattr*/
+	0,				/*tp_compare*/
+	0,				/*tp_repr*/
+	0,				/*tp_as_number*/
+	0,				/*tp_as_sequence*/
+	0,				/*tp_as_mapping*/
+	0,				/*tp_hash */
+};
+
+static PyMethodDef CameraMethods[] = {
+	{"setEye", (PyCFunction)Camera_setEye, METH_VARARGS, "Set eye of the camera."},
+	{"rotate", (PyCFunction)Camera_rotate, METH_VARARGS, "Rotate camera with a quaternion."},
+	{NULL, NULL}
+};
+
+static PyObject* Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
+{
+	CameraObject *v;
+	static char *kwdlist[] = {"eye", "p", "u", "v", NULL};
+	PyObject *TEye = NULL, *Tp = NULL, *Tu = NULL, *Tv = NULL;
+	Float ex=0.0,  ey=0.0, ez=10.0;
+	Float px=0.0,  py=0.0, pz=-1.0;
+	Float ux=-1.0, uy=0.0, uz=0.0;
+	Float vx=0.0,  vy=1.0, vz=0.0;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwd, "|O!O!O!O!", kwdlist,
+		&PyTuple_Type, &TEye, &PyTuple_Type, &Tp, &PyTuple_Type, &Tu, &PyTuple_Type, &Tv))
+		return NULL;
+
+	if (TEye)
+		if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez))
+			return NULL;
+
+	if (Tp)
+		if (!PyArg_ParseTuple(Tp, "fff", &px, &py, &pz))
+			return NULL;
+
+	if (Tu)
+		if (!PyArg_ParseTuple(Tu, "fff", &ux, &uy, &uz))
+			return NULL;
+
+	if (Tv)
+		if (!PyArg_ParseTuple(Tv, "fff", &vx, &vy, &vz))
+			return NULL;
+
+	v = PyObject_New(CameraObject, &CameraType);
+	v->camera = new Camera(Vector3(ex, ey, ez),
+		Vector3(px, py, pz), Vector3(ux, uy, uz), Vector3(vx, vy, vz));
+	return (PyObject*)v;
+}
+
+static void Camera_Destructor(PyObject* self)
+{
+	delete ((CameraObject *)self)->camera;
+	PyObject_Del(self);
+}
+
+static PyObject *Camera_Getattr(PyObject *self, char *name)
+{
+	return Py_FindMethod(CameraMethods, self, name);
+}
+
+
+static PyObject *Camera_setEye(PyObject* self, PyObject* args)
+{
+	PyObject *TEye = NULL;
+	Float ex=0.0,  ey=0.0, ez=10.0;
+
+	if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &TEye))
+		return NULL;
+
+	if (TEye)
+		if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez))
+			return NULL;
+
+	((CameraObject *)self)->camera->setEye(Vector3(ex, ey, ez));
+
+	Py_INCREF(Py_None);
+	return Py_None;
+}
+
+static PyObject *Camera_rotate(PyObject* self, PyObject* args)
+{
+	PyObject *Tq = NULL;
+	Float qa, qb, qc, qd;
+
+	if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &Tq))
+		return NULL;
+
+	if (!PyArg_ParseTuple(Tq, "ffff", &qa, &qb, &qc, &qd))
+		return NULL;
+
+	((CameraObject *)self)->camera->rotate(Quaternion(qa, qb, qc, qd).normalize());
+
+	Py_INCREF(Py_None);
+	return Py_None;
+}
+
+
 //=========================== Material Object ===========================
 
 typedef struct {
@@ -546,6 +668,7 @@
 static void Raytracer_Destructor(PyObject* self);
 static PyObject *Raytracer_Getattr(PyObject *self, char *name);
 static PyObject *Raytracer_render(PyObject* self, PyObject* args);
+static PyObject *Raytracer_setcamera(PyObject* self, PyObject* args);
 static PyObject *Raytracer_addshape(PyObject* self, PyObject* args);
 static PyObject *Raytracer_addlight(PyObject* self, PyObject* args);
 static PyObject *Raytracer_ambientocclusion(PyObject* self, PyObject* args, PyObject *kwd);
@@ -571,6 +694,7 @@
 
 static PyMethodDef RaytracerMethods[] = {
 	{"render", (PyCFunction)Raytracer_render, METH_VARARGS, "Render scene and return image data."},
+	{"setcamera", (PyCFunction)Raytracer_setcamera, METH_VARARGS, "Set camera for the scene."},
 	{"addshape", (PyCFunction)Raytracer_addshape, METH_VARARGS, "Add new shape to scene."},
 	{"addlight", (PyCFunction)Raytracer_addlight, METH_VARARGS, "Add new light source to scene."},
 	{"ambientocclusion", (PyCFunction)Raytracer_ambientocclusion, METH_VARARGS | METH_KEYWORDS,
@@ -647,6 +771,20 @@
 	return o;
 }
 
+static PyObject* Raytracer_setcamera(PyObject* self, PyObject* args)
+{
+	CameraObject *cam;
+
+	if (!PyArg_ParseTuple(args, "O!", &CameraType, &cam))
+		return NULL;
+
+	((RaytracerObject *)self)->raytracer->setCamera(cam->camera);
+
+	Py_INCREF(cam);
+	Py_INCREF(Py_None);
+	return Py_None;
+}
+
 static PyObject* Raytracer_addshape(PyObject* self, PyObject* args)
 {
 	PyObject *obj;
@@ -698,6 +836,8 @@
 		METH_VARARGS, "Raytracer object constructor."},
 	{"Light", (PyCFunction) Light_Constructor,
 		METH_VARARGS | METH_KEYWORDS, "Light source object constructor."},
+	{"Camera", (PyCFunction) Camera_Constructor,
+		METH_VARARGS | METH_KEYWORDS, "Camera object constructor."},
 	{"Material", (PyCFunction) Material_Constructor,
 		METH_VARARGS | METH_KEYWORDS, "Material object constructor."},
 	{"NormalVertex", (PyCFunction) NormalVertex_Constructor,