Класс абстрактной текстуры
This commit is contained in:
		
							parent
							
								
									1cc794829a
								
							
						
					
					
						commit
						9c42edaac2
					
				| @ -13,25 +13,33 @@ enum TexType { | |||||||
|     TEX_AVAILABLE_COUNT |     TEX_AVAILABLE_COUNT | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| class Texture | // Абстрактный класс базовой текстуры
 | ||||||
|  | class BaseTexture | ||||||
| { | { | ||||||
|     public: |     public: | ||||||
|         Texture(GLuint type = TEX_AVAILABLE_COUNT, const std::string& filename = ""); // Загрузка текстуры с диска или использование "пустой"
 |         ~BaseTexture(); | ||||||
|         Texture(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере
 |         virtual void use() = 0; // Привязка текстуры
 | ||||||
|         Texture(const Texture& other); // Конструктор копирования
 |  | ||||||
|         ~Texture(); |  | ||||||
| 
 |  | ||||||
|         Texture& operator=(const Texture& other); // Оператор присваивания
 |  | ||||||
| 
 |  | ||||||
|         void use(); // Привязка текстуры
 |  | ||||||
|         static void disable(GLuint type); // Отвязка текстуры по типу
 |         static void disable(GLuint type); // Отвязка текстуры по типу
 | ||||||
|         GLuint getType(); // Возвращает тип текстуры
 |         GLuint getType(); // Возвращает тип текстуры
 | ||||||
|         void setType(GLuint type); // Задает тип текстуры
 |         void setType(GLuint type); // Задает тип текстуры
 | ||||||
|     private: |     protected: | ||||||
|         GLuint handler; // Дескриптор текстуры
 |         GLuint handler; // Дескриптор текстуры
 | ||||||
|         GLuint type; // Тип текстуры, соответствует её слоту
 |         GLuint type; // Тип текстуры, соответствует её слоту
 | ||||||
|         static std::map<std::string, int> filename_handler; // Получение дескриптора текстуры по её имени
 |         static std::map<std::string, int> filename_handler; // Получение дескриптора текстуры по её имени
 | ||||||
|         static std::map<int, int> handler_count; // Получение количества использований по дескриптору текстуры (Shared pointer)
 |         static std::map<int, int> handler_count; // Получение количества использований по дескриптору текстуры (Shared pointer)
 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | // Класс 2D текстуры
 | ||||||
|  | class Texture : public BaseTexture | ||||||
|  | { | ||||||
|  |     public: | ||||||
|  |         Texture(GLuint type = TEX_AVAILABLE_COUNT, const std::string& filename = ""); // Загрузка текстуры с диска или использование "пустой"
 | ||||||
|  |         Texture(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере
 | ||||||
|  |         Texture(const Texture& other); // Конструктор копирования
 | ||||||
|  | 
 | ||||||
|  |         Texture& operator=(const Texture& other); // Оператор присваивания
 | ||||||
|  | 
 | ||||||
|  |         virtual void use(); // Привязка текстуры       
 | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| #endif // TEXTURE_H
 | #endif // TEXTURE_H
 | ||||||
|  | |||||||
| @ -3,12 +3,13 @@ | |||||||
| #define STB_IMAGE_IMPLEMENTATION | #define STB_IMAGE_IMPLEMENTATION | ||||||
| #include <stb_image.h> | #include <stb_image.h> | ||||||
| 
 | 
 | ||||||
| std::map<std::string, int> Texture::filename_handler; // Получение дескриптора текстуры по её имени
 | std::map<std::string, int> BaseTexture::filename_handler; // Получение дескриптора текстуры по её имени
 | ||||||
| std::map<int, int> Texture::handler_count; // Получение количества использований по дескриптору текстуры (Shared pointer)
 | std::map<int, int> BaseTexture::handler_count; // Получение количества использований по дескриптору текстуры (Shared pointer)
 | ||||||
| 
 | 
 | ||||||
| // Загрузка текстуры с диска или использование "пустой"
 | // Загрузка текстуры с диска или использование "пустой"
 | ||||||
| Texture::Texture(GLuint t, const std::string& filename) : type(t) | Texture::Texture(GLuint t, const std::string& filename) | ||||||
| { | { | ||||||
|  |     type = t; | ||||||
|     if (!filename_handler.count(filename)) |     if (!filename_handler.count(filename)) | ||||||
|     { |     { | ||||||
|         std::string empty = ""; |         std::string empty = ""; | ||||||
| @ -60,8 +61,9 @@ Texture::Texture(GLuint t, const std::string& filename) : type(t) | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Конструктор текстуры заданного размера для использования в буфере
 | // Конструктор текстуры заданного размера для использования в буфере
 | ||||||
| Texture::Texture(GLuint width, GLuint height, GLuint attachment, GLuint texType, GLint internalformat, GLint format, GLenum dataType) : type(texType) | Texture::Texture(GLuint width, GLuint height, GLuint attachment, GLuint texType, GLint internalformat, GLint format, GLenum dataType)  | ||||||
| { | { | ||||||
|  |     type = texType; | ||||||
|     // Генерация текстуры заданного размера
 |     // Генерация текстуры заданного размера
 | ||||||
|     glGenTextures(1, &handler); |     glGenTextures(1, &handler); | ||||||
|     glBindTexture(GL_TEXTURE_2D, handler); |     glBindTexture(GL_TEXTURE_2D, handler); | ||||||
| @ -77,8 +79,10 @@ Texture::Texture(GLuint width, GLuint height, GLuint attachment, GLuint texType, | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Конструктор копирования
 | // Конструктор копирования
 | ||||||
| Texture::Texture(const Texture& other) : handler(other.handler), type(other.type) | Texture::Texture(const Texture& other) | ||||||
| { | { | ||||||
|  |     handler = other.handler; | ||||||
|  |     type = other.type; | ||||||
|     // Делаем копию и увеличиваем счетчик
 |     // Делаем копию и увеличиваем счетчик
 | ||||||
|     handler_count[handler]++; |     handler_count[handler]++; | ||||||
| } | } | ||||||
| @ -99,7 +103,7 @@ Texture& Texture::operator=(const Texture& other) | |||||||
|     return *this; |     return *this; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Texture::~Texture() | BaseTexture::~BaseTexture() | ||||||
| { | { | ||||||
|     if (!--handler_count[handler]) // Если количество ссылок = 0
 |     if (!--handler_count[handler]) // Если количество ссылок = 0
 | ||||||
|     { |     { | ||||||
| @ -123,20 +127,20 @@ void Texture::use() | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Отвязка текстуры по типу
 | // Отвязка текстуры по типу
 | ||||||
| void Texture::disable(GLuint type) | void BaseTexture::disable(GLuint type) | ||||||
| { | { | ||||||
|     glActiveTexture(type + GL_TEXTURE0); |     glActiveTexture(type + GL_TEXTURE0); | ||||||
|     glBindTexture(GL_TEXTURE_2D, 0); // Отвязка текстуры
 |     glBindTexture(GL_TEXTURE_2D, 0); // Отвязка текстуры
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Возвращает тип текстуры
 | // Возвращает тип текстуры
 | ||||||
| GLuint Texture::getType() | GLuint BaseTexture::getType() | ||||||
| { | { | ||||||
|     return type; |     return type; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Задает тип текстуры
 | // Задает тип текстуры
 | ||||||
| void Texture::setType(GLuint type) | void BaseTexture::setType(GLuint type) | ||||||
| { | { | ||||||
|     this->type = type; |     this->type = type; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user