Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Google deepmind Mujoco mjr readPixels

From Leeroopedia
Knowledge Sources
Domains Rendering, Computer_Vision, OpenGL
Last Updated 2026-02-15 06:00 GMT

Overview

Concrete tool for reading rendered pixels from an OpenGL framebuffer provided by the MuJoCo rendering API.

Description

The mjr_readPixels function reads RGB color and/or depth data from the currently active OpenGL framebuffer into CPU memory buffers. When reading from a multisampled offscreen FBO, it performs a resolve-blit first. Companion functions mjr_setBuffer and mjr_maxViewport control which framebuffer is active and query its dimensions.

Usage

Call after mjr_render to capture the rendered frame. Set mjFB_OFFSCREEN buffer first for headless rendering. Pass NULL for rgb or depth to skip that readback.

Code Reference

Source Location

  • Repository: mujoco
  • File: src/render/render_gl2.c
  • Lines (mjr_readPixels): 139-148
  • Lines (mjr_setBuffer): 33-77
  • Lines (mjr_maxViewport): 635-648

Signature

void mjr_readPixels(unsigned char* rgb, float* depth,
                    mjrRect viewport, const mjrContext* con);
void mjr_setBuffer(int framebuffer, mjrContext* con);
mjrRect mjr_maxViewport(const mjrContext* con);

Import

#include <mujoco/mujoco.h>

I/O Contract

Inputs

Name Type Required Description
rgb unsigned char* No Output RGB buffer (3 bytes/pixel, NULL to skip)
depth float* No Output depth buffer (1 float/pixel, NULL to skip)
viewport mjrRect Yes Rectangle defining which pixels to read
con const mjrContext* Yes Active rendering context
framebuffer int Yes mjFB_WINDOW or mjFB_OFFSCREEN (for mjr_setBuffer)

Outputs

Name Type Description
rgb (modified) unsigned char* Raw RGB pixel data (3 * width * height bytes)
depth (modified) float* Depth values in [0, 1] (width * height floats)
return (mjr_maxViewport) mjrRect Maximum viewport dimensions for active buffer

Usage Examples

#include <mujoco/mujoco.h>

// Switch to offscreen buffer
mjr_setBuffer(mjFB_OFFSCREEN, &con);

// Get maximum viewport
mjrRect viewport = mjr_maxViewport(&con);

// Render to offscreen buffer
mjv_updateScene(m, d, &opt, NULL, &cam, mjCAT_ALL, &scn);
mjr_render(viewport, &scn, &con);

// Read pixels
int W = viewport.width;
int H = viewport.height;
unsigned char* rgb = (unsigned char*)malloc(3 * W * H);
float* depth = (float*)malloc(sizeof(float) * W * H);
mjr_readPixels(rgb, depth, viewport, &con);

// Process or save pixels...
free(rgb);
free(depth);

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment