diff options
author | chai <chaifix@163.com> | 2018-10-23 12:23:58 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-23 12:23:58 +0800 |
commit | 40fc27154fe754181934dc7ee31375e6bdfb33fc (patch) | |
tree | 897ad98d759bc308ef66561181ba88b85f2ccd47 /src/libjin/Graphics/Texture.cpp | |
parent | 1480c9445100075c9e1a894eb07c0ef727b509a1 (diff) |
*merge from minimal
Diffstat (limited to 'src/libjin/Graphics/Texture.cpp')
-rw-r--r-- | src/libjin/Graphics/Texture.cpp | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/src/libjin/Graphics/Texture.cpp b/src/libjin/Graphics/Texture.cpp deleted file mode 100644 index 4c6707d..0000000 --- a/src/libjin/Graphics/Texture.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "../modules.h" -#if JIN_MODULES_RENDER - -#include <fstream> -#include "texture.h" -#include "../3rdparty/stb/stb_image.h" -#include "../utils/utils.h" -#include "../Math/Math.h" - -namespace jin -{ -namespace graphics -{ - - using namespace jin::math; - - Texture* Texture::createTexture(const char* file) - { - std::ifstream fs; - fs.open(file, std::ios::binary); - Texture* tex = nullptr; - if (fs.is_open()) - { - fs.seekg(0, std::ios::end); - int size = fs.tellg(); - fs.seekg(0, std::ios::beg); - char* buffer = (char*)malloc(size); - memset(buffer, 0, size); - fs.read(buffer, size); - tex = createTexture(buffer, size); - free(buffer); - } - fs.close(); - return tex; - } - - Texture* Texture::createTexture(const void* mem, size_t size) - { - Texture* tex = new Texture(); - if(!tex->loadb(mem, size)) - { - delete tex; - tex = nullptr; - } - return tex; - } - - Texture::Texture() - : Drawable(), pixels(0) - { - } - - Texture::~Texture() - { - stbi_image_free(pixels); - } - - color Texture::getPixel(int x, int y) - { - if (without(x, 0, width) || without(y, 0, height)) - { - return { 0 }; - } - return pixels[x + y * width]; - } - - bool Texture::loadb(const void* b, size_t size) - { - // ʹstbi_load_from_memory - unsigned char* textureData = stbi_load_from_memory((unsigned char *)b, size, &width, &height, NULL, STBI_rgb_alpha); - if (textureData == 0) return false; - pixels = (color*)textureData; - - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, - height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData); - - // set render vertices - Drawable::setVertices( - new float [DRAWABLE_V_SIZE] { - 0, 0, - 0, (float)height, - (float)width, (float)height, - (float)width, 0, - }, - new float [DRAWABLE_V_SIZE] { - 0, 0, - 0, 1, - 1, 1, - 1, 0 - } - ); - - return true; - } -} -} - -#endif // JIN_MODULES_RENDER
\ No newline at end of file |