From 7b95e3a9eacf296f215c73e5d8ad9090a24adb20 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 4 Jan 2018 21:36:30 +0000 Subject: [Engine] Now using stb_image to laod all kinds of textures. --- yage/core/imageloader.cpp | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) (limited to 'yage/core/imageloader.cpp') 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 -#include -#include #include #include @@ -21,30 +21,22 @@ namespace yage Texture ImageLoader::loadPng(const std::string &file_path) { - std::vector in; - std::vector 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(width), static_cast(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; } -- cgit