apicici / cimgui-love

LÖVE module for Dear ImGui obtained by wrapping cimgui with LuaJIT FFI.
MIT License
76 stars 6 forks source link

Could you create a font texture by r8 format and draw it with a shader? #7

Closed xiejiangzhi closed 2 years ago

xiejiangzhi commented 2 years ago
C.ImFontAtlas_GetTexDataAsAlpha8(io.Fonts, pixels, width, height, nil)
local imgdata = love.image.newImageData(width[0], height[0], "r8", ffi.string(pixels[0], width[0]*height[0]))
local FontShader = love.graphics.newShader([[
    #pragma language glsl3

    vec4 effect(vec4 color, Image tex, vec2 tex_coord, vec2 sc) {
        float alpha = Texel(tex, tex_coord).r;
        return vec4(color.rgb, color.a * alpha);
    }
]])

It will save 3/4 font texture memory.

And, I think the textureObject rename to FontTextureObject is better.

xiejiangzhi commented 2 years ago

Fot this way, the way will broken colour font, maybe we should find a way to support two.

apicici commented 2 years ago

Great idea, I'll try to implement it in a way that both options are supported.

apicici commented 2 years ago

I implemented the support for Alpha8 textures. By default RGBA32 is used (for backwards compatibility), to use the new format pass "Alpha8" as an argument when calling imgui.love.Init and imgui.love.BuildFontAtlas.

Thanks for the suggestion! I had tried to do this in the past, but everything ended up being red and I didn't think of using a shader to fix that.