[PYTHON MATRIX-SIG] Python OpenGL Module and NumPy

Tom Schwaller tom.schwaller@linux-magazin.de
Fri, 02 May 1997 19:02:21 +0200


Kevin Rodgers (x39079, WT-07) wrote:
> 
> (I'm crossposting to both the main list and the matrix-sig because I'm not
> sure where this best fits)
> 
> I'm using the OpenGL module to do a flight simulation program, and have run
> into a situation where I'm confused about the integration of NumPy and the
> OpenGL module.  I'd like to read in a vehicle definition that may consist of
> hundreds of polygons, each with potentially hundreds of vertices, then render
> these with OpenGL.  This seems like a natural integration of PyOpenGL and
> NumPy; for each polygon, you could pass a Numeric array of dimension (3,nPoly)
> where nPoly is the number of polygons and 3 is the number of coordinates per
> vertex; the C implementation of PyOpenGL would then call gl_Vertex3* as
> appropriate.  This would obviously be much faster than writing the calls to
> gl.Vertex3* as a loop in Python.  I've looked through the source to PyOpenGL,
> though, and it appears that only the gl.ColorVertex2 function actually
> implements this.  Am I correct?  If not, then how does one use Numeric with
> PyOpenGL?  If I am correct, has anyone implemented the functionality I want,
> or should I bite the bullet and do it myself?  Thanks in advance for any help.
> --
> Kevin Rodgers    Lockheed Martin Vought Systems    rodgers@vs.lmco.com
> "This one goes up to eleven."  --  Nigel Tufnel
> 

One should implement the OpenGL1.1 features,
I was using Mesa and could not try that out for a long time,
but that's history now :-)
I startet with it a while ago, but got some strange problems,
which are still unresolved. Maybe you give it a try..
Forget the ColorVertex2 stuff, this should become obsolete
with a clean OpenGL 1.1 implementation


/* 
############################### OpenGL 1.1 ########################
*/

#ifdef NUMERIC

static int type2gl[] = {-1, GL_UNSIGNED_BYTE, GL_BYTE, GL_SHORT, GL_INT,
-1, GL_FLOAT, GL_DOUBLE, -1, -1, -1, -1};

static PyObject* gl_VertexPointer(PyObject* self, PyObject* args)
{
	GLint size;
	GLenum type;
	GLsizei stride;
	PyObject *op;
	PyArrayObject *ap;	
	TRY (PyArg_ParseTuple(args, "iiO", &size, &stride, &op)); 
	if (PyArray_Check(op)) 
		ap = (PyArrayObject *)op;
	else {
		TRY(ap = (PyArrayObject *)PyArray_ContiguousFromObject(op,
PyArray_DOUBLE, 1, 0));
	}
	type = type2gl[ap->descr->type_num];
	ASSERT(type != -1, "Can't convert this type of array!");
	glVertexPointer(size, type, stride, ap->data);
	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject* gl_ColorPointer(PyObject* self, PyObject* args)
{
	GLint size;
	GLenum type;
	GLsizei stride;
	PyObject *op;
	PyArrayObject *ap;	
	TRY (PyArg_ParseTuple(args, "iiO", &size, &stride, &op)); 
	if (PyArray_Check(op)) 
		ap = (PyArrayObject *)op;
	else {
		TRY(ap = (PyArrayObject *)PyArray_ContiguousFromObject(op,
PyArray_DOUBLE, 1, 0));
	}
	type = type2gl[ap->descr->type_num];
	ASSERT(type != -1, "Can't convert this type of array!");
	glColorPointer(size, type, stride, ap->data);
	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject* gl_DrawArrays(PyObject* self, PyObject* args)
{
	GLenum mode;
	GLint first;
	GLsizei count;
	TRY (PyArg_ParseTuple(args, "iii", &mode, &first, &count)); 
	glDrawArrays(mode, first, count);
	Py_INCREF(Py_None);
	return Py_None;
}

#endif /* Not NUMERIC */


...................


	{"TranslateScene",	gl_TranslateScene, 		1 },
	{"RotateScene", 	gl_RotateScene, 		1 },
	{"DistFromLine",	gl_DistFromLine,		1 },


This is the old code which does not work corrrrectly,
just go on with it.. It's the way to go..


-- 

Tom

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________