38 lines
1.1 KiB
C++
38 lines
1.1 KiB
C++
#include "texture.h"
|
|
#include "stb_image.h"
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
Texture::Texture(std::string fileName) {
|
|
int width, height, numComponents;
|
|
unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
|
|
|
|
if (imageData == NULL)
|
|
std::cout << "Loading failed for texture: " << fileName << std::endl;
|
|
|
|
glGenTextures(1, &m_texture);
|
|
glBindTexture(GL_TEXTURE_2D, m_texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
|
|
|
|
stbi_image_free(imageData);
|
|
std::cout << "Loaded texture: " << fileName << std::endl;
|
|
}
|
|
|
|
void Texture::Bind(unsigned int unit) {
|
|
assert(unit >= 0 && unit <= 31);
|
|
|
|
glActiveTexture(GL_TEXTURE0 + unit);
|
|
glBindTexture(GL_TEXTURE_2D, m_texture);
|
|
}
|
|
|
|
Texture::~Texture() {
|
|
glDeleteTextures(1, &m_texture);
|
|
}
|