Raycasting a scene with OBJ models

This example demonstrates how to render a scene with OBJ models using raycasting.

[1]:
import lmenv
env = lmenv.load('.lmenv')
[2]:
import os
import numpy as np
import imageio
%matplotlib inline
import matplotlib.pyplot as plt
import lightmetrica as lm
%load_ext lightmetrica_jupyter
[4]:
lm.init()
lm.log.init('jupyter')
lm.progress.init('jupyter')
lm.info()
[I|0.000] Lightmetrica -- Version 3.0.0 (rev. 70601db) Linux x64

Following is the definition of assets. To load an OBJ model, we can use model::wavefrontobj asset. This asset internally creates meshes and materials by reading the associated MTL file.

Note

A model asset can be considered as a special asset containing (a part of) the scene graph and assets reference by the structure.

[5]:
# Film for the rendered image
film = lm.load_film('film', 'bitmap', w=1920, h=1080)

# Pinhole camera
camera = lm.load_camera('camera', 'pinhole',
    position=[5.101118, 1.083746, -2.756308],
    center=[4.167568, 1.078925, -2.397892],
    up=[0,1,0],
    vfov=43.001194,
    aspect=16/9)

# OBJ model
model = lm.load_model('model', 'wavefrontobj',
    path=os.path.join(env.scene_path, 'fireplace_room/fireplace_room.obj'))
[I|0.009] Loading asset [name='film']
[I|0.086] Loading asset [name='camera']
[I|0.087] Loading asset [name='model']
[I|0.087] .. Loading OBJ file [path='fireplace_room.obj']
[I|0.087] .. Loading MTL file [path='fireplace_room.mtl']
[I|0.417] .. Loading texture [path='wood.ppm']
[I|0.508] .. Loading texture [path='leaf.ppm']
[I|0.511] .. Loading texture [path='picture8.ppm']
[I|0.543] .. Loading texture [path='wood5.ppm']

We can create primitives from the loaded mode using ``model` parameter for the lm::Scene::add_primitive() function.

[6]:
accel = lm.load_accel('accel', 'sahbvh')
scene = lm.load_scene('scene', 'default', accel=accel)
scene.add_primitive(camera=camera)
scene.add_primitive(model=model)
scene.build()
[I|0.602] Loading asset [name='accel']
[I|0.603] Loading asset [name='scene']
[I|0.606] Building acceleration structure [name='accel']
[I|0.607] .. Flattening scene
[I|0.635] .. Building

Executing the renderer will produce the following image.

[7]:
renderer = lm.load_renderer('renderer', 'raycast',
    scene=scene,
    output=film,
    bg_color=[0,0,0])
renderer.render()
[I|1.264] Loading asset [name='renderer']
[7]:
{'elapsed': 2.690107961}
[8]:
img = np.copy(film.buffer())
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()
../_images/executed_functest_example_raycast_11_0.png