render: fix Texture mipmap generation

This commit is contained in:
Edgaru089 2022-01-21 21:08:12 +08:00
parent 73669001c3
commit dfe0ff8b63
2 changed files with 30 additions and 11 deletions

View File

@ -44,6 +44,7 @@ func (r *WorldRenderer) Init(w *world.World) (err error) {
asset.InitWorldTextureAtlas() asset.InitWorldTextureAtlas()
r.texture = NewTextureRGBA(asset.WorldTextureAtlas.Image) r.texture = NewTextureRGBA(asset.WorldTextureAtlas.Image)
r.texture.GenerateMipMap()
r.shader.SetUniformTexture("tex", r.texture) r.shader.SetUniformTexture("tex", r.texture)
r.shader.SetUniformMat4("model", mgl32.Ident4()) r.shader.SetUniformMat4("model", mgl32.Ident4())

View File

@ -17,6 +17,7 @@ type Texture struct {
tex uint32 tex uint32
hasMipmap bool hasMipmap bool
smooth bool
} }
// NewTexture creates a new, empty Texture. // NewTexture creates a new, empty Texture.
@ -65,19 +66,36 @@ func NewTextureRGBA(image *image.RGBA) *Texture {
return &Texture{tex: tex} return &Texture{tex: tex}
} }
// SetSmooth sets the min/mag filters to LINEAR(smooth) or NEAREST(not smooth) // updateFilters updates the MIN/MAG_FILTER parameters of the texture based on t.smooth and t.hasMipmap.
// TODO: Not working //
func (t *Texture) SetSmooth(smooth bool) { // It does not bind the texture; the caller has to do that
defer gl.BindTexture(gl.TEXTURE_2D, curTextureBinding()) func (t *Texture) updateFilters() {
gl.BindTexture(gl.TEXTURE_2D, t.tex) if t.smooth {
if smooth { if t.hasMipmap {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
} else {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
}
} else {
if t.hasMipmap {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
} else { } else {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
} }
} }
}
// SetSmooth sets the min/mag filters to LINEAR(smooth) or NEAREST(not smooth)
func (t *Texture) SetSmooth(smooth bool) {
defer gl.BindTexture(gl.TEXTURE_2D, curTextureBinding())
gl.BindTexture(gl.TEXTURE_2D, t.tex)
t.smooth = smooth
t.updateFilters()
}
// UpdateRGBA updates the content of the texture with image. // UpdateRGBA updates the content of the texture with image.
// It deletes existing mipmap, you need to generate it again. // It deletes existing mipmap, you need to generate it again.
@ -101,7 +119,7 @@ func (t *Texture) UpdateRGBA(image *image.RGBA) {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
t.hasMipmap = false t.hasMipmap = false
t.updateFilters()
} }
// GenerateMipMap generates mipmap for the texture. // GenerateMipMap generates mipmap for the texture.
@ -112,10 +130,9 @@ func (t *Texture) GenerateMipMap() {
gl.BindTexture(gl.TEXTURE_2D, t.tex) gl.BindTexture(gl.TEXTURE_2D, t.tex)
gl.GenerateMipmap(gl.TEXTURE_2D) gl.GenerateMipmap(gl.TEXTURE_2D)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR)
t.hasMipmap = true t.hasMipmap = true
t.updateFilters()
} }
// InvalidateMipMap invalidates mipmap for the texture. // InvalidateMipMap invalidates mipmap for the texture.
@ -128,6 +145,7 @@ func (t *Texture) InvalidateMipMap() {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
t.hasMipmap = false t.hasMipmap = false
t.updateFilters()
} }
// Handle returns the OpenGL handle of the texture. // Handle returns the OpenGL handle of the texture.