NogginBops / ImGui.NET_OpenTK_Sample

A sample project showing an ImGui (using ImGui.NET) renderer for OpenTK in C#
106 stars 27 forks source link

Window is not correctly shown #7

Closed uzername closed 4 years ago

uzername commented 4 years ago

I have downloaded your sample, restored packages and started it. To make it look more clear, I have set GL.ClearColor to white: GL.ClearColor(new Color4(255, 255, 255, 255));

Window is not rendered correctly, text is not readable:

image

uzername commented 4 years ago

It looked similar with a color specified in original code

NogginBops commented 4 years ago

@uzername If you are able to, could you open the project using RenderDoc and send me a capture?

Basically you start the program through RenderDoc and then save the capture. It would be a great help for getting a clue as to why this issue is happening. https://renderdoc.org/docs/how/how_capture_frame.html

I would also greatly appreciate if you could tell me what os, .net version, and gpu you are using as that could also help in figuring out what the problem is. 🙂

NogginBops commented 4 years ago

@uzername This should be fixed with the latest commit. Can you try and see if it's fixed?

uzername commented 4 years ago

image

It works now, thanks

uzername commented 4 years ago

image

Now window may disappear Happens when you quickly move mouse to it, or outside of it

jbracey2004 commented 4 years ago

@uzername , were you able to get the RenderDoc capture? I ran into the same issue, but was unable to get RenderDoc to start the program. I will try again later.

jbracey2004 commented 4 years ago

@NogginBops, quick update, I don't know if this will help, but I stumbled upon this Reddit post about gl buffers . It's about glBufferData, but it also mentions something about how in OpenGL 4.4+ you cannot resize a buffer, you have to delete it. I know your code does not use glBufferData, but glNamedBufferData, but I heard that the glNamedBufferData is exclusive to OpenGL 4.5+. I wonder if this is the issue. Only way to know for sure is if I test it, so I will see what happens if I have it delete the old buffer and create a new one when resizing. Also I'm still curious about what OpenGL is actually doing giving the current code, so I will still try to get that RenderDoc capture.

uzername commented 4 years ago

I got RenderDoc, but when I launch this program with RenderDoc window stays on place. When I launch it WITHOUT RenderDoc a window can disappear. It tends to disappear when I move pointer ove Menu

NogginBops commented 4 years ago

@uzername I've just created an opengl3.3 branch that only uses functions actually available in 3.3 (it actually used a few 4.5 features). The master branch is updated to use opengl 4.5.

So if you could test is the issue persists on both versions or if the issue is fixed I would greatly appreciate that.

jbracey2004 commented 4 years ago

@NogginBops Good News: I was able to get the sample to work correctly by having the buffers deleted before recreating them when resizing. Below is the corrected code:

uint totalVBSize = (uint)(draw_data.TotalVtxCount * Unsafe.SizeOf<ImDrawVert>());
if (totalVBSize > _vertexBufferSize)
{
    int newSize = (int)Math.Max(_vertexBufferSize * 1.5f, totalVBSize);
    GL.DeleteBuffer(_vertexBuffer);
    Util.CreateVertexBuffer("ImGui", out _vertexBuffer);
    GL.NamedBufferData(_vertexBuffer, newSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
    GL.VertexArrayVertexBuffer(_vertexArray, 0, _vertexBuffer, IntPtr.Zero, Unsafe.SizeOf<ImDrawVert>());
    _vertexBufferSize = newSize;
    Console.WriteLine($"Resized vertex buffer to new size {_vertexBufferSize}");
}
uint totalIBSize = (uint)(draw_data.TotalIdxCount * sizeof(ushort));
if (totalIBSize > _indexBufferSize)
{
    int newSize = (int)Math.Max(_indexBufferSize * 1.5f, totalIBSize);
    GL.DeleteBuffer(_indexBuffer);
    Util.CreateElementBuffer("ImGui", out _indexBuffer);
    GL.NamedBufferData(_indexBuffer, newSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
    GL.VertexArrayElementBuffer(_vertexArray, _indexBuffer);
    _indexBufferSize = newSize;
    Console.WriteLine($"Resized index buffer to new size {_indexBufferSize}");
}

Also note, I also have a forked version that uses the old-fashioned glBuffer calls instead of the glNamedBuffer calls. It seems to be able to resize buffers without having to delete and recreate them.

Also, I have tested the app on the opengl3.3 branch. That one works too.

uzername commented 4 years ago

opengl 3.3 branch works like a charm ! No flickering, window is not disappearing. Good!

NogginBops commented 4 years ago

@uzername I thought I would let you know that I just discovered a problem with the projection matrix that is used, and it's making the text blurry. When creating the projection matrix the call should look like this:

Matrix4 mvp = Matrix4.CreateOrthographicOffCenter(
                0.0f,
                io.DisplaySize.X,
                io.DisplaySize.Y,
                0.0f,
                -1.0f,
                1.0f);

The change is that the first argument is changed from -1.0 to 0.0.

LuisMerinoP commented 2 years ago

Just as info, with the @jbracey2004 code that deletes the buffer when resized the sample works in the default branch "opentk4.0". Without this fix this problem persists (imgui disappears eventually on mouse move or when the imgui window is clicked). Although that is corrected in the "opentk4.0-dev" branch, in case the code wants to be corrected in the considered latest working version in the default branch.