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()
r.texture = NewTextureRGBA(asset.WorldTextureAtlas.Image)
r.texture.GenerateMipMap()
r.shader.SetUniformTexture("tex", r.texture)
r.shader.SetUniformMat4("model", mgl32.Ident4())

View File

@ -17,6 +17,7 @@ type Texture struct {
tex uint32
hasMipmap bool
smooth bool
}
// NewTexture creates a new, empty Texture.
@ -65,18 +66,35 @@ func NewTextureRGBA(image *image.RGBA) *Texture {
return &Texture{tex: tex}
}
// updateFilters updates the MIN/MAG_FILTER parameters of the texture based on t.smooth and t.hasMipmap.
//
// It does not bind the texture; the caller has to do that
func (t *Texture) updateFilters() {
if t.smooth {
if t.hasMipmap {
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)
} else {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_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)
// TODO: Not working
func (t *Texture) SetSmooth(smooth bool) {
defer gl.BindTexture(gl.TEXTURE_2D, curTextureBinding())
gl.BindTexture(gl.TEXTURE_2D, t.tex)
if smooth {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
} else {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
}
t.smooth = smooth
t.updateFilters()
}
// UpdateRGBA updates the content of the texture with image.
@ -101,7 +119,7 @@ func (t *Texture) UpdateRGBA(image *image.RGBA) {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
t.hasMipmap = false
t.updateFilters()
}
// GenerateMipMap generates mipmap for the texture.
@ -112,10 +130,9 @@ func (t *Texture) GenerateMipMap() {
gl.BindTexture(gl.TEXTURE_2D, t.tex)
gl.GenerateMipmap(gl.TEXTURE_2D)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR)
t.hasMipmap = true
t.updateFilters()
}
// 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)
t.hasMipmap = false
t.updateFilters()
}
// Handle returns the OpenGL handle of the texture.