ExMujoco.Renderer (ExMujoco v0.2.3)

Copy Markdown View Source

Offscreen rendering: the images a scene has, through a camera in it.

A renderer belongs to a model — it uploads the model's meshes and textures into OpenGL objects — so it is made once per model and kept. It also belongs to a resolution: MuJoCo's colour and depth buffers are one size, and a second size is a second renderer.

{:ok, model} = ExMujoco.load_string("<mujoco>…</mujoco>")
data = ExMujoco.data(model)
ExMujoco.forward(data)

renderer = ExMujoco.Renderer.new(model, 64, 64)
[rgb, depth, segmentation] = ExMujoco.Renderer.read(renderer, data, "cam", [:rgb, :depth, :segmentation])

What comes back

One binary per pass, top row first:

passshapeholds
:rgbwidth * height * 3 bytesred, green, blue, in 0..255
:depthwidth * height * 4 bytesfloat32 metres from the camera
:segmentationwidth * height * 4 bytesint32 geom id, -1 where nothing was drawn

ExMujoco.Tensor turns a binary into numbers; these are plain binaries because a picture is one and a tensor of 4096 values is a list of 4096 floats.

Where it works

Rendering needs an OpenGL context, and this package makes one itself: CGL on macOS, EGL on Linux, and nowhere else yet. Nothing is linked — the platform's library is opened when the first renderer is asked for — so a machine without one still loads this package and runs everything that does not draw. available?/0 answers whether this machine draws, and unavailable_reason/0 says why not in words.

The resolution is the model's to allow

MuJoCo renders into an offscreen buffer whose size the model declares (<visual><global offwidth=… offheight=…/>, 640×480 by default). An image larger than that is refused here rather than rendered black.

Summary

Types

A pass to draw, and the shape of the binary it answers.

t()

A renderer for one model at one resolution.

Functions

Whether this machine can render offscreen at all.

A renderer for model, drawing width by height images.

Draws passes through the camera camera names, and answers one binary per pass, in the order asked for.

The camera and the image size a renderer was made for.

Why this machine cannot render, in words — empty when it can.

Types

pass()

@type pass() :: :rgb | :depth | :segmentation

A pass to draw, and the shape of the binary it answers.

t()

@type t() :: %ExMujoco.Renderer{
  height: pos_integer(),
  model: ExMujoco.model(),
  resource: term(),
  width: pos_integer()
}

A renderer for one model at one resolution.

It holds the model it was made for, so the model cannot be freed underneath a context that has uploaded it.

Functions

available?()

@spec available?() :: boolean()

Whether this machine can render offscreen at all.

The answer is memoised by the NIF: the platform's OpenGL library is opened once.

new(model, width, height)

@spec new(ExMujoco.model(), pos_integer(), pos_integer()) :: t()

A renderer for model, drawing width by height images.

Raises ExMujoco.Error when the machine has no OpenGL context, or when the model's offscreen buffer is smaller than the image asked for.

read(renderer, data, camera, passes)

@spec read(t(), ExMujoco.data(), String.t(), [pass(), ...]) :: [binary()]

Draws passes through the camera camera names, and answers one binary per pass, in the order asked for.

data has to be stepped or forwarded first: a render reads the model's kinematics, and what it reads is whatever mj_forward last computed.

size(renderer)

@spec size(t()) :: {pos_integer(), pos_integer()}

The camera and the image size a renderer was made for.

unavailable_reason()

@spec unavailable_reason() :: String.t()

Why this machine cannot render, in words — empty when it can.