#How to get ImGui with GLFW-OpenGL backend working, on Windows?

1 messages · Page 1 of 1 (latest)

dreamy narwhal
#

Hello, me again!

I'm still on that same project that involves GLFW, OpenGL and Windows.
The libraries I use for it so far are: zgl, zglfw and zlm, with emidoots' glfw to interface with C's GLFW.

This time, I want to add ImGui to the mix. So looking this up, I found zgui, which seems fairly easy to add into the mix.
So, I set the build.zig up (and the build.zig.zon):

    // ...
    const zgui = b.dependency("zgui", .{
        .target = target,
        .backend = .glfw_opengl3,
    });
    exe.root_module.addImport("zgui", zgui.module("root"));
    exe.linkLibrary(zgui.artifact("imgui"));

I use the 2 to 3 lines of code that initialize the backend

But then I get a compile error, which seems to indicate that imgui cannot compile as it cannot find OpenGL, even though my program that uses it has been working great so far.

So, how can I solve this?

dreamy narwhal
#

anyone?

#

How to get ImGui with GLFW-OpenGL backend working, on Windows?

dapper wind
#

Generated Bindings

This library includes OpenGL 4.5 bindings, generated by zig-opengl. Bindings for a different version may be substituted by replacing binding.zig.
from the zgl github page

#

but your erreor seams to indicate that zgui is locking for opengl 3
looks like a version missmatch for me

dreamy narwhal
#

I mean, even if it was, glActiveTexture is a function that is an inherent part of OpenGL, regardless of v3 or v4. Rn, imgui can't even identify it

#

I kinda know that the way OpenGL works is it needs to have the functions provided to it by the graphical backend (in my case, glfw)

#

(which imgui supports)

dapper wind
dreamy narwhal
dapper wind
#

hmmmm

dreamy narwhal
#

To me, it seems more like a case of "zgui doesn't even see zgl"

dapper wind
#

that is possible

dreamy narwhal
#

which kinda makes sense, given that it's loaded in the project, not in the imgui

#

and from what I understand, it tries to compile the C++ imgui first

#

But if the C++ doesn't find OpenGL (which is typically a shared library), it doesn't compile

#

that's where my issue is: What does Zig use as a C++ compiler, on Windows?

dapper wind
#

should be clang

#

not sure tho

dreamy narwhal
#

okay, but which clang? which lib directory? how can I make sure that opengl is installed there, and if it's not, how can I install it?

dapper wind
#

i know little about the build system

dreamy narwhal
#

no, that's for editting build.zig

#

in theory, I shouldn't have to change build.zig to cater for windows, right? I should be able to setup windows to cater to build.zig

dapper wind
#

linux user here

dreamy narwhal
#

Me too

#

Also

#

I just checked: opengl IS in Zig

#

opengl3 on top of that

dapper wind
#

grasping at straws but what if you put 4 in there instead of 3?

    `.backend = .glfw_opengl3,`
dreamy narwhal
#

Do you even know what this is?

dapper wind
#

the glfw backend to use

dreamy narwhal
#

No I mean

#

Inherently

dapper wind
#

as i said grasping at straws

#

i am just blindly guessing at this point

dreamy narwhal
#

It's an enum, meaning it has a set of possible values that will change how zgui compiles.

.gflw_opengl4 is not a possible value, because again, it doesn't need to be explicitly compatible with opengl4

Like I said, this feels like the issue lies in Zig's C++ compiler not being able to find and use OpenGL

#

Hence, my question is: How do I make it able to find it?

rocky nova
#

I think you just need to link opengl

dreamy narwhal
#

I downloaded the zip (zig master) and placed its content in D:\apps\zig-windows-x86_64-0.14.0-dev.2851+b074fb7dd
so, what abi did I choose?

#

it's worth mentioning I have MSYS2 installed, and thus, MinGW

rocky nova
#

I think by default it uses lld

#

And your error message confirms that.

dreamy narwhal
#

doesn't the message also show that I did link opengl?

rocky nova
#

just do exe.linkSystemLibrary("Opengl")

#

what happens then

#

Ok yea it is linking opengl32

dreamy narwhal
#

I import glfw (on top of zglfw) as a module, which already does that

#

no wait, it does it on the lib output

dreamy narwhal
#

what if I install zig on msys2 and use that instead? maybe it'd work?

rocky nova
#

I don't think it would make too much of a difference

#

I'm going to try compile on my machine

dreamy narwhal
#

if it works, you should see an orange circle that shrinks

rocky nova
dreamy narwhal
#

on linux?

rocky nova
#

windows

dreamy narwhal
#

oh good

rocky nova
#

imgui's opengl backend, if not provided by a loader, will use extern functions

#

which is why its (.refptr.gl*)

dreamy narwhal
#

oh

#

ohhhh

#

but I do use a loader... in the project

#

no, wait

#

do I?

#

A loader would be glad or glew, right?
I use neither, I use zgl, which does provide a function to load opengl

try zgl.loadExtensions(void, glfw.getProcAddress(symbolName));
rocky nova
#

ill try to figure out how to

#

ok i think you'd have to export the symbols

dreamy narwhal
rocky nova
#
export const glActiveTexture = zgl.binding.activeTexture;
export const glAttachShader = zgl.binding.attachShader;
export const glBindBuffer = zgl.binding.bindBuffer;
export const glBindSampler = zgl.binding.bindSampler;
export const glBindVertexArray = zgl.binding.bindVertexArray;
export const glBlendEquation = zgl.binding.blendEquation;
export const glBlendEquationSeparate = zgl.binding.blendEquationSeparate;
export const glBlendFuncSeparate = zgl.binding.blendFuncSeparate;
export const glBufferData = zgl.binding.bufferData;
export const glBufferSubData = zgl.binding.bufferSubData;
export const glCompileShader = zgl.binding.compileShader;
export const glCreateProgram = zgl.binding.createProgram;
export const glCreateShader = zgl.binding.createShader;
export const glDeleteBuffers = zgl.binding.deleteBuffers;
export const glDeleteProgram = zgl.binding.deleteProgram;
export const glDeleteShader = zgl.binding.deleteShader;
export const glDeleteVertexArrays = zgl.binding.deleteVertexArrays;
export const glDetachShader = zgl.binding.detachShader;
export const glDrawElementsBaseVertex = zgl.binding.drawElementsBaseVertex;
export const glEnableVertexAttribArray = zgl.binding.enableVertexAttribArray;
export const glGenBuffers = zgl.binding.genBuffers;
export const glGenVertexArrays = zgl.binding.genVertexArrays;
export const glGetAttribLocation = zgl.binding.getAttribLocation;
export const glGetProgramInfoLog = zgl.binding.getProgramInfoLog;
export const glGetProgramiv = zgl.binding.getProgramiv;
export const glGetShaderInfoLog = zgl.binding.getShaderInfoLog;
export const glGetShaderiv = zgl.binding.getShaderiv;
export const glGetStringi = zgl.binding.getStringi;
export const glGetUniformLocation = zgl.binding.getUniformLocation;
export const glIsProgram = zgl.binding.isProgram;
export const glLinkProgram = zgl.binding.linkProgram;
export const glShaderSource = zgl.binding.shaderSource;
export const glUniform1i = zgl.binding.uniform1i;
export const glUniformMatrix4fv = zgl.binding.uniformMatrix4fv;
export const glUseProgram = zgl.binding.useProgram;
export const glVertexAttribPointer = zgl.binding.vertexAttribPointer;
dreamy narwhal
#

So, it appears to be working

#

However....

Segmentation fault at address 0xffffffffffffffff
D:\Documents\Projects\particles\modules\zgui\src\backend_glfw_opengl.zig:10:27: 0x7ff7fdb6332a in initWithGlSlVersion (particles.exe.obj)
    ImGui_ImplOpenGL3_Init(@ptrCast(glsl_version));
                          ^
D:\Documents\Projects\particles\modules\zgui\src\backend_glfw_opengl.zig:16:24: 0x7ff7fdb582c9 in init (particles.exe.obj)
    initWithGlSlVersion(window, null);
                       ^
D:\Documents\Projects\particles\src\main.zig:22:22: 0x7ff7fdb57d23 in main (particles.exe.obj)
    zgui.backend.init(init.window);
                     ^
D:\apps\zig-windows-x86_64-0.14.0-dev.2851+b074fb7dd\lib\std\start.zig:631:28: 0x7ff7fdb58fba in main (particles.exe.obj)
    return callMainWithArgs(@as(usize, @intCast(c_argc)), @as([*][*:0]u8, @ptrCast(c_argv)), envp);
                           ^
D:\apps\zig-windows-x86_64-0.14.0-dev.2851+b074fb7dd\lib\libc\mingw\crt\crtexe.c:259:0: 0x7ff7fdca2132 in __tmainCRTStartup (crt2.obj)
    mainret = _tmain (argc, argv, envp);

D:\apps\zig-windows-x86_64-0.14.0-dev.2851+b074fb7dd\lib\libc\mingw\crt\crtexe.c:180:0: 0x7ff7fdca219b in mainCRTStartup (crt2.obj)
  ret = __tmainCRTStartup ();

???:?:?: 0x7ffb5efc259c in ??? (KERNEL32.DLL)
???:?:?: 0x7ffb6032af37 in ??? (ntdll.dll)
rocky nova
#

can you see whats happening using the debugger

dreamy narwhal
#

no, it doesn't point to anything

#

I just commited the thing
basically, it's the manual opengl exports + a minimal part to ensure imgui does work when it works (a lil' hud)

dreamy narwhal
#

UPDATE
So, I tried compiling it on Linux (WSL), and two interesting things happened:

  1. There was more OpenGL functions missing, which I added to the exports
  2. It still crashed, but this time with more details:
General protection exception (no address available)
modules/zgui/libs/imgui/backends/imgui_impl_opengl3.cpp:314:48: 0x147fb0f in ImGui_ImplOpenGL3_Init (/home/totoshampoin/projects/particles/modules/zgui/libs/imgui/backends/imgui_impl_opengl3.cpp)
    const char *gl_version_str = (const char *)glGetString(GL_VERSION);
                                               ^
/home/totoshampoin/projects/particles/modules/zgui/src/backend_glfw_opengl.zig:10:27: 0x112b118 in initWithGlSlVersion (particles)
    ImGui_ImplOpenGL3_Init(@ptrCast(glsl_version));
                          ^

......

This is starting to look like the crash comes from manually exporting the opengl functions

rocky nova
#

im not sure how else you'd export them

#

unless zgl has a way to or something

dreamy narwhal
#

Well tbh, zgui does come from zig-gamedev, and I always feel like that lib is thought of as an ecosystem that's supposed to work within itself
additionally, I tried downloading and building zig-gamedev, and it didn't compile either; it could download "zwindows"

So, I'm exploring alternatives. Currently looking at cimgui, but it only supports Vulkan. Maybe I'll jave more luck just going back to using Zig-ImGui