I implemented a Rect class in my custom game engine that utilizes openGL. I decided to approach the whole Rect class design like Unity's implementation.

Essentially, what I do is I directly manipulate the X and Y coordinates of the rectangle and also its width and height and then I update the Vertex Buffer Object using the following function glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data).

Here is my design:

class Rect
    VertexArray* vao = nullptr;
    VertexBuffer* vbo = nullptr;
    IndexBuffer* ibo = nullptr;
    real* dataBuffer = nullptr;
    uint32* indicesBuffer = nullptr;
    real x, y;
    real width, height;
    real xMax, yMax;
    Rect(real x, real y, real width, real height);
    void SetX(real x);
    void SetY(real y);
    void SetWidth(real width);
    void SetHeight(real height);
    void SetUV(Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4);
    real GetX();
    real GetY();
    real GetWidth();
    real GetHeight();
    std::vector<Vector2> GetUV();
    Vector2 GetCenter();
    bool Contains(Vector2 point);
    void Bind();
    void UnBind();

Now the issue occurs when I multiply the rectangle's Vertex Buffer Object with model matrix which causes the actual vertex data inside the GPU to change but the Rect object data remains the same inside the CPU (The rectangle could be at coordiantes (100,250) but the X and Y coordinates stored in the object are (50, 50)) which will cause all kind of issues especially when performing hit testing for UI or picking up GameObjects.

How can I approach such a problem while remaining true to Unity's design.

  • \$\begingroup\$ Wouldn't the UI hit test transform the ray from view space into world space to match the rect's CPU-side coordinate system? \$\endgroup\$
    – DMGregory
    Jul 15, 2021 at 22:34
  • \$\begingroup\$ I haven't figured out that yet, I'm more concerned with figuring out how to update the CPU data when GLSL performs matrix multiplication on the rect's vertices. \$\endgroup\$ Jul 15, 2021 at 22:45


You must log in to answer this question.

Browse other questions tagged .