TextureLoadOptions+fix DT bug+remove SetAttribute

This commit is contained in:
bloeys
2022-07-02 21:38:48 +04:00
parent e1bf0697fc
commit 51057b8a0d
6 changed files with 45 additions and 39 deletions

View File

@ -5,7 +5,7 @@ var (
TexturePaths map[string]uint32 = make(map[string]uint32)
)
func SetTexture(t Texture) {
func AddTextureToCache(t Texture) {
if _, ok := TexturePaths[t.Path]; ok {
return
@ -16,12 +16,12 @@ func SetTexture(t Texture) {
TexturePaths[t.Path] = t.TexID
}
func GetTexture(texID uint32) (Texture, bool) {
func GetTextureFromCacheID(texID uint32) (Texture, bool) {
tex, ok := Textures[texID]
return tex, ok
}
func GetTexturePath(path string) (Texture, bool) {
func GetTextureFromCachePath(path string) (Texture, bool) {
tex, ok := Textures[TexturePaths[path]]
return tex, ok
}

View File

@ -24,10 +24,22 @@ type Texture struct {
Pixels []byte
}
func LoadPNGTexture(file string) (Texture, error) {
type TextureLoadOptions struct {
TryLoadFromCache bool
WriteToCache bool
GenMipMaps bool
}
if tex, ok := GetTexturePath(file); ok {
return tex, nil
func LoadPNGTexture(file string, loadOptions *TextureLoadOptions) (Texture, error) {
if loadOptions == nil {
loadOptions = &TextureLoadOptions{}
}
if loadOptions.TryLoadFromCache {
if tex, ok := GetTextureFromCachePath(file); ok {
return tex, nil
}
}
//Load from disk
@ -72,14 +84,19 @@ func LoadPNGTexture(file string) (Texture, error) {
// set the texture wrapping/filtering options (on the currently bound texture object)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
// load and generate the texture
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0]))
gl.GenerateMipmap(gl.TEXTURE_2D)
SetTexture(tex)
if loadOptions.GenMipMaps {
gl.GenerateMipmap(tex.TexID)
}
if loadOptions.WriteToCache {
AddTextureToCache(tex)
}
return tex, nil
}