Bases: ABC
Abstract base class for Vertex Array Objects (VAOs).
Defines the interface for different VAO implementations, including
methods for binding, drawing, setting data, and managing the VAO's
lifecycle.
Source code in ncca/ngl/opengl/abstract_vao.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 | class AbstractVAO(abc.ABC):
"""Abstract base class for Vertex Array Objects (VAOs).
Defines the interface for different VAO implementations, including
methods for binding, drawing, setting data, and managing the VAO's
lifecycle.
"""
def __init__(self, mode: int = gl.GL_TRIANGLES) -> None:
"""Generate a new OpenGL VAO id and initialize default state.
Args:
mode: OpenGL primitive drawing mode (e.g. GL_TRIANGLES).
"""
self.id = gl.glGenVertexArrays(1)
self.mode = mode
self.bound = False
self.allocated = False
self.indices_count = 0
def bind(self) -> None:
"""Bind this VAO as the current OpenGL vertex array."""
gl.glBindVertexArray(self.id)
self.bound = True
def unbind(self) -> None:
"""Unbind the current OpenGL vertex array."""
gl.glBindVertexArray(0)
self.bound = False
def __enter__(self) -> "AbstractVAO":
"""Bind the VAO on entering a `with` block."""
self.bind()
return self
def __exit__(self, exc_type: type, exc_val: BaseException, exc_tb: Any) -> None:
"""Unbind the VAO on exiting a `with` block."""
self.unbind()
@abc.abstractmethod
def draw(self) -> None:
"""Draw the contents of this VAO."""
raise NotImplementedError
@abc.abstractmethod
def set_data(self, data: VertexData) -> None:
"""Upload vertex data to the VAO's buffer(s)."""
raise NotImplementedError
@abc.abstractmethod
def remove_vao(self) -> None:
"""Delete the VAO and its associated buffers."""
raise NotImplementedError
def set_vertex_attribute_pointer(
self,
id: int,
size: int,
type: int,
stride: int,
offset: int,
normalize: bool = False,
) -> None:
"""Configure and enable a vertex attribute pointer for the bound buffer.
Args:
id: Attribute location index.
size: Number of components per vertex attribute.
type: OpenGL data type of each component (e.g. GL_FLOAT).
stride: Byte offset between consecutive vertex attributes.
offset: Byte offset of the first component in the buffer.
normalize: Whether integer data should be normalized.
"""
if not self.bound:
logger.error("VAO not bound in set_vertex_attribute_pointer")
gl.glVertexAttribPointer(
id, size, type, normalize, stride, ctypes.c_void_p(offset)
)
gl.glEnableVertexAttribArray(id)
def set_num_indices(self, count: int) -> None:
"""Set the number of indices/vertices to draw."""
self.indices_count = count
def num_indices(self) -> int:
"""Return the number of indices/vertices to draw."""
return self.indices_count
def get_mode(self) -> int:
"""Return the OpenGL primitive drawing mode."""
return self.mode
def set_mode(self, mode: int) -> None:
"""Set the OpenGL primitive drawing mode."""
self.mode = mode
@abc.abstractmethod
def get_buffer_id(self, index: int = 0) -> int:
"""Return the OpenGL buffer id at the given index."""
raise NotImplementedError
@abc.abstractmethod
def map_buffer(self, index: int = 0, access_mode: int = gl.GL_READ_WRITE) -> Any:
"""Map the buffer at the given index into client memory."""
raise NotImplementedError
def unmap_buffer(self) -> None:
"""Unmap the currently mapped GL_ARRAY_BUFFER."""
gl.glUnmapBuffer(gl.GL_ARRAY_BUFFER)
def get_id(self) -> int:
"""Return the OpenGL VAO id."""
return self.id
|
__enter__()
Bind the VAO on entering a with block.
Source code in ncca/ngl/opengl/abstract_vao.py
| def __enter__(self) -> "AbstractVAO":
"""Bind the VAO on entering a `with` block."""
self.bind()
return self
|
__exit__(exc_type, exc_val, exc_tb)
Unbind the VAO on exiting a with block.
Source code in ncca/ngl/opengl/abstract_vao.py
| def __exit__(self, exc_type: type, exc_val: BaseException, exc_tb: Any) -> None:
"""Unbind the VAO on exiting a `with` block."""
self.unbind()
|
__init__(mode=gl.GL_TRIANGLES)
Generate a new OpenGL VAO id and initialize default state.
| Parameters: |
-
mode
(int, default:
GL_TRIANGLES
)
–
OpenGL primitive drawing mode (e.g. GL_TRIANGLES).
|
Source code in ncca/ngl/opengl/abstract_vao.py
42
43
44
45
46
47
48
49
50
51
52 | def __init__(self, mode: int = gl.GL_TRIANGLES) -> None:
"""Generate a new OpenGL VAO id and initialize default state.
Args:
mode: OpenGL primitive drawing mode (e.g. GL_TRIANGLES).
"""
self.id = gl.glGenVertexArrays(1)
self.mode = mode
self.bound = False
self.allocated = False
self.indices_count = 0
|
bind()
Bind this VAO as the current OpenGL vertex array.
Source code in ncca/ngl/opengl/abstract_vao.py
| def bind(self) -> None:
"""Bind this VAO as the current OpenGL vertex array."""
gl.glBindVertexArray(self.id)
self.bound = True
|
draw()
abstractmethod
Draw the contents of this VAO.
Source code in ncca/ngl/opengl/abstract_vao.py
| @abc.abstractmethod
def draw(self) -> None:
"""Draw the contents of this VAO."""
raise NotImplementedError
|
get_buffer_id(index=0)
abstractmethod
Return the OpenGL buffer id at the given index.
Source code in ncca/ngl/opengl/abstract_vao.py
| @abc.abstractmethod
def get_buffer_id(self, index: int = 0) -> int:
"""Return the OpenGL buffer id at the given index."""
raise NotImplementedError
|
get_id()
Return the OpenGL VAO id.
Source code in ncca/ngl/opengl/abstract_vao.py
| def get_id(self) -> int:
"""Return the OpenGL VAO id."""
return self.id
|
get_mode()
Return the OpenGL primitive drawing mode.
Source code in ncca/ngl/opengl/abstract_vao.py
| def get_mode(self) -> int:
"""Return the OpenGL primitive drawing mode."""
return self.mode
|
map_buffer(index=0, access_mode=gl.GL_READ_WRITE)
abstractmethod
Map the buffer at the given index into client memory.
Source code in ncca/ngl/opengl/abstract_vao.py
| @abc.abstractmethod
def map_buffer(self, index: int = 0, access_mode: int = gl.GL_READ_WRITE) -> Any:
"""Map the buffer at the given index into client memory."""
raise NotImplementedError
|
num_indices()
Return the number of indices/vertices to draw.
Source code in ncca/ngl/opengl/abstract_vao.py
| def num_indices(self) -> int:
"""Return the number of indices/vertices to draw."""
return self.indices_count
|
remove_vao()
abstractmethod
Delete the VAO and its associated buffers.
Source code in ncca/ngl/opengl/abstract_vao.py
| @abc.abstractmethod
def remove_vao(self) -> None:
"""Delete the VAO and its associated buffers."""
raise NotImplementedError
|
set_data(data)
abstractmethod
Upload vertex data to the VAO's buffer(s).
Source code in ncca/ngl/opengl/abstract_vao.py
| @abc.abstractmethod
def set_data(self, data: VertexData) -> None:
"""Upload vertex data to the VAO's buffer(s)."""
raise NotImplementedError
|
set_mode(mode)
Set the OpenGL primitive drawing mode.
Source code in ncca/ngl/opengl/abstract_vao.py
| def set_mode(self, mode: int) -> None:
"""Set the OpenGL primitive drawing mode."""
self.mode = mode
|
set_num_indices(count)
Set the number of indices/vertices to draw.
Source code in ncca/ngl/opengl/abstract_vao.py
| def set_num_indices(self, count: int) -> None:
"""Set the number of indices/vertices to draw."""
self.indices_count = count
|
set_vertex_attribute_pointer(id, size, type, stride, offset, normalize=False)
Configure and enable a vertex attribute pointer for the bound buffer.
| Parameters: |
-
id
(int)
–
Attribute location index.
-
size
(int)
–
Number of components per vertex attribute.
-
type
(int)
–
OpenGL data type of each component (e.g. GL_FLOAT).
-
stride
(int)
–
Byte offset between consecutive vertex attributes.
-
offset
(int)
–
Byte offset of the first component in the buffer.
-
normalize
(bool, default:
False
)
–
Whether integer data should be normalized.
|
Source code in ncca/ngl/opengl/abstract_vao.py
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 | def set_vertex_attribute_pointer(
self,
id: int,
size: int,
type: int,
stride: int,
offset: int,
normalize: bool = False,
) -> None:
"""Configure and enable a vertex attribute pointer for the bound buffer.
Args:
id: Attribute location index.
size: Number of components per vertex attribute.
type: OpenGL data type of each component (e.g. GL_FLOAT).
stride: Byte offset between consecutive vertex attributes.
offset: Byte offset of the first component in the buffer.
normalize: Whether integer data should be normalized.
"""
if not self.bound:
logger.error("VAO not bound in set_vertex_attribute_pointer")
gl.glVertexAttribPointer(
id, size, type, normalize, stride, ctypes.c_void_p(offset)
)
gl.glEnableVertexAttribArray(id)
|
unbind()
Unbind the current OpenGL vertex array.
Source code in ncca/ngl/opengl/abstract_vao.py
| def unbind(self) -> None:
"""Unbind the current OpenGL vertex array."""
gl.glBindVertexArray(0)
self.bound = False
|
unmap_buffer()
Unmap the currently mapped GL_ARRAY_BUFFER.
Source code in ncca/ngl/opengl/abstract_vao.py
| def unmap_buffer(self) -> None:
"""Unmap the currently mapped GL_ARRAY_BUFFER."""
gl.glUnmapBuffer(gl.GL_ARRAY_BUFFER)
|