karts/texture/load.odin

49 lines
1.3 KiB
Odin
Raw Normal View History

2025-02-16 11:28:18 +13:00
package texture
import "../image"
import "core:math"
import gl "vendor:OpenGL"
import "core:fmt"
@(require_results)
load :: proc(
img: image.Image,
image_error: image.Error = nil,
) -> (
texture: Texture,
image_error_fallthrough: image.Error,
) {
image_error_fallthrough = image_error
gl.CreateTextures(gl.TEXTURE_2D, 1, &texture.id)
mipmap_levels := math.log2(f32(max(img.width, img.height))) + 1
// fmt.printfln("{}, {}: using {} levels", img.width, img.height, mipmap_levels)
gl.TextureStorage2D(texture.id, i32(mipmap_levels), gl.RGBA8, img.width, img.height)
gl.TextureSubImage2D(
texture.id,
0,
0,
0,
img.width,
img.height,
gl.RGBA,
gl.UNSIGNED_BYTE,
&img.pixels[0],
)
max_aniso: f32
gl.GetFloatv(gl.MAX_TEXTURE_MAX_ANISOTROPY, &max_aniso)
gl.TextureParameterf(texture.id, gl.TEXTURE_MAX_ANISOTROPY, max_aniso)
gl.TextureParameteri(texture.id, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
gl.TextureParameteri(texture.id, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
//
// gl.TextureParameteri(texture.id, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
// gl.TextureParameteri(texture.id, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.GenerateTextureMipmap(texture.id)
return
}