5Texture_2d_Array::Texture_2d_Array() :
Texture() {
6 target = GL_TEXTURE_2D_ARRAY;
9Texture_2d_Array::Texture_2d_Array(
unsigned int res_x,
12 GLenum internal_format,
14 GLenum format) : Texture() {
16 target = GL_TEXTURE_2D_ARRAY;
20Texture_2d_Array::Texture_2d_Array(
const std::vector<Image>& images) :
Texture() {
21 target = GL_TEXTURE_2D_ARRAY;
25Texture_2d_Array::Texture_2d_Array(
const std::vector<std::string>& paths) : Texture() {
26 target = GL_TEXTURE_2D_ARRAY;
30Texture_2d_Array::~Texture_2d_Array() {
36 GLenum internal_format,
37 GLenum type, GLenum format) {
44 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, internal_format,
45 res_x, res_y,
num_layers, 0, format, type,
nullptr);
46 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
47 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
48 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
49 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
54 std::vector<Image> images(paths.size());
55 for(
unsigned int i = 0; i < paths.size(); i++) {
56 if(!images[i].
load(paths[i]))
67 std::vector<SDL_Surface *> images_tmp(images.size());
74 width = images[i].width;
80 images_tmp[i] = SDL_ConvertSurfaceFormat(images[i].image,
81 SDL_PIXELFORMAT_RGBA8888, 0);
88 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
89 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
90 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
92 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA,
94 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,
nullptr);
95 for(
unsigned int i = 0; i < images_tmp.size(); i++) {
96 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0,
98 GL_UNSIGNED_INT_8_8_8_8, images_tmp[i]->pixels);
100 glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
103 for(SDL_Surface *image : images_tmp) {
104 SDL_FreeSurface(image);
111 glActiveTexture(GL_TEXTURE0 + texture_unit);
112 glBindTexture(GL_TEXTURE_2D_ARRAY,
texture);
116 glActiveTexture(GL_TEXTURE0 + texture_unit);
117 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
void bind(unsigned int texture_unit=0)
Bind the texture to be used by the shader.
bool load(const std::vector< sgltk::Image > &images)
Load a new image as texture.
void unbind(unsigned int texture_unit=0)
Unbind the texture.
void create_empty(unsigned int res_x, unsigned int res_y, unsigned int num_layers, GLenum internal_format, GLenum type, GLenum format)
Creates an empty texture.
unsigned int num_layers
The number of layers.
unsigned int width
The width of the texture.
GLenum target
The texture target.
GLuint texture
The texture name.
unsigned int height
The height of the texture.