def draw(size, mode, *data): '''Draw a primitive immediately. :Parameters: `size` : int Number of vertices given `mode` : int OpenGL drawing mode, e.g. ``GL_TRIANGLES`` `data` : data items Attribute formats and data. See the module summary for details. ''' glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) buffers = [] for format, array in data: attribute = vertexattribute.create_attribute(format) assert size == len(array) // attribute.count, \ 'Data for %s is incorrect length' % format buffer = vertexbuffer.create_mappable_buffer(size * attribute.stride, vbo=False) attribute.set_region(buffer, 0, size, array) attribute.enable() attribute.set_pointer(buffer.ptr) buffers.append(buffer) glDrawArrays(mode, 0, size) glFlush() glPopClientAttrib()
def draw(size, mode, *data): '''Draw a primitive immediately. :Parameters: `size` : int Number of vertices given `mode` : int OpenGL drawing mode, e.g. ``GL_TRIANGLES`` `data` : data items Attribute formats and data. See the module summary for details. ''' glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) buffers = [] for format, array in data: attribute = vertexattribute.create_attribute(format) assert size == len(array) // attribute.count, \ 'Data for %s is incorrect length' % format buffer = vertexbuffer.create_mappable_buffer( size * attribute.stride, vbo=False) attribute.set_region(buffer, 0, size, array) attribute.enable() attribute.set_pointer(buffer.ptr) buffers.append(buffer) glDrawArrays(mode, 0, size) glFlush() glPopClientAttrib()
def create_attribute_usage(format): '''Create an attribute and usage pair from a format string. The format string is as documented in `pyglet.graphics.vertexattribute`, with the addition of an optional usage component:: usage ::= attribute ( '/' ('static' | 'dynamic' | 'stream' | 'none') )? If the usage is not given it defaults to 'dynamic'. The usage corresponds to the OpenGL VBO usage hint, and for ``static`` also indicates a preference for interleaved arrays. If ``none`` is specified a buffer object is not created, and vertex data is stored in system memory. Some examples: ``v3f/stream`` 3D vertex position using floats, for stream usage ``c4b/static`` 4-byte color attribute, for static usage :return: attribute, usage ''' match = _usage_format_re.match(format) attribute_format = match.group('attribute') attribute = vertexattribute.create_attribute(attribute_format) usage = match.group('usage') if usage: vbo = not usage == 'none' usage = _gl_usages[usage] else: usage = GL_DYNAMIC_DRAW vbo = True return (attribute, usage, vbo)
def draw_indexed(size, mode, indices, *data): '''Draw a primitive with indexed vertices immediately. :Parameters: `size` : int Number of vertices given `mode` : int OpenGL drawing mode, e.g. ``GL_TRIANGLES`` `indices` : sequence of int Sequence of integers giving indices into the vertex list. `data` : data items Attribute formats and data. See the module summary for details. ''' glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) buffers = [] for format, array in data: attribute = vertexattribute.create_attribute(format) assert size == len(array) // attribute.count, \ 'Data for %s is incorrect length' % format buffer = vertexbuffer.create_mappable_buffer(size * attribute.stride, vbo=False) attribute.set_region(buffer, 0, size, array) attribute.enable() attribute.set_pointer(buffer.ptr) buffers.append(buffer) if size <= 0xff: index_type = GL_UNSIGNED_BYTE index_c_type = ctypes.c_ubyte elif size <= 0xffff: index_type = GL_UNSIGNED_SHORT index_c_type = ctypes.c_ushort else: index_type = GL_UNSIGNED_INT index_c_type = ctypes.c_uint index_array = (index_c_type * len(indices))(*indices) glDrawElements(mode, len(indices), index_type, index_array) glFlush() glPopClientAttrib()
def draw_indexed(size, mode, indices, *data): '''Draw a primitive with indexed vertices immediately. :Parameters: `size` : int Number of vertices given `mode` : int OpenGL drawing mode, e.g. ``GL_TRIANGLES`` `indices` : sequence of int Sequence of integers giving indices into the vertex list. `data` : data items Attribute formats and data. See the module summary for details. ''' glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) buffers = [] for format, array in data: attribute = vertexattribute.create_attribute(format) assert size == len(array) // attribute.count, \ 'Data for %s is incorrect length' % format buffer = vertexbuffer.create_mappable_buffer( size * attribute.stride, vbo=False) attribute.set_region(buffer, 0, size, array) attribute.enable() attribute.set_pointer(buffer.ptr) buffers.append(buffer) if size <= 0xff: index_type = GL_UNSIGNED_BYTE index_c_type = ctypes.c_ubyte elif size <= 0xffff: index_type = GL_UNSIGNED_SHORT index_c_type = ctypes.c_ushort else: index_type = GL_UNSIGNED_INT index_c_type = ctypes.c_uint index_array = (index_c_type * len(indices))(*indices) glDrawElements(mode, len(indices), index_type, index_array) glFlush() glPopClientAttrib()