Table of contents
1.
Introduction
2.
Pyglet.graphics.vertexbuffer
2.1.
1.classAbstractBuffer
2.1.1.
bind()
2.1.2.
delete()
2.1.3.
map(invalidate=False)
2.1.4.
resize(size)
2.1.5.
set_data(data)
2.1.6.
set_data_region(data, start, length)
2.1.7.
unbind()
2.1.8.
unmap()
2.2.
2.classAbstractBufferRegion
2.2.1.
invalidate()
2.3.
3.classAbstractMappable
2.3.1.
get_region(start, size, ptr_type)
2.4.
4.classIndirectArrayRegion(region, size, component_count, component_stride)
2.4.1.
invalidate()
2.5.
5.classMappableVertexBufferObject(size, target, usage)
2.5.1.
bind()
2.5.2.
get_region(start, size, ptr_type)
2.5.3.
map(invalidate=False)
2.5.4.
resize(size)
2.5.5.
set_data(data)
2.5.6.
set_data_region(data, start, length)
2.5.7.
unmap()
2.6.
6.classVertexArray(size)
2.6.1.
bind()
2.6.2.
delete()
2.6.3.
get_region(start, size, ptr_type)
2.6.4.
map(invalidate=False)
2.6.5.
resize(size)
2.6.6.
set_data(data)
2.6.7.
set_data_region(data, start, length)
2.6.8.
unbind()
2.6.9.
unmap()
2.7.
7.classVertexArrayRegion(array)
2.8.
8.classVertexBufferObject(size, target, usage)
2.8.1.
bind()
2.8.2.
delete()
2.8.3.
map(invalidate=False)
2.8.4.
resize(size)
2.8.5.
set_data(data)
2.8.6.
set_data_region(data, start, length)
2.8.7.
unbind()
2.8.8.
unmap()
2.9.
9.classVertexBufferObjectRegion(buffer, start, end, array)
2.9.1.
invalidate()
2.9.2.
create_buffer(size, target=34862, usage=35048, vbo=True)
2.10.
10.create_mappable_buffer(size, target=34862, usage=35048, vbo=True)
3.
Frequently Asked Questions
3.1.
What is a pyglet?
3.2.
How is a vertex buffer object created?
3.3.
What is classAbstractBufferRegion?
3.4.
What is classAbstractBuffer?
4.
Conclusion
Last Updated: Mar 27, 2024

pyglet.graphics.vertexbuffer

Author Nagendra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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))
You can also try this code with Online Python Compiler
Run Code

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))
You can also try this code with Online Python Compiler
Run Code

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))
You can also try this code with Online Python Compiler
Run Code

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

Frequently Asked Questions

What is a pyglet?

Pyglet is a Python library that provides an object-oriented application programming interface for the development of games and other multimedia applications.

How is a vertex buffer object created?

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.

What is classAbstractBufferRegion?

It's a buffer's mapped region.get_region() is used to get the buffer regions.

What is classAbstractBuffer?

classAbstractBuffer is the abstract buffer of byte data.

Conclusion

In this article, we have extensively discussed pyglet.graphics.vertexbuffer. The article explains the details of pyglet.graphics.vertexbuffer, and details of its different classes and predefined functions.
We hope that this blog has helped you enhance your knowledge regarding pyglet.graphics.vertexbuffer, and if you would like to learn more, check out our articles on pyglet. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. 

Happy Coding!!

Live masterclass