.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_demos/viz_fine_tuning_gl_context.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_04_demos_viz_fine_tuning_gl_context.py: ======================================================= Fine-tuning the OpenGL state using shader callbacks ======================================================= Sometimes we need to get more control about how OpenGL will render the actors. This example shows how to change the OpenGL state of one or more actors. This can be useful when we need to create specialized visualization effects. .. GENERATED FROM PYTHON SOURCE LINES 13-14 First, let's import some functions .. GENERATED FROM PYTHON SOURCE LINES 14-21 .. code-block:: Python import itertools import numpy as np import fury .. GENERATED FROM PYTHON SOURCE LINES 22-24 We just proceed as usual: creating the actors and initializing a scene in FURY .. GENERATED FROM PYTHON SOURCE LINES 24-74 .. code-block:: Python centers = np.array([[0, 0, 0], [-0.1, 0, 0], [0.1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actor_no_depth_test = fury.actor.markers( centers, marker="s", colors=colors, marker_opacity=0.5, scales=0.2, ) actor_normal_blending = fury.actor.markers( centers - np.array([[0, -0.5, 0]]), marker="s", colors=colors, marker_opacity=0.5, scales=0.2, ) actor_add_blending = fury.actor.markers( centers - np.array([[0, -1, 0]]), marker="s", colors=colors, marker_opacity=0.5, scales=0.2, ) actor_sub_blending = fury.actor.markers( centers - np.array([[0, -1.5, 0]]), marker="s", colors=colors, marker_opacity=0.5, scales=0.2, ) actor_mul_blending = fury.actor.markers( centers - np.array([[0, -2, 0]]), marker="s", colors=colors, marker_opacity=0.5, scales=0.2, ) scene = fury.window.Scene() scene.background((0.5, 0.5, 0.5)) showm = fury.window.ShowManager( scene, size=(900, 768), reset_camera=False, order_transparent=False ) .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages/sphinx_gallery/gen_rst.py:722: UserWarning: We'll no longer accept the way you call the __init__ function in future versions of FURY. Here's how to call the Function __init__: __init__(self_value, scene='value', title='value', size='value', png_magnify='value', reset_camera='value', order_transparent='value', interactor_style='value', stereo='value', multi_samples='value', max_peels='value', occlusion_ratio='value') exec(self.code, self.fake_main.__dict__) .. GENERATED FROM PYTHON SOURCE LINES 75-76 All actors must be added in the scene .. GENERATED FROM PYTHON SOURCE LINES 76-82 .. code-block:: Python scene.add(actor_no_depth_test) scene.add(actor_normal_blending) scene.add(actor_add_blending) scene.add(actor_sub_blending) scene.add(actor_mul_blending) .. GENERATED FROM PYTHON SOURCE LINES 83-89 Now, we will enter in the topic of this example. First, we need to create (or use one of the pre-built gl_function of FURY) to change the OpenGL state of a given fury window instance (showm.window). Here we're using the pre-build FURY window functions which has already a set of specific behaviors to be applied in the OpenGL context .. GENERATED FROM PYTHON SOURCE LINES 89-126 .. code-block:: Python fury.shaders.shader_apply_effects( showm.window, actor_normal_blending, effects=fury.window.gl_set_normal_blending ) # ############################################################################### # It's also possible use a list of effects. The final opengl state it'll # be the composition of each effect that each function has in the opengl state id_observer = fury.shaders.shader_apply_effects( showm.window, actor_no_depth_test, effects=[ fury.window.gl_reset_blend, fury.window.gl_disable_blend, fury.window.gl_disable_depth, ], ) fury.shaders.shader_apply_effects( showm.window, actor_add_blending, effects=[ fury.window.gl_reset_blend, fury.window.gl_enable_depth, fury.window.gl_set_additive_blending, ], ) fury.shaders.shader_apply_effects( showm.window, actor_sub_blending, effects=fury.window.gl_set_subtractive_blending ) fury.shaders.shader_apply_effects( showm.window, actor_mul_blending, effects=fury.window.gl_set_multiplicative_blending ) .. rst-class:: sphx-glr-script-out .. code-block:: none 5 .. GENERATED FROM PYTHON SOURCE LINES 127-128 Finally, just render and see the results .. GENERATED FROM PYTHON SOURCE LINES 128-158 .. code-block:: Python counter = itertools.count() # After some steps we will remove the no_depth_test effect def timer_callback(obj, event): cnt = next(counter) showm.render() # we will rotate the visualization just to help you to see # the results of each specific opengl-state showm.scene.azimuth(1) if cnt == 400: fury.utils.remove_observer_from_actor(actor_no_depth_test, id_observer) fury.shaders.shader_apply_effects( showm.window, actor_no_depth_test, effects=fury.window.gl_set_additive_blending, ) if cnt == 1000: showm.exit() interactive = False showm.add_timer_callback(interactive, 5, timer_callback) if interactive: showm.start() fury.window.record(scene, out_path="viz_fine_tuning_gl_context.png", size=(600, 600)) .. image-sg:: /auto_examples/04_demos/images/sphx_glr_viz_fine_tuning_gl_context_001.png :alt: viz fine tuning gl context :srcset: /auto_examples/04_demos/images/sphx_glr_viz_fine_tuning_gl_context_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages/sphinx_gallery/gen_rst.py:722: UserWarning: We'll no longer accept the way you call the record function in future versions of FURY. Here's how to call the Function record: record(scene='value', cam_pos='value', cam_focal='value', cam_view='value', out_path='value', path_numbering='value', n_frames='value', az_ang='value', magnification='value', size='value', reset_camera='value', screen_clip='value', stereo='value', verbose='value') exec(self.code, self.fake_main.__dict__) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.084 seconds) .. _sphx_glr_download_auto_examples_04_demos_viz_fine_tuning_gl_context.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_fine_tuning_gl_context.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_fine_tuning_gl_context.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_