Introduction
Pyglet is a simple yet powerful library for creating aesthetically rich GUI programs on Windows, Mac OS, and Linux, such as games and multimedia. This Python-only package includes capabilities such as windowing, user interface event management, joysticks, OpenGL graphics, loading photos and movies, and playing sounds and music. Pyglet is released under the BSD open-source licence, which allows it to be used in both commercial and open-source projects with few restrictions. The article explains the details of the pyglet.graphics.vertexbuffer module, different classes of pyglet.graphics.vertexbuffer, along with its predefined functions.
Pyglet.graphics.vertexbuffer
Vertex Buffer Objects and vertex arrays are represented as byte abstractions. A Vertex Buffer Object is created with create_buffer() or create_mappable_buffer(), or a vertex array if the current context does not allow VBOs.
Buffers can be made "mappable" if desired (incorporating the AbstractMappable mix-in). The buffer provides a get_region() method in this scenario, which is the most efficient way of updating partial data within the buffer.
Let's have a detailed discussion on the various classes and methods of pyglet.graphics.vertexbuffer.
1.classAbstractBuffer
Abstract buffer of byte data.
Variables:
- size (int) – It represents the size of the buffer in bytes
- ptr (int) – It represents the memory offset of the buffer, as used by the glVertexPointer family of functions
- target (int) – OpenGL buffer target, for example, GL_ARRAY_BUFFER
- usage (int) – OpenGL buffer usage, for example, GL_DYNAMIC_DRAW
bind()
Bind this buffer to its OpenGL target.
delete()
Delete this buffer, reducing system resource usage.
map(invalidate=False)
It fills the system memory with the entire buffer.
Before executing any other operations on the buffer, the mapped region must be unmapped with unmap.
Parameters:
- invalidate (bool) – If True, the mapped block's initial contents do not have to match the buffer's real contents.
Return type: POINTER(ctypes.c_ubyte)
Returns: It returns the pointer to the mapped block in memory
resize(size)
It's used to change the size of the buffer.
Parameters:
- size (int) – The buffer's new size, in bytes
set_data(data)
It's used to set the buffer's whole contents.
Parameters:
- data (ctypes pointer) – The byte array to set.
set_data_region(data, start, length)
It is used to set part of the buffer contents.
Parameters:
- data (ctypes pointer) – The byte array of data to set
- start (int) – This is the offset to start replacing data
- length (int) – It represents the length of the region to replace
unbind()
It is used to reset the buffer’s OpenGL target.
unmap()
It unmaps a previously mapped memory block.
ptr= 0
size= 0
2.classAbstractBufferRegion
It's a buffer's mapped region.get_region() is used to get the buffer regions.
Variables:
- array (ctypes array) – This is the Array of data requested by get_region().
invalidate()
Mark this area as modified. Until this function is called, the buffer may not be updated with the array's most recent contents. (However, for efficiency, it may not be updated until the next time the buffer is utilised.)
3.classAbstractMappable
get_region(start, size, ptr_type)
Transform a section of the buffer into the desired type's ctypes array. This section does not need to be unmapped, but if the buffer is resized, it will become invalid.
An array is mapped, despite the fact that a pointer type is required. For instance:
get_region(0, ctypes.sizeof(c_int) * 20, ctypes.POINTER(c_int * 20))
Bytes 0 to 80 of the buffer will be mapped to a 20-int array.
Until the region's AbstractBufferRegion.invalidate() method is executed, changes to the array may not be recognised.
Parameters:
- start (int) – This is the offset into the buffer to map from (bytes)
- size (int) – It depicts the size of the buffer region to map (bytes)
- ptr_type (ctypes pointer type) – It is the Pointer type that describes the array format to create
Return type:
AbstractBufferRegion
4.classIndirectArrayRegion(region, size, component_count, component_stride)
A mapped area whose data items are not always contiguous.
This region class is designed to surround buffer zones that require stride access to the data. In an interleaved buffer, for example, this region can be utilised to access a single interleaved component as if it were contiguous data.
invalidate()
Mark this area as modified. Until this function is called, the buffer may not be updated with the array's most recent contents. (However, for efficiency, it may not be updated until the next time the buffer is utilised.)
5.classMappableVertexBufferObject(size, target, usage)
A VBO has a store backed by system memory. Set data(), set data region(), and map() updates to the data are retained in local memory until bind() is called from this class. The benefit is that fewer OpenGL calls are required, which improves performance. Resizing this buffer may also have a lower performance cost. Data updates made with map() are immediately committed.
bind()
Bind this buffer to its OpenGL target.
get_region(start, size, ptr_type)
Transform a section of the buffer into the desired type's ctypes array. This section does not need to be unmapped, but if the buffer is resized, it will become invalid.
An array is mapped, despite the fact that a pointer type is required. For instance:
get_region(0, ctypes.sizeof(c_int) * 20, ctypes.POINTER(c_int * 20))
Bytes 0 to 80 of the buffer will be mapped to a 20-int array.
Until the region's AbstractBufferRegion.invalidate() method is executed, changes to the array may not be recognised.
Parameters:
- start (int) – This is the offset into the buffer to map from (bytes)
- size (int) – It depicts the size of the buffer region to map (bytes)
- ptr_type (ctypes pointer type) – It is the pointer type that describes the array format to create
Return type : AbstractBufferRegion
map(invalidate=False)
It maps the system memory with the entire buffer. Before executing any other operations on the buffer, the mapped region must be unmapped with unmap.
Parameters:
- invalidate (bool) – If True, the initial contents of the mapped block need not reflect the actual contents of the buffer.
Return type : POINTER(ctypes.c_ubyte)
Returns: Pointer to the mapped block in memory
resize(size)
It is used to resize the buffer to a new size.
Parameters:
- size (int) – It depicts the new size of the buffer region (bytes)
set_data(data)
It is used to set the entire contents of the buffer.
Parameters:
- data (ctypes pointer) – The byte array to set.
set_data_region(data, start, length)
It is used to set part of the buffer contents.
Parameters:
- data (ctypes pointer) – It is used to set the byte array of data
- start (int) – It represents the Offset to start replacing data
- length (int) – This is the length of the region to replace
unmap()
It unmaps a previously mapped memory block.
6.classVertexArray(size)
A vertex array is implemented in ctypes. Many of the methods on this class, such as bind(), unbind(), map(), unmap(), and delete(), are functionally no-ops; they exist to provide a consistent interface with VertexBufferObject. Get region() can be utilised because this buffer type is mappable.
bind()
Bind this buffer to its OpenGL target.
delete()
Delete this buffer, reducing system resource usage.
get_region(start, size, ptr_type)
Transform a section of the buffer into the desired type's ctypes array. This section does not need to be unmapped, but if the buffer is resized, it will become invalid.
An array is mapped, despite the fact that a pointer type is required. For instance:
get_region(0, ctypes.sizeof(c_int) * 40, ctypes.POINTER(c_int * 40))
It will convert the buffer's bytes 0 to 80 into a 40-int array. Until the region's AbstractBufferRegion.invalidate() method is executed, changes to the array may not be recognised.
Parameters:
- start (int) – This is the offset into the buffer to map from (bytes)
- size (int) – It depicts the size of the buffer region to map (bytes)
- ptr_type (ctypes pointer type) – It is the Pointer type that describes the array format to create
Return type:
It returns the abstractBufferRegion.
map(invalidate=False)
It maps the system memory with the entire buffer.
Before executing any other operations on the buffer, the mapped region must be unmapped with unmap.
Parameters:
- invalidate (bool) – If True, the mapped block's initial contents do not have to match the buffer's real contents.
Return type : POINTER(ctypes.c_ubyte)
Returns: It returns the pointer to the mapped block in memory
resize(size)
It's used to change the size of the buffer.
Parameters:
- size (int) – It sets the new size of the buffer in bytes
set_data(data)
It changes the buffer's whole contents.
Parameters:
- data ( ctypes pointer) – It contains the byte array to set.
set_data_region(data, start, length)
It is used to set part of the buffer contents.
Parameters:
- data (ctypes pointer) – The byte array of data to set
- start (int) – This is the offset to start replacing data
- length (int) – It represents the length of the region to replace
unbind()
It is used to reset the buffer’s OpenGL target.
unmap()
It unmaps a previously mapped memory block.
7.classVertexArrayRegion(array)
It is a vertex array's mapped region. The invalidate() method is a no-op, but it's included to keep the VertexBufferObjectRegion() interface consistent.
8.classVertexBufferObject(size, target, usage)
It is a lightweight OpenGL VBO representation. There is no system memory replication of the data in the buffer (unless it is done so by the video driver). While this may reduce memory usage and improve performance, changes to the buffer are slow. This class lacks the get region() method since it does not implement AbstractMappable.
bind()
Bind this buffer to its OpenGL target.
delete()
Delete this buffer, reducing system resource usage.
map(invalidate=False)
It maps the system memory with the entire buffer. Before executing any other operations on the buffer, the mapped region must be unmapped with unmap.
Parameters:
- invalidate (bool) –If True, the mapped block's initial contents do not have to match the buffer's real contents.
Return type : POINTER(ctypes.c_ubyte)
Returns: A pointer to the memory block that has been mapped.
resize(size)
It's used to change the size of the buffer.
Parameters:
- size (int) – This is the new size of the buffer, in bytes
set_data(data)
It is used to set the entire contents of the buffer.
Parameters:
- data (ctypes pointer) – The byte array to set.
set_data_region(data, start, length)
It's used to set the contents of a portion of the buffer.
Parameters:
- data (ctypes pointer) – It is used to set the byte array of data
- start (int) – This is the Offset to start replacing data
- length (int) – It represents the length of the region to replace
unbind()
It is used to reset the buffer’s OpenGL target.
unmap()
It unmaps a previously mapped memory block.
9.classVertexBufferObjectRegion(buffer, start, end, array)
It is a mapped region of a VBO.
invalidate()
It marks this region as changed. Until this function is called, the buffer may not be updated with the array's most recent contents. (However, for efficiency, it may not be updated until the next time the buffer is utilised.)
create_buffer(size, target=34862, usage=35048, vbo=True)
It is used to create a buffer of vertex data.
Parameters:
- size (int) – This is the size of the buffer in bytes
- target (int) – It consists of the OpenGL target buffer
- usage (int) – It consists of OpenGL usage constant
- vbo (bool) – If the driver supports it, a VertexBufferObject should be produced(true); otherwise, only a VertexArray is formed.
Return type:
It returns the abstractBuffer
10.create_mappable_buffer(size, target=34862, usage=35048, vbo=True)
It is used to create a mappable buffer of vertex data.
Parameters:
- size (int) – This is the size of the buffer in bytes
- target (int) – It consists of the OpenGL target buffer
- usage (int) – It consists of OpenGL usage constant
- vbo (bool) – If the driver supports it, a VertexBufferObject should be produced(true); otherwise, only a VertexArray is formed.
Return type:
AbstractBuffer with AbstractMappable