src/raytracermodule.cc
branchpyrit
changeset 22 76b7bd51d64a
parent 21 79b516a3803d
child 28 ffe83ca074f3
equal deleted inserted replaced
21:79b516a3803d 22:76b7bd51d64a
    51 static PyObject* Light_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
    51 static PyObject* Light_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
    52 {
    52 {
    53 	LightObject *v;
    53 	LightObject *v;
    54 	static char *kwdlist[] = {"position", "colour", NULL};
    54 	static char *kwdlist[] = {"position", "colour", NULL};
    55 	PyObject *TPos, *TCol = NULL;
    55 	PyObject *TPos, *TCol = NULL;
    56 	float px, py, pz;
    56 	Float px, py, pz;
    57 	float cr = 1.0, cg = 1.0, cb = 1.0;
    57 	Float cr = 1.0, cg = 1.0, cb = 1.0;
    58 
    58 
    59 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!|O!", kwdlist,
    59 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!|O!", kwdlist,
    60 		&PyTuple_Type, &TPos, &PyTuple_Type, &TCol))
    60 		&PyTuple_Type, &TPos, &PyTuple_Type, &TCol))
    61 		return NULL;
    61 		return NULL;
    62 
    62 
   131 static PyObject* Material_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   131 static PyObject* Material_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   132 {
   132 {
   133 	MaterialObject *v;
   133 	MaterialObject *v;
   134 	static char *kwdlist[] = {"colour", NULL};
   134 	static char *kwdlist[] = {"colour", NULL};
   135 	PyObject *TCol = NULL;
   135 	PyObject *TCol = NULL;
   136 	float cr=1.0, cg=1.0, cb=1.0;
   136 	Float cr=1.0, cg=1.0, cb=1.0;
   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 
   196 {
   196 {
   197 	SphereObject *v;
   197 	SphereObject *v;
   198 	MaterialObject *material;
   198 	MaterialObject *material;
   199 	static char *kwdlist[] = {"centre", "radius", "material", NULL};
   199 	static char *kwdlist[] = {"centre", "radius", "material", NULL};
   200 	PyObject *TCentre = NULL;
   200 	PyObject *TCentre = NULL;
   201 	float cx, cy, cz, radius;
   201 	Float cx, cy, cz, radius;
   202 
   202 
   203 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!fO!", kwdlist,
   203 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!fO!", kwdlist,
   204 		&PyTuple_Type, &TCentre, &radius, &MaterialType, &material))
   204 		&PyTuple_Type, &TCentre, &radius, &MaterialType, &material))
   205 		return NULL;
   205 		return NULL;
   206 
   206 
   263 	BoxObject *v;
   263 	BoxObject *v;
   264 	MaterialObject *material;
   264 	MaterialObject *material;
   265 	static char *kwdlist[] = {"L", "H", "material", NULL};
   265 	static char *kwdlist[] = {"L", "H", "material", NULL};
   266 	PyObject *TL = NULL;
   266 	PyObject *TL = NULL;
   267 	PyObject *TH = NULL;
   267 	PyObject *TH = NULL;
   268 	float lx, ly, lz, hx, hy, hz;
   268 	Float lx, ly, lz, hx, hy, hz;
   269 
   269 
   270 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!O!O!", kwdlist,
   270 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!O!O!", kwdlist,
   271 		&PyTuple_Type, &TL, &PyTuple_Type, &TH, &MaterialType, &material))
   271 		&PyTuple_Type, &TL, &PyTuple_Type, &TH, &MaterialType, &material))
   272 		return NULL;
   272 		return NULL;
   273 
   273 
   332 {
   332 {
   333 	TriangleObject *v;
   333 	TriangleObject *v;
   334 	MaterialObject *material;
   334 	MaterialObject *material;
   335 	static char *kwdlist[] = {"A", "B", "C", "material", NULL};
   335 	static char *kwdlist[] = {"A", "B", "C", "material", NULL};
   336 	PyObject *A = NULL, *B = NULL, *C = NULL;
   336 	PyObject *A = NULL, *B = NULL, *C = NULL;
   337 	float ax, ay, az, bx, by, bz, cx, cy, cz;
   337 	Float ax, ay, az, bx, by, bz, cx, cy, cz;
   338 
   338 
   339 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!O!O!O!", kwdlist,
   339 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!O!O!O!", kwdlist,
   340 		&PyTuple_Type, &A, &PyTuple_Type, &B, &PyTuple_Type, &C,
   340 		&PyTuple_Type, &A, &PyTuple_Type, &B, &PyTuple_Type, &C,
   341 		&MaterialType, &material))
   341 		&MaterialType, &material))
   342 		return NULL;
   342 		return NULL;
   445 
   445 
   446 static PyObject* Raytracer_render(PyObject* self, PyObject* args)
   446 static PyObject* Raytracer_render(PyObject* self, PyObject* args)
   447 {
   447 {
   448 	int w = 0, h = 0;
   448 	int w = 0, h = 0;
   449 	char *chardata;
   449 	char *chardata;
   450 	float *data;
   450 	Float *data;
   451 	PyObject *o;
   451 	PyObject *o;
   452 
   452 
   453 	if (!PyArg_ParseTuple(args, "(ii)", &w, &h))
   453 	if (!PyArg_ParseTuple(args, "(ii)", &w, &h))
   454 		return NULL;
   454 		return NULL;
   455 
   455 
   456 	printf("[pyrit] Raytracing...\n");
   456 	printf("[pyrit] Raytracing...\n");
   457 	((RaytracerObject *)self)->raytracer->getTop()->optimize();
   457 	((RaytracerObject *)self)->raytracer->getTop()->optimize();
   458 	data = (float *) malloc(w*h*3*sizeof(float));
   458 	data = (Float *) malloc(w*h*3*sizeof(Float));
   459 	((RaytracerObject *)self)->raytracer->render(w, h, data);
   459 	((RaytracerObject *)self)->raytracer->render(w, h, data);
   460 	if (!data) {
   460 	if (!data) {
   461 		Py_INCREF(Py_None);
   461 		Py_INCREF(Py_None);
   462 		return Py_None;
   462 		return Py_None;
   463 	}
   463 	}
   464 
   464 
   465 	// convert data to char
   465 	// convert data to char
   466 	printf("[pyrit] Converting image data (float to char)...\n");
   466 	printf("[pyrit] Converting image data (float to char)...\n");
   467 	chardata = (char *) malloc(w*h*3);
   467 	chardata = (char *) malloc(w*h*3);
   468 	float *d = data;
   468 	Float *d = data;
   469 	for (char *c = chardata; c != chardata + w*h*3; c++, d++) {
   469 	for (char *c = chardata; c != chardata + w*h*3; c++, d++) {
   470 		if (*d > 1.0)
   470 		if (*d > 1.0)
   471 			*c = 255;
   471 			*c = 255;
   472 		else
   472 		else
   473 			*c = (unsigned char)(*d * 255.0);
   473 			*c = (unsigned char)(*d * 255.0);
   509 }
   509 }
   510 
   510 
   511 static PyObject* Raytracer_ambientocclusion(PyObject* self, PyObject* args, PyObject *kwd)
   511 static PyObject* Raytracer_ambientocclusion(PyObject* self, PyObject* args, PyObject *kwd)
   512 {
   512 {
   513 	int samples = 0;
   513 	int samples = 0;
   514 	float distance = 0.0, angle = 0.0;
   514 	Float distance = 0.0, angle = 0.0;
   515 	static char *kwdlist[] = {"samples", "distance", "angle", NULL};
   515 	static char *kwdlist[] = {"samples", "distance", "angle", NULL};
   516 
   516 
   517 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "iff", kwdlist,
   517 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "iff", kwdlist,
   518 		&samples, &distance, &angle))
   518 		&samples, &distance, &angle))
   519 		return NULL;
   519 		return NULL;