{ "cells": [ { "cell_type": "raw", "id": "c24aecc3", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. _example_pt:\n", "\n", "Rendering with path tracing\n", "===========================\n", "\n", "This example describes how to render the scene with path tracing. Path tracing is a rendering technique based on Monte Carlo method and notably one of the most basic (yet practical) rendering algorithms taking global illumination into account. Our framework implements path tracing as ``renderer::pt`` renderer. \n", "\n", "The use of the renderer is straightforward; we just need to create ``renderer::pt`` renderer and call :cpp:func:`lm::Renderer::render` function with some renderer-specific parameters. Thanks to the modular design of the framework, the most of the code can be the same as :ref:`example_raycast`." ] }, { "cell_type": "code", "execution_count": 1, "id": "1b8ad419", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:22.373868Z", "iopub.status.busy": "2021-10-22T11:24:22.373164Z", "iopub.status.idle": "2021-10-22T11:24:22.465709Z", "shell.execute_reply": "2021-10-22T11:24:22.466155Z" } }, "outputs": [], "source": [ "import lmenv\n", "env = lmenv.load('.lmenv')" ] }, { "cell_type": "code", "execution_count": 2, "id": "d315a52f", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:22.469807Z", "iopub.status.busy": "2021-10-22T11:24:22.468642Z", "iopub.status.idle": "2021-10-22T11:24:22.690928Z", "shell.execute_reply": "2021-10-22T11:24:22.691380Z" } }, "outputs": [], "source": [ "import os\n", "import numpy as np\n", "import imageio\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import lightmetrica as lm\n", "%load_ext lightmetrica_jupyter" ] }, { "cell_type": "code", "execution_count": 3, "id": "52812a8d", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:22.696171Z", "iopub.status.busy": "2021-10-22T11:24:22.695705Z", "iopub.status.idle": "2021-10-22T11:24:22.699743Z", "shell.execute_reply": "2021-10-22T11:24:22.699328Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.000] Lightmetrica -- Version 3.0.0 (rev. 70601db) Linux x64\n" ] } ], "source": [ "lm.init()\n", "lm.log.init('jupyter')\n", "lm.progress.init('jupyter')\n", "lm.info()" ] }, { "cell_type": "code", "execution_count": 4, "id": "ff61d9ed", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:23.288486Z", "iopub.status.busy": "2021-10-22T11:24:23.201614Z", "iopub.status.idle": "2021-10-22T11:24:23.291358Z", "shell.execute_reply": "2021-10-22T11:24:23.291716Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.009] Loading asset [name='film']\n", "[I|0.087] Loading asset [name='camera1']\n", "[I|0.087] Loading asset [name='model']\n", "[I|0.087] .. Loading OBJ file [path='fireplace_room.obj']\n", "[I|0.087] .. Loading MTL file [path='fireplace_room.mtl']\n", "[I|0.416] .. Loading texture [path='wood.ppm']\n", "[I|0.507] .. Loading texture [path='leaf.ppm']\n", "[I|0.509] .. Loading texture [path='picture8.ppm']\n", "[I|0.541] .. Loading texture [path='wood5.ppm']\n" ] } ], "source": [ "# Film for the rendered image\n", "film = lm.load_film('film', 'bitmap', w=1920, h=1080)\n", "\n", "# Pinhole camera\n", "camera = lm.load_camera('camera1', 'pinhole',\n", " position=[5.101118, 1.083746, -2.756308],\n", " center=[4.167568, 1.078925, -2.397892],\n", " up=[0,1,0],\n", " vfov=43.001194,\n", " aspect=16/9)\n", "\n", "# OBJ model\n", "model = lm.load_model('model', 'wavefrontobj',\n", " path=os.path.join(env.scene_path, 'fireplace_room/fireplace_room.obj'))" ] }, { "cell_type": "code", "execution_count": 5, "id": "943e60c8", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:23.965539Z", "iopub.status.busy": "2021-10-22T11:24:23.965026Z", "iopub.status.idle": "2021-10-22T11:24:23.969337Z", "shell.execute_reply": "2021-10-22T11:24:23.968929Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|0.600] Loading asset [name='accel']\n", "[I|0.601] Loading asset [name='scene']\n", "[I|0.604] Building acceleration structure [name='accel']\n", "[I|0.604] .. Flattening scene\n", "[I|0.632] .. Building\n" ] } ], "source": [ "accel = lm.load_accel('accel', 'sahbvh')\n", "scene = lm.load_scene('scene', 'default', accel=accel)\n", "scene.add_primitive(camera=camera)\n", "scene.add_primitive(model=model)\n", "scene.build()" ] }, { "cell_type": "code", "execution_count": 6, "id": "9b02ee2d", "metadata": { "execution": { "iopub.execute_input": "2021-10-22T11:24:23.973755Z", "iopub.status.busy": "2021-10-22T11:24:23.973172Z", "iopub.status.idle": "2021-10-22T11:28:01.960441Z", "shell.execute_reply": "2021-10-22T11:28:01.960014Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[I|1.278] Loading asset [name='renderer']\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6bcc47f33d0b49cb8e65e9bf3e83a01c", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/10368000 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "img = np.copy(film.buffer())\n", "f = plt.figure(figsize=(15,15))\n", "ax = f.add_subplot(111)\n", "ax.imshow(np.clip(np.power(img,1/2.2),0,1), origin='lower')\n", "plt.show()" ] } ], "metadata": { "jupytext": { "cell_metadata_json": true, "formats": "ipynb,py:light", "text_representation": { "extension": ".py", "format_name": "light", "format_version": "1.5", "jupytext_version": "1.3.3" } }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "1a3f7691f92b4ff59ea231a701ead85f": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "1b237177349a47e4b7b40766345e418b": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "ProgressStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } }, "252ae0d0bb804887b5cec5442326c057": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "2ac72ef6e5dd4e6a84b7eadd62122c0c": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "42a685066d8c4a849e58812a0ea5d517": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "514de6b09c5c4854845c1aaf36724cf6": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "5217e92f19c14061bbca7b26cb24b929": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "HTMLModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_1a3f7691f92b4ff59ea231a701ead85f", "placeholder": "​", "style": "IPY_MODEL_6b161893a69a44dab3cdf52dcc8326a5", "value": "100%" } }, "6b161893a69a44dab3cdf52dcc8326a5": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "6bcc47f33d0b49cb8e65e9bf3e83a01c": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "HBoxModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_5217e92f19c14061bbca7b26cb24b929", "IPY_MODEL_6d925602bbed40958aaa2578d33f0cf8", "IPY_MODEL_dd23695b6d5a4f3a9486509512bc1ead" ], "layout": "IPY_MODEL_514de6b09c5c4854845c1aaf36724cf6" } }, "6d925602bbed40958aaa2578d33f0cf8": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "FloatProgressModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_42a685066d8c4a849e58812a0ea5d517", "max": 10368000.0, "min": 0.0, "orientation": "horizontal", "style": "IPY_MODEL_1b237177349a47e4b7b40766345e418b", "value": 10368000.0 } }, "dd23695b6d5a4f3a9486509512bc1ead": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "HTMLModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_252ae0d0bb804887b5cec5442326c057", "placeholder": "​", "style": "IPY_MODEL_2ac72ef6e5dd4e6a84b7eadd62122c0c", "value": " 10368000/10368000 [03:37<00:00, 46820.57it/s]" } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }