def test_use_texture3D(): """Test using a 3D texture""" vals = [0, 200, 100, 0, 255, 0, 100] d, h, w = len(vals), 3, 5 data = np.zeros((d, h, w), np.float32) VERT_SHADER = """ attribute vec2 a_pos; varying vec2 v_pos; void main (void) { v_pos = a_pos; gl_Position = vec4(a_pos, 0., 1.); } """ FRAG_SHADER = """ uniform sampler3D u_texture; varying vec2 v_pos; uniform float i; void main() { gl_FragColor = texture3D(u_texture, vec3((v_pos.y+1.)/2., (v_pos.x+1.)/2., i)); gl_FragColor.a = 1.; } """ # populate the depth "slices" with different gray colors in the bottom left for ii, val in enumerate(vals): data[ii, :2, :3] = val / 255. with Canvas(size=(100, 100)) as c: if not has_pyopengl(): t = Texture3D(data) assert_raises(ImportError, t.glir.flush, c.context.shared.parser) return program = Program(VERT_SHADER, FRAG_SHADER) program['a_pos'] = [[-1., -1.], [1., -1.], [-1., 1.], [1., 1.]] tex = Texture3D(data, interpolation='nearest') assert_equal(tex.width, w) assert_equal(tex.height, h) assert_equal(tex.depth, d) program['u_texture'] = tex for ii, val in enumerate(vals): set_viewport(0, 0, w, h) clear(color='black') iii = (ii + 0.5) / float(d) print(ii, iii) program['i'] = iii program.draw('triangle_strip') out = _screenshot()[:, :, 0].astype(int)[::-1] expected = np.zeros_like(out) expected[:2, :3] = val assert_allclose(out, expected, atol=1./255.)
def test_use_texture3D(): """Test using a 3D texture""" vals = [0, 200, 100, 0, 255, 0, 100] d, h, w = len(vals), 3, 5 data = np.zeros((d, h, w), np.float32) VERT_SHADER = """ attribute vec2 a_pos; varying vec2 v_pos; void main (void) { v_pos = a_pos; gl_Position = vec4(a_pos, 0., 1.); } """ FRAG_SHADER = """ uniform sampler3D u_texture; varying vec2 v_pos; uniform float i; void main() { gl_FragColor = texture3D(u_texture, vec3((v_pos.y+1.)/2., (v_pos.x+1.)/2., i)); gl_FragColor.a = 1.; } """ # populate the depth "slices" with different gray colors in the bottom left for ii, val in enumerate(vals): data[ii, :2, :3] = val / 255. with Canvas(size=(100, 100)) as c: if not has_pyopengl(): t = Texture3D(data) assert_raises(ImportError, t.glir.flush, c.context.shared.parser) return program = Program(VERT_SHADER, FRAG_SHADER) program['a_pos'] = [[-1., -1.], [1., -1.], [-1., 1.], [1., 1.]] tex = Texture3D(data, interpolation='nearest') assert_equal(tex.width, w) assert_equal(tex.height, h) assert_equal(tex.depth, d) program['u_texture'] = tex for ii, val in enumerate(vals): set_viewport(0, 0, w, h) clear(color='black') iii = (ii + 0.5) / float(d) print(ii, iii) program['i'] = iii program.draw('triangle_strip') out = _screenshot()[:, :, 0].astype(int)[::-1] expected = np.zeros_like(out) expected[:2, :3] = val assert_allclose(out, expected, atol=1. / 255.)