Skip to content

Commit 1924187

Browse files
Rewrite VertexBuffer and update example code.
Delete dtype_vertex_buffer.py. Rework VertexBuffer to be a simple data store. Remove pointer calls from VertexBuffer. Add new Attribute classes that wrap the gl*AttribPointer calls. Add new BufferAttributes class that stores and activates buffer attributes.
1 parent 5787dca commit 1924187

File tree

6 files changed

+696
-1019
lines changed

6 files changed

+696
-1019
lines changed

pygly/dtype_vertex_buffer.py

Lines changed: 0 additions & 280 deletions
This file was deleted.

pygly/examples/renderable_colour_cube.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from OpenGL import GL
55

66
from pygly.shader import Shader, VertexShader, FragmentShader, ShaderProgram
7-
from pygly.dtype_vertex_buffer import DtypeVertexBuffer
7+
from pygly.vertex_buffer import VertexBuffer, BufferAttributes, GenericAttribute, VertexAttribute, TextureCoordAttribute
88
from pygly.vertex_array import VertexArray
99

1010

@@ -114,34 +114,41 @@ def __init__( self ):
114114
# we pass in a list of regions we want to define
115115
# we only have 1 region here
116116
# for each region, we pass in how many rows, and the dtype
117-
self.buffer = DtypeVertexBuffer(
118-
vertices.dtype,
117+
self.buffer = VertexBuffer(
119118
GL.GL_ARRAY_BUFFER,
120119
GL.GL_STATIC_DRAW,
121-
data = vertices
120+
data = vertices,
122121
)
123-
124-
self.vao = VertexArray()
125-
122+
126123
# pass the shader and region to our VAO
127124
# and bind each of the attributes to a VAO index
128125
# the shader name is the variable name used in the shader
129126
# the buffer name is the name of the property in our vertex dtype
127+
self.buffer_attributes = BufferAttributes()
128+
self.buffer_attributes[ 'position' ] = GenericAttribute.from_dtype(
129+
self.buffer,
130+
vertices.dtype,
131+
'position',
132+
location = self.shader.attributes[ 'in_position' ]
133+
)
134+
130135
# create our vertex array
136+
self.vao = VertexArray()
137+
131138
self.vao.bind()
132139
self.buffer.bind()
133-
self.buffer.set_attribute_pointer_dtype( self.shader, 'in_position', 'position' )
140+
self.buffer_attributes.set()
134141
self.buffer.unbind()
135142
self.vao.unbind()
136143

137144
def draw( self, projection, model_view, colour ):
138145
self.shader.bind()
139-
self.shader.uniforms['projection'].value = projection
140-
self.shader.uniforms['model_view'].value = model_view
141-
self.shader.uniforms['in_colour'].value = colour
146+
self.shader.uniforms[ 'projection' ].value = projection
147+
self.shader.uniforms[ 'model_view' ].value = model_view
148+
self.shader.uniforms[ 'in_colour' ].value = colour
142149

143150
self.vao.bind()
144-
GL.glDrawArrays( GL.GL_TRIANGLES, 0, self.buffer.rows )
151+
GL.glDrawArrays( GL.GL_TRIANGLES, 0, len( vertices ) )
145152
self.vao.unbind()
146153

147154
self.shader.unbind()
@@ -186,11 +193,18 @@ def __init__( self ):
186193
)
187194

188195
# create our vertex buffer
189-
self.buffer = DtypeVertexBuffer(
190-
vertices.dtype,
196+
self.buffer = VertexBuffer(
191197
GL.GL_ARRAY_BUFFER,
192198
GL.GL_STATIC_DRAW,
193-
data = vertices
199+
data = vertices,
200+
)
201+
202+
self.buffer_attributes = BufferAttributes()
203+
self.buffer_attributes[ 'position' ] = GenericAttribute.from_dtype(
204+
self.buffer,
205+
vertices.dtype,
206+
'position',
207+
location = self.shader.attributes[ 'in_position' ]
194208
)
195209

196210
def draw( self, colour ):
@@ -204,9 +218,9 @@ def draw( self, colour ):
204218
GL.glColor4f( *colour )
205219

206220
# set the vertex pointer to the position data
207-
self.buffer.set_vertex_pointer_dtype( 'position' )
221+
self.buffer_attributes.set()
208222

209-
GL.glDrawArrays( GL.GL_TRIANGLES, 0, self.buffer.rows )
223+
GL.glDrawArrays( GL.GL_TRIANGLES, 0, len( vertices ) )
210224

211225
self.buffer.pop_attributes()
212226
self.buffer.unbind()

0 commit comments

Comments
 (0)