Skip to content

Instantly share code, notes, and snippets.

@wpk-
Created April 30, 2018 14:55
Show Gist options
  • Save wpk-/ea1d20c57cce214ffc46c8b413035411 to your computer and use it in GitHub Desktop.
Save wpk-/ea1d20c57cce214ffc46c8b413035411 to your computer and use it in GitHub Desktop.
import struct
import unittest
import moderngl
_static = {
'context': None
}
def get_context() -> moderngl.Context:
ctx = _static.get('context')
if ctx is None:
ctx = moderngl.create_standalone_context(size=(100, 100))
_static['context'] = ctx
return ctx
class VertexArrayBindTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.ctx = get_context()
def test_vec4(self):
prog = self.ctx.program(
vertex_shader='''
#version 400
in vec4 in_vert;
out vec4 out_vert;
void main() {
out_vert = in_vert;
}
''',
varyings=['out_vert']
)
vbo1 = self.ctx.buffer(struct.pack('8f', 0, 1, 2, 3, 4, 5, 6, 7))
vbo2 = self.ctx.buffer(reserve=16)
vao = self.ctx.vertex_array(prog, [(vbo1, '4f', 'in_vert')])
# No offset.
vao.transform(vbo2, moderngl.POINTS, vertices=1)
self.assertTupleEqual(struct.unpack('4f', vbo2.read()),
(0., 1., 2., 3.),
'Copy vec4 failed.')
# Zero offset.
in_location = prog['in_vert'].location
vao.bind(in_location, 'f', vbo1, '4f', offset=0)
vao.transform(vbo2, moderngl.POINTS, vertices=1)
self.assertTupleEqual(struct.unpack('4f', vbo2.read()),
(0., 1., 2., 3.),
'Copy vec4 failed with vao.bind, offset=0.')
# Skip first 4 numbers.
vao.bind(in_location, 'f', vbo1, '4f', offset=16)
vao.transform(vbo2, moderngl.POINTS, vertices=1)
self.assertTupleEqual(struct.unpack('4f', vbo2.read()),
(4., 5., 6., 7.),
'Copy vec4 failed with vao.bind, offset=16.')
def test_mat2(self):
prog = self.ctx.program(
vertex_shader='''
#version 400
in mat2 in_vert;
out mat2 out_vert;
void main() {
out_vert = in_vert;
}
''',
varyings=['out_vert']
)
vbo1 = self.ctx.buffer(struct.pack('8f', 0, 1, 2, 3, 4, 5, 6, 7))
vbo2 = self.ctx.buffer(reserve=16)
vao = self.ctx.vertex_array(prog, [(vbo1, '4f', 'in_vert')])
# No offset.
vao.transform(vbo2, moderngl.POINTS, vertices=1)
self.assertTupleEqual(struct.unpack('4f', vbo2.read()),
(0., 1., 2., 3.),
'Copy mat2 failed.')
# Zero offset.
in_location = prog['in_vert'].location
vao.bind(in_location, 'f', vbo1, '4f', offset=0)
vao.transform(vbo2, moderngl.POINTS, vertices=1)
self.assertTupleEqual(struct.unpack('4f', vbo2.read()),
(0., 1., 2., 3.),
'Copy mat2 failed with vao.bind, offset = 0.')
# Skip first 4 numbers.
vao.bind(in_location, 'f', vbo1, '4f', offset=16)
vao.transform(vbo2, moderngl.POINTS, vertices=1)
self.assertTupleEqual(struct.unpack('4f', vbo2.read()),
(4., 5., 6., 7.),
'Copy mat2 failed with vao.bind, offset = 16.')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment