src/raytracermodule.cc
branchpyrit
changeset 14 fc18ac4833f2
parent 1 e74bf781067e
child 19 4e0955fca797
equal deleted inserted replaced
13:fbd1d2f7d94e 14:fc18ac4833f2
   222 static PyObject *Sphere_Getattr(PyObject *self, char *name)
   222 static PyObject *Sphere_Getattr(PyObject *self, char *name)
   223 {
   223 {
   224 	return Py_FindMethod(SphereMethods, self, name);
   224 	return Py_FindMethod(SphereMethods, self, name);
   225 }
   225 }
   226 
   226 
   227 //=========================== Plane Object ===========================
   227 //=========================== Box Object ===========================
   228 
   228 
   229 typedef struct {
   229 typedef struct {
   230 	PyObject_HEAD
   230 	PyObject_HEAD
   231 	Plane *shape;
   231 	Box *shape;
   232 } PlaneObject;
   232 } BoxObject;
   233 
   233 
   234 static PyObject *Plane_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
   234 static PyObject *Box_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
   235 static void Plane_Destructor(PyObject* self);
   235 static void Box_Destructor(PyObject* self);
   236 static PyObject *Plane_Getattr(PyObject *self, char *name);
   236 static PyObject *Box_Getattr(PyObject *self, char *name);
   237 
   237 
   238 static PyTypeObject PlaneType = {
   238 static PyTypeObject BoxType = {
   239 	PyObject_HEAD_INIT(NULL)
   239 	PyObject_HEAD_INIT(NULL)
   240 	0,				/*ob_size*/
   240 	0,				/*ob_size*/
   241 	"Plane",			/*tp_name*/
   241 	"Box",			/*tp_name*/
   242 	sizeof(PlaneObject),		/*tp_basicsize*/
   242 	sizeof(BoxObject),		/*tp_basicsize*/
   243 	0,				/*tp_itemsize*/
   243 	0,				/*tp_itemsize*/
   244 	/* methods */
   244 	/* methods */
   245 	Plane_Destructor,		/*tp_dealloc*/
   245 	Box_Destructor,		/*tp_dealloc*/
   246 	0,				/*tp_print*/
   246 	0,				/*tp_print*/
   247 	Plane_Getattr,			/*tp_getattr*/
   247 	Box_Getattr,			/*tp_getattr*/
   248 	0,				/*tp_setattr*/
   248 	0,				/*tp_setattr*/
   249 	0,				/*tp_compare*/
   249 	0,				/*tp_compare*/
   250 	0,				/*tp_repr*/
   250 	0,				/*tp_repr*/
   251 	0,				/*tp_as_number*/
   251 	0,				/*tp_as_number*/
   252 	0,				/*tp_as_sequence*/
   252 	0,				/*tp_as_sequence*/
   253 	0,				/*tp_as_mapping*/
   253 	0,				/*tp_as_mapping*/
   254 	0,				/*tp_hash */
   254 	0,				/*tp_hash */
   255 };
   255 };
   256 
   256 
   257 static PyMethodDef PlaneMethods[] = {
   257 static PyMethodDef BoxMethods[] = {
   258 	{NULL, NULL}
   258 	{NULL, NULL}
   259 };
   259 };
   260 
   260 
   261 static PyObject* Plane_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   261 static PyObject* Box_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   262 {
   262 {
   263 	PlaneObject *v;
   263 	BoxObject *v;
   264 	MaterialObject *material;
   264 	MaterialObject *material;
   265 	static char *kwdlist[] = {"normal", "d", "material", NULL};
   265 	static char *kwdlist[] = {"L", "H", "material", NULL};
   266 	PyObject *TNorm = NULL;
   266 	PyObject *TL = NULL;
   267 	float nx, ny, nz, d;
   267 	PyObject *TH = NULL;
   268 
   268 	float lx, ly, lz, hx, hy, hz;
   269 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!fO!", kwdlist,
   269 
   270 		&PyTuple_Type, &TNorm, &d, &MaterialType, &material))
   270 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!O!O!", kwdlist,
   271 		return NULL;
   271 		&PyTuple_Type, &TL, &PyTuple_Type, &TH, &MaterialType, &material))
   272 
   272 		return NULL;
   273 	if (!PyArg_ParseTuple(TNorm, "fff", &nx, &ny, &nz))
   273 
   274 		return NULL;
   274 	if (!PyArg_ParseTuple(TL, "fff", &lx, &ly, &lz))
   275 
   275 		return NULL;
   276 	v = PyObject_New(PlaneObject, &PlaneType);
   276 
   277 	v->shape = new Plane(Vector3(nx, ny, nz), d, material->material);
   277 	if (!PyArg_ParseTuple(TH, "fff", &hx, &hy, &hz))
       
   278 		return NULL;
       
   279 
       
   280 	v = PyObject_New(BoxObject, &BoxType);
       
   281 	v->shape = new Box(Vector3(lx, ly, lz), Vector3(hx, hy, hz), material->material);
   278 	Py_INCREF(material);
   282 	Py_INCREF(material);
   279 	return (PyObject*)v;
   283 	return (PyObject*)v;
   280 }
   284 }
   281 
   285 
   282 static void Plane_Destructor(PyObject* self)
   286 static void Box_Destructor(PyObject* self)
   283 {
   287 {
   284 	delete ((PlaneObject *)self)->shape;
   288 	delete ((BoxObject *)self)->shape;
   285 	PyObject_Del(self);
   289 	PyObject_Del(self);
   286 }
   290 }
   287 
   291 
   288 static PyObject *Plane_Getattr(PyObject *self, char *name)
   292 static PyObject *Box_Getattr(PyObject *self, char *name)
   289 {
   293 {
   290 	return Py_FindMethod(PlaneMethods, self, name);
   294 	return Py_FindMethod(BoxMethods, self, name);
   291 }
   295 }
   292 
   296 
   293 //=========================== Triangle Object ===========================
   297 //=========================== Triangle Object ===========================
   294 
   298 
   295 typedef struct {
   299 typedef struct {
   477 
   481 
   478 	if (!PyArg_ParseTuple(args, "O", &obj))
   482 	if (!PyArg_ParseTuple(args, "O", &obj))
   479 		return NULL;
   483 		return NULL;
   480 
   484 
   481 	((RaytracerObject *)self)->raytracer->addshape(
   485 	((RaytracerObject *)self)->raytracer->addshape(
   482 		((PlaneObject*)obj)->shape);
   486 		((BoxObject*)obj)->shape);
   483 
   487 
   484 	((RaytracerObject *)self)->children->push_back(obj);
   488 	((RaytracerObject *)self)->children->push_back(obj);
   485 	Py_INCREF(obj);
   489 	Py_INCREF(obj);
   486 	Py_INCREF(Py_None);
   490 	Py_INCREF(Py_None);
   487 	return Py_None;
   491 	return Py_None;
   524 		METH_VARARGS | METH_KEYWORDS, "Light source object constructor."},
   528 		METH_VARARGS | METH_KEYWORDS, "Light source object constructor."},
   525 	{"Material", (PyCFunction) Material_Constructor,
   529 	{"Material", (PyCFunction) Material_Constructor,
   526 		METH_VARARGS | METH_KEYWORDS, "Material object constructor."},
   530 		METH_VARARGS | METH_KEYWORDS, "Material object constructor."},
   527 	{"Sphere", (PyCFunction) Sphere_Constructor,
   531 	{"Sphere", (PyCFunction) Sphere_Constructor,
   528 		METH_VARARGS | METH_KEYWORDS, "Sphere object constructor."},
   532 		METH_VARARGS | METH_KEYWORDS, "Sphere object constructor."},
   529 	{"Plane", (PyCFunction) Plane_Constructor,
   533 	{"Box", (PyCFunction) Box_Constructor,
   530 		METH_VARARGS | METH_KEYWORDS, "Plane object constructor."},
   534 		METH_VARARGS | METH_KEYWORDS, "Box object constructor."},
   531 	{"Triangle", (PyCFunction) Triangle_Constructor,
   535 	{"Triangle", (PyCFunction) Triangle_Constructor,
   532 		METH_VARARGS | METH_KEYWORDS, "Triangle object constructor."},
   536 		METH_VARARGS | METH_KEYWORDS, "Triangle object constructor."},
   533 	{NULL, NULL}
   537 	{NULL, NULL}
   534 };
   538 };
   535 
   539