diff options
Diffstat (limited to 'src/libjin/Graphics/Texture.cpp')
-rw-r--r-- | src/libjin/Graphics/Texture.cpp | 92 |
1 files changed, 24 insertions, 68 deletions
diff --git a/src/libjin/Graphics/Texture.cpp b/src/libjin/Graphics/Texture.cpp index 708042e..3ef3e7e 100644 --- a/src/libjin/Graphics/Texture.cpp +++ b/src/libjin/Graphics/Texture.cpp @@ -3,7 +3,6 @@ #include <fstream> #include "texture.h" -#include "../3rdparty/stb/stb_image.h" #include "../utils/utils.h" #include "../Math/Math.h" @@ -14,84 +13,41 @@ namespace graphics using namespace jin::math; - Texture* Texture::createTexture(const char* file) + /*static*/ Texture* Texture::createTexture(Bitmap* bitmap) { - 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* tex = new Texture(); + const Color* pixels = bitmap->getPixels(); + tex->size.x = bitmap->getWidth(); + tex->size.y = bitmap->getHeight(); + unsigned int w = tex->size.x; + unsigned int h = tex->size.y; + + glGenTextures(1, &tex->texture); + glBindTexture(GL_TEXTURE_2D, tex->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, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + + tex->vertCoord[0] = 0; tex->vertCoord[1] = 1; + tex->vertCoord[2] = 0; tex->vertCoord[3] = h; + tex->vertCoord[4] = w; tex->vertCoord[5] = h; + tex->vertCoord[6] = w; tex->vertCoord[7] = 1; + + tex->textCoord[0] = 0; tex->textCoord[1] = 0; + tex->textCoord[2] = 0; tex->textCoord[3] = 1; + tex->textCoord[4] = 1; tex->textCoord[5] = 1; + tex->textCoord[6] = 1; tex->textCoord[7] = 0; - 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) + : Drawable() { } Texture::~Texture() { - stbi_image_free(pixels); - } - - color Texture::getPixel(int x, int y) - { - int w = size.x; - int h = size.y; - if (without(x, 0, w) || without(y, 0, h)) - { - return { 0 }; - } - return pixels[x + y * w]; - } - - bool Texture::loadb(const void* b, size_t s) - { - int w; - int h; - pixels = (color*)stbi_load_from_memory((unsigned char *)b, s, &w, &h, NULL, STBI_rgb_alpha); - if (pixels == 0) return false; - size.x = w; - size.y = h; - - 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, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - - vertCoord[0] = 0; vertCoord[1] = 1; - vertCoord[2] = 0; vertCoord[3] = h; - vertCoord[4] = w; vertCoord[5] = h; - vertCoord[6] = w; vertCoord[7] = 1; - - textCoord[0] = 0; textCoord[1] = 0; - textCoord[2] = 0; textCoord[3] = 1; - textCoord[4] = 1; textCoord[5] = 1; - textCoord[6] = 1; textCoord[7] = 0; - - return true; } } // graphics |