summary refs log tree commit diff
path: root/src/texture.odin
diff options
context:
space:
mode:
Diffstat (limited to 'src/texture.odin')
-rw-r--r--src/texture.odin32
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
 }