summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author0undefined <oscar@nelin.dk>2025-06-13 22:48:46 +0000
committer0undefined <oscar@nelin.dk>2025-06-13 22:48:46 +0000
commit576f79c8ec877b62d55cc79493c0c2d660fde407 (patch)
tree8fe07ef2686da23acd3c45b39d1eac44afa52573 /src
parentd0d3236687e265e6507a275fae506b17f2c85f6a (diff)
Use image components for GL texture loading
Diffstat (limited to 'src')
-rw-r--r--src/rendering/include/engine/rendering/rendering.h2
-rw-r--r--src/rendering/src/rendering.c14
-rw-r--r--src/resources/src/resources.c1
-rw-r--r--src/resources/src/textures.c3
4 files changed, 16 insertions, 4 deletions
diff --git a/src/rendering/include/engine/rendering/rendering.h b/src/rendering/include/engine/rendering/rendering.h
index 5badec7..a996257 100644
--- a/src/rendering/include/engine/rendering/rendering.h
+++ b/src/rendering/include/engine/rendering/rendering.h
@@ -280,7 +280,7 @@ u32 ComposeShader(u32 *shaders, usize shaders_len);
ShaderType guess_shadertype_from_filename(const char *restrict fname);
-Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 height);
+Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 height, u8 components);
#ifdef __cplusplus
}
diff --git a/src/rendering/src/rendering.c b/src/rendering/src/rendering.c
index b96e85a..4296a7a 100644
--- a/src/rendering/src/rendering.c
+++ b/src/rendering/src/rendering.c
@@ -659,7 +659,7 @@ Sprite sprite_new(u64 tid, u8 coord) {
}};
}
-Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 height) {
+Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 height, u8 components) {
Window* restrict w = GLOBAL_PLATFORM->window;
Texture t;
t.width = width;
@@ -674,8 +674,18 @@ Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 hei
gl->GenTextures(1, &t.id);
gl->BindTexture(GL_TEXTURE_2D, t.id);
+ u32 err = gl->GetError();
+ if (err) {
+ ERROR("Failed to bind texture from image data!");
+ }
+
+ /* TODO: Support more formats than rgb and rgba, such as gray, gray/alpha, etc.*/
+ u32 format = GL_RGB;
+ if (components == 4) format = GL_RGBA;
+
+ /* TODO: Don't force internal format to RGB */
- gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA,
+ gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format,
GL_UNSIGNED_BYTE, image_data);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
diff --git a/src/resources/src/resources.c b/src/resources/src/resources.c
index 52546db..6e35a4f 100644
--- a/src/resources/src/resources.c
+++ b/src/resources/src/resources.c
@@ -119,6 +119,7 @@ i32 resources_load(Resources *resources) {
case Asset_texture:
resources->texture[resources->texture_len] = load_texture(&resources->assets[i].texture);
resources->texture_len++;
+ LOG("Loaded texture \"%s\"!", resources->assets[i].texture.path);
break;
case Asset_error:
diff --git a/src/resources/src/textures.c b/src/resources/src/textures.c
index 7c205ef..2ba4691 100644
--- a/src/resources/src/textures.c
+++ b/src/resources/src/textures.c
@@ -23,12 +23,13 @@ Texture load_texture(const Asset_TextureSpec *restrict ts) {
}
img = stbi_load(ts->path, &width, &height, &components_per_pixel, 0);
+ ERROR("components per pixel: %d", components_per_pixel);
if (img == NULL) {
ERROR("Failed to load image %s", ts->path);
return err;
} else {
- t = createTextureFromImageData(img, width, height);
+ t = createTextureFromImageData(img, width, height, (u8)components_per_pixel);
stbi_image_free(img);
}