let OpenGL handle sRGB conversion

This commit is contained in:
2022-02-21 14:13:24 +08:00
parent 5155008bf0
commit 4204a15b1e
4 changed files with 35 additions and 3 deletions

View File

@ -126,7 +126,8 @@ func (r *WorldRenderer) Init(w *world.World) (err error) {
gl.GetFloatv(gl.MAX_TEXTURE_MAX_ANISOTROPY, &maxaf)
asset.InitWorldTextureAtlas()
r.texture = NewTextureRGBA(asset.WorldTextureAtlas.Image)
r.texture = NewTexture()
r.texture.UpdatesRGB(asset.WorldTextureAtlas.Image)
r.texture.GenerateMipMap()
gl.BindTexture(gl.TEXTURE_2D, r.texture.Handle())
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAX_ANISOTROPY, maxaf)

View File

@ -127,6 +127,37 @@ func (t *Texture) UpdateRGBA(image *image.RGBA) {
t.updateFilters()
}
// UpdatesRGB updates the content of the texture with image in sRGB space.
// It deletes existing mipmap, you need to generate it again.
//
// The internal data is converted from sRGB to linear space by OpenGL:
//
// linear = sRGB/12.92 (if sRGB <= 0.04045)
// [(sRGB+0.055)/1.055]^2.4 (if sRGB > 0.04045)
//
// The Alpha component is not converted.
func (t *Texture) UpdatesRGB(image *image.RGBA) {
// Restore current texture binding
defer gl.BindTexture(gl.TEXTURE_2D, curTextureBinding())
gl.BindTexture(gl.TEXTURE_2D, t.tex)
gl.TexImage2D(
gl.TEXTURE_2D,
0,
gl.SRGB_ALPHA,
int32(image.Rect.Size().X),
int32(image.Rect.Size().Y),
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(image.Pix),
)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
t.hasMipmap = false
t.updateFilters()
}
// GenerateMipMap generates mipmap for the texture.
func (t *Texture) GenerateMipMap() {