Files
Tower-Defense/src/render/loader/TextureLoader.cpp
2022-02-16 18:34:49 +01:00

47 lines
1.1 KiB
C++

/*
* TextureLoader.cpp
*
* Created on: 15 nov. 2020
* Author: simon
*/
#include "render/loader/TextureLoader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "render/loader/stb_image.h"
#include <iostream>
#include "render/GL.h"
namespace TextureLoader {
unsigned int loadGLTexture(const char* fileName) {
int width, height, comp;
const unsigned char* image = stbi_load(fileName, &width, &height, &comp, STBI_rgb_alpha);
if (image == nullptr) {
std::cerr << "Erreur lors du chargement de la texture !" << std::endl;
throw(std::runtime_error("Failed to load texture"));
}
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (comp == 3)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, image);
else if (comp == 4)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free((void*)image);
return textureID;
}
}