src/raytracermodule.cc
branchpyrit
changeset 72 7c3f38dff082
parent 69 303583d2fb97
child 75 20dee9819b17
equal deleted inserted replaced
71:4fedf7290929 72:7c3f38dff082
    71 {
    71 {
    72 	LightObject *v;
    72 	LightObject *v;
    73 	static char *kwdlist[] = {"position", "colour", NULL};
    73 	static char *kwdlist[] = {"position", "colour", NULL};
    74 	PyObject *TPos, *TCol = NULL;
    74 	PyObject *TPos, *TCol = NULL;
    75 	Float px, py, pz;
    75 	Float px, py, pz;
    76 	Float cr = 1.0, cg = 1.0, cb = 1.0;
    76 	Float cr = 0.9, cg = 0.9, cb = 0.9;
    77 
    77 
    78 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!|O!", kwdlist,
    78 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "O!|O!", kwdlist,
    79 		&PyTuple_Type, &TPos, &PyTuple_Type, &TCol))
    79 		&PyTuple_Type, &TPos, &PyTuple_Type, &TCol))
    80 		return NULL;
    80 		return NULL;
    81 
    81 
   122 
   122 
   123 static PyObject *Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
   123 static PyObject *Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd);
   124 static void Camera_Destructor(PyObject* self);
   124 static void Camera_Destructor(PyObject* self);
   125 static PyObject *Camera_Getattr(PyObject *self, char *name);
   125 static PyObject *Camera_Getattr(PyObject *self, char *name);
   126 static PyObject *Camera_setEye(PyObject* self, PyObject* args);
   126 static PyObject *Camera_setEye(PyObject* self, PyObject* args);
       
   127 static PyObject *Camera_setAngle(PyObject* self, PyObject* args);
   127 static PyObject *Camera_rotate(PyObject* self, PyObject* args);
   128 static PyObject *Camera_rotate(PyObject* self, PyObject* args);
   128 
   129 
   129 static PyTypeObject CameraType = {
   130 static PyTypeObject CameraType = {
   130 	PyObject_HEAD_INIT(NULL)
   131 	PyObject_HEAD_INIT(NULL)
   131 	0,				/*ob_size*/
   132 	0,				/*ob_size*/
   145 	0,				/*tp_hash */
   146 	0,				/*tp_hash */
   146 };
   147 };
   147 
   148 
   148 static PyMethodDef CameraMethods[] = {
   149 static PyMethodDef CameraMethods[] = {
   149 	{"setEye", (PyCFunction)Camera_setEye, METH_VARARGS, "Set eye of the camera."},
   150 	{"setEye", (PyCFunction)Camera_setEye, METH_VARARGS, "Set eye of the camera."},
       
   151 	{"setAngle", (PyCFunction)Camera_setAngle, METH_VARARGS, "Set vertical angle of view."},
   150 	{"rotate", (PyCFunction)Camera_rotate, METH_VARARGS, "Rotate camera with a quaternion."},
   152 	{"rotate", (PyCFunction)Camera_rotate, METH_VARARGS, "Rotate camera with a quaternion."},
   151 	{NULL, NULL}
   153 	{NULL, NULL}
   152 };
   154 };
   153 
   155 
   154 static PyObject* Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   156 static PyObject* Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd)
   155 {
   157 {
   156 	CameraObject *v;
   158 	CameraObject *v;
   157 	static char *kwdlist[] = {"eye", "p", "u", "v", NULL};
   159 	static char *kwdlist[] = {"eye", "lookat", "up", "p", "u", "v", NULL};
   158 	PyObject *TEye = NULL, *Tp = NULL, *Tu = NULL, *Tv = NULL;
   160 	PyObject *TEye = NULL, *TLookAt = NULL, *TUp = NULL,
       
   161 		*Tp = NULL, *Tu = NULL, *Tv = NULL;
   159 	Float ex=0.0,  ey=0.0, ez=10.0;
   162 	Float ex=0.0,  ey=0.0, ez=10.0;
       
   163 	Float lax=0.0, lay=0.0, laz=0.0;
       
   164 	Float upx=0.0, upy=1.0, upz=0.0;
   160 	Float px=0.0,  py=0.0, pz=-1.0;
   165 	Float px=0.0,  py=0.0, pz=-1.0;
   161 	Float ux=-1.0, uy=0.0, uz=0.0;
   166 	Float ux=-1.0, uy=0.0, uz=0.0;
   162 	Float vx=0.0,  vy=1.0, vz=0.0;
   167 	Float vx=0.0,  vy=1.0, vz=0.0;
   163 
   168 
   164 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "|O!O!O!O!", kwdlist,
   169 	if (!PyArg_ParseTupleAndKeywords(args, kwd, "|O!O!O!O!O!O!", kwdlist,
   165 		&PyTuple_Type, &TEye, &PyTuple_Type, &Tp, &PyTuple_Type, &Tu, &PyTuple_Type, &Tv))
   170 		&PyTuple_Type, &TEye, &PyTuple_Type, &TLookAt, &PyTuple_Type, &TUp,
       
   171 		&PyTuple_Type, &Tp, &PyTuple_Type, &Tu, &PyTuple_Type, &Tv))
   166 		return NULL;
   172 		return NULL;
   167 
   173 
   168 	if (TEye)
   174 	if (TEye)
   169 		if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez))
   175 		if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez))
   170 			return NULL;
   176 			return NULL;
   171 
   177 
       
   178 	if (TLookAt)
       
   179 		if (!PyArg_ParseTuple(TLookAt, "fff", &lax, &lay, &laz))
       
   180 			return NULL;
       
   181 
       
   182 	if (TUp)
       
   183 		if (!PyArg_ParseTuple(TUp, "fff", &upx, &upy, &upz))
       
   184 			return NULL;
       
   185 
   172 	if (Tp)
   186 	if (Tp)
   173 		if (!PyArg_ParseTuple(Tp, "fff", &px, &py, &pz))
   187 		if (!PyArg_ParseTuple(Tp, "fff", &px, &py, &pz))
   174 			return NULL;
   188 			return NULL;
   175 
   189 
   176 	if (Tu)
   190 	if (Tu)
   180 	if (Tv)
   194 	if (Tv)
   181 		if (!PyArg_ParseTuple(Tv, "fff", &vx, &vy, &vz))
   195 		if (!PyArg_ParseTuple(Tv, "fff", &vx, &vy, &vz))
   182 			return NULL;
   196 			return NULL;
   183 
   197 
   184 	v = PyObject_New(CameraObject, &CameraType);
   198 	v = PyObject_New(CameraObject, &CameraType);
   185 	v->camera = new Camera(Vector3(ex, ey, ez),
   199 	if (TLookAt)
   186 		Vector3(px, py, pz), Vector3(ux, uy, uz), Vector3(vx, vy, vz));
   200 		v->camera = new Camera(Vector3(ex, ey, ez),
       
   201 			Vector3(lax, lay, laz), Vector3(upx, upy, upz));
       
   202 	else
       
   203 		v->camera = new Camera(Vector3(ex, ey, ez),
       
   204 			Vector3(px, py, pz), Vector3(ux, uy, uz), Vector3(vx, vy, vz));
   187 	return (PyObject*)v;
   205 	return (PyObject*)v;
   188 }
   206 }
   189 
   207 
   190 static void Camera_Destructor(PyObject* self)
   208 static void Camera_Destructor(PyObject* self)
   191 {
   209 {
   210 	if (TEye)
   228 	if (TEye)
   211 		if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez))
   229 		if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez))
   212 			return NULL;
   230 			return NULL;
   213 
   231 
   214 	((CameraObject *)self)->camera->setEye(Vector3(ex, ey, ez));
   232 	((CameraObject *)self)->camera->setEye(Vector3(ex, ey, ez));
       
   233 
       
   234 	Py_INCREF(Py_None);
       
   235 	return Py_None;
       
   236 }
       
   237 
       
   238 static PyObject *Camera_setAngle(PyObject* self, PyObject* args)
       
   239 {
       
   240 	Float angle;
       
   241 
       
   242 	if (!PyArg_ParseTuple(args, "f", &angle))
       
   243 		return NULL;
       
   244 
       
   245 	((CameraObject *)self)->camera->setAngle(angle);
   215 
   246 
   216 	Py_INCREF(Py_None);
   247 	Py_INCREF(Py_None);
   217 	return Py_None;
   248 	return Py_None;
   218 }
   249 }
   219 
   250 
   682 static PyObject *Raytracer_Constructor(PyObject* self, PyObject* args);
   713 static PyObject *Raytracer_Constructor(PyObject* self, PyObject* args);
   683 static void Raytracer_Destructor(PyObject* self);
   714 static void Raytracer_Destructor(PyObject* self);
   684 static PyObject *Raytracer_Getattr(PyObject *self, char *name);
   715 static PyObject *Raytracer_Getattr(PyObject *self, char *name);
   685 static PyObject *Raytracer_render(PyObject* self, PyObject* args);
   716 static PyObject *Raytracer_render(PyObject* self, PyObject* args);
   686 static PyObject *Raytracer_setcamera(PyObject* self, PyObject* args);
   717 static PyObject *Raytracer_setcamera(PyObject* self, PyObject* args);
       
   718 static PyObject *Raytracer_setbgcolour(PyObject* self, PyObject* args);
   687 static PyObject *Raytracer_addshape(PyObject* self, PyObject* args);
   719 static PyObject *Raytracer_addshape(PyObject* self, PyObject* args);
   688 static PyObject *Raytracer_addlight(PyObject* self, PyObject* args);
   720 static PyObject *Raytracer_addlight(PyObject* self, PyObject* args);
   689 static PyObject *Raytracer_ambientocclusion(PyObject* self, PyObject* args, PyObject *kwd);
   721 static PyObject *Raytracer_ambientocclusion(PyObject* self, PyObject* args, PyObject *kwd);
   690 
   722 
   691 static PyTypeObject RaytracerType = {
   723 static PyTypeObject RaytracerType = {
   708 };
   740 };
   709 
   741 
   710 static PyMethodDef RaytracerMethods[] = {
   742 static PyMethodDef RaytracerMethods[] = {
   711 	{"render", (PyCFunction)Raytracer_render, METH_VARARGS, "Render scene and return image data."},
   743 	{"render", (PyCFunction)Raytracer_render, METH_VARARGS, "Render scene and return image data."},
   712 	{"setcamera", (PyCFunction)Raytracer_setcamera, METH_VARARGS, "Set camera for the scene."},
   744 	{"setcamera", (PyCFunction)Raytracer_setcamera, METH_VARARGS, "Set camera for the scene."},
       
   745 	{"setbgcolour", (PyCFunction)Raytracer_setbgcolour, METH_VARARGS, "Set background colour."},
   713 	{"addshape", (PyCFunction)Raytracer_addshape, METH_VARARGS, "Add new shape to scene."},
   746 	{"addshape", (PyCFunction)Raytracer_addshape, METH_VARARGS, "Add new shape to scene."},
   714 	{"addlight", (PyCFunction)Raytracer_addlight, METH_VARARGS, "Add new light source to scene."},
   747 	{"addlight", (PyCFunction)Raytracer_addlight, METH_VARARGS, "Add new light source to scene."},
   715 	{"ambientocclusion", (PyCFunction)Raytracer_ambientocclusion, METH_VARARGS | METH_KEYWORDS,
   748 	{"ambientocclusion", (PyCFunction)Raytracer_ambientocclusion, METH_VARARGS | METH_KEYWORDS,
   716 		"Set ambient occlusion parametrs - samples: int (0 = disable), distance: float, angle: float."},
   749 		"Set ambient occlusion parametrs - samples: int (0 = disable), distance: float, angle: float."},
   717 	{NULL, NULL}
   750 	{NULL, NULL}
   798 	Py_INCREF(cam);
   831 	Py_INCREF(cam);
   799 	Py_INCREF(Py_None);
   832 	Py_INCREF(Py_None);
   800 	return Py_None;
   833 	return Py_None;
   801 }
   834 }
   802 
   835 
       
   836 static PyObject* Raytracer_setbgcolour(PyObject* self, PyObject* args)
       
   837 {
       
   838 	Float r,g,b;
       
   839 
       
   840 	if (!PyArg_ParseTuple(args, "(fff)", &r, &g, &b))
       
   841 		return NULL;
       
   842 
       
   843 	((RaytracerObject *)self)->raytracer->setBgColour(Colour(r,g,b));
       
   844 
       
   845 	Py_INCREF(Py_None);
       
   846 	return Py_None;
       
   847 }
       
   848 
   803 static PyObject* Raytracer_addshape(PyObject* self, PyObject* args)
   849 static PyObject* Raytracer_addshape(PyObject* self, PyObject* args)
   804 {
   850 {
   805 	PyObject *obj;
   851 	PyObject *obj;
   806 
   852 
   807 	if (!PyArg_ParseTuple(args, "O", &obj))
   853 	if (!PyArg_ParseTuple(args, "O", &obj))
   808 		return NULL;
   854 		return NULL;
   809 
   855 
   810 	((RaytracerObject *)self)->raytracer->addshape(
   856 	((RaytracerObject *)self)->raytracer->addShape(
   811 		((BoxObject*)obj)->shape);
   857 		((BoxObject*)obj)->shape);
   812 
   858 
   813 	((RaytracerObject *)self)->children->push_back(obj);
   859 	((RaytracerObject *)self)->children->push_back(obj);
   814 	Py_INCREF(obj);
   860 	Py_INCREF(obj);
   815 	Py_INCREF(Py_None);
   861 	Py_INCREF(Py_None);
   820 {
   866 {
   821 	LightObject *lightobj;
   867 	LightObject *lightobj;
   822 
   868 
   823 	if (!PyArg_ParseTuple(args, "O!", &LightType, &lightobj))
   869 	if (!PyArg_ParseTuple(args, "O!", &LightType, &lightobj))
   824 		return NULL;
   870 		return NULL;
   825 	((RaytracerObject *)self)->raytracer->addlight(lightobj->light);
   871 	((RaytracerObject *)self)->raytracer->addLight(lightobj->light);
   826 	((RaytracerObject *)self)->children->push_back((PyObject*)lightobj);
   872 	((RaytracerObject *)self)->children->push_back((PyObject*)lightobj);
   827 	Py_INCREF(lightobj);
   873 	Py_INCREF(lightobj);
   828 	Py_INCREF(Py_None);
   874 	Py_INCREF(Py_None);
   829 	return Py_None;
   875 	return Py_None;
   830 }
   876 }