Implementation:Google deepmind Mujoco mjr makeContext
| Knowledge Sources | |
|---|---|
| Domains | Rendering, OpenGL, GPU |
| Last Updated | 2026-02-15 06:00 GMT |
Overview
Concrete tool for initializing MuJoCo's OpenGL rendering context with textures, shaders, and framebuffers provided by the MuJoCo rendering API.
Description
The mjr_makeContext function initializes all GPU rendering resources: loads the GLAD OpenGL function pointers, uploads model textures, creates shaders, sets up offscreen FBOs with multisampled color and depth buffers, and generates font bitmaps at the specified scale. It automatically detects whether a window or headless context is active.
Usage
Call after creating an OpenGL context (GLFW window, EGL, or OSMesa) and loading a model. Must be called before any mjr_render calls.
Code Reference
Source Location
- Repository: mujoco
- File: src/render/render_context.c
- Lines: 1525-1677
Signature
void mjr_makeContext(const mjModel* m, mjrContext* con, int fontscale);
Import
#include <mujoco/mujoco.h>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| m | const mjModel* | Yes | Model (for textures and visual settings) |
| con | mjrContext* | Yes | Context struct to initialize |
| fontscale | int | Yes | Font scale enum: mjFONTSCALE_50, mjFONTSCALE_100, mjFONTSCALE_150, mjFONTSCALE_200, mjFONTSCALE_250, mjFONTSCALE_300 |
Outputs
| Name | Type | Description |
|---|---|---|
| con (modified) | mjrContext* | Initialized with GPU textures, shaders, FBOs, fonts |
Usage Examples
#include <mujoco/mujoco.h>
#include <GLFW/glfw3.h>
// Create GLFW window first
glfwInit();
GLFWwindow* window = glfwCreateWindow(1200, 900, "MuJoCo", NULL, NULL);
glfwMakeContextCurrent(window);
// Load model
mjModel* m = mj_loadXML("scene.xml", NULL, error, 1000);
// Create rendering context
mjrContext con;
mjr_makeContext(m, &con, mjFONTSCALE_150);
// ... rendering loop ...
// Cleanup
mjr_freeContext(&con);