34 lines
923 B
C++
34 lines
923 B
C++
#include <cassert>
|
|
#include <stb_image.h>
|
|
#include <texture.hpp>
|
|
|
|
Texture::Texture(GLenum type, const char *path) : type(type) {
|
|
glGenTextures(1, &ID);
|
|
glBindTexture(type, ID);
|
|
|
|
glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
|
|
int width, height, nrChannels;
|
|
unsigned char *data;
|
|
stbi_set_flip_vertically_on_load(true);
|
|
|
|
data = stbi_load(path, &width, &height, &nrChannels, 0);
|
|
assert(data && "Failed to load image");
|
|
|
|
glTexImage2D(type, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
|
|
data);
|
|
glGenerateMipmap(type);
|
|
|
|
stbi_image_free(data);
|
|
};
|
|
|
|
Texture::~Texture() {glDeleteTextures(1, &ID);}
|
|
|
|
void Texture::bind(GLenum texture) {
|
|
glActiveTexture(texture);
|
|
glBindTexture(type, ID);
|
|
}
|