aboutsummaryrefslogtreecommitdiffstats
path: root/yage/core/imageloader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'yage/core/imageloader.cpp')
-rw-r--r--yage/core/imageloader.cpp36
1 files changed, 14 insertions, 22 deletions
diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp
index 7b34312d..5a60e83b 100644
--- a/yage/core/imageloader.cpp
+++ b/yage/core/imageloader.cpp
@@ -8,10 +8,10 @@
#include "imageloader.h"
#include "../data/texture.h"
+#include "logger.h"
+#include "stb_image.h"
#include <glad/glad.h>
-#include <yage/core/iomanager.h>
-#include <yage/core/picopng.h>
#include <iostream>
#include <stdexcept>
@@ -21,30 +21,22 @@ namespace yage
Texture ImageLoader::loadPng(const std::string &file_path)
{
- std::vector<unsigned char> in;
- std::vector<unsigned char> out;
- unsigned long width, height;
-
- if (!IoManager::readFileToBuffer(file_path, in)) {
- throw std::runtime_error("Failed to load '" + file_path +
- "' to buffer");
- }
-
- int error_code = decodePNG(out, width, height, &in[0], in.size());
- if (error_code != 0) {
- throw std::runtime_error("Failed to load '" + file_path +
- "' to png with error code" +
- std::to_string(error_code));
- }
+ int width, height, num_channels;
+ unsigned char *data =
+ stbi_load(file_path.c_str(), &width, &height, &num_channels, 0);
Texture texture(0, static_cast<int>(width), static_cast<int>(height));
- std::cout << "Geometry: " << texture.width << "x" << texture.height << "\n";
-
glGenTextures(1, &texture.id);
glBindTexture(GL_TEXTURE_2D, texture.id);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, &out[0]);
+
+ if (num_channels == 4) {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
+ GL_UNSIGNED_BYTE, data);
+ } else {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB,
+ GL_UNSIGNED_BYTE, data);
+ }
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
@@ -54,7 +46,7 @@ Texture ImageLoader::loadPng(const std::string &file_path)
glGenerateMipmap(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, 0);
+ yLog << "Successfully loaded texture: " << file_path;
return texture;
}