DEV Community

Kai Jellinghaus
Kai Jellinghaus

Posted on

WebGL in C# with Silk.NET: Part 1 - Emscripten interop

TIL: Emscripten (the LLVM to WASM compiler) is capable of picking up on OpenGL and automatically translating to WebGL. Yes, really.
I don't know why, but I know that I want to trigger this functionality from C# with Silk.NET

So first of all, what does a basic GLFW/OpenGL Emscripten compatible program look like in C++?
I've found this gist and that seems very reasonable, almost everything here can be done with Silk.NET already, just missing one thing:
emscripten_set_main_loop so in this blog post I just try and figure out how I can call this from C#.

It's a little complicated, but the easiest way to make this work is to just

  • create a file with #include "emscripten.h" in it.
  • Reference that file using NativeFileReference
  • use the DllImports like normal.

This allows us to set an emscripten main loop already:
Image description

In the next part I'll look at basic OpenGL interop. I'm very confident now this first huddle is overcome!

Discussion (0)