Compare commits

...

1 Commits

Author SHA1 Message Date
b101d54049 Flip textures before uploading to gpu 2023-10-07 09:22:32 +04:00

View File

@ -80,6 +80,7 @@ func LoadTexturePNG(file string, loadOptions *TextureLoadOptions) (Texture, erro
Height: int32(nrgbaImg.Bounds().Dy()),
Width: int32(nrgbaImg.Bounds().Dx()),
}
flipImgPixelsVertically(tex.Pixels, int(tex.Width), int(tex.Height))
//Prepare opengl stuff
gl.GenTextures(1, &tex.TexID)
@ -122,6 +123,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))
//Prepare opengl stuff
gl.GenTextures(1, &tex.TexID)
@ -181,6 +183,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))
//Prepare opengl stuff
gl.GenTextures(1, &tex.TexID)
@ -267,3 +270,20 @@ func LoadCubemapTextures(rightTex, leftTex, topTex, botTex, frontTex, backTex st
return cmap, nil
}
func flipImgPixelsVertically(bytes []byte, width, height 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)
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)
}
}