src/raytracermodule.cc
branchpyrit
changeset 28 ffe83ca074f3
parent 22 76b7bd51d64a
child 31 b4e09433934a
--- a/src/raytracermodule.cc	Fri Dec 07 16:39:42 2007 +0100
+++ b/src/raytracermodule.cc	Sat Dec 08 12:37:45 2007 +0100
@@ -139,8 +139,9 @@
 		&PyTuple_Type, &TCol))
 		return NULL;
 
-	if (!PyArg_ParseTuple(TCol, "fff", &cr, &cg, &cb))
-		return NULL;
+	if (TCol)
+		if (!PyArg_ParseTuple(TCol, "fff", &cr, &cg, &cb))
+			return NULL;
 
 	v = PyObject_New(MaterialObject, &MaterialType);
 	v->material = new Material(Colour(cr, cg, cb));
@@ -158,6 +159,94 @@
 	return Py_FindMethod(MaterialMethods, self, name);
 }
 
+//=========================== NormalVertex Object ===========================
+
+typedef struct {
+	PyObject_HEAD
+	NormalVertex *nvertex;
+} NormalVertexObject;
+
+static PyObject *NormalVertex_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
+static void NormalVertex_Destructor(PyObject* self);
+static PyObject *NormalVertex_Getattr(PyObject *self, char *name);
+static PyObject *NormalVertex_setNormal(PyObject* self, PyObject* args);
+
+static PyTypeObject NormalVertexType = {
+	PyObject_HEAD_INIT(NULL)
+	0,				/*ob_size*/
+	"NormalVertex",			/*tp_name*/
+	sizeof(NormalVertexObject),		/*tp_basicsize*/
+	0,				/*tp_itemsize*/
+	/* methods */
+	NormalVertex_Destructor,		/*tp_dealloc*/
+	0,				/*tp_print*/
+	NormalVertex_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 NormalVertexMethods[] = {
+	{"setNormal", (PyCFunction)NormalVertex_setNormal, METH_VARARGS, "Set normal of this vertex."},
+	{NULL, NULL}
+};
+
+static PyObject* NormalVertex_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
+{
+	NormalVertexObject *v;
+	static char *kwdlist[] = {"vector", "normal", NULL};
+	PyObject *TVer = NULL;
+	PyObject *TNor = NULL;
+	Float vx, vy, vz, nx=0, ny=0, nz=0;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!|O!", kwdlist,
+		&PyTuple_Type, &TVer, &PyTuple_Type, &TNor))
+		return NULL;
+
+	if (!PyArg_ParseTuple(TVer, "fff", &vx, &vy, &vz))
+		return NULL;
+
+	if (TNor)
+		if (!PyArg_ParseTuple(TNor, "fff", &nx, &ny, &nz))
+			return NULL;
+
+	v = PyObject_New(NormalVertexObject, &NormalVertexType);
+	v->nvertex = new NormalVertex(Vector3(vx, vy, vz), Vector3(nx, ny, nz));
+	return (PyObject*)v;
+}
+
+static void NormalVertex_Destructor(PyObject* self)
+{
+	delete ((NormalVertexObject *)self)->nvertex;
+	PyObject_Del(self);
+}
+
+static PyObject *NormalVertex_Getattr(PyObject *self, char *name)
+{
+	return Py_FindMethod(NormalVertexMethods, self, name);
+}
+
+static PyObject *NormalVertex_setNormal(PyObject* self, PyObject* args)
+{
+	PyObject *TNor = NULL;
+	Float nx, ny, nz;
+
+	if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &TNor))
+		return NULL;
+
+	if (!PyArg_ParseTuple(TNor, "fff", &nx, &ny, &nz))
+		return NULL;
+
+	((NormalVertexObject *)self)->nvertex->setNormal(Vector3(nx,ny,nz).normalize());
+
+	Py_INCREF(Py_None);
+	return Py_None;
+}
+
 //=========================== Sphere Object ===========================
 
 typedef struct {
@@ -298,12 +387,14 @@
 
 typedef struct {
 	PyObject_HEAD
-	Triangle *shape;
+	Triangle *triangle;
 } TriangleObject;
 
 static PyObject *Triangle_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
 static void Triangle_Destructor(PyObject* self);
 static PyObject *Triangle_Getattr(PyObject *self, char *name);
+static PyObject *Triangle_getNormal(PyObject* self, PyObject* args);
+static PyObject *Triangle_setSmooth(PyObject* self, PyObject* args);
 
 static PyTypeObject TriangleType = {
 	PyObject_HEAD_INIT(NULL)
@@ -325,6 +416,8 @@
 };
 
 static PyMethodDef TriangleMethods[] = {
+	{"getNormal", (PyCFunction)Triangle_getNormal, METH_NOARGS, "Get normal of whole triangle."},
+	{"setSmooth", (PyCFunction)Triangle_setSmooth, METH_NOARGS, "Set the triangle smooth."},
 	{NULL, NULL}
 };
 
@@ -333,33 +426,25 @@
 	TriangleObject *v;
 	MaterialObject *material;
 	static char *kwdlist[] = {"A", "B", "C", "material", NULL};
-	PyObject *A = NULL, *B = NULL, *C = NULL;
-	Float ax, ay, az, bx, by, bz, cx, cy, cz;
+	NormalVertexObject *A, *B, *C;
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!O!O!O!", kwdlist,
-		&PyTuple_Type, &A, &PyTuple_Type, &B, &PyTuple_Type, &C,
+		&NormalVertexType, &A, &NormalVertexType, &B, &NormalVertexType, &C,
 		&MaterialType, &material))
 		return NULL;
 
-	if (!PyArg_ParseTuple(A, "fff", &ax, &ay, &az))
-		return NULL;
-
-	if (!PyArg_ParseTuple(B, "fff", &bx, &by, &bz))
-		return NULL;
-
-	if (!PyArg_ParseTuple(C, "fff", &cx, &cy, &cz))
-		return NULL;
-
 	v = PyObject_New(TriangleObject, &TriangleType);
-	v->shape = new Triangle(Vector3(ax, ay, az), Vector3(bx, by, bz),
-		Vector3(cx, cy, cz), material->material);
+	v->triangle = new Triangle(A->nvertex, B->nvertex, C->nvertex, material->material);
 	Py_INCREF(material);
+	Py_INCREF(A);
+	Py_INCREF(B);
+	Py_INCREF(C);
 	return (PyObject*)v;
 }
 
 static void Triangle_Destructor(PyObject* self)
 {
-	delete ((TriangleObject *)self)->shape;
+	delete ((TriangleObject *)self)->triangle;
 	PyObject_Del(self);
 }
 
@@ -368,6 +453,23 @@
 	return Py_FindMethod(TriangleMethods, self, name);
 }
 
+static PyObject* Triangle_getNormal(PyObject* self, PyObject* args)
+{
+	PyObject *obj;
+
+	Vector3 N = ((TriangleObject *)self)->triangle->getNormal();
+
+	obj = Py_BuildValue("(fff)", N.x, N.y, N.z);
+	return obj;
+}
+
+static PyObject* Triangle_setSmooth(PyObject* self, PyObject* args)
+{
+	((TriangleObject *)self)->triangle->setSmooth();
+	Py_INCREF(Py_None);
+	return Py_None;
+}
+
 //=========================== Raytracer Object ===========================
 
 typedef struct {
@@ -532,6 +634,8 @@
 		METH_VARARGS | METH_KEYWORDS, "Light source object constructor."},
 	{"Material", (PyCFunction) Material_Constructor,
 		METH_VARARGS | METH_KEYWORDS, "Material object constructor."},
+	{"NormalVertex", (PyCFunction) NormalVertex_Constructor,
+		METH_VARARGS | METH_KEYWORDS, "NormalVertex object constructor."},
 	{"Sphere", (PyCFunction) Sphere_Constructor,
 		METH_VARARGS | METH_KEYWORDS, "Sphere object constructor."},
 	{"Box", (PyCFunction) Box_Constructor,