Allow texture loading from in-mem images

This commit is contained in:
bloeys
2022-07-02 22:54:36 +04:00
parent fe2aef6b6d
commit 2690014fc5
3 changed files with 65 additions and 5 deletions

View File

@ -7,13 +7,18 @@ var (
func AddTextureToCache(t Texture) {
if t.Path != "" {
if _, ok := TexturePaths[t.Path]; ok {
return
}
println("Loaded texture:", t.Path)
println("Loaded texture from path:", t.Path)
Textures[t.TexID] = t
TexturePaths[t.Path] = t.TexID
return
}
println("Loaded in-mem texture with ID:", t.TexID)
TexturePaths[t.Path] = t.TexID
}
func GetTextureFromCacheID(texID uint32) (Texture, bool) {

View File

@ -2,6 +2,7 @@ package assets
import (
"bytes"
"image"
"image/color"
"image/png"
"os"
@ -17,6 +18,7 @@ const (
)
type Texture struct {
//Path only exists for textures loaded from disk
Path string
TexID uint32
Width int32
@ -30,7 +32,7 @@ type TextureLoadOptions struct {
GenMipMaps bool
}
func LoadPNGTexture(file string, loadOptions *TextureLoadOptions) (Texture, error) {
func LoadTexturePNG(file string, loadOptions *TextureLoadOptions) (Texture, error) {
if loadOptions == nil {
loadOptions = &TextureLoadOptions{}
@ -100,3 +102,56 @@ func LoadPNGTexture(file string, loadOptions *TextureLoadOptions) (Texture, erro
return tex, nil
}
func LoadTextureInMemImg(img image.Image, loadOptions *TextureLoadOptions) (Texture, error) {
if loadOptions == nil {
loadOptions = &TextureLoadOptions{}
}
tex := Texture{
Width: int32(img.Bounds().Dx()),
Height: int32(img.Bounds().Dy()),
Pixels: make([]byte, img.Bounds().Dx()*img.Bounds().Dy()*4),
}
//NOTE: Load bottom left to top right because this is the texture coordinate system used by OpenGL
//NOTE: We only support 8-bit channels (32-bit colors) for now
i := 0
for y := img.Bounds().Dy() - 1; y >= 0; y-- {
for x := 0; x < img.Bounds().Dx(); x++ {
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
tex.Pixels[i] = c.R
tex.Pixels[i+1] = c.G
tex.Pixels[i+2] = c.B
tex.Pixels[i+3] = c.A
i += 4
}
}
//Prepare opengl stuff
gl.GenTextures(1, &tex.TexID)
gl.BindTexture(gl.TEXTURE_2D, tex.TexID)
// 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)
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]))
if loadOptions.GenMipMaps {
gl.GenerateMipmap(tex.TexID)
}
if loadOptions.WriteToCache {
AddTextureToCache(tex)
}
return tex, nil
}

View File

@ -66,7 +66,7 @@ func (g *OurGame) Init() {
}
//Load textures
tex, err := assets.LoadPNGTexture("./res/textures/Low poly planet.png", nil)
tex, err := assets.LoadTexturePNG("./res/textures/Low poly planet.png", nil)
if err != nil {
logging.ErrLog.Fatalln("Failed to load texture. Err: ", err)
}