Implementation:Google deepmind Mujoco GLFW Adapter
| Knowledge Sources | |
|---|---|
| Domains | Windowing, Input Handling, GLFW, Platform Abstraction |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
GLFW-based platform adapter implementation that provides window management, input event handling, and OpenGL context operations for MuJoCo's simulate viewer.
Description
This file implements the GlfwAdapter class, a concrete platform adapter backed by GLFW. It initializes GLFW, creates a multisampled window at 2/3 of the primary monitor resolution, and registers callbacks for keyboard, mouse, scroll, window resize, and file drop events. The adapter provides methods for cursor position queries, framebuffer/window size retrieval, VSync control, fullscreen toggling, clipboard access, and input state queries (mouse buttons, modifier keys). On macOS, it optionally integrates with CoreVideo for display synchronization.
Usage
Instantiated as the platform-specific adapter when launching the MuJoCo simulate viewer on desktop platforms. Passed as a std::unique_ptr<PlatformUIAdapter> to the Simulate constructor, which uses it for all window and input operations.
Code Reference
Source Location
- Repository: Google_deepmind_Mujoco
- File: simulate/glfw_adapter.cc
- Lines: 1-252
Key Functions
// Constructor: initialize GLFW, create window, register callbacks
GlfwAdapter::GlfwAdapter();
// Destructor: destroy GLFW window
GlfwAdapter::~GlfwAdapter();
// Query cursor position in window coordinates
std::pair<double, double> GlfwAdapter::GetCursorPosition() const;
// Query framebuffer dimensions (pixels)
std::pair<int, int> GlfwAdapter::GetFramebufferSize() const;
// Query window dimensions (screen coordinates)
std::pair<int, int> GlfwAdapter::GetWindowSize() const;
// Check if GPU-accelerated rendering is available
bool GlfwAdapter::IsGPUAccelerated() const;
// Poll for pending input events
void GlfwAdapter::PollEvents();
// Enable/disable vertical synchronization
void GlfwAdapter::SetVSync(bool enabled);
// Toggle between windowed and fullscreen mode
void GlfwAdapter::ToggleFullscreen();
// Swap front and back buffers
void GlfwAdapter::SwapBuffers();
// Input state queries
bool GlfwAdapter::IsLeftMouseButtonPressed() const;
bool GlfwAdapter::IsMiddleMouseButtonPressed() const;
bool GlfwAdapter::IsRightMouseButtonPressed() const;
bool GlfwAdapter::IsAltKeyPressed() const;
bool GlfwAdapter::IsCtrlKeyPressed() const;
bool GlfwAdapter::IsShiftKeyPressed() const;
Import
#include "glfw_adapter.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none for constructor) | - | - | Reads primary monitor video mode from GLFW to determine window size |
| enabled | bool | Yes (for SetVSync) | Whether to enable vertical sync |
| title | const char* | Yes (for SetWindowTitle) | Window title string |
| text | const char* | Yes (for SetClipboardString) | Text to copy to system clipboard |
Outputs
| Name | Type | Description |
|---|---|---|
| GLFWwindow* | pointer (internal) | The created GLFW window handle stored as window_ |
| Cursor position | std::pair<double, double> | Current mouse position in window coordinates |
| Framebuffer size | std::pair<int, int> | Framebuffer dimensions in pixels |
| Window size | std::pair<int, int> | Window dimensions in screen coordinates |
| Input state | bool | Mouse button and modifier key state |