Skip to content

Compiling Mujoco to wasm with emscripten #2585

Open
@LazyYuuki

Description

@LazyYuuki

The feature, motivation and pitch

I'm working on compiling Mujoco to wasm using emscripten and would like to be able to make the Mujoco rencer to match with GLES2 instead of GL.

I have consulted:

https://github.com/zalo/mujoco_wasm

For direction on how I can do so, but the method proposed by Zalo above is not efficient and native. It requires the mapping of Mujoco to Three.js function to then be able to render in the browser, while theoretically, you should be able to just run OpenGL map to WebGL directly through GLES2, reducing 1 layer of abstraction that would require more maintenance otherwise.

Furthermore, zalo guide is under the assumption that you already successfully compile Mujoco to static archive to be able to compile to wasm, however, the guide and version they based on is now outdated, and I have provided a new method as detailed down here (albeit incomplete and might contain mistake as I am a novice to all of this):

https://github.com/menloresearch/mujoco-wasm

The main problem I am having right now is that the current way Mujoco handling OpenGL rendering is by using glad to generate a loader for OpenGL v 1.5. I can built the source code, but when I invoke the binary to call for mjr_updateContext it will throw an error back saying cannot find dl_pthread_iterate. Upon further digging, I realise that it is because it is dynamically trying to find this function in a shared library on Linux system instead, and could not find it.

Further research show me that, to make this compatible easier, it is better for me to go and generate a new glad loader specifically for GLES2 using this folllowing link:

https://gen.glad.sh/

Here is my setting after generated the file:

 * Loader generated by glad 2.0.8 on Sat Apr 19 02:15:03 2025
 *
 * SPDX-License-Identifier: (WTFPL OR CC0-1.0) AND Apache-2.0
 *
 * Generator: C/C++
 * Specification: gl
 * Extensions: 45
 *
 * APIs:
 *  - gles2=3.2
 *
 * Options:
 *  - ALIAS = True
 *  - DEBUG = False
 *  - HEADER_ONLY = False
 *  - LOADER = True
 *  - MX = True
 *  - ON_DEMAND = False

As far as I know, I don't include any extensions for now for stability reason.

Right now I am stuck at the part where I can see that there is quite a bit of modifciation to change all the glad function call to become mjGlad_ instead, and it is causing confusion for me to know how should I approach this modification.

It is also a problem that the GLES2 generated version using the gen.glad.sh is very much difference from what is present on Mujoco also.

I would really appreciate if I can get some help or guidance on how should I proceed on what I need to change and where. Once I get it workings, the code will be subsequently free and open for everyone else to use also on the github link provided above. It would be very exciting to make Mujoco even more portable and available to more platform to be used on.

Alternatives

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions