Rendering blank image¶
Let’s start to use Lightmetrica by rendering a blank image.
We first import the lightmetrica module, where we use lm as an alias of lightmetrica for simplicity.
Note
Although we recommend to use Python API to organize the experiments, similar APIs can be accessible from C++. See example directory for the detail.
Note
lmfunctest is a simple module to configure Lightmetrica envrionment from a file named .lmenv. You want to create your own .lmenv file if you want to execute examples or tests by yourself. For detail, see todo.
[1]:
import lmfunctest as ft
[2]:
import lightmetrica as lm
Lightmetrica offers an extension for the Jupyter notebook to support logging or interactive progress reporting inside the notebook. The extension can be loaded by a line magic command as below.
[3]:
%load_ext lightmetrica_jupyter
After importing the module, you can initialize the framwork by calling lm::init() function. You can pass various arguments to configure the framework to the function, but here we keep it empty so that everything is configured to be default.
[4]:
lm.init()
Logging and progress reporting in Jupyter notebook can be enabled by lm::log::init() and lm::progress::init() functions with corresponding types.
[5]:
lm.log.init('logger::jupyter')
lm.progress.init('progress::jupyter')
Once the framework has been initialized properly, you can get an splash message using lm::info() function.
[6]:
lm.info()
[I|0.007|114@user ] Lightmetrica -- Version 3.0.0 (rev. fe30e7c) Linux x64
Next we define assets necessary to dispatch renderer, like materials, meshes, etc. In this example, we only need a film to which the renderer outputs the image. We can define assets by lm::asset() function. The first argument (film) specifies the name of the asset to be referenced. The second argument (film::bitmap) is given as the type of the assets formatted as <type of asset>::<implementation> where the last argument ({...}) specifies the parameters passed to the instance. film::bitmap takes two parameters w and h which respectively specify width and height of the film.
[7]:
lm.asset('film', 'film::bitmap', {
'w': 1920,
'h': 1080
})
[I|0.017|48@assets ] Loading asset [name='film']
[7]:
'$.assets.film'
Now we are ready for rendering. lm::render() function dispatches rendering where we speficy type of the renderer and the parameters as arguments. renderer::blank is a toy renderer that only produces a blank image to the film specifies by film parameter where we can use predefined ID of the asset. Also, we can specify the background color by color parameter.
[8]:
lm.render('renderer::blank', {
'output': lm.asset('film'),
'color': [1,1,1]
})
[I|0.151|151@user ] Starting render [name='renderer::blank']
After rendering, the generated image is kept in film. lm::save() function outputs this film to the disk as an image.
[9]:
lm.save(lm.asset('film'), 'blank.png')
[I|0.330|97@film_bi] Saving image [file='blank.png']
You can also visualize the film directly in Jupyter notebook. lm::buffer() gets the internal image data as numpy array which you can visualize using matplotlib. We rendered a white blank image thus the following image is as we expected.
[10]:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
[11]:
img = np.copy(lm.buffer(lm.asset('film')))
f = plt.figure(figsize=(15,15))
ax = f.add_subplot(111)
ax.imshow(np.clip(np.power(img,1/2.2),0,1), origin='lower')
plt.show()
Finally, we gracefully shutdown the framework with lm::shutdown() function. Usually you don’t want to explicitly call shutdown function.
[12]:
lm.shutdown()