On 21 October 2011 17:26, Gary Daniels <[email protected]> wrote:
> On Oct 21, 1:21 am, Richard Jones <[email protected]> wrote:
>> that doesn't work out then yeah, you'll probably have to write a
>> simple extension (in C or, more nicely, cython) which manipulates a
>> vertex buffer directly.
>
> I tried to work with a vertex buffer using glBegin, glEnd, etc. and it
> got 0.2 fps. Do you know of a simple example I can look at to see how
> to do it correctly? I think I was just doing something wrong.
glBegin, glEnd and etc (assuming you mean glVertex* etc) are the
old-style "immediate mode" way of doing OpenGL which involve a lot of
calls into OpenGL. This is very slow in pyglet, and slower than it
needs to be even in C.
That mode of working with OpenGL is still supported but deprecated and
replaced with Vertex Buffer Objects (VBOs). These are the objects that
pyglet's graphics module encapsulates (though on some older
implementations of OpenGL it will fall back to using vertex arrays, an
older pre-VBO approach). The benefit of VBOs is that you dump all the
geometry information into OpenGL (and therefore the hardware) in one
go. For example, some code from one of my projects (it's cython, so
it's pretty close to C in Python):
glGenBuffers(1, &(self.vbo))
gl_error_check()
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
gl_error_check()
cdef GLvoid *data
data = malloc(some amount of memory)
... load the triangle, normal and color information into a
memory at "data"
glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_DYNAMIC_DRAW)
free(data)
gl_error_check()
... then later drawing that vbo is achieved by these few calls:
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
glVertexPointer(3, GL_FLOAT, 0, NULL)
glNormalPointer(GL_FLOAT, 0,
<GLvoid *>(<char *>NULL + <ssize_t>self.vertex_size))
glColorPointer(self.bytes_per_color, GL_UNSIGNED_BYTE, 0,
<GLvoid *>(<char *>NULL + <ssize_t>self.vertex_size +
<ssize_t>self.normal_size))
glDrawArrays(GL_TRIANGLES, 0, 3 * self.num_triangles)
There's obviously some other stuff going on here to set the other
variables you see, but these are the OpenGL calls of interest.
Note that the above is very few calls into OpenGL so should be quite
reasonable to do from Python directly - as the pyglet.graphics module
does.
Richard
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en.