@@ -9,6 +9,10 @@ class VAOError(Exception):
99class ArrayBuffer :
1010 """Container for a vbo with additional information"""
1111 def __init__ (self , format , vbo ):
12+ """
13+ :param format: The format of the buffer
14+ :param vbo: The vbo object
15+ """
1216 self .format = format
1317 self .vbo = vbo
1418 self .stride = 0
@@ -21,21 +25,30 @@ def __init__(self, format, vbo):
2125
2226 @property
2327 def target (self ):
24- """GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER etc"""
28+ """
29+ :return: Returns the trarget of the vbo. GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER etc
30+ """
2531 return self .vbo .target
2632
2733 @property
2834 def usage (self ):
29- """GL_DYNAMIC_DRAW / GL_STATIC_DRAW"""
35+ """
36+ :return: The usage of the vbo. GL_DYNAMIC_DRAW / GL_STATIC_DRAW
37+ """
3038 return self .usage
3139
3240 @property
3341 def size (self ):
34- """Byte size"""
42+ """
43+ :return: Size of the vbo in bytes
44+ """
3545 return self .vbo .size
3646
3747 @property
3848 def vertices (self ):
49+ """
50+ :return: The number of vertices based on the current stride
51+ """
3952 if self .size % self .stride != 0 :
4053 raise VAOError ("size % stride != 0" )
4154 return self .size // self .stride
@@ -44,27 +57,46 @@ def vertices(self):
4457class ArrayMapping :
4558 """Keeps track of vbo to attribute mapping"""
4659 def __init__ (self , array_buffer , attrib_name , components , offset ):
60+ """
61+ :param array_buffer: The array buffer
62+ :param attrib_name: Name of the attribute
63+ :param components: Number of components
64+ :param offset: Byte offset in the buffer
65+ """
4766 self .array_buffer = array_buffer
4867 self .attrib_name = attrib_name
4968 self .components = components
5069 self .offset = offset
5170
5271
5372class VAOCombo :
54- """VAO of a specific attribute configuration"""
73+ """
74+ VAO of a specific attribute configuration.
75+ These are the actual VAOs used when drawing.
76+ """
5577 def __init__ (self , key ):
78+ """
79+ :param key: The unique key for this bind combination
80+ """
5681 self .vao = GL .glGenVertexArrays (1 )
5782 self .key = key
5883 self .array_mapping_list = []
5984 self .array_mapping_map = {}
6085
6186 def bind (self ):
87+ """
88+ Bind the VAO
89+ """
6290 # FIXME: Bind counter
6391 # FIXME: Track currently bound VAO to avoid re-binding
6492 GL .glBindVertexArray (self .vao )
6593
6694 def add_array_mapping (self , mapping ):
67- """Add ArrayMappings relevant to this VAO version"""
95+ """
96+ Add ArrayMappings relevant to this VAO version
97+
98+ :param mapping: The ArrayMapping to add
99+ """
68100 self .array_mapping_list .append (mapping )
69101 self .array_mapping_map [mapping .attrib_name ] = mapping
70102
@@ -74,6 +106,7 @@ class VAO:
74106 def __init__ (self , name , mode = GL .GL_TRIANGLES ):
75107 """
76108 Create and empty VAO
109+
77110 :param name: The name for debug purposes
78111 :param mode: Default draw mode for this VAO
79112 """
@@ -91,6 +124,15 @@ def __init__(self, name, mode=GL.GL_TRIANGLES):
91124 self .bind_context = VAOBindContext (self )
92125
93126 def bind (self , shader ):
127+ """
128+ Bind the VAO using a shader.
129+ This is the standard way of binding so the shader and VAO can negotiate
130+ the needed attributes. This will generate new VAOs in the background on the
131+ fly (caching them) if needed.
132+
133+ :param shader: The shader
134+ :return: A VAOBindContext object (optional use)
135+ """
94136 shader .bind ()
95137 combo = self .generate_vao_combo (shader )
96138 combo .bind ()
@@ -100,8 +142,11 @@ def bind(self, shader):
100142
101143 def draw (self , mode = None ):
102144 """
103- Draw the VAO
104- :param mode: Override the default GL_TRIANGLES
145+ Draw the VAO.
146+ Will use ``glDrawElements`` if an element buffer is present
147+ and ``glDrawArrays`` if no element array is present.
148+
149+ :param mode: Override the draw mode (GL_TRIANGLES etc)
105150 """
106151 if self .element_buffer :
107152 if mode is not None :
@@ -121,6 +166,13 @@ def draw(self, mode=None):
121166 GL .glDrawArrays (self .mode , 0 , self .vertex_count )
122167
123168 def add_array_buffer (self , format , vbo ):
169+ """
170+ Register a vbo in the VAO. This can be called multiple times.
171+ This can be one or multiple buffers (interleaved or not)
172+
173+ :param format: The format of the buffer
174+ :param vbo: The vbo object
175+ """
124176 if not isinstance (vbo , VBO ):
125177 raise VAOError ("vbo parameter must be an OpenGL.arrays.vbo.VBO instance" )
126178
@@ -132,7 +184,12 @@ def add_array_buffer(self, format, vbo):
132184 self .array_buffer_map [id (vbo )] = ArrayBuffer (format , vbo )
133185
134186 def set_element_buffer (self , format , vbo ):
135- """Set the index buffer for this VAO"""
187+ """
188+ Set the index buffer for this VAO
189+
190+ :param format: The format of the element buffer
191+ :param vbo: the vbo object
192+ """
136193 if not isinstance (vbo , VBO ):
137194 raise VAOError ("vbo parameter must be an OpenGL.arrays.vbo.VBO instance" )
138195
@@ -146,7 +203,10 @@ def set_element_buffer(self, format, vbo):
146203
147204 def map_buffer (self , vbo , attrib_name , components ):
148205 """
149- Map parts of the vbo to an attribute name
206+ Map parts of the vbos to an attribute name.
207+ This can be called multiple times to describe hos the buffers map to attribute names.
208+ If the same vbo is passed more than once it must be an interleaved buffer.
209+
150210 :param vbo: The vbo
151211 :param attrib_name: Name of the attribute in the shader
152212 :param components: Number of components (for example 3 for a x, y, x position)
@@ -168,7 +228,10 @@ def map_buffer(self, vbo, attrib_name, components):
168228 self .array_mapping_map [attrib_name ] = am
169229
170230 def build (self ):
171- """Finalize the VAO"""
231+ """
232+ Finalize the VAO.
233+ This runs various sanity checks on the input data.
234+ """
172235 if len (self .array_buffer_map ) == 0 :
173236 raise VAOError ("VAO has no buffers" )
174237
@@ -188,7 +251,14 @@ def build(self):
188251 print ("VAO {} has {} vertices" .format (self .name , self .vertex_count ))
189252
190253 def generate_vao_combo (self , shader ):
191- """Create a VAO based on the shader's attribute specification."""
254+ """
255+ Create a VAO based on the shader's attribute specification.
256+ This is called by ``bind(shader)`` and should not be messed with
257+ unless you are absolutely sure about what you are doing.
258+
259+ :param shader: The shader we are generating the combo for
260+ :return: A new VAOCombo object with the correct attribute binding
261+ """
192262 # Return the combo if already generated
193263 combo = self .combos .get (shader .attribute_key )
194264 if combo :
@@ -247,9 +317,15 @@ def __init__(self, vao):
247317 self .shader = None
248318
249319 def __enter__ (self ):
320+ """
321+ Entering the context
322+
323+ :return: The shader object
324+ """
250325 return self .shader
251326
252327 def __exit__ (self , exc_type , exc_val , exc_tb ):
328+ """Exist the context (No action)"""
253329 pass
254330
255331
0 commit comments