1

I am trying to get the implicit conversion between ImGui's (ImVec) and glm's (glm::vec) vector types working.

In here I read, that I have to change the following lines in the imconfig.h file:

#define IM_VEC2_CLASS_EXTRA                                                     \
        constexpr ImVec2(const MyVec2& f) : x(f.x), y(f.y) {}                   \
        operator MyVec2() const { return MyVec2(x,y); }

#define IM_VEC4_CLASS_EXTRA                                                     \
        constexpr ImVec4(const MyVec4& f) : x(f.x), y(f.y), z(f.z), w(f.w) {}   \
        operator MyVec4() const { return MyVec4(x,y,z,w); }

The first line makes sense to me, but I don't see the point of the second making a new constructor for MyVec. Since I really have no idea what is going on here, I just tried to replace MyVecN with either glm::vecN or vecN, but neither works.

Also I don't get why there are these backslashes, I guess they're to comment out? Either way, I removed them, and it still didn't work.

The compiler ends up throwing tons of errors so I don't know where the problem is.

noergel1
  • 35
  • 5
  • If you search for `IM_VEC2_CLASS_EXTRA` in the imgui codebase you'll find the content of your macro is inserted INSIDE in the definition of the ImVec2 class. So the line you are reading as "making a new constructor for MyVec" is actually not a MyVec constructor but a casting-member function of ImVec2 to allow casting from ImVec2 to MyVec. – Omar Mar 23 '23 at 18:26

1 Answers1

1

You have to defined/include your struct before including imgui:


// define glm::vecN or include it from another file
namespace glm
{
    struct vec2
    {
        float x, y;
        vec2(float x, float y) : x(x), y(y) {};
    };
    struct vec4
    {
        float x, y, z, w;
        vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {};
    };
}

// define extra conversion here before including imgui, don't do it in the imconfig.h
#define IM_VEC2_CLASS_EXTRA \
    constexpr ImVec2(glm::vec2& f) : x(f.x), y(f.y) {} \
    operator glm::vec2() const { return glm::vec2(x, y); }

#define IM_VEC4_CLASS_EXTRA \
        constexpr ImVec4(const glm::vec4& f) : x(f.x), y(f.y), z(f.z), w(f.w) {} \
        operator glm::vec4() const { return glm::vec4(x,y,z,w); }

#include "imgui/imgui.h"

And the backslashes \ are just line breaks in the #define, to specify a continuous definition

thedemons
  • 1,139
  • 2
  • 9
  • 25