diff --git a/include/Texture.h b/include/Texture.h index f501671..9d78434 100644 --- a/include/Texture.h +++ b/include/Texture.h @@ -44,4 +44,18 @@ class Texture : public BaseTexture virtual void use(); // Привязка текстуры }; +// Класс 3D текстуры +class TextureArray : public BaseTexture +{ + public: + TextureArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере + TextureArray(const TextureArray& other); // Конструктор копирования + + TextureArray& operator=(const TextureArray& other); // Оператор присваивания + + void reallocate(GLuint levels, GLuint width, GLuint height, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Пересоздает текстуру для имеющегося дескриптора + + virtual void use(); // Привязка текстуры +}; + #endif // TEXTURE_H diff --git a/src/Texture.cpp b/src/Texture.cpp index e590ce7..5d8f5fe 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -151,3 +151,62 @@ void BaseTexture::setType(GLuint type) { this->type = type; } + +// Конструктор текстуры заданного размера для использования в буфере +TextureArray::TextureArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType, GLint internalformat, GLint format, GLenum dataType) +{ + type = texType; + // Генерация текстуры заданного размера + glGenTextures(1, &handler); + glBindTexture(GL_TEXTURE_2D_ARRAY, handler); + glTexImage3D( + GL_TEXTURE_2D_ARRAY, 0, internalformat, width, height, levels, 0, format, dataType, 0); + + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + // Привязка к буферу кадра + glFramebufferTexture(GL_FRAMEBUFFER, attachment, handler, 0); + + // Создаем счетчик использований дескриптора + handler_count[handler] = 1; +} + +// Конструктор копирования +TextureArray::TextureArray(const TextureArray& other) +{ + handler = other.handler; + type = other.type; + // Делаем копию и увеличиваем счетчик + handler_count[handler]++; +} + +// Оператор присваивания +TextureArray& TextureArray::operator=(const TextureArray& other) +{ + // Если это разные текстуры + if (handler != other.handler) + { + this->~TextureArray(); // Уничтожаем имеющуюся + // Заменяем новой + handler = other.handler; + handler_count[handler]++; + } + type = other.type; + + return *this; +} + +// Пересоздает текстуру для имеющегося дескриптора +void TextureArray::reallocate(GLuint levels, GLuint width, GLuint height, GLuint texType, GLint internalformat, GLint format, GLenum dataType) +{ + use(); + glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, dataType, NULL); +} + +// Привязка текстуры +void TextureArray::use() +{ + glActiveTexture(type + GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D_ARRAY, handler); // Привязка текстуры как активной +}