src/raytracermodule.cc
branchpyrit
changeset 59 64e456ab823d
parent 48 a4913301c626
child 60 a23b5089b9c3
equal deleted inserted replaced
58:cfe98f5c0ae9 59:64e456ab823d
   111 
   111 
   112 	Py_INCREF(Py_None);
   112 	Py_INCREF(Py_None);
   113 	return Py_None;
   113 	return Py_None;
   114 }
   114 }
   115 
   115 
       
   116 //=========================== Camera Object ===========================
       
   117 
       
   118 typedef struct {
       
   119 	PyObject_HEAD
       
   120 	Camera *camera;
       
   121 } CameraObject;
       
   122 
       
   123 static PyObject *Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
       
   124 static void Camera_Destructor(PyObject* self);
       
   125 static PyObject *Camera_Getattr(PyObject *self, char *name);
       
   126 static PyObject *Camera_setEye(PyObject* self, PyObject* args);
       
   127 static PyObject *Camera_rotate(PyObject* self, PyObject* args);
       
   128 
       
   129 static PyTypeObject CameraType = {
       
   130 	PyObject_HEAD_INIT(NULL)
       
   131 	0,				/*ob_size*/
       
   132 	"Camera",			/*tp_name*/
       
   133 	sizeof(CameraObject),		/*tp_basicsize*/
       
   134 	0,				/*tp_itemsize*/
       
   135 	/* methods */
       
   136 	Camera_Destructor,		/*tp_dealloc*/
       
   137 	0,				/*tp_print*/
       
   138 	Camera_Getattr,			/*tp_getattr*/
       
   139 	0,				/*tp_setattr*/
       
   140 	0,				/*tp_compare*/
       
   141 	0,				/*tp_repr*/
       
   142 	0,				/*tp_as_number*/
       
   143 	0,				/*tp_as_sequence*/
       
   144 	0,				/*tp_as_mapping*/
       
   145 	0,				/*tp_hash */
       
   146 };
       
   147 
       
   148 static PyMethodDef CameraMethods[] = {
       
   149 	{"setEye", (PyCFunction)Camera_setEye, METH_VARARGS, "Set eye of the camera."},
       
   150 	{"rotate", (PyCFunction)Camera_rotate, METH_VARARGS, "Rotate camera with a quaternion."},
       
   151 	{NULL, NULL}
       
   152 };
       
   153 
       
   154 static PyObject* Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
       
   155 {
       
   156 	CameraObject *v;
       
   157 	static char *kwdlist[] = {"eye", "p", "u", "v", NULL};
       
   158 	PyObject *TEye = NULL, *Tp = NULL, *Tu = NULL, *Tv = NULL;
       
   159 	Float ex=0.0,  ey=0.0, ez=10.0;
       
   160 	Float px=0.0,  py=0.0, pz=-1.0;
       
   161 	Float ux=-1.0, uy=0.0, uz=0.0;
       
   162 	Float vx=0.0,  vy=1.0, vz=0.0;
       
   163 
       
   164 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "|O!O!O!O!", kwdlist,
       
   165 		&PyTuple_Type, &TEye, &PyTuple_Type, &Tp, &PyTuple_Type, &Tu, &PyTuple_Type, &Tv))
       
   166 		return NULL;
       
   167 
       
   168 	if (TEye)
       
   169 		if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez))
       
   170 			return NULL;
       
   171 
       
   172 	if (Tp)
       
   173 		if (!PyArg_ParseTuple(Tp, "fff", &px, &py, &pz))
       
   174 			return NULL;
       
   175 
       
   176 	if (Tu)
       
   177 		if (!PyArg_ParseTuple(Tu, "fff", &ux, &uy, &uz))
       
   178 			return NULL;
       
   179 
       
   180 	if (Tv)
       
   181 		if (!PyArg_ParseTuple(Tv, "fff", &vx, &vy, &vz))
       
   182 			return NULL;
       
   183 
       
   184 	v = PyObject_New(CameraObject, &CameraType);
       
   185 	v->camera = new Camera(Vector3(ex, ey, ez),
       
   186 		Vector3(px, py, pz), Vector3(ux, uy, uz), Vector3(vx, vy, vz));
       
   187 	return (PyObject*)v;
       
   188 }
       
   189 
       
   190 static void Camera_Destructor(PyObject* self)
       
   191 {
       
   192 	delete ((CameraObject *)self)->camera;
       
   193 	PyObject_Del(self);
       
   194 }
       
   195 
       
   196 static PyObject *Camera_Getattr(PyObject *self, char *name)
       
   197 {
       
   198 	return Py_FindMethod(CameraMethods, self, name);
       
   199 }
       
   200 
       
   201 
       
   202 static PyObject *Camera_setEye(PyObject* self, PyObject* args)
       
   203 {
       
   204 	PyObject *TEye = NULL;
       
   205 	Float ex=0.0,  ey=0.0, ez=10.0;
       
   206 
       
   207 	if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &TEye))
       
   208 		return NULL;
       
   209 
       
   210 	if (TEye)
       
   211 		if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez))
       
   212 			return NULL;
       
   213 
       
   214 	((CameraObject *)self)->camera->setEye(Vector3(ex, ey, ez));
       
   215 
       
   216 	Py_INCREF(Py_None);
       
   217 	return Py_None;
       
   218 }
       
   219 
       
   220 static PyObject *Camera_rotate(PyObject* self, PyObject* args)
       
   221 {
       
   222 	PyObject *Tq = NULL;
       
   223 	Float qa, qb, qc, qd;
       
   224 
       
   225 	if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &Tq))
       
   226 		return NULL;
       
   227 
       
   228 	if (!PyArg_ParseTuple(Tq, "ffff", &qa, &qb, &qc, &qd))
       
   229 		return NULL;
       
   230 
       
   231 	((CameraObject *)self)->camera->rotate(Quaternion(qa, qb, qc, qd).normalize());
       
   232 
       
   233 	Py_INCREF(Py_None);
       
   234 	return Py_None;
       
   235 }
       
   236 
       
   237 
   116 //=========================== Material Object ===========================
   238 //=========================== Material Object ===========================
   117 
   239 
   118 typedef struct {
   240 typedef struct {
   119 	PyObject_HEAD
   241 	PyObject_HEAD
   120 	Material *material;
   242 	Material *material;
   544 
   666 
   545 static PyObject *Raytracer_Constructor(PyObject* self, PyObject* args);
   667 static PyObject *Raytracer_Constructor(PyObject* self, PyObject* args);
   546 static void Raytracer_Destructor(PyObject* self);
   668 static void Raytracer_Destructor(PyObject* self);
   547 static PyObject *Raytracer_Getattr(PyObject *self, char *name);
   669 static PyObject *Raytracer_Getattr(PyObject *self, char *name);
   548 static PyObject *Raytracer_render(PyObject* self, PyObject* args);
   670 static PyObject *Raytracer_render(PyObject* self, PyObject* args);
       
   671 static PyObject *Raytracer_setcamera(PyObject* self, PyObject* args);
   549 static PyObject *Raytracer_addshape(PyObject* self, PyObject* args);
   672 static PyObject *Raytracer_addshape(PyObject* self, PyObject* args);
   550 static PyObject *Raytracer_addlight(PyObject* self, PyObject* args);
   673 static PyObject *Raytracer_addlight(PyObject* self, PyObject* args);
   551 static PyObject *Raytracer_ambientocclusion(PyObject* self, PyObject* args, PyObject *kwd);
   674 static PyObject *Raytracer_ambientocclusion(PyObject* self, PyObject* args, PyObject *kwd);
   552 
   675 
   553 static PyTypeObject RaytracerType = {
   676 static PyTypeObject RaytracerType = {
   569 	0,				/*tp_hash */
   692 	0,				/*tp_hash */
   570 };
   693 };
   571 
   694 
   572 static PyMethodDef RaytracerMethods[] = {
   695 static PyMethodDef RaytracerMethods[] = {
   573 	{"render", (PyCFunction)Raytracer_render, METH_VARARGS, "Render scene and return image data."},
   696 	{"render", (PyCFunction)Raytracer_render, METH_VARARGS, "Render scene and return image data."},
       
   697 	{"setcamera", (PyCFunction)Raytracer_setcamera, METH_VARARGS, "Set camera for the scene."},
   574 	{"addshape", (PyCFunction)Raytracer_addshape, METH_VARARGS, "Add new shape to scene."},
   698 	{"addshape", (PyCFunction)Raytracer_addshape, METH_VARARGS, "Add new shape to scene."},
   575 	{"addlight", (PyCFunction)Raytracer_addlight, METH_VARARGS, "Add new light source to scene."},
   699 	{"addlight", (PyCFunction)Raytracer_addlight, METH_VARARGS, "Add new light source to scene."},
   576 	{"ambientocclusion", (PyCFunction)Raytracer_ambientocclusion, METH_VARARGS | METH_KEYWORDS,
   700 	{"ambientocclusion", (PyCFunction)Raytracer_ambientocclusion, METH_VARARGS | METH_KEYWORDS,
   577 		"Set ambient occlusion parametrs - samples: int (0 = disable), distance: float, angle: float."},
   701 		"Set ambient occlusion parametrs - samples: int (0 = disable), distance: float, angle: float."},
   578 	{NULL, NULL}
   702 	{NULL, NULL}
   645 	free(chardata);
   769 	free(chardata);
   646 	printf("[pyrit] Done.\n");
   770 	printf("[pyrit] Done.\n");
   647 	return o;
   771 	return o;
   648 }
   772 }
   649 
   773 
       
   774 static PyObject* Raytracer_setcamera(PyObject* self, PyObject* args)
       
   775 {
       
   776 	CameraObject *cam;
       
   777 
       
   778 	if (!PyArg_ParseTuple(args, "O!", &CameraType, &cam))
       
   779 		return NULL;
       
   780 
       
   781 	((RaytracerObject *)self)->raytracer->setCamera(cam->camera);
       
   782 
       
   783 	Py_INCREF(cam);
       
   784 	Py_INCREF(Py_None);
       
   785 	return Py_None;
       
   786 }
       
   787 
   650 static PyObject* Raytracer_addshape(PyObject* self, PyObject* args)
   788 static PyObject* Raytracer_addshape(PyObject* self, PyObject* args)
   651 {
   789 {
   652 	PyObject *obj;
   790 	PyObject *obj;
   653 
   791 
   654 	if (!PyArg_ParseTuple(args, "O", &obj))
   792 	if (!PyArg_ParseTuple(args, "O", &obj))
   696 static PyMethodDef ModuleMethods[] = {
   834 static PyMethodDef ModuleMethods[] = {
   697 	{"Raytracer", (PyCFunction) Raytracer_Constructor,
   835 	{"Raytracer", (PyCFunction) Raytracer_Constructor,
   698 		METH_VARARGS, "Raytracer object constructor."},
   836 		METH_VARARGS, "Raytracer object constructor."},
   699 	{"Light", (PyCFunction) Light_Constructor,
   837 	{"Light", (PyCFunction) Light_Constructor,
   700 		METH_VARARGS | METH_KEYWORDS, "Light source object constructor."},
   838 		METH_VARARGS | METH_KEYWORDS, "Light source object constructor."},
       
   839 	{"Camera", (PyCFunction) Camera_Constructor,
       
   840 		METH_VARARGS | METH_KEYWORDS, "Camera object constructor."},
   701 	{"Material", (PyCFunction) Material_Constructor,
   841 	{"Material", (PyCFunction) Material_Constructor,
   702 		METH_VARARGS | METH_KEYWORDS, "Material object constructor."},
   842 		METH_VARARGS | METH_KEYWORDS, "Material object constructor."},
   703 	{"NormalVertex", (PyCFunction) NormalVertex_Constructor,
   843 	{"NormalVertex", (PyCFunction) NormalVertex_Constructor,
   704 		METH_VARARGS | METH_KEYWORDS, "NormalVertex object constructor."},
   844 		METH_VARARGS | METH_KEYWORDS, "NormalVertex object constructor."},
   705 	{"Sphere", (PyCFunction) Sphere_Constructor,
   845 	{"Sphere", (PyCFunction) Sphere_Constructor,