openblocks/core/src/rendering/texture.cpp

44 lines
1.2 KiB
C++

#include "texture.h"
#include <GL/glew.h>
#include <GL/gl.h>
#include <stb/stb_image.h>
Texture::Texture(const char* texturePath, unsigned int format, bool noMipMaps) {
glGenTextures(1, &this->ID);
glBindTexture(GL_TEXTURE_2D, this->ID);
// Wrapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Interpolation
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// stbi_set_flip_vertically_on_load(true);
int width, height, nrChannels;
unsigned char *data = stbi_load(texturePath, &width, &height,
&nrChannels, 0);
if (!data) {
printf("Failed to load texture '%s'\n", texturePath);
abort();
}
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
GL_UNSIGNED_BYTE, data);
if (!noMipMaps) glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
}
Texture::~Texture() {
glDeleteTextures(1, &this->ID);
}
void Texture::activate(unsigned int textureIdx) {
glActiveTexture(GL_TEXTURE0 + textureIdx);
glBindTexture(GL_TEXTURE_2D, this->ID);
}