Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using ImGui.GetWindowDrawList(), nothing is rendered #264

Closed
odalet opened this issue Aug 2, 2021 · 2 comments
Closed

When using ImGui.GetWindowDrawList(), nothing is rendered #264

odalet opened this issue Aug 2, 2021 · 2 comments

Comments

@odalet
Copy link

odalet commented Aug 2, 2021

I'm trying to port over to C# ocornut's 'ImDrawList' test bench (see here: ocornut/imgui#3606).

I'm retrieving the window draw list with ImGui.GetWindowDrawList() and then using primitives on the resulting draw list. However, I can't seem to get results displayed.

Here is the code I'm using based on ImGui.NET.SampleProgram: I replaced SubmitUI but the rest of the code is the same:

private static unsafe void SubmitUI()    
{
    // See (https://gist.github.com/ocornut/51367cc7dfd2c41d607bb0acfa6caf66)
    _ = ImGui.Begin("FX", ImGuiWindowFlags.AlwaysAutoResize);
    ImGui.Text("BEFORE");

    var size = new Vector2(320f, 180f);
    _ = ImGui.InvisibleButton("canvas", size);
    var p0 = ImGui.GetItemRectMin();
    var p1 = ImGui.GetItemRectMax();

    var drawList = ImGui.GetWindowDrawList();
    drawList.PushClipRect(p0, p1);

    var io = ImGui.GetIO();
    var mouseData = new Vector4
    {
        X = (io.MousePos.X - p0.X) / size.X,
        Y = (io.MousePos.Y - p0.Y) / size.Y,
        Z = io.MouseDownDuration[0],
        W = io.MouseDownDuration[1]
    };

    //drawList.AddRectFilled(p0, p1, MakeColor32(255, 0, 0, 255));
    Fx(drawList, p0, p1, size, mouseData, (float)ImGui.GetTime());

    drawList.PopClipRect();

    ImGui.Text("AFTER");
    ImGui.End();
}

private static void Fx(ImDrawListPtr d, Vector2 a, Vector2 b, Vector2 sz, Vector4 mouse, float t)
{
    for (var n = 0; n < (1.0f + Math.Sin(t * 5.7f)) * 40.0f; n++) d.AddCircle(
        new Vector2(a.X + sz.X * 0.5f, a.Y + sz.Y * 0.5f),
        sz.Y * (0.01f + n * 0.03f), MakeColor32(255, (byte)(140 - n * 4), (byte)(n * 3), 255));
}

private static uint MakeColor32(int r, int g, int b, int a) => (uint)((byte)a << 24 & (byte)b << 16 & (byte)g << 8 & (byte)r);

All that I obtain is an empty placeholder:

image

Any idea why this does not work?

Thanks!

@zaafar
Copy link
Collaborator

zaafar commented Aug 13, 2021

@odalet

Your color function is wrong, I use the following one.

public static uint Color(byte r, byte g, byte b, byte a) { uint ret = a; ret <<= 8; ret += b; ret <<= 8; ret += g; ret <<= 8; ret += r; return ret; }

Screen Shot 2021-08-13 at 9 56 59 AM

OR just update your color function to following

(uint)((byte)a << 24 | (byte)b << 16 | (byte)g << 8 | (byte)r);

@odalet
Copy link
Author

odalet commented Aug 14, 2021

@zaafar Thank you... By examining your implementation and comparing it with mine, I saw that all my & should be replaced by | then I saw your second suggestion: that is exactly what I ended up with!

I feel ashamed for having got mixed up between AND and OR... no wonder my code didn't work, I was drawing everything with a transparent color :(
Thanks again!

@odalet odalet closed this as completed Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants