diff --git a/assets/textures.go b/assets/textures.go index ca52248..a40d3b8 100755 --- a/assets/textures.go +++ b/assets/textures.go @@ -23,11 +23,20 @@ const ( ) type Texture struct { - //Path only exists for textures loaded from disk - Path string - TexID uint32 - Width int32 + // Path only exists for textures loaded from disk + Path string + + TexID uint32 + + // Width is the width of the texture in pixels (pixels per row). + // Note that the number of bytes constituting a row is MORE than this (e.g. for RGBA8, bytesPerRow=width*4, since we have 4 bytes per pixel) + Width int32 + + // Height is the height of the texture in pixels (pixels per column). + // Note that the number of bytes constituting a column is MORE than this (e.g. for RGBA8, bytesPerColumn=height*4, since we have 4 bytes per pixel) Height int32 + + // Pixels usually stored in RGBA format Pixels []byte } @@ -77,10 +86,10 @@ func LoadTexturePNG(file string, loadOptions *TextureLoadOptions) (Texture, erro tex := Texture{ Path: file, Pixels: nrgbaImg.Pix, - Height: int32(nrgbaImg.Bounds().Dy()), Width: int32(nrgbaImg.Bounds().Dx()), + Height: int32(nrgbaImg.Bounds().Dy()), } - flipImgPixelsVertically(tex.Pixels, int(tex.Width), int(tex.Height)) + flipImgPixelsVertically(tex.Pixels, int(tex.Width), int(tex.Height), 4) //Prepare opengl stuff gl.GenTextures(1, &tex.TexID) @@ -93,7 +102,7 @@ func LoadTexturePNG(file string, loadOptions *TextureLoadOptions) (Texture, erro gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) // load and generate the texture - gl.TexImage2D(gl.TEXTURE_2D, 0, gl.SRGB8_ALPHA8, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0])) + gl.TexImage2D(gl.TEXTURE_2D, 0, gl.SRGB_ALPHA, tex.Width, tex.Height, 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tex.Pixels[0])) if loadOptions.GenMipMaps { gl.GenerateMipmap(tex.TexID) @@ -123,7 +132,7 @@ func LoadTextureInMemPngImg(img image.Image, loadOptions *TextureLoadOptions) (T Height: int32(nrgbaImg.Bounds().Dy()), Width: int32(nrgbaImg.Bounds().Dx()), } - flipImgPixelsVertically(tex.Pixels, int(tex.Width), int(tex.Height)) + flipImgPixelsVertically(tex.Pixels, int(tex.Width), int(tex.Height), 4) //Prepare opengl stuff gl.GenTextures(1, &tex.TexID) @@ -183,7 +192,7 @@ func LoadTextureJpeg(file string, loadOptions *TextureLoadOptions) (Texture, err Height: int32(nrgbaImg.Bounds().Dy()), Width: int32(nrgbaImg.Bounds().Dx()), } - flipImgPixelsVertically(tex.Pixels, int(tex.Width), int(tex.Height)) + flipImgPixelsVertically(tex.Pixels, int(tex.Width), int(tex.Height), 4) //Prepare opengl stuff gl.GenTextures(1, &tex.TexID) @@ -271,19 +280,20 @@ func LoadCubemapTextures(rightTex, leftTex, topTex, botTex, frontTex, backTex st return cmap, nil } -func flipImgPixelsVertically(bytes []byte, width, height int) { +func flipImgPixelsVertically(bytes []byte, width, height, bytesPerPixel int) { // Flip the image vertically such that (e.g. in an image of 10 rows) rows 0<->9, 1<->8, 2<->7 etc are swapped. // We do this because images are usually stored top-left to bottom-right, while opengl stores textures bottom-left to top-right, so if we don't swap // rows textures will appear inverted - rowData := make([]byte, width) + widthInBytes := width * bytesPerPixel + rowData := make([]byte, width*bytesPerPixel) for rowNum := 0; rowNum < height/2; rowNum++ { - upperRowStartIndex := rowNum * width - lowerRowStartIndex := (height - rowNum - 1) * width - copy(rowData, bytes[upperRowStartIndex:upperRowStartIndex+width]) - copy(bytes[upperRowStartIndex:upperRowStartIndex+width], bytes[lowerRowStartIndex:lowerRowStartIndex+width]) - copy(bytes[lowerRowStartIndex:lowerRowStartIndex+width], rowData) + upperRowStartIndex := rowNum * widthInBytes + lowerRowStartIndex := (height - rowNum - 1) * widthInBytes + copy(rowData, bytes[upperRowStartIndex:upperRowStartIndex+widthInBytes]) + copy(bytes[upperRowStartIndex:upperRowStartIndex+widthInBytes], bytes[lowerRowStartIndex:lowerRowStartIndex+widthInBytes]) + copy(bytes[lowerRowStartIndex:lowerRowStartIndex+widthInBytes], rowData) } } diff --git a/go.mod b/go.mod index 9ac337d..d9855b6 100755 --- a/go.mod +++ b/go.mod @@ -11,9 +11,9 @@ require ( github.com/bloeys/gglm v0.43.0 ) -require github.com/AllenDang/cimgui-go v0.0.0-20230720025235-f2ff398a66b2 - require ( - github.com/mandykoh/go-parallel v0.1.0 // indirect - github.com/mandykoh/prism v0.35.1 // indirect + github.com/AllenDang/cimgui-go v0.0.0-20230720025235-f2ff398a66b2 + github.com/mandykoh/prism v0.35.1 ) + +require github.com/mandykoh/go-parallel v0.1.0 // indirect diff --git a/go.sum b/go.sum index 81d4694..ff9c53e 100755 --- a/go.sum +++ b/go.sum @@ -15,6 +15,7 @@ github.com/veandco/go-sdl2 v0.4.35/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofe github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI= golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=