src/raytracermodule.cc
branchpyrit
changeset 31 b4e09433934a
parent 28 ffe83ca074f3
child 34 28f6e8b9d5d1
equal deleted inserted replaced
30:33f95441790e 31:b4e09433934a
   102 } MaterialObject;
   102 } MaterialObject;
   103 
   103 
   104 static PyObject *Material_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
   104 static PyObject *Material_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
   105 static void Material_Destructor(PyObject* self);
   105 static void Material_Destructor(PyObject* self);
   106 static PyObject *Material_Getattr(PyObject *self, char *name);
   106 static PyObject *Material_Getattr(PyObject *self, char *name);
       
   107 static PyObject *Material_setPhong(PyObject* self, PyObject* args);
       
   108 static PyObject *Material_setReflectivity(PyObject* self, PyObject* args);
       
   109 static PyObject *Material_setTransmissivity(PyObject* self, PyObject* args);
   107 
   110 
   108 static PyTypeObject MaterialType = {
   111 static PyTypeObject MaterialType = {
   109 	PyObject_HEAD_INIT(NULL)
   112 	PyObject_HEAD_INIT(NULL)
   110 	0,				/*ob_size*/
   113 	0,				/*ob_size*/
   111 	"Material",			/*tp_name*/
   114 	"Material",			/*tp_name*/
   123 	0,				/*tp_as_mapping*/
   126 	0,				/*tp_as_mapping*/
   124 	0,				/*tp_hash */
   127 	0,				/*tp_hash */
   125 };
   128 };
   126 
   129 
   127 static PyMethodDef MaterialMethods[] = {
   130 static PyMethodDef MaterialMethods[] = {
       
   131 	{"setPhong", (PyCFunction)Material_setPhong, METH_VARARGS, "Set ambient, diffuse, specular and shininess Phong model constants."},
       
   132 	{"setReflectivity", (PyCFunction)Material_setReflectivity, METH_VARARGS, "Set reflectivity."},
       
   133 	{"setTransmissivity", (PyCFunction)Material_setTransmissivity, METH_VARARGS, "Set transmissivity and refraction index."},
   128 	{NULL, NULL}
   134 	{NULL, NULL}
   129 };
   135 };
   130 
   136 
   131 static PyObject* Material_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   137 static PyObject* Material_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   132 {
   138 {
   155 }
   161 }
   156 
   162 
   157 static PyObject *Material_Getattr(PyObject *self, char *name)
   163 static PyObject *Material_Getattr(PyObject *self, char *name)
   158 {
   164 {
   159 	return Py_FindMethod(MaterialMethods, self, name);
   165 	return Py_FindMethod(MaterialMethods, self, name);
       
   166 }
       
   167 
       
   168 static PyObject *Material_setPhong(PyObject* self, PyObject* args)
       
   169 {
       
   170 	Float amb, dif, spec, shin = 0.5;
       
   171 
       
   172 	if (!PyArg_ParseTuple(args, "fff|f", &amb, &dif, &spec, &shin))
       
   173 		return NULL;
       
   174 
       
   175 	((MaterialObject *)self)->material->setPhong(amb, dif, spec, shin);
       
   176 
       
   177 	Py_INCREF(Py_None);
       
   178 	return Py_None;
       
   179 }
       
   180 
       
   181 static PyObject *Material_setReflectivity(PyObject* self, PyObject* args)
       
   182 {
       
   183 	Float refl;
       
   184 
       
   185 	if (!PyArg_ParseTuple(args, "f", &refl))
       
   186 		return NULL;
       
   187 
       
   188 	((MaterialObject *)self)->material->setReflectivity(refl);
       
   189 
       
   190 	Py_INCREF(Py_None);
       
   191 	return Py_None;
       
   192 }
       
   193 
       
   194 static PyObject *Material_setTransmissivity(PyObject* self, PyObject* args)
       
   195 {
       
   196 	Float trans, rindex = 1.3;
       
   197 
       
   198 	if (!PyArg_ParseTuple(args, "f|f", &trans, &rindex))
       
   199 		return NULL;
       
   200 
       
   201 	((MaterialObject *)self)->material->setTransmissivity(trans, rindex);
       
   202 
       
   203 	Py_INCREF(Py_None);
       
   204 	return Py_None;
   160 }
   205 }
   161 
   206 
   162 //=========================== NormalVertex Object ===========================
   207 //=========================== NormalVertex Object ===========================
   163 
   208 
   164 typedef struct {
   209 typedef struct {
   553 	PyObject *o;
   598 	PyObject *o;
   554 
   599 
   555 	if (!PyArg_ParseTuple(args, "(ii)", &w, &h))
   600 	if (!PyArg_ParseTuple(args, "(ii)", &w, &h))
   556 		return NULL;
   601 		return NULL;
   557 
   602 
   558 	printf("[pyrit] Raytracing...\n");
   603 	printf("[pyrit] Running ray tracer\n");
   559 	((RaytracerObject *)self)->raytracer->getTop()->optimize();
   604 	((RaytracerObject *)self)->raytracer->getTop()->optimize();
   560 	data = (Float *) malloc(w*h*3*sizeof(Float));
   605 	data = (Float *) malloc(w*h*3*sizeof(Float));
   561 	((RaytracerObject *)self)->raytracer->render(w, h, data);
   606 	((RaytracerObject *)self)->raytracer->render(w, h, data);
   562 	if (!data) {
   607 	if (!data) {
   563 		Py_INCREF(Py_None);
   608 		Py_INCREF(Py_None);
   564 		return Py_None;
   609 		return Py_None;
   565 	}
   610 	}
   566 
   611 
   567 	// convert data to char
   612 	// convert data to char
   568 	printf("[pyrit] Converting image data (float to char)...\n");
   613 	printf("[pyrit] Converting image data (float to char)\n");
   569 	chardata = (char *) malloc(w*h*3);
   614 	chardata = (char *) malloc(w*h*3);
   570 	Float *d = data;
   615 	Float *d = data;
   571 	for (char *c = chardata; c != chardata + w*h*3; c++, d++) {
   616 	for (char *c = chardata; c != chardata + w*h*3; c++, d++) {
   572 		if (*d > 1.0)
   617 		if (*d > 1.0)
   573 			*c = 255;
   618 			*c = 255;