Skip to content

Simple Rendering

mattloper edited this page Sep 8, 2014 · 3 revisions

Below, we step through what happens in the demonstration given by...

import opendr
opendr.demo('point_light')

First, we import some prerequisites and create a renderer. Here it is a colored renderer, but others (for boundary rendering, textured rendering, and depth rendering) are also available from the same renderer module.

import chumpy as ch
from opendr.renderer import ColoredRenderer
rn = ColoredRenderer()

Next we load our geometry. This will be defined as a set of triangles: by "v" (the Vx3 chumpy array of vertices) and "f" (the Fx3 array of vertex indices). Here we load a mesh, but we could also drive one from parameters.

from opendr.util_tests import get_earthmesh
m = get_earthmesh(trans=ch.array([0,0,4]), rotation=ch.zeros(3))
v = ch.array(m.v)
vc = ch.array(m.vc) # albedo
f = m.f

Now we set up the camera, filling in the camera parameters necessary to set up a perspective projection.

from opendr.camera import ProjectPoints
w, h = (320, 240)
rn.camera = ProjectPoints(v=v, rt=ch.zeros(3), t=ch.zeros(3), 
    f=ch.array([w,w])/2.,c=ch.array([w,h])/2., k=ch.zeros(5))
rn.frustum = {'near': 1., 'far': 10., 'width': w, 'height': h}
rn.v = v
rn.f = f
rn.bgcolor = ch.zeros(3)

Next we construct a Vx3 array of per-vertex colors; each vertex color will be what is observed onscreen, and must therefore incorporate both lighting and albedo. We could construct it from many kinds of differentiable functions, but here we assign a simple light source to it. Simple modifications could include having sums of light sources or spherical harmonics (also built-in).

from opendr.lighting import LambertianPointLight
rn.vc = LambertianPointLight(
    f=f,
    v=v,
    num_verts=len(v),
    light_pos=ch.array([-1000,-1000,-1000]),
    vc=vc,
    light_color=ch.array([1., 1., 1.]))

Finally, we can view the result.

import matplotlib.pyplot as plt
plt.ion()
plt.imshow(rn.r)
plt.draw()
Point light sphere demo

It is important to realize that this is a reactive framework, in the sense that changes to values will affect function which depend on them. As an example, let us perturb the vertices, and see what happens to the result:

rn.v = rn.v + ch.random.randn(rn.v.size).reshape(rn.v.shape)*.1
rn.vc.v = rn.v
plt.imshow(rn.r)
plt.draw()
Point light sphere demo
Clone this wiki locally