summaryrefslogtreecommitdiff
path: root/src/texture.c
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-05-20 21:29:09 -0500
committersanine-a <sanine.not@pm.me>2020-05-20 21:29:09 -0500
commitc6ec37cd355b313083af5be3435162224020fe5e (patch)
tree9a6f069812e62e97052fd6fff6bd23ac1472c756 /src/texture.c
parent040ba6826237eb124aaa4576fc302e2e07dd40c5 (diff)
add honey_texture
Diffstat (limited to 'src/texture.c')
-rw-r--r--src/texture.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/texture.c b/src/texture.c
new file mode 100644
index 0000000..b041b4f
--- /dev/null
+++ b/src/texture.c
@@ -0,0 +1,38 @@
+#include "include/texture.h"
+
+enum honey_texture_result honey_texture_new(honey_texture* texture,
+ char* texture_path,
+ bool alpha_channel) {
+ unsigned int texture_id;
+ glGenTextures(1, &texture_id);
+ glBindTexture(GL_TEXTURE_2D, texture_id);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ int width, height, channels;
+ unsigned char* image_data = stbi_load(texture_path, &width, &height, &channels, 0);
+ if (image_data == NULL) {
+ fprintf(stderr, "ERROR: failed to load '%s'\n", texture_path);
+ return TEXTURE_FAILED;
+ }
+
+ if (alpha_channel) {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
+ }
+ else {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data);
+ }
+
+ glGenerateMipmap(GL_TEXTURE_2D);
+ stbi_image_free(image_data);
+
+ (*texture).texture_id = texture_id;
+ (*texture).width = width;
+ (*texture).height = height;
+ (*texture).channels = channels;
+
+ return TEXTURE_OK;
+}