# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/BachiLi/redner/blob/master/tutorials/hello_redner_tensorflow.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="Ehkn6YdwVQ0c" colab_type="text"
# This is the first tutorial of redner. In this tutorial we will load a 3D object and render it with redner.

# + id="6_qmdZiqVVRs" colab_type="code" outputId="4de9a276-cbc1-4bda-b746-8b3b06829795" colab={"base_uri": "https://localhost:8080/", "height": 394}
# !pip install --upgrade redner-gpu

# + id="dMEENz-fVQ0g" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="8fe53249-55e2-4303-f07c-55284f461d84"
# Tensorflow by default allocates all GPU memory, leaving very little for rendering.
# We set the environment variable TF_FORCE_GPU_ALLOW_GROWTH to true to enforce on demand
# memory allocation to reduce page faults.
import os
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
# %tensorflow_version 2.x
import tensorflow as tf
tf.compat.v1.enable_eager_execution() # redner only supports eager mode

# + id="hQo6l3knVQ0l" colab_type="code" colab={}
# pyredner will be the main Python module we import for redner.
import pyredner_tensorflow as pyredner

# + [markdown] id="RueE8g7zVQ0r" colab_type="text"
# We begin by loading a [Wavefront object file](https://en.wikipedia.org/wiki/Wavefront_.obj_file), that contains the description of geometry and material of 3D objects. 

# + [markdown] id="V97l9KBYVQ0s" colab_type="text"
# We will download the famous [Utah teapot](https://en.wikipedia.org/wiki/Utah_teapot) from <NAME>'s awesome [website](https://casual-effects.com/data/):

# + id="9atnmAjwVQ0s" colab_type="code" colab={}
import urllib
import zipfile
# wget
filedata = urllib.request.urlretrieve('https://casual-effects.com/g3d/data10/common/model/teapot/teapot.zip', 'teapot.zip')
# unzip
zip_ref = zipfile.ZipFile('teapot.zip', 'r')
zip_ref.extractall('teapot/')

# + id="F75PEnstVQ0v" colab_type="code" colab={}
# Now we load the obj file using pyredner.load_obj. Since we set return_objects=True,
# load_obj returns a list of redner "Object", which contains geometry, material, and lighting information.
objects = pyredner.load_obj('teapot/teapot.obj', return_objects=True)

# + [markdown] id="YFEo-qh1VQ0y" colab_type="text"
# Now the teapot geometry is stored in `objects`. To render the scene, we need to have a camera. Redner provides an automatic camera placement function we can use for this. It uses the bounding boxes of the objects to find a position where you can see all the objects.

# + id="SyfsEjsGVQ00" colab_type="code" colab={}
camera = pyredner.automatic_camera_placement(objects, resolution = (512, 512))

# + [markdown] id="Ep7h96UdVQ02" colab_type="text"
# Once we have the objects and camera, we can build a redner "Scene":

# + id="HLvrpsWOVQ05" colab_type="code" colab={}
scene = pyredner.Scene(camera = camera, objects = objects)

# + [markdown] id="_HTJoidIVQ07" colab_type="text"
# Now we can render the scene. Since we did not set up the light sources, we visualize the albedo color of the teapot here.

# + id="eeZh_Y-yVQ08" colab_type="code" outputId="c005b1af-2bca-4454-c8c9-cb5ebee8d9e5" colab={"base_uri": "https://localhost:8080/", "height": 320}
img = pyredner.render_albedo(scene)
# Visualize img
from matplotlib.pyplot import imshow
# %matplotlib inline
# Need to gamma compress the image for displaying.
imshow(tf.pow(img, 1.0/2.2))

StarcoderdataJupyterScriptsDedupFiltered