Implementation:Google deepmind Mujoco initOpenGL Pattern
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Rendering, OpenGL, Headless |
| Last Updated | 2026-02-15 06:00 GMT |
Overview
Concrete pattern for initializing headless OpenGL contexts from the MuJoCo record sample.
Description
The initOpenGL function in sample/record.cc demonstrates the standard pattern for creating a headless OpenGL context. It uses compile-time defines (MJ_EGL, MJ_OSMESA) to select the backend. The EGL path creates a surfaceless context; the OSMesa path creates a software-rendered context; the GLFW fallback creates a hidden window.
Usage
Adapt this pattern for headless rendering applications. Select the backend via CMake defines at build time.
Code Reference
Source Location
- Repository: mujoco
- File: sample/record.cc
- Lines: 92-175
Signature
// Local helper function (not part of public API)
void initOpenGL(void);
// Selects backend based on compile-time defines:
// MJ_EGL -> EGL headless context
// MJ_OSMESA -> OSMesa software context
// else -> GLFW hidden window
Import
// Backend-specific headers
#ifdef MJ_EGL
#include <EGL/egl.h>
#elif defined(MJ_OSMESA)
#include <GL/osmesa.h>
#else
#include <GLFW/glfw3.h>
#endif
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | Backend selected at compile time |
Outputs
| Name | Type | Description |
|---|---|---|
| side effect | OpenGL context | Active OpenGL context suitable for offscreen rendering |
Usage Examples
// EGL headless initialization (simplified from record.cc)
#ifdef MJ_EGL
const EGLint configAttribs[] = {
EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_NONE
};
EGLDisplay eglDpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(eglDpy, NULL, NULL);
EGLConfig eglCfg;
EGLint numConfigs;
eglChooseConfig(eglDpy, configAttribs, &eglCfg, 1, &numConfigs);
eglBindAPI(EGL_OPENGL_API);
EGLContext eglCtx = eglCreateContext(eglDpy, eglCfg, EGL_NO_CONTEXT, NULL);
eglMakeCurrent(eglDpy, EGL_NO_SURFACE, EGL_NO_SURFACE, eglCtx);
#endif
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment