def quaternion(cfg): '''Animated quaternion used to rotate a plane''' cfg.duration = 10. step = cfg.duration / 5. x = math.sqrt(0.5) quat_animkf = [ ngl.AnimKeyFrameQuat(0 * step, (0, 0, 0, 1)), ngl.AnimKeyFrameQuat(1 * step, (0, 0,-x, x)), ngl.AnimKeyFrameQuat(2 * step, (0, 1, 0, 0)), ngl.AnimKeyFrameQuat(3 * step, (1, 0, 0, 0)), ngl.AnimKeyFrameQuat(4 * step, (x, 0, 0, x)), ngl.AnimKeyFrameQuat(5 * step, (0, 0, 0, 1)), ] quat = ngl.UniformQuat(anim=ngl.AnimatedQuat(quat_animkf)) q = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) m = ngl.Media(cfg.medias[0].filename) t = ngl.Texture2D(data_src=m) p = ngl.Program(vertex=cfg.get_vert('uniform-mat4')) render = ngl.Render(q, p) render.update_textures(tex0=t) render.update_uniforms(transformation_matrix=quat) camera = ngl.Camera(render) camera.set_eye(0.0, 0.0, 4.0) camera.set_center(0.0, 0.0, 0.0) camera.set_up(0.0, 1.0, 0.0) camera.set_perspective(45.0, cfg.aspect_ratio_float) camera.set_clipping(1.0, 10.0) return camera
def api_livectls(): # Build a scene and extract its live controls rng = random.Random(0) scene = ngl.Group( children=( ngl.UniformBool(live_id="b"), ngl.UniformFloat(live_id="f"), ngl.UniformIVec3(live_id="iv3"), ngl.UserSwitch( ngl.Group( children=( ngl.UniformMat4(live_id="m4"), ngl.UniformColor(live_id="clr"), ngl.UniformQuat(as_mat4=True, live_id="rot"), ) ), live_id="switch", ), ngl.Text(live_id="txt"), ) ) livectls = ngl.get_livectls(scene) assert len(livectls) == 8 # Attach scene and run a dummy draw to make sure it's valid ctx = ngl.Context() ret = ctx.configure(offscreen=1, width=16, height=16, backend=_backend) assert ret == 0 assert ctx.set_scene(scene) == 0 assert ctx.draw(0) == 0 # Apply live changes on nodes previously tracked by get_livectls() values = dict( b=True, f=rng.uniform(-1, 1), iv3=[rng.randint(-100, 100) for i in range(3)], switch=False, m4=[rng.uniform(-1, 1) for i in range(16)], clr=(0.9, 0.3, 0.8), rot=(0.1, -0.2, 0.5, -0.3), txt="test string", ) for live_id, value in values.items(): node = livectls[live_id]["node"] node_type = livectls[live_id]["node_type"] assert node_type == node.__class__.__name__ if node_type == "UserSwitch": node.set_enabled(value) elif node_type == "Text": node.set_text(value) elif hasattr(value, "__iter__"): node.set_value(*value) else: node.set_value(value) # Detach scene from context and grab all live controls again assert ctx.set_scene(None) == 0 livectls = ngl.get_livectls(scene) # Inspect nodes to check if they were properly altered by the live changes for live_id, expected_value in values.items(): value = livectls[live_id]["val"] node_type = livectls[live_id]["node_type"] if node_type == "Text": assert value == expected_value, (value, expected_value) elif hasattr(value, "__iter__"): assert all(math.isclose(v, e, rel_tol=1e-6) for v, e in zip(value, expected_value)) else: assert math.isclose(value, expected_value, rel_tol=1e-6)
array_mat4=lambda data: ngl.BufferMat4(data=data), array_vec2=lambda data: ngl.BufferVec2(data=data), array_vec3=lambda data: ngl.BufferVec3(data=data), array_vec4=lambda data: ngl.BufferVec4(data=data), single_bool=lambda data: ngl.UniformBool(data), single_float=lambda data: ngl.UniformFloat(data), single_int=lambda data: ngl.UniformInt(data), single_ivec2=lambda data: ngl.UniformIVec2(data), single_ivec3=lambda data: ngl.UniformIVec3(data), single_ivec4=lambda data: ngl.UniformIVec4(data), single_uint=lambda data: ngl.UniformUInt(data), single_uvec2=lambda data: ngl.UniformUIVec2(data), single_uvec3=lambda data: ngl.UniformUIVec3(data), single_uvec4=lambda data: ngl.UniformUIVec4(data), single_mat4=lambda data: ngl.UniformMat4(data), single_quat_mat4=lambda data: ngl.UniformQuat(data, as_mat4=True), single_quat_vec4=lambda data: ngl.UniformQuat(data, as_mat4=False), single_vec2=lambda data: ngl.UniformVec2(data), single_vec3=lambda data: ngl.UniformVec3(data), single_vec4=lambda data: ngl.UniformVec4(data), ) def _get_field_decl(layout, f): t = f['type'] t = t.split('_')[1] if t.startswith('quat') else t return '{}{:<5} {}{}{}'.format('uniform ' if layout == 'uniform' else '', t, 'field_' if layout == 'uniform' else '', f['name'], '[%d]' % f['len'] if 'len' in f else '')