memononen / nanovg

Antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
zlib License
5.2k stars 779 forks source link

Integrating nanovg into a game engine #321

Open sabotage3d opened 8 years ago

sabotage3d commented 8 years ago

I am integrating nanovg into a game engine. Is there a way I can access the draw command lists, similar to imgui where you can get ImDrawData and pass it to another graphics API. Where I can access the vertex and index buffers. Is it recommended to follow this approach or to use render to texture approach using nvglCreateImageFromHandle?

jb55 commented 8 years ago

Not sure if this is helpful or not, but nanovg was integrated into the bgfx graphics engine here: https://github.com/bkaradzic/bgfx/blob/9f0ff9f2ffe3f23434a434abea2f98fbe692c6d9/examples/common/nanovg/nanovg_bgfx.cpp

sabotage3d commented 8 years ago

Thanks but this implementation is replacing all the OpenGL calls with bgfx ones this won't help me as I want to use nanovg as it is.

memononen commented 8 years ago

Writing a new backend is that way to go. You can use the opengl one as a base if you're engine is opengl based.

sabotage3d commented 8 years ago

Thank you for your reply. The engine is already OpenGL. The problem is if I render directly at the end of frame nanovg renders correctly but the game engine geometry would disappear. I am not sure which approach I need to use. Do you have an example for similar backend?

memononen commented 8 years ago

in that case you should reset/restore the opengl state you expect your engine to have after rendering with nanovg. the readme has the state that is touched.

sabotage3d commented 8 years ago

I see. So there is no other option copying the buffers or rendering to texture won't help?

bengarney commented 8 years ago

We've done this in Loom if you want to take a look: github.com/LoomSDK/LoomSDK

I'd specifically check out the nanovg code at https://github.com/LoomSDK/LoomSDK/blob/master/loom/vendor/nanovg/src/ and https://github.com/LoomSDK/LoomSDK/blob/master/loom/graphics/gfxGraphics.cpp and friends.

We have two 2D renderer paths, so it's a bit simpler than a full 3d game engine, but has to contend with the same basic issues (nanovg and other path set different state).

I'd give you more exact links but there's not really a minimal case for "show 2 renderers cooperating." Sorry. :)

On Fri, Oct 7, 2016 at 1:54 PM, sabotage3d notifications@github.com wrote:

I see. So there is no other option copying the buffers or rendering to texture won't help?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/memononen/nanovg/issues/321#issuecomment-252358611, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIG7KMwURsr0oFF6zESGH10nHK2CCDGks5qxrF-gaJpZM4KQEBy .

sabotage3d commented 8 years ago

Thanks a lot. I managed to get it working by baking up and restoring the modified GL states. I think it would be better if I use the engines API instead of raw OpenGL. At the moment is there a good way to query the all the draw commands like in imgui, similar to these structures?

struct ImDrawData
{
    bool            Valid;                  // Only valid after Render() is called and before the next NewFrame() is called.
    ImDrawList**    CmdLists;
    int             CmdListsCount;
    int             TotalVtxCount;          // For convenience, sum of all cmd_lists vtx_buffer.Size
    int             TotalIdxCount;          // For convenience, sum of all cmd_lists idx_buffer.Size
}

struct ImDrawList
{
    // This is what you have to render
    ImVector<ImDrawCmd>     CmdBuffer;          // Commands. Typically 1 command = 1 gpu draw call.
    ImVector<ImDrawIdx>     IdxBuffer;          // Index buffer. Each command consume ImDrawCmd::ElemCount of those
    ImVector<ImDrawVert>    VtxBuffer;          // Vertex buffer.
}
memononen commented 8 years ago

That is not possible. Currently the front end is made simple and the back end is responsible for caching.

sabotage3d commented 8 years ago

How are you guys handling the stencil buffer on mobile devices it seems there are quite a few devices which are having problems. And some that do not support it at all. Is there alternative approach for something like color picker gradient like in the example?