fjvallarino/monomer

Widget for direct backend use

Closed this issue · 5 comments

I'm looking for a way of rendering part of the GUI using OpenGL, e.g; to have a 3D preview of a model embedded in a ui panel.

Perhaps a solution that is not very intrusive is to render to texture (Ideally from a shared context) and display it in Monomer.
A widget encapsulating this behavior would be ideal.

Hi @edmeme!

It is possible to make OpenGL-based widgets without rendering them to a texture. The render function of Widget runs in IO instead of a restricted monad; this allows calling any OpenGL function you need. If other Renderer instances are added in the future (for Vulkan or Metal), this escape hatch will allow making low-level rendering with them too. The objective of choosing IO instead of a restricted monad was to give flexibility to applications that need this kind of functionality without tying the library to any specific backend.

When doing low-level OpenGL rendering, some care needs to be taken:

  • Since NanoVG depends on OpenGL's context, modifying it may cause unexpected results. You will need to call Renderer's saveContext/restoreContext for this.
  • Unless you are using appRenderOnMainThread, doing anything related to OpenGL outside the render function will cause a crash due to OpenGL's threading constraints. Consequently, shader compilation has to happen in the render function; you will need to use an IORef for storing state (that you can create on init) to avoid re-compiling the shader or re-creating vertex buffers every time. I will improve the API to remove this workaround and allow initializing OpenGL resources with a mechanism equivalent to RunTask.

I'll add an example with proper documentation; I had this in my backlog, but I never got around to implementing it. I'll keep you posted.

Hi @edmeme!

I just released version 1.1.0.0, which includes better support for initializing OpenGL resources. I also added a new example that explains how to integrate OpenGL into a Monomer application. You can find the source code of the example here.

I hope it's useful for your use case!

Thank you very much, it looks great :)
I like a lot the concept and design of Monomer so far, thanks for writing it.

Should I close the issue?

Yes, go ahead!

Done!