diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-04 21:36:30 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-04 21:36:30 +0000 |
commit | 7b95e3a9eacf296f215c73e5d8ad9090a24adb20 (patch) | |
tree | 77125b5b30e08e48903e802ad4cdfddbb03074c1 /yage/core/imageloader.cpp | |
parent | a47649786fb94684c415b230669fbf5343cb7c5d (diff) | |
download | YAGE-7b95e3a9eacf296f215c73e5d8ad9090a24adb20.tar.gz YAGE-7b95e3a9eacf296f215c73e5d8ad9090a24adb20.zip |
[Engine] Now using stb_image to laod all kinds of textures.
Diffstat (limited to 'yage/core/imageloader.cpp')
-rw-r--r-- | yage/core/imageloader.cpp | 36 |
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; } |