To main page | 3dengine.org

Example of NumPy in PyOpenGL Call


If you came here looking for PyOpenGL example - try Spectator (PyOpenGL).

Example:
points=numpy.array([2.4,0,0,0,0,2,0,0,0,0,0,2,2.4,0,0,\
2.4,0,2,0,0,2,0,-1.66,0,0,0,0,0,-1.66,0,2.4,0,0,0,0,0,\
2.4,0,0,2.4,-1.66,2,2.4,0,2,2.4,-1.66,2,0,0,2,2.4,0,2,0,\
-1.66,0,0,0,2,0,-1.66,2,2.4,0,0,0,-1.66,0,2.4,-1.66,0,\
2.4,-1.66,2,2.4,0,0,2.4,-1.66,0,0,0,2,2.4,-1.66,2,0,\
-1.66,2,2.4,-1.66,2,0,-1.66,0,0,-1.66,2,0,-1.66,0,2.4,
-1.66,2,2.4,-1.66,0],'f').reshape(-1,3)

indices=numpy.arange(36,'i')

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointerf( points )
glDrawElementsui(GL_TRIANGLES, indices)
Basically some functions just accept NumPy arrays as variables. And this call is extremely fast. It can draw millions of points at real time. Even if you modify points values in between.

To compare speed - raw PyOpenGL suffers from speed issues at 300 vertices via glVertex3f.
points[6,2] = 5.   # set Z of point #7 (zero-based)
points[0,2] = 5.   # set Z of point #1 (zero-based)
points[9,0] = 5.   # set X of point #10 (zero-based)

See also

OpenGL and Cython