1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
import numpy as np
import numpy.testing as npt
import moderngl
def test_1(ctx):
prog = ctx.program(
vertex_shader='''
#version 330
in vec2 in_vert;
out vec2 v_text;
void main() {
v_text = in_vert / 2.0 + 0.5;
gl_Position = vec4(in_vert, 0.0, 1.0);
}
''',
fragment_shader='''
#version 330
uniform sampler2D Texture;
in vec2 v_text;
out vec4 f_color;
void main() {
f_color = texture(Texture, v_text);
}
''',
)
pixels = np.tile([0.0, 0.5, 1.0], 4 * 4).astype('f2')
tex = ctx.texture((4, 4), 3, pixels, dtype='f2')
vbo = ctx.buffer(np.array([-1, -1, -1, 1, 1, -1, 1, 1], dtype='f4'))
vao = ctx.simple_vertex_array(prog, vbo, 'in_vert')
fbo = ctx.simple_framebuffer((4, 4))
fbo.use()
fbo.clear()
tex.use()
vao.render(moderngl.TRIANGLE_STRIP)
res = np.frombuffer(fbo.read(dtype='f2'), dtype='f2')
npt.assert_almost_equal(res, pixels, decimal=2)
|