def test_pack_mixed_rows(): data = glnext.pack('1f 1i', [ 0.2, 10, 0.7, 20, ]) assert data.hex() == 'cdcc4c3e0a0000003333333f14000000'
def test_pack_padding(): data = glnext.pack('2x 1f 1i', [ 0.2, 10, 0.7, 20, ]) assert data.hex() == '0000cdcc4c3e0a00000000003333333f14000000' data = glnext.pack('1f 2x 1i', [ 0.2, 10, 0.7, 20, ]) assert data.hex() == 'cdcc4c3e00000a0000003333333f000014000000' data = glnext.pack('1f 1i 2x', [ 0.2, 10, 0.7, 20, ]) assert data.hex() == 'cdcc4c3e0a00000000003333333f140000000000'
}, }], }, ], ) pipeline.update( vertex_buffer=glnext.pack([ -0.05, -0.05, 0.0, 1.0, -0.05, 0.05, 0.0, 0.0, 0.05, -0.05, 1.0, 1.0, 0.05, 0.05, 1.0, 0.0, ]), instance_buffer=b'Hello World!', ) task.run() data = framebuffer.output[0].read() Image.frombuffer('RGBA', (512, 512), data, 'raw', 'RGBA', 0, -1).show()
'sampled_image', 'images': [{ 'image': image, 'sampler': { 'min_filter': 'linear', 'mag_filter': 'linear', }, }], }, ], ) pipeline.update(vertex_buffer=glnext.pack([ -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, 1.0, 0.0, 0.0, 0.5, 0.5, 1.0, ]), ) task.run() data = framebuffer.output[0].read() Image.frombuffer('RGBA', (512, 512), data, 'raw', 'RGBA', 0, -1).show()
out_color = colors[gl_VertexIndex]; } '''), fragment_shader=glsl(''' #version 450 #pragma shader_stage(fragment) layout (location = 0) in vec3 in_color; layout (location = 0) out vec4 out_color; void main() { out_color = vec4(in_color, 1.0); } '''), vertex_count=3, bindings=[ { 'binding': 0, 'name': 'uniform_buffer', 'type': 'uniform_buffer', 'size': 8, }, ], ) pipeline['uniform_buffer'].write(glnext.pack([0.3, 1.5])) task.run() data = framebuffer.output[0].read() Image.frombuffer('RGBA', (512, 512), data, 'raw', 'RGBA', 0, -1).show()
#pragma shader_stage(fragment) layout (location = 0) in vec4 in_color; layout (location = 0) out vec4 out_color; void main() { out_color = in_color; } '''), instance_format='2f', vertex_count=3, instance_count=5, ) pipeline.update(instance_buffer=glnext.pack([ -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.0, 0.0, ]), ) task.run() data = framebuffer.output[0].read() Image.frombuffer('RGBA', (512, 512), data, 'raw', 'RGBA', 0, -1).show()
], ) pipeline.update( uniform_buffer=glnext.camera((1.0, 1.0, 1.0), (2.0, 2.0, 1.0)), vertex_buffer=glnext.pack([ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0, 0.0, 0.0, 20.0, 100.0, 0.0, 0.0, 20.0, 0.0, 100.0, 100.0, 0.0, 20.0, 20.0, ]), ) task.run() data = framebuffer.output[0].read() Image.frombuffer('RGBA', (512, 512), data, 'raw', 'RGBA', 0, -1).show()
instance = glnext.instance() task = instance.task() vertex_size = 24 mesh = Obj.open('examples/monkey.obj').pack('vx vy vz nx ny nz') vertex_count = len(mesh) // vertex_size uniform_buffer = instance.buffer('uniform_buffer', 160) vertex_buffer = instance.buffer('vertex_buffer', len(mesh)) vertex_buffer.write(mesh) shadow_fbo = task.framebuffer((512, 512), '1f', samples=1, mode='texture') shadow_fbo.update( clear_values=glnext.pack([1.0, 0.0, 0.0, 0.0]), clear_depth=1.0, ) shadow_pipeline = shadow_fbo.render( vertex_shader=glsl(''' #version 450 #pragma shader_stage(vertex) layout (binding = 0) uniform Buffer { mat4 mvp_camera; mat4 mvp_light; vec3 camera; vec3 light; };
) pipeline.update( vertex_buffer=glnext.pack([ -0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, ]), index_buffer=glnext.pack([ 0, 1,
return vertnorm(av, an) + vertnorm(bv, bn) + vertnorm(cv, cn) text = open('shapes.obj').read() verts = [x for x in text.splitlines() if x.startswith('v ')] norms = [x for x in text.splitlines() if x.startswith('vn ')] faces = {} for line in text.splitlines(): if line.startswith('o '): name = line.split()[1] faces[name] = [] if line.startswith('f '): faces[name].append(line) data = [] result = {'shapes': {}} for name in ['cube', 'sphere', 'cylinder', 'cone', 'monkey', 'plane', 'circle', 'capsule', 'torus']: result['shapes'][name] = { 'offset': len(data) // 6, 'count': len(faces[name]) * 3, } for face in faces[name]: data += triangle(face) result['data'] = glnext.pack('3h 3h', data).hex() open('shapes.json', 'w').write(json.dumps(result))
vertex_format='3h 3h', instance_format='3f 4f 3f', vertex_buffer=bytes.fromhex(shapes['data']), indirect_count=len(objects), instance_count=len(objects), buffers=[ { 'name': 'uniform_buffer', 'type': 'uniform_buffer', 'binding': 0, 'size': 64, } ] ) render['uniform_buffer'].write(glnext.camera((4, 3, 2), (0, 0, 0))) instance_buffer = [] indirect_buffer = [] for i, obj in enumerate(objects): instance_buffer += [*obj['position'], *obj['rotation'], *obj['size']] indirect_buffer += [shapes['shapes'][obj['shape']]['count'], 1, shapes['shapes'][obj['shape']]['offset'], i] render['instance_buffer'].write(glnext.pack(instance_buffer)) render['indirect_buffer'].write(glnext.pack(indirect_buffer)) instance.run() Image.frombuffer('RGBA', (512, 512), fbo.output[0].read(), 'raw', 'RGBA', 0, -1).show()
def test_pack_int(): array = [512, 0, -1] assert glnext.pack(array) == np.array(array, 'i4').tobytes()
def test_pack_floats(): array = [1.0, 2.5, 0.3333] assert glnext.pack(array) == np.array(array, 'f4').tobytes()
'size': 64, }, ], ) framebuffer.update(clear_values=glnext.pack([ 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ]), ) pipeline.update( uniform_buffer=glnext.camera((0.0, 0.0, 100.0), (0.0, 0.0, 0.0), (0.0, 1.0, 0.0), fov=4.0),
void main() { gl_Position = vec4(in_vert, 0.0, 1.0); out_color = in_color; } '''), fragment_shader=glsl(''' #version 450 #pragma shader_stage(fragment) layout (location = 0) in vec3 in_color; layout (location = 0) out vec4 out_color; void main() { out_color = vec4(in_color, 1.0); } '''), vertex_format='2f 3f', vertex_count=3, ) pipeline['vertex_buffer'].write(glnext.pack([ -0.5, -0.5, 0.0, 0.0, 1.0, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 0.5, 1.0, 0.0, 0.0, ])) task.run() data = framebuffer.output[0].read() Image.frombuffer('RGBA', (512, 512), data, 'raw', 'RGBA', 0, -1).show()
} '''), vertex_format='3f', vertex_count=vertex_count, bindings=[ { 'binding': 0, 'name': 'uniform_buffer', 'type': 'uniform_buffer', 'size': 64, }, ], ) framebuffer.update( clear_values=glnext.pack([1.0, 0.0, 0.0, 0.0]), clear_depth=1.0, ) pipeline.update( uniform_buffer=glnext.camera((4.0, 3.0, 2.0), (0.0, 0.0, 0.0), near=1.0, far=7.0), vertex_buffer=mesh, ) task.run() data = framebuffer.output[0].read() array = np.ndarray((512, 512), 'f4', data)[::-1] plt.imshow(array, 'YlGnBu', vmin=0.8, vmax=1.0) plt.show()
def test_pack_mixed(): data = glnext.pack('2f 2i', [1.0, 2.0, 1, 2]) assert data.hex() == '0000803f000000400100000002000000'