src/raytracermodule.cc
branchpyrit
changeset 28 ffe83ca074f3
parent 22 76b7bd51d64a
child 31 b4e09433934a
equal deleted inserted replaced
27:e9bb83c2b8b9 28:ffe83ca074f3
   137 
   137 
   138 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "|O!", kwdlist,
   138 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "|O!", kwdlist,
   139 		&PyTuple_Type, &TCol))
   139 		&PyTuple_Type, &TCol))
   140 		return NULL;
   140 		return NULL;
   141 
   141 
   142 	if (!PyArg_ParseTuple(TCol, "fff", &cr, &cg, &cb))
   142 	if (TCol)
   143 		return NULL;
   143 		if (!PyArg_ParseTuple(TCol, "fff", &cr, &cg, &cb))
       
   144 			return NULL;
   144 
   145 
   145 	v = PyObject_New(MaterialObject, &MaterialType);
   146 	v = PyObject_New(MaterialObject, &MaterialType);
   146 	v->material = new Material(Colour(cr, cg, cb));
   147 	v->material = new Material(Colour(cr, cg, cb));
   147 	return (PyObject*)v;
   148 	return (PyObject*)v;
   148 }
   149 }
   154 }
   155 }
   155 
   156 
   156 static PyObject *Material_Getattr(PyObject *self, char *name)
   157 static PyObject *Material_Getattr(PyObject *self, char *name)
   157 {
   158 {
   158 	return Py_FindMethod(MaterialMethods, self, name);
   159 	return Py_FindMethod(MaterialMethods, self, name);
       
   160 }
       
   161 
       
   162 //=========================== NormalVertex Object ===========================
       
   163 
       
   164 typedef struct {
       
   165 	PyObject_HEAD
       
   166 	NormalVertex *nvertex;
       
   167 } NormalVertexObject;
       
   168 
       
   169 static PyObject *NormalVertex_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
       
   170 static void NormalVertex_Destructor(PyObject* self);
       
   171 static PyObject *NormalVertex_Getattr(PyObject *self, char *name);
       
   172 static PyObject *NormalVertex_setNormal(PyObject* self, PyObject* args);
       
   173 
       
   174 static PyTypeObject NormalVertexType = {
       
   175 	PyObject_HEAD_INIT(NULL)
       
   176 	0,				/*ob_size*/
       
   177 	"NormalVertex",			/*tp_name*/
       
   178 	sizeof(NormalVertexObject),		/*tp_basicsize*/
       
   179 	0,				/*tp_itemsize*/
       
   180 	/* methods */
       
   181 	NormalVertex_Destructor,		/*tp_dealloc*/
       
   182 	0,				/*tp_print*/
       
   183 	NormalVertex_Getattr,			/*tp_getattr*/
       
   184 	0,				/*tp_setattr*/
       
   185 	0,				/*tp_compare*/
       
   186 	0,				/*tp_repr*/
       
   187 	0,				/*tp_as_number*/
       
   188 	0,				/*tp_as_sequence*/
       
   189 	0,				/*tp_as_mapping*/
       
   190 	0,				/*tp_hash */
       
   191 };
       
   192 
       
   193 static PyMethodDef NormalVertexMethods[] = {
       
   194 	{"setNormal", (PyCFunction)NormalVertex_setNormal, METH_VARARGS, "Set normal of this vertex."},
       
   195 	{NULL, NULL}
       
   196 };
       
   197 
       
   198 static PyObject* NormalVertex_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
       
   199 {
       
   200 	NormalVertexObject *v;
       
   201 	static char *kwdlist[] = {"vector", "normal", NULL};
       
   202 	PyObject *TVer = NULL;
       
   203 	PyObject *TNor = NULL;
       
   204 	Float vx, vy, vz, nx=0, ny=0, nz=0;
       
   205 
       
   206 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!|O!", kwdlist,
       
   207 		&PyTuple_Type, &TVer, &PyTuple_Type, &TNor))
       
   208 		return NULL;
       
   209 
       
   210 	if (!PyArg_ParseTuple(TVer, "fff", &vx, &vy, &vz))
       
   211 		return NULL;
       
   212 
       
   213 	if (TNor)
       
   214 		if (!PyArg_ParseTuple(TNor, "fff", &nx, &ny, &nz))
       
   215 			return NULL;
       
   216 
       
   217 	v = PyObject_New(NormalVertexObject, &NormalVertexType);
       
   218 	v->nvertex = new NormalVertex(Vector3(vx, vy, vz), Vector3(nx, ny, nz));
       
   219 	return (PyObject*)v;
       
   220 }
       
   221 
       
   222 static void NormalVertex_Destructor(PyObject* self)
       
   223 {
       
   224 	delete ((NormalVertexObject *)self)->nvertex;
       
   225 	PyObject_Del(self);
       
   226 }
       
   227 
       
   228 static PyObject *NormalVertex_Getattr(PyObject *self, char *name)
       
   229 {
       
   230 	return Py_FindMethod(NormalVertexMethods, self, name);
       
   231 }
       
   232 
       
   233 static PyObject *NormalVertex_setNormal(PyObject* self, PyObject* args)
       
   234 {
       
   235 	PyObject *TNor = NULL;
       
   236 	Float nx, ny, nz;
       
   237 
       
   238 	if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &TNor))
       
   239 		return NULL;
       
   240 
       
   241 	if (!PyArg_ParseTuple(TNor, "fff", &nx, &ny, &nz))
       
   242 		return NULL;
       
   243 
       
   244 	((NormalVertexObject *)self)->nvertex->setNormal(Vector3(nx,ny,nz).normalize());
       
   245 
       
   246 	Py_INCREF(Py_None);
       
   247 	return Py_None;
   159 }
   248 }
   160 
   249 
   161 //=========================== Sphere Object ===========================
   250 //=========================== Sphere Object ===========================
   162 
   251 
   163 typedef struct {
   252 typedef struct {
   296 
   385 
   297 //=========================== Triangle Object ===========================
   386 //=========================== Triangle Object ===========================
   298 
   387 
   299 typedef struct {
   388 typedef struct {
   300 	PyObject_HEAD
   389 	PyObject_HEAD
   301 	Triangle *shape;
   390 	Triangle *triangle;
   302 } TriangleObject;
   391 } TriangleObject;
   303 
   392 
   304 static PyObject *Triangle_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
   393 static PyObject *Triangle_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
   305 static void Triangle_Destructor(PyObject* self);
   394 static void Triangle_Destructor(PyObject* self);
   306 static PyObject *Triangle_Getattr(PyObject *self, char *name);
   395 static PyObject *Triangle_Getattr(PyObject *self, char *name);
       
   396 static PyObject *Triangle_getNormal(PyObject* self, PyObject* args);
       
   397 static PyObject *Triangle_setSmooth(PyObject* self, PyObject* args);
   307 
   398 
   308 static PyTypeObject TriangleType = {
   399 static PyTypeObject TriangleType = {
   309 	PyObject_HEAD_INIT(NULL)
   400 	PyObject_HEAD_INIT(NULL)
   310 	0,				/*ob_size*/
   401 	0,				/*ob_size*/
   311 	"Triangle",			/*tp_name*/
   402 	"Triangle",			/*tp_name*/
   323 	0,				/*tp_as_mapping*/
   414 	0,				/*tp_as_mapping*/
   324 	0,				/*tp_hash */
   415 	0,				/*tp_hash */
   325 };
   416 };
   326 
   417 
   327 static PyMethodDef TriangleMethods[] = {
   418 static PyMethodDef TriangleMethods[] = {
       
   419 	{"getNormal", (PyCFunction)Triangle_getNormal, METH_NOARGS, "Get normal of whole triangle."},
       
   420 	{"setSmooth", (PyCFunction)Triangle_setSmooth, METH_NOARGS, "Set the triangle smooth."},
   328 	{NULL, NULL}
   421 	{NULL, NULL}
   329 };
   422 };
   330 
   423 
   331 static PyObject* Triangle_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   424 static PyObject* Triangle_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   332 {
   425 {
   333 	TriangleObject *v;
   426 	TriangleObject *v;
   334 	MaterialObject *material;
   427 	MaterialObject *material;
   335 	static char *kwdlist[] = {"A", "B", "C", "material", NULL};
   428 	static char *kwdlist[] = {"A", "B", "C", "material", NULL};
   336 	PyObject *A = NULL, *B = NULL, *C = NULL;
   429 	NormalVertexObject *A, *B, *C;
   337 	Float ax, ay, az, bx, by, bz, cx, cy, cz;
       
   338 
   430 
   339 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!O!O!O!", kwdlist,
   431 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!O!O!O!", kwdlist,
   340 		&PyTuple_Type, &A, &PyTuple_Type, &B, &PyTuple_Type, &C,
   432 		&NormalVertexType, &A, &NormalVertexType, &B, &NormalVertexType, &C,
   341 		&MaterialType, &material))
   433 		&MaterialType, &material))
   342 		return NULL;
   434 		return NULL;
   343 
   435 
   344 	if (!PyArg_ParseTuple(A, "fff", &ax, &ay, &az))
       
   345 		return NULL;
       
   346 
       
   347 	if (!PyArg_ParseTuple(B, "fff", &bx, &by, &bz))
       
   348 		return NULL;
       
   349 
       
   350 	if (!PyArg_ParseTuple(C, "fff", &cx, &cy, &cz))
       
   351 		return NULL;
       
   352 
       
   353 	v = PyObject_New(TriangleObject, &TriangleType);
   436 	v = PyObject_New(TriangleObject, &TriangleType);
   354 	v->shape = new Triangle(Vector3(ax, ay, az), Vector3(bx, by, bz),
   437 	v->triangle = new Triangle(A->nvertex, B->nvertex, C->nvertex, material->material);
   355 		Vector3(cx, cy, cz), material->material);
       
   356 	Py_INCREF(material);
   438 	Py_INCREF(material);
       
   439 	Py_INCREF(A);
       
   440 	Py_INCREF(B);
       
   441 	Py_INCREF(C);
   357 	return (PyObject*)v;
   442 	return (PyObject*)v;
   358 }
   443 }
   359 
   444 
   360 static void Triangle_Destructor(PyObject* self)
   445 static void Triangle_Destructor(PyObject* self)
   361 {
   446 {
   362 	delete ((TriangleObject *)self)->shape;
   447 	delete ((TriangleObject *)self)->triangle;
   363 	PyObject_Del(self);
   448 	PyObject_Del(self);
   364 }
   449 }
   365 
   450 
   366 static PyObject *Triangle_Getattr(PyObject *self, char *name)
   451 static PyObject *Triangle_Getattr(PyObject *self, char *name)
   367 {
   452 {
   368 	return Py_FindMethod(TriangleMethods, self, name);
   453 	return Py_FindMethod(TriangleMethods, self, name);
       
   454 }
       
   455 
       
   456 static PyObject* Triangle_getNormal(PyObject* self, PyObject* args)
       
   457 {
       
   458 	PyObject *obj;
       
   459 
       
   460 	Vector3 N = ((TriangleObject *)self)->triangle->getNormal();
       
   461 
       
   462 	obj = Py_BuildValue("(fff)", N.x, N.y, N.z);
       
   463 	return obj;
       
   464 }
       
   465 
       
   466 static PyObject* Triangle_setSmooth(PyObject* self, PyObject* args)
       
   467 {
       
   468 	((TriangleObject *)self)->triangle->setSmooth();
       
   469 	Py_INCREF(Py_None);
       
   470 	return Py_None;
   369 }
   471 }
   370 
   472 
   371 //=========================== Raytracer Object ===========================
   473 //=========================== Raytracer Object ===========================
   372 
   474 
   373 typedef struct {
   475 typedef struct {
   530 		METH_VARARGS, "Raytracer object constructor."},
   632 		METH_VARARGS, "Raytracer object constructor."},
   531 	{"Light", (PyCFunction) Light_Constructor,
   633 	{"Light", (PyCFunction) Light_Constructor,
   532 		METH_VARARGS | METH_KEYWORDS, "Light source object constructor."},
   634 		METH_VARARGS | METH_KEYWORDS, "Light source object constructor."},
   533 	{"Material", (PyCFunction) Material_Constructor,
   635 	{"Material", (PyCFunction) Material_Constructor,
   534 		METH_VARARGS | METH_KEYWORDS, "Material object constructor."},
   636 		METH_VARARGS | METH_KEYWORDS, "Material object constructor."},
       
   637 	{"NormalVertex", (PyCFunction) NormalVertex_Constructor,
       
   638 		METH_VARARGS | METH_KEYWORDS, "NormalVertex object constructor."},
   535 	{"Sphere", (PyCFunction) Sphere_Constructor,
   639 	{"Sphere", (PyCFunction) Sphere_Constructor,
   536 		METH_VARARGS | METH_KEYWORDS, "Sphere object constructor."},
   640 		METH_VARARGS | METH_KEYWORDS, "Sphere object constructor."},
   537 	{"Box", (PyCFunction) Box_Constructor,
   641 	{"Box", (PyCFunction) Box_Constructor,
   538 		METH_VARARGS | METH_KEYWORDS, "Box object constructor."},
   642 		METH_VARARGS | METH_KEYWORDS, "Box object constructor."},
   539 	{"Triangle", (PyCFunction) Triangle_Constructor,
   643 	{"Triangle", (PyCFunction) Triangle_Constructor,