diff options
author | iurii <[email protected]> | 2024-09-07 13:17:56 +0200 |
---|---|---|
committer | iurii <[email protected]> | 2024-09-07 13:17:56 +0200 |
commit | f962ac82a0689560a2ebe1070c7693a78d70172c (patch) | |
tree | f439f9cc7a5ef415d2903cc1abe51578b2eff6f5 /src/texture.odin | |
parent | 7019eba5e9fe2500e30b542e8a982a4edae40940 (diff) | |
download | funhalla-f962ac82a0689560a2ebe1070c7693a78d70172c.tar.gz |
texture loader
Diffstat (limited to '')
-rw-r--r-- | src/texture.odin | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/texture.odin b/src/texture.odin index 45edf56..4e652ae 100644 --- a/src/texture.odin +++ b/src/texture.odin @@ -2,11 +2,39 @@ package funhalla import gl "vendor:OpenGL" import "vendor:stb/image" +import "core:log" -load_texture :: proc(path: cstring) { +load_texture :: proc(path: cstring) -> u32 { texture_id : u32 gl.GenTextures(1, &texture_id) - width, height, nr_components : u32 + x, y, nr_channels: i32 + + data := image.load(path, &x, &y, &nr_channels, 0) + defer image.image_free(data) + + if data != nil { + format : i32 + if nr_channels == 1 { + format = gl.RED + } else if nr_channels == 3 { + format = gl.RGB + } else if nr_channels == 4 { + format = gl.RGBA + } + gl.BindTexture(gl.TEXTURE_2D, texture_id) + gl.TexImage2D(gl.TEXTURE_2D, 0, format, x, y, 0, auto_cast format, gl.UNSIGNED_BYTE, data) + gl.GenerateMipmap(gl.TEXTURE_2D) + + 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_MIPMAP_LINEAR) + gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) + } else { + log.errorf("Failed to load texture at path: %s", path) + return 0 + } + + return texture_id } |