Skip to content

Commit 4e95969

Browse files
committed
Major docstring update #2
1 parent 89e0e4a commit 4e95969

24 files changed

+414
-15
lines changed
File renamed without changes.

demosys/core/management/commands/__init__.py

Whitespace-only changes.

demosys/core/shaderfiles/__init__.py

Whitespace-only changes.

demosys/core/texturefiles/__init__.py

Whitespace-only changes.

demosys/opengl/vao.py

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class VAOError(Exception):
99
class 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):
4457
class 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

5372
class 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

demosys/resources/shaders.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,40 @@
55

66

77
class Shaders:
8+
"""
9+
A registry for shaders requested by effects.
10+
Once all effects are initialized, we ask this class to load the shaders.
11+
"""
812
def __init__(self):
913
self.shaders = {}
1014

1115
@property
1216
def count(self):
17+
"""
18+
:return: Number of shaders
19+
"""
1320
return len(self.shaders)
1421

1522
def get(self, path, create=False):
23+
"""
24+
Get or create a shader object.
25+
This may return an empty object that will be filled during load
26+
based on the ``create`` parameter.
27+
28+
:param path: Path to the shader
29+
:param create: (bool) Create an empty shader object if it doesn't exist
30+
:return: Shader object
31+
"""
1632
shader = self.shaders.get(path)
1733
if create and not shader:
1834
shader = Shader(path)
1935
self.shaders[path] = shader
2036
return shader
2137

2238
def load(self):
39+
"""
40+
Loads all the shaders using the configured finders.
41+
"""
2342
finders = list(get_finders())
2443
print("Loading shaders:")
2544
for name, shader in self.shaders.items():

demosys/resources/textures.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,40 @@
66

77

88
class Textures:
9+
"""
10+
A registry for textures requested by effects.
11+
Once all effects are initialized, we ask this class to load the textures.
12+
"""
913
def __init__(self):
1014
self.textures = {}
1115

1216
@property
1317
def count(self):
18+
"""
19+
:return: Number of textures
20+
"""
1421
return len(self.textures)
1522

1623
def get(self, path, create=False):
24+
"""
25+
Get or create a texture object.
26+
This may return an empty object that will be filled during load
27+
based on the ``create`` parameter.
28+
29+
:param path: Path to the texture
30+
:param create: (bool) Create an empty texture object if it doesn't exist
31+
:return: Texture object
32+
"""
1733
texture = self.textures.get(path)
1834
if create and not texture:
1935
texture = Texture.from_image(path)
2036
self.textures[path] = texture
2137
return texture
2238

2339
def load(self):
40+
"""
41+
Loads all the textures using the configured finders.
42+
"""
2443
finders = list(get_finders())
2544
print("Loading textures:")
2645
for name, texture in self.textures.items():

demosys/resources/tracks.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55

66

77
class Tracks:
8+
"""Registry for requested rocket tracks"""
89
def __init__(self):
910
self.tacks = []
1011
self.track_map = {}
1112

1213
def get(self, name):
14+
"""
15+
Get or create a Track object.
16+
17+
:param name: Name of the track
18+
:return: Track object
19+
"""
1320
name = name.lower()
1421
track = self.track_map.get(name)
1522
if not track:
@@ -19,6 +26,9 @@ def get(self, name):
1926
return track
2027

2128
def load(self):
29+
"""
30+
Dummy. This will be handled by the rocket library,
31+
"""
2232
pass
2333

2434

demosys/scene/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)