# HG changeset patch # User Radek Brich # Date 1207310087 -7200 # Node ID 64e456ab823d80e59794cfa832f84d0bf0d8308c # Parent cfe98f5c0ae969365debff6fd17a34e78a060840 add color support to lwo reader python binding for Camera diff -r cfe98f5c0ae9 -r 64e456ab823d demos/car.py --- a/demos/car.py Fri Apr 04 01:55:25 2008 +0200 +++ b/demos/car.py Fri Apr 04 13:54:47 2008 +0200 @@ -3,15 +3,22 @@ import sys sys.path.append(open('ModulePath').read().strip()) -from raytracer import Raytracer, Light, Sphere, Triangle, Material +from raytracer import Raytracer, Light, Sphere, Triangle, Material, Camera from lworeader import LoadLightwaveLwoFile import Image +from math import * rt = Raytracer() + +cam = Camera(eye=(0.,5.,8.)) +rotx=0.3 +cam.rotate((cos(rotx),-sin(rotx),0.,0.)) +rt.setcamera(cam) + mat = Material(colour=(0.9, 0.9, 0.9)) -LoadLightwaveLwoFile(rt, "../models/car/Nissan300ZX.lwo", mat, smooth=True, scale=0.4) #, , trans=(0,-3,0)) +LoadLightwaveLwoFile(rt, "../models/car/Nissan300ZX.lwo", mat, smooth=False, scale=0.4) -light = Light(position=(-5.0, 2.0, 8.0), colour=(0.2, 0.4, 0.6)) +light = Light(position=(-5.0, 2.0, 8.0), colour=(0.7, 0.7, 0.7)) rt.addlight(light) imagesize = (800, 600) diff -r cfe98f5c0ae9 -r 64e456ab823d demos/lworeader.py --- a/demos/lworeader.py Fri Apr 04 01:55:25 2008 +0200 +++ b/demos/lworeader.py Fri Apr 04 13:54:47 2008 +0200 @@ -1,7 +1,7 @@ # LightWave .lwo file loader from struct import * -from raytracer import Triangle, NormalVertex +from raytracer import Triangle, NormalVertex, Material def read_int4(f): return unpack('>i', f.read(4))[0] @@ -109,6 +109,11 @@ vertices = [] normals = [] vertex_face_num = [] + materials = [] + + for surf in surfaces: + mat = Material(colour=tuple(float(x)/255. for x in surf['color'])) + materials.append(mat) for point in points: v0 = scale[0]*point[0] + trans[0] @@ -118,12 +123,10 @@ normals.append([0.,0.,0.]) vertex_face_num.append(0) - for f in faces: v = [vertices[x] for x in f[0:3]] - surf = f[3] - mat = defmat - face = Triangle(v[0], v[1], v[2], mat) + matidx = f[3]-1 + face = Triangle(v[0], v[1], v[2], materials[matidx]) n = face.getNormal() for x in f[0:3]: for i in range(3): diff -r cfe98f5c0ae9 -r 64e456ab823d src/raytracermodule.cc --- a/src/raytracermodule.cc Fri Apr 04 01:55:25 2008 +0200 +++ b/src/raytracermodule.cc Fri Apr 04 13:54:47 2008 +0200 @@ -113,6 +113,128 @@ return Py_None; } +//=========================== Camera Object =========================== + +typedef struct { + PyObject_HEAD + Camera *camera; +} CameraObject; + +static PyObject *Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd); +static void Camera_Destructor(PyObject* self); +static PyObject *Camera_Getattr(PyObject *self, char *name); +static PyObject *Camera_setEye(PyObject* self, PyObject* args); +static PyObject *Camera_rotate(PyObject* self, PyObject* args); + +static PyTypeObject CameraType = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "Camera", /*tp_name*/ + sizeof(CameraObject), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + /* methods */ + Camera_Destructor, /*tp_dealloc*/ + 0, /*tp_print*/ + Camera_Getattr, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ +}; + +static PyMethodDef CameraMethods[] = { + {"setEye", (PyCFunction)Camera_setEye, METH_VARARGS, "Set eye of the camera."}, + {"rotate", (PyCFunction)Camera_rotate, METH_VARARGS, "Rotate camera with a quaternion."}, + {NULL, NULL} +}; + +static PyObject* Camera_Constructor(PyObject* self, PyObject* args, PyObject *kwd) +{ + CameraObject *v; + static char *kwdlist[] = {"eye", "p", "u", "v", NULL}; + PyObject *TEye = NULL, *Tp = NULL, *Tu = NULL, *Tv = NULL; + Float ex=0.0, ey=0.0, ez=10.0; + Float px=0.0, py=0.0, pz=-1.0; + Float ux=-1.0, uy=0.0, uz=0.0; + Float vx=0.0, vy=1.0, vz=0.0; + + if (!PyArg_ParseTupleAndKeywords(args, kwd, "|O!O!O!O!", kwdlist, + &PyTuple_Type, &TEye, &PyTuple_Type, &Tp, &PyTuple_Type, &Tu, &PyTuple_Type, &Tv)) + return NULL; + + if (TEye) + if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez)) + return NULL; + + if (Tp) + if (!PyArg_ParseTuple(Tp, "fff", &px, &py, &pz)) + return NULL; + + if (Tu) + if (!PyArg_ParseTuple(Tu, "fff", &ux, &uy, &uz)) + return NULL; + + if (Tv) + if (!PyArg_ParseTuple(Tv, "fff", &vx, &vy, &vz)) + return NULL; + + v = PyObject_New(CameraObject, &CameraType); + v->camera = new Camera(Vector3(ex, ey, ez), + Vector3(px, py, pz), Vector3(ux, uy, uz), Vector3(vx, vy, vz)); + return (PyObject*)v; +} + +static void Camera_Destructor(PyObject* self) +{ + delete ((CameraObject *)self)->camera; + PyObject_Del(self); +} + +static PyObject *Camera_Getattr(PyObject *self, char *name) +{ + return Py_FindMethod(CameraMethods, self, name); +} + + +static PyObject *Camera_setEye(PyObject* self, PyObject* args) +{ + PyObject *TEye = NULL; + Float ex=0.0, ey=0.0, ez=10.0; + + if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &TEye)) + return NULL; + + if (TEye) + if (!PyArg_ParseTuple(TEye, "fff", &ex, &ey, &ez)) + return NULL; + + ((CameraObject *)self)->camera->setEye(Vector3(ex, ey, ez)); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject *Camera_rotate(PyObject* self, PyObject* args) +{ + PyObject *Tq = NULL; + Float qa, qb, qc, qd; + + if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &Tq)) + return NULL; + + if (!PyArg_ParseTuple(Tq, "ffff", &qa, &qb, &qc, &qd)) + return NULL; + + ((CameraObject *)self)->camera->rotate(Quaternion(qa, qb, qc, qd).normalize()); + + Py_INCREF(Py_None); + return Py_None; +} + + //=========================== Material Object =========================== typedef struct { @@ -546,6 +668,7 @@ static void Raytracer_Destructor(PyObject* self); static PyObject *Raytracer_Getattr(PyObject *self, char *name); static PyObject *Raytracer_render(PyObject* self, PyObject* args); +static PyObject *Raytracer_setcamera(PyObject* self, PyObject* args); static PyObject *Raytracer_addshape(PyObject* self, PyObject* args); static PyObject *Raytracer_addlight(PyObject* self, PyObject* args); static PyObject *Raytracer_ambientocclusion(PyObject* self, PyObject* args, PyObject *kwd); @@ -571,6 +694,7 @@ static PyMethodDef RaytracerMethods[] = { {"render", (PyCFunction)Raytracer_render, METH_VARARGS, "Render scene and return image data."}, + {"setcamera", (PyCFunction)Raytracer_setcamera, METH_VARARGS, "Set camera for the scene."}, {"addshape", (PyCFunction)Raytracer_addshape, METH_VARARGS, "Add new shape to scene."}, {"addlight", (PyCFunction)Raytracer_addlight, METH_VARARGS, "Add new light source to scene."}, {"ambientocclusion", (PyCFunction)Raytracer_ambientocclusion, METH_VARARGS | METH_KEYWORDS, @@ -647,6 +771,20 @@ return o; } +static PyObject* Raytracer_setcamera(PyObject* self, PyObject* args) +{ + CameraObject *cam; + + if (!PyArg_ParseTuple(args, "O!", &CameraType, &cam)) + return NULL; + + ((RaytracerObject *)self)->raytracer->setCamera(cam->camera); + + Py_INCREF(cam); + Py_INCREF(Py_None); + return Py_None; +} + static PyObject* Raytracer_addshape(PyObject* self, PyObject* args) { PyObject *obj; @@ -698,6 +836,8 @@ METH_VARARGS, "Raytracer object constructor."}, {"Light", (PyCFunction) Light_Constructor, METH_VARARGS | METH_KEYWORDS, "Light source object constructor."}, + {"Camera", (PyCFunction) Camera_Constructor, + METH_VARARGS | METH_KEYWORDS, "Camera object constructor."}, {"Material", (PyCFunction) Material_Constructor, METH_VARARGS | METH_KEYWORDS, "Material object constructor."}, {"NormalVertex", (PyCFunction) NormalVertex_Constructor,