render: fix Texture mipmap generation
This commit is contained in:
parent
73669001c3
commit
dfe0ff8b63
@ -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())
|
||||||
|
@ -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,18 +66,35 @@ func NewTextureRGBA(image *image.RGBA) *Texture {
|
|||||||
return &Texture{tex: tex}
|
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)
|
// SetSmooth sets the min/mag filters to LINEAR(smooth) or NEAREST(not smooth)
|
||||||
// TODO: Not working
|
|
||||||
func (t *Texture) SetSmooth(smooth bool) {
|
func (t *Texture) SetSmooth(smooth bool) {
|
||||||
defer gl.BindTexture(gl.TEXTURE_2D, curTextureBinding())
|
defer gl.BindTexture(gl.TEXTURE_2D, curTextureBinding())
|
||||||
gl.BindTexture(gl.TEXTURE_2D, t.tex)
|
gl.BindTexture(gl.TEXTURE_2D, t.tex)
|
||||||
if smooth {
|
t.smooth = smooth
|
||||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
t.updateFilters()
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRGBA updates the content of the texture with image.
|
// 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)
|
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.
|
||||||
|
Loading…
Reference in New Issue
Block a user