2025-01-19 14:17:36 +01:00

98 lines
1.8 KiB
C++

#pragma once
#include <stb_image.h>
#include <gl/glew.h>
#include <GLFW/glfw3.h>
class Texture {
private:
GLuint _id;
protected:
const char* _texture;
public:
GLuint Reference() const {
return _id;
}
public:
Texture() : _id(0) { }
Texture(const char* file) {
int width, height, len;
stbi_set_flip_vertically_on_load(1);
unsigned char* data = stbi_load(file, &width, &height, &len, STBI_rgb_alpha);
glGenTextures(1, &_id);
glBindTexture(GL_TEXTURE_2D, _id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(data);
}
Texture(const Texture& other) : Texture(other._texture) {
}
Texture& operator=(const Texture& other) {
if (this == &other)
return *this;
Texture copy(other._texture);
this->_id = other._id;
this->_texture = other._texture;
copy._id = 0;
copy._texture = nullptr;
return *this;
}
Texture(Texture&& other) noexcept {
_id = other._id;
_texture = other._texture;
other._id = 0;
other._texture = nullptr;
}
Texture& operator= (Texture&& other) noexcept {
if (this == &other)
return *this;
this->_id = other._id;
this->_texture = other._texture;
other._id = 0;
other._texture = nullptr;
}
~Texture() {
if (_id == 0)
return;
glDeleteTextures(1, &_id);
}
void Use() const {
glBindTexture(GL_TEXTURE_2D, _id);
}
void Disable() const {
glBindTexture(GL_TEXTURE_2D, 0);
}
};