2024-01-19 23:55:07 +02:00
|
|
|
package zephr
|
|
|
|
|
|
2024-01-27 04:05:00 +02:00
|
|
|
import "core:log"
|
|
|
|
|
|
2024-01-19 23:55:07 +02:00
|
|
|
import gl "vendor:OpenGL"
|
|
|
|
|
import stb "vendor:stb/image"
|
|
|
|
|
|
|
|
|
|
TextureId :: u32
|
|
|
|
|
|
|
|
|
|
load_texture :: proc(path: cstring) -> TextureId {
|
2024-01-27 04:05:00 +02:00
|
|
|
context.logger = logger
|
|
|
|
|
|
2024-01-20 06:39:49 +02:00
|
|
|
texture_id: TextureId
|
2024-01-19 23:55:07 +02:00
|
|
|
width, height, channels: i32
|
|
|
|
|
data := stb.load(path, &width, &height, &channels, 4) // RGBA
|
2024-01-27 04:05:00 +02:00
|
|
|
if data == nil {
|
|
|
|
|
log.errorf("Failed to load texture: \"%s\"", path)
|
|
|
|
|
}
|
2024-01-19 23:55:07 +02:00
|
|
|
defer stb.image_free(data)
|
|
|
|
|
|
2024-01-20 06:39:49 +02:00
|
|
|
format := gl.RGBA
|
2024-01-19 23:55:07 +02:00
|
|
|
|
|
|
|
|
switch (channels) {
|
|
|
|
|
case 1:
|
2024-01-20 06:39:49 +02:00
|
|
|
format = gl.RED
|
|
|
|
|
break
|
2024-01-19 23:55:07 +02:00
|
|
|
case 2:
|
2024-01-20 06:39:49 +02:00
|
|
|
format = gl.RG
|
|
|
|
|
break
|
2024-01-19 23:55:07 +02:00
|
|
|
case 3:
|
2024-01-20 06:39:49 +02:00
|
|
|
format = gl.RGB
|
|
|
|
|
break
|
2024-01-19 23:55:07 +02:00
|
|
|
case 4:
|
2024-01-20 06:39:49 +02:00
|
|
|
format = gl.RGBA
|
|
|
|
|
break
|
2024-01-19 23:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-20 06:39:49 +02:00
|
|
|
gl.GenTextures(1, &texture_id)
|
|
|
|
|
gl.BindTexture(gl.TEXTURE_2D, texture_id)
|
2024-01-19 23:55:07 +02:00
|
|
|
|
2024-01-20 06:39:49 +02:00
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
|
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
|
2024-01-19 23:55:07 +02:00
|
|
|
|
2024-01-20 06:39:49 +02:00
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
|
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
|
2024-01-19 23:55:07 +02:00
|
|
|
|
2024-01-20 06:39:49 +02:00
|
|
|
gl.TexImage2D(gl.TEXTURE_2D, 0, cast(i32)format, width, height, 0, cast(u32)format, gl.UNSIGNED_BYTE, data)
|
2024-01-19 23:55:07 +02:00
|
|
|
|
2024-01-20 06:39:49 +02:00
|
|
|
gl.GenerateMipmap(gl.TEXTURE_2D)
|
2024-01-19 23:55:07 +02:00
|
|
|
|
2024-01-20 06:39:49 +02:00
|
|
|
return texture_id
|
2024-01-19 23:55:07 +02:00
|
|
|
}
|