gearlib/src/textures.c

30 lines
1 KiB
C
Raw Normal View History

2024-04-30 23:02:19 +12:00
#include <textures.h>
#include <stb_image.h>
#include <opengl.h>
#include <stdio.h>
uint32_t load_texture(const char* path) {
stbi_set_flip_vertically_on_load(1);
uint32_t id;
glCreateTextures(GL_TEXTURE_2D, 1, &id);
glTextureParameteri(id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTextureParameteri(id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, channels;
unsigned char* pixels = stbi_load(path, &width, &height, &channels, 0);
if(pixels) {
glTextureStorage2D(id, 1, GL_RGBA8, width, height);
glTextureSubImage2D(id, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
printf("[Texture %d, %s] Loaded successfully (%dx%d, %d channels)\n", id, path, width, height, channels);
} else {
printf("[Texture %d, %s] Failed to load image data: %s\n", id, path, stbi_failure_reason());
exit(-1);
}
return id;
}