Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with self.subTest(vert_src=vert_src, vtype=vtype):
if self.ctx.version_code < vtype['version']:
warnings.warn('skipping version %s' % vtype['version'])
continue
prog = self.ctx.program(vertex_shader=vert_src % vtype, varyings=['v_out'])
if 'v_in' not in prog.attributes:
warnings.warn('skipping %s' % vtype['type'])
continue
fmt = moderngl.detect_format(prog, ['v_in'])
vbo1 = self.ctx.buffer(struct.pack(fmt, *vtype['input']))
vbo2 = self.ctx.buffer(b'\xAA' * struct.calcsize(fmt))
vao = self.ctx.simple_vertex_array(prog, vbo1, 'v_in')
vao.transform(vbo2, moderngl.POINTS, 1)
for a, b in zip(struct.unpack(fmt, vbo2.read()), vtype['output']):
self.assertAlmostEqual(a, b)
buf_v = self.ctx.buffer(struct.pack('2f', 4, 7))
res = self.ctx.buffer(reserve=buf_v.size)
vao = self.ctx.vertex_array(self.prog, [
(buf_m, '4f', 'in_m'),
(buf_v, '2f', 'in_v'),
])
self.prog['mult'].value = 0.0
vao.transform(res, moderngl.POINTS)
x, y = struct.unpack('2f', res.read())
self.assertAlmostEqual(x, 0.0)
self.assertAlmostEqual(y, 0.0)
self.prog['mult'].value = 1.0
vao.transform(res, moderngl.POINTS)
x, y = struct.unpack('2f', res.read())
self.assertAlmostEqual(x, 11.0)
self.assertAlmostEqual(y, 18.0)
self.prog['mult'].value = 2.0
vao.transform(res, moderngl.POINTS)
x, y = struct.unpack('2f', res.read())
self.assertAlmostEqual(x, 22.0)
self.assertAlmostEqual(y, 36.0)
def test_geometry_shader(self):
shader = self.create_program(path='vgf_quads.glsl')
self.assertAttributes(shader)
assert shader.geometry_input == moderngl.POINTS
assert shader.geometry_output == moderngl.TRIANGLE_STRIP
assert shader.geometry_vertices == 4
self.assertAlmostEqual(a, b)
vao.bind(0, 'f', vbo, '4f', offset=4, stride=0, divisor=0)
vao.transform(res, moderngl.POINTS, 2)
for a, b in zip(struct.unpack('8f', res.read()), (2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)):
self.assertAlmostEqual(a, b)
vao.bind(0, 'f', vbo, '4f', offset=8, stride=20, divisor=0)
vao.transform(res, moderngl.POINTS, 2)
for a, b in zip(struct.unpack('8f', res.read()), (3.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0)):
self.assertAlmostEqual(a, b)
vao.bind(0, 'f', vbo, '4f', offset=12, stride=0, divisor=1)
vao.transform(res, moderngl.POINTS, 2)
for a, b in zip(struct.unpack('8f', res.read()), (4.0, 5.0, 6.0, 7.0, 4.0, 5.0, 6.0, 7.0)):
self.assertAlmostEqual(a, b)
varyings=['out_vert']
)
vertices = [
4.0, 2.0, 7.5, 1.8,
3.0, 2.8, 6.0, 10.0
]
count = 10
indices = [0, 1] * 10
# UNSIGNED_INT index
vbo1 = self.ctx.buffer(np.array(vertices, dtype='f4').tobytes())
vbo2 = self.ctx.buffer(reserve=vbo1.size * count)
index = self.ctx.buffer(np.array(indices, dtype='u4').tobytes())
vao = self.ctx.simple_vertex_array(prog, vbo1, 'in_vert', index_buffer=index, index_element_size=4)
vao.transform(vbo2, moderngl.POINTS)
res = np.frombuffer(vbo2.read(), dtype='f4')
np.testing.assert_almost_equal(res, vertices * count)
# UNSIGNED_SHORT index
vbo1 = self.ctx.buffer(np.array(vertices, dtype='f4').tobytes())
vbo2 = self.ctx.buffer(reserve=vbo1.size * count)
index = self.ctx.buffer(np.array(indices, dtype='u2').tobytes())
vao = self.ctx.simple_vertex_array(prog, vbo1, 'in_vert', index_buffer=index, index_element_size=2)
vao.transform(vbo2, moderngl.POINTS)
res = np.frombuffer(vbo2.read(), dtype='f4')
np.testing.assert_almost_equal(res, vertices * count)
# UNSIGNED_BYTE index
vbo1 = self.ctx.buffer(np.array(vertices, dtype='f4').tobytes())
vbo2 = self.ctx.buffer(reserve=vbo1.size * count)
index = self.ctx.buffer(np.array(indices, dtype='u1').tobytes())
def test_vertex_attrib_per_render_3(self):
self.vao2.transform(self.res, moderngl.POINTS, vertices=1, instances=4)
a, b, c, d = struct.unpack('4f', self.res.read(16))
self.assertAlmostEqual(a, 111.0)
self.assertAlmostEqual(b, 121.0)
self.assertAlmostEqual(c, 131.0)
self.assertAlmostEqual(d, 141.0)
def test_vertex_attrib_per_instance_1(self):
self.vao1.transform(self.res, moderngl.POINTS, vertices=4, instances=1)
a, b, c, d = struct.unpack('4f', self.res.read(16))
self.assertAlmostEqual(a, 11.0)
self.assertAlmostEqual(b, 12.0)
self.assertAlmostEqual(c, 13.0)
self.assertAlmostEqual(d, 14.0)
seed (int): The random seed
Returns:
A :py:class:`demosys.opengl.vao.VAO` instance
"""
random.seed(seed)
def gen():
for _ in range(count):
yield random.uniform(*range_x)
yield random.uniform(*range_y)
yield random.uniform(*range_z)
data = numpy.fromiter(gen(), count=count * 3, dtype=numpy.float32)
vao = VAO("geometry:points_random_3d", mode=moderngl.POINTS)
vao.buffer(data, '3f', ['in_position'])
return vao
# Shader for linearizing depth (debug visualization)
self.linearize_depth_program = self.load_program('programs/fragment_picking/linearize_depth.glsl')
self.linearize_depth_program['texture0'].value = 0
self.linearize_depth_program['near'].value = self.projection.near
self.linearize_depth_program['far'].value = self.projection.far
# Shader for picking the world position of a fragment
self.fragment_picker_program = self.load_program('programs/fragment_picking/picker.glsl')
self.fragment_picker_program['position_texture'].value = 0 # Read from texture channel 0
self.fragment_picker_program['normal_texture'].value = 1 # Read from texture channel 1
self.fragment_picker_program['diffuse_texture'].value = 2 # Read from texture channel 2
# Picker geometry
self.marker_byte_size = 7 * 4 # position + normal + temperature (7 x 32bit floats)
self.picker_output = self.ctx.buffer(reserve=self.marker_byte_size)
self.picker_vao = VAO(mode=moderngl.POINTS)
# Shader for rendering markers
self.marker_program = self.load_program('programs/fragment_picking/markers.glsl')
self.marker_program['color'].value = 1.0, 0.0, 0.0, 1.0
# Marker geometry
self.marker_buffer = self.ctx.buffer(reserve=self.marker_byte_size * 1000) # Resever room for 1000 points
self.marker_vao = VAO(name="markers", mode=moderngl.POINTS)
self.marker_vao.buffer(self.marker_buffer, '3f 3f 1f', ['in_position', 'in_normal', 'temperature'])
self.num_markers = 0
# Debug geometry
self.quad_normals = geometry.quad_2d(size=(0.25, 0.25), pos=(0.75, 0.875))
self.quad_depth = geometry.quad_2d(size=(0.25, 0.25), pos=(0.5, 0.875))
self.quad_positions = geometry.quad_2d(size=(0.25, 0.25), pos=(0.25, 0.875))
def render(self, time, frame_time):
self.ctx.clear(1.0, 1.0, 1.0)
self.ctx.point_size = 2.0
for i in range(8):
self.vbo1.write(particle(), offset=self.idx * 16)
self.idx = (self.idx + 1) % 1024
self.render_vao.render(moderngl.POINTS, 1024)
self.vao1.transform(self.vbo2, moderngl.POINTS, 1024)
self.ctx.copy_buffer(self.vbo1, self.vbo2)