From f962ac82a0689560a2ebe1070c7693a78d70172c Mon Sep 17 00:00:00 2001 From: iurii Date: Sat, 7 Sep 2024 14:17:56 +0300 Subject: texture loader --- src/texture.odin | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src') 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 } -- cgit 1.4.1-2-gfad0