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.cpp40
1 files changed, 16 insertions, 24 deletions
diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp
index fb4d1e44..23bc9a25 100644
--- a/yage/core/imageloader.cpp
+++ b/yage/core/imageloader.cpp
@@ -7,11 +7,11 @@
*/
#include "imageloader.h"
-#include "texture.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,40 +21,32 @@ 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);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, 0);
+ yLogDebug << "Successfully loaded texture: " << file_path;
return texture;
}