38 #include <Python/structmember.h>
40 #include <structmember.h>
44 #include "SDL_thread.h"
49 {
"x", T_FLOAT, offsetof(Object3D, x), 0,
"X translation"},
50 {
"y", T_FLOAT, offsetof(Object3D, y), 0,
"Y translation"},
51 {
"z", T_FLOAT, offsetof(Object3D, z), 0,
"Z translation"},
52 {
"rx", T_FLOAT, offsetof(Object3D, rx), 0,
"X rotation"},
53 {
"ry", T_FLOAT, offsetof(Object3D, ry), 0,
"Y rotation"},
54 {
"rz", T_FLOAT, offsetof(Object3D, rz), 0,
"Z rotation"},
55 {
"sx", T_FLOAT, offsetof(Object3D, sx), 0,
"X scale"},
56 {
"sy", T_FLOAT, offsetof(Object3D, sy), 0,
"Y scale"},
57 {
"sz", T_FLOAT, offsetof(Object3D, sz), 0,
"Z scale"},
58 {
"shadeless", T_UINT, offsetof(Object3D, shadeless), 0,
"Whether this object is affected by scene lights or not."},
59 {
"texture", T_UINT, offsetof(Object3D, texture), 0,
"A texture id or 0 if this object doesn't have a texture."},
60 {
"shader", T_UINT, offsetof(Object3D, shader), 0,
"A shader id or 0 if this object doesn't have a shader."},
61 {
"visibility", T_INT, offsetof(Object3D, isVisible), 0,
"Whether this object is currently visible or not."},
62 {
"cameraMode", T_INT, offsetof(Object3D, inMovableCamera), 0,
"Whether this object uses the Movable or Fixed camera mode."},
63 {
"pickable", T_INT, offsetof(Object3D, isPickable), 0,
"Whether this object can be picked."},
64 {
"solid", T_INT, offsetof(Object3D, isSolid), 0,
"Whether this object is solid or wireframe."},
92 {
"shaderParameters", (getter)
Object3D_getShaderParameters, (setter)NULL,
"The dictionary containing the shader parameters, read only.", NULL},
102 PyObject_HEAD_INIT(NULL)
122 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
150 if (PyType_Ready(&Object3DType) < 0)
153 Py_INCREF(&Object3DType);
154 PyModule_AddObject(module,
"Object3D", (PyObject*)&Object3DType);
173 self->ob_type->tp_free((PyObject*)
self);
181 PyObject *
Object3D_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
184 Object3D *
self = (Object3D*)type->tp_alloc(type, 0);
192 self->shaderParameters = NULL;
194 self->inMovableCamera = 1;
195 self->isPickable = 1;
213 self->colors2 = NULL;
222 return (PyObject*)
self;
233 int numVerts, numQuads;
234 PyObject *indexBuffer;
236 if (!PyArg_ParseTuple(args,
"iO", &numVerts, &indexBuffer) || !PyList_Check(indexBuffer))
240 numQuads = (int)PyList_Size(indexBuffer) / 4;
249 self->nVerts = numVerts;
252 self->nNorms = numVerts * 3;
253 self->nQuads = numQuads;
254 self->nColors = numVerts * 3;
255 self->nColors2 = numVerts * 4;
259 PyObject *iterator = PyObject_GetIter(indexBuffer);
263 for (item = PyIter_Next(iterator); item; item = PyIter_Next(iterator))
265 self->quads[index++] = PyInt_AsLong(item);
295 if (!PyArg_ParseTuple(args,
"i(fff)", &index, &x, &y, &z))
298 if (index < 0 || index >= self->nVerts)
300 PyErr_Format(PyExc_IndexError,
"index out of range, %i is not between 0 and %i", index, self->nVerts);
304 self->verts[index * 3] = x;
305 self->verts[index * 3 + 1] = y;
306 self->verts[index * 3 + 2] = z;
308 return Py_BuildValue(
"");
331 if (!PyArg_ParseTuple(args,
"i(fff)", &index, &x, &y, &z))
334 if (index < 0 || index >= self->nVerts)
336 PyErr_Format(PyExc_IndexError,
"index out of range, %i is not between 0 and %i", index, self->nVerts);
340 self->norms[index * 3] = x;
341 self->norms[index * 3 + 1] = y;
342 self->norms[index * 3 + 2] = z;
344 return Py_BuildValue(
"");
365 if (!PyArg_ParseTuple(args,
"i(ff)", &index, &u, &v))
368 if (index < 0 || index >= self->nVerts)
370 PyErr_Format(PyExc_IndexError,
"index out of range, %i is not between 0 and %i", index, self->nVerts);
374 self->UVs[index * 2] = u;
375 self->UVs[index * 2 + 1] = v;
377 return Py_BuildValue(
"");
397 unsigned char r, g, b;
400 if (!PyArg_ParseTuple(args,
"i(BBB)", &index, &r, &g, &b))
403 if (index < 0 || index >= self->nVerts)
405 PyErr_Format(PyExc_IndexError,
"index out of range, %i is not between 0 and %i", index, self->nVerts);
409 self->colors[index * 3] = r;
410 self->colors[index * 3 + 1] = g;
411 self->colors[index * 3 + 2] = b;
413 return Py_BuildValue(
"");
432 unsigned char r, g, b, a;
435 if (!PyArg_ParseTuple(args,
"i(BBBB)", &index, &r, &g, &b, &a))
438 if (index < 0 || index >= self->nVerts)
440 PyErr_Format(PyExc_IndexError,
"index out of range, %i is not between 0 and %i", index, self->nVerts);
444 self->colors2[index * 4] = r;
445 self->colors2[index * 4 + 1] = g;
446 self->colors2[index * 4 + 2] = b;
447 self->colors2[index * 4 + 3] = a;
449 return Py_BuildValue(
"");
459 if (!self->shaderParameters)
460 self->shaderParameters = PyDict_New();
462 Py_INCREF(self->shaderParameters);
463 return self->shaderParameters;
473 return Py_BuildValue(
"[f,f,f]", self->x, self->y, self->z);
484 if (!PySequence_Check(value))
487 if (PySequence_Size(value) != 3)
493 self->x = PyFloat_AsDouble(PySequence_GetItem(value, 0));
494 self->y = PyFloat_AsDouble(PySequence_GetItem(value, 1));
495 self->z = PyFloat_AsDouble(PySequence_GetItem(value, 2));
507 return Py_BuildValue(
"[f,f,f]", self->rx, self->ry, self->rz);
518 if (!PySequence_Check(value))
521 if (PySequence_Size(value) != 3)
527 self->rx = PyFloat_AsDouble(PySequence_GetItem(value, 0));
528 self->ry = PyFloat_AsDouble(PySequence_GetItem(value, 1));
529 self->rz = PyFloat_AsDouble(PySequence_GetItem(value, 2));
541 return Py_BuildValue(
"[f,f,f]", self->sx, self->sy, self->sz);
552 if (!PySequence_Check(value))
555 if (PySequence_Size(value) != 3)
561 self->sx = PyFloat_AsDouble(PySequence_GetItem(value, 0));
562 self->sy = PyFloat_AsDouble(PySequence_GetItem(value, 1));
563 self->sz = PyFloat_AsDouble(PySequence_GetItem(value, 2));
578 if (
G.mouseDownCallback && !PyObject_CallFunction(
G.mouseDownCallback,
"iii", b, x, y))
592 if (
G.mouseUpCallback && !PyObject_CallFunction(
G.mouseUpCallback,
"iii", b, x, y))
610 if (
G.mouseMovedCallback && !PyObject_CallFunction(
G.mouseMovedCallback,
"iiiii", s, x, y, xrel, yrel))
621 void callKeyDown(
int key,
unsigned short character,
int modifiers)
623 if (
G.keyDownCallback &&
625 !PyObject_CallFunction(
G.keyDownCallback,
"iu#i", key, &character, 1, modifiers))
627 !PyObject_CallFunction(
G.keyDownCallback,
"ici", key, key, modifiers))
632 void callKeyUp(
int key,
unsigned short character,
int modifiers)
634 if (
G.keyUpCallback &&
636 !PyObject_CallFunction(
G.keyUpCallback,
"iu#i", key, &character, 1, modifiers))
638 !PyObject_CallFunction(
G.keyUpCallback,
"ici", key, key, modifiers))
645 if (
G.resizeCallback && !PyObject_CallFunction(
G.resizeCallback,
"iii", w, h, fullscreen))
666 iptr = (
float *)malloc(n *
sizeof(
float));
670 for (i = 0; i < n; i++)
677 printf (
"Out of memory!\n");
691 iptr = (
unsigned char *)malloc(n *
sizeof(
unsigned char));
695 for (i = 0; i < n; i++)
702 printf (
"Out of memory!\n");
717 iptr = (
int *)malloc(n *
sizeof(
int));
721 for (i = 0; i < n; i++)
728 printf (
"Out of memory!\n");
PyObject * Object3D_setNormCoo(Object3D *self, PyObject *args)
Sets a single normal component value (x, y or z) for a vertex in G.world.
int Object3D_setRotation(Object3D *self, PyObject *value)
Sets the rotation for this Object3D object as a list.
void callResize(int w, int h, int fullscreen)
void callMouseButtonUp(int b, int x, int y)
Invokes the Python mouseButtonUp function.
int Object3D_setTranslation(Object3D *self, PyObject *value)
Sets the translation for this Object3D object as a list.
PyObject * Object3D_getRotation(Object3D *self, void *closure)
Gets the rotation for this Object3D object as a list.
PyObject * Object3D_setUVCoo(Object3D *self, PyObject *args)
Sets a single UV component value (U or V) for a vertex in G.world.
static PyGetSetDef Object3D_getset[]
PyObject * Object3D_getShaderParameters(Object3D *self, void *closure)
Gets the shader parameter dictionary for this Object3D object.
PyObject * Object3D_setVertCoo(Object3D *self, PyObject *args)
Sets a single coordinate value (x, y or z) for a vertex in G.world.
PyObject * Object3D_setColorComponent(Object3D *self, PyObject *args)
Sets a single color component value (R, G, B or A) for a vertex in G.world.
void callKeyUp(int key, unsigned short character, int modifiers)
PyObject * Object3D_setColorIDComponent(Object3D *self, PyObject *args)
Sets a single color component value (R, G or B) for a vertex in G.world.
unsigned char * makeUCharArray(int n)
Creates an array of n characters and returns a pointer to that array.
PyObject * Object3D_getTranslation(Object3D *self, void *closure)
Gets the translation for this Object3D object as a list.
void RegisterObject3D(PyObject *module)
Registers the Object3D object in the Python environment.
int Object3D_init(Object3D *self, PyObject *args, PyObject *kwds)
The constructor of the Object3D object.
static PyMethodDef Object3D_methods[]
PyObject * Object3D_getScale(Object3D *self, void *closure)
Gets the scale for this Object3D object as a list.
void callKeyDown(int key, unsigned short character, int modifiers)
Invokes the Python keyDown function.
PyObject * Object3D_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Takes care of the initialization of the Object3D object members.
void callMouseButtonDown(int b, int x, int y)
Invokes the Python mouseButtonDown function.
void Object3D_dealloc(Object3D *self)
Takes care of the deallocation of the vertices, faces and text the Object3D object.
void callMouseMotion(int s, int x, int y, int xrel, int yrel)
Invokes the Python mouseMotion function.
void setClearColor(float r, float g, float b, float a)
static PyMemberDef Object3D_members[]
PyTypeObject Object3DType
int * makeIntArray(int n)
Creates an array of n integers and returns a pointer to that array.
float * makeFloatArray(int n)
Creates an array of n floats and returns a pointer to that array.
int Object3D_setScale(Object3D *self, PyObject *value)
Sets the scale for this Object3D object as a list.