refactor ImGUI code files

This commit is contained in:
2022-02-10 19:58:51 +08:00
parent bb33972616
commit 06cd549eb7
13 changed files with 172 additions and 50 deletions

View File

@ -1,4 +1,4 @@
package igwrap
package backend
import (
"time"

View File

@ -1,4 +1,4 @@
package igwrap
package backend
import "github.com/inkyblackness/imgui-go/v4"

View File

@ -1,8 +1,9 @@
package igwrap
package backend
import (
_ "embed"
"edgaru089.ml/go/gl01/internal/igwrap"
"edgaru089.ml/go/gl01/internal/render"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
@ -86,6 +87,7 @@ func CreateFontsTexture() {
)
io.Fonts().SetTextureID(imgui.TextureID(tex))
igwrap.SetTextureFlag(tex, igwrap.TextureFlag_Red)
gl.BindTexture(gl.TEXTURE_2D, uint32(lastTexture))
}
@ -202,6 +204,7 @@ func Render(win *glfw.Window) {
if cmd.HasUserCallback() {
cmd.CallUserCallback(list)
} else {
shader.SetUniformInt("flags", int32(igwrap.GetTextureFlag(uint32(cmd.TextureID()))))
gl.BindTexture(gl.TEXTURE_2D, uint32(cmd.TextureID()))
clipRect := cmd.ClipRect()
gl.Scissor(int32(clipRect.X), int32(fbHeight)-int32(clipRect.W), int32(clipRect.Z-clipRect.X), int32(clipRect.W-clipRect.Y))

View File

@ -0,0 +1,33 @@
#version 330
#define FLAG_RED (1<<0)
#define FLAG_GREEN (1<<1)
#define FLAG_BLUE (1<<2)
#define FLAG_ALPHA (1<<3)
#define FLAG_COLORS (FLAG_RED | FLAG_GREEN | FLAG_BLUE | FLAG_ALPHA)
#define FLAG_LINEAR (1<<4)
uniform sampler2D tex;
uniform int flags;
in vec2 fragUV;
in vec4 fragColor;
out vec4 outputColor;
const float gamma = 2.2;
void main() {
if ((flags & FLAG_COLORS) == FLAG_RED) {
outputColor = vec4(fragColor.rgb, fragColor.a * texture(tex, fragUV.st).r);
} else {
vec4 color = texture(tex, fragUV.st);
if ((flags & FLAG_LINEAR) != 0)
color.rgb = pow(color.rgb, vec3(1.0/gamma));
outputColor = color * fragColor;
if ((flags & FLAG_ALPHA) == 0)
outputColor.a = 1;
}
}

View File

@ -1,13 +0,0 @@
package igwrap
import "github.com/inkyblackness/imgui-go/v4"
// Color converts non-premultiplied RGBA color into imgui.Vec4.
func Color(r, g, b, a uint8) imgui.Vec4 {
return imgui.Vec4{
X: float32(r) / 255,
Y: float32(g) / 255,
Z: float32(b) / 255,
W: float32(a) / 255,
}
}

View File

@ -0,0 +1,26 @@
package igwrap
import (
"edgaru089.ml/go/gl01/internal/util/itype"
"github.com/inkyblackness/imgui-go/v4"
)
// Color converts non-premultiplied RGBA color into imgui.Vec4.
func Color(r, g, b, a uint8) imgui.Vec4 {
return imgui.Vec4{
X: float32(r) / 255,
Y: float32(g) / 255,
Z: float32(b) / 255,
W: float32(a) / 255,
}
}
// Vec2 converts itype.Vec2f to imgui.Vec2.
func Vec2(v itype.Vec2f) imgui.Vec2 {
return imgui.Vec2{X: v[0], Y: v[1]}
}
// Vec4 converts itype.Vec4f to imgui.Vec4.
func Vec4(v itype.Vec4f) imgui.Vec4 {
return imgui.Vec4{X: v[0], Y: v[1], Z: v[2], W: v[3]}
}

View File

@ -0,0 +1,77 @@
package igwrap
import (
"sync"
"edgaru089.ml/go/gl01/internal/util/itype"
"github.com/inkyblackness/imgui-go/v4"
)
// Image creates a ImGUI image based on the given texture.
// For *render.Texture, use tex.Handle().
//
// size is in pixels. texRange is in [0, 1].
func Image(tex uint32, size itype.Vec2f, texRange itype.Rectf) {
min, max := texRange.MinPoint(), texRange.MaxPoint()
imgui.ImageV(
imgui.TextureID(tex),
Vec2(size),
imgui.Vec2{X: min[0], Y: max[1]},
imgui.Vec2{X: max[0], Y: min[1]},
Color(255, 255, 255, 255),
imgui.Vec4{},
)
}
// ImageV creates a ImGUI image based on the given texture.
// For *render.Texture, use tex.Handle().
//
// size is in pixels. texRange is in [0, 1].
func ImageV(tex uint32, size itype.Vec2f, texRange itype.Rectf, texColor itype.Vec4f, borderColor itype.Vec4f) {
imgui.ImageV(
imgui.TextureID(tex),
Vec2(size),
Vec2(texRange.MinPoint()),
Vec2(texRange.MaxPoint()),
Vec4(texColor),
Vec4(borderColor),
)
}
type TextureFlag int
const (
TextureFlag_Red TextureFlag = 1 << iota // Renders the Red channel.
TextureFlag_Green // Renders the Green channel.
TextureFlag_Blue // Renders the Blue channel.
TextureFlag_Alpha // Renders the Alpha channel.
TextureFlag_Linear // Linear source data, requires gamma correction.
TextureFlag_RGB = TextureFlag_Red | TextureFlag_Green | TextureFlag_Blue
TextureFlag_RGBA = TextureFlag_Red | TextureFlag_Green | TextureFlag_Blue | TextureFlag_Alpha
)
var (
texflags map[uint32]TextureFlag = make(map[uint32]TextureFlag)
texflagslock sync.RWMutex
)
// SetTextureFlag changes the flag of a texture to the given flags combined.
func SetTextureFlag(tex uint32, flags ...TextureFlag) {
var f TextureFlag
for _, f0 := range flags {
f |= f0
}
texflagslock.Lock()
defer texflagslock.Unlock()
texflags[tex] = f
}
// TextureFlag returns the flags of a given texture.
func GetTextureFlag(tex uint32) TextureFlag {
texflagslock.RLock()
defer texflagslock.RUnlock()
return texflags[tex]
}

View File

@ -1,13 +0,0 @@
#version 330
uniform sampler2D tex;
in vec2 fragUV;
in vec4 fragColor;
out vec4 outputColor;
void main() {
outputColor = vec4(fragColor.rgb, fragColor.a * texture(tex, fragUV.st).r);
}

View File

@ -28,7 +28,7 @@ func Text(format string, a ...interface{}) {
// returns true.
func Begin(id string, open *bool, flags imgui.WindowFlags) bool {
// skip if the window is not open
if !(*open) {
if open != nil && !(*open) {
return false
}