sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
texture_2d_array.cpp
1#include "texture.h"
2
3using namespace sgltk;
4
5Texture_2d_Array::Texture_2d_Array() : Texture() {
6 target = GL_TEXTURE_2D_ARRAY;
7}
8
9Texture_2d_Array::Texture_2d_Array(unsigned int res_x,
10 unsigned int res_y,
11 unsigned int num_layers,
12 GLenum internal_format,
13 GLenum type,
14 GLenum format) : Texture() {
15
16 target = GL_TEXTURE_2D_ARRAY;
17 create_empty(res_x, res_y, num_layers, internal_format, type, format);
18}
19
20Texture_2d_Array::Texture_2d_Array(const std::vector<Image>& images) : Texture() {
21 target = GL_TEXTURE_2D_ARRAY;
22 load(images);
23}
24
25Texture_2d_Array::Texture_2d_Array(const std::vector<std::string>& paths) : Texture() {
26 target = GL_TEXTURE_2D_ARRAY;
27 load(paths);
28}
29
30Texture_2d_Array::~Texture_2d_Array() {
31}
32
33void Texture_2d_Array::create_empty(unsigned int res_x,
34 unsigned int res_y,
35 unsigned int num_layers,
36 GLenum internal_format,
37 GLenum type, GLenum format) {
38
39 width = res_x;
40 height = res_y;
41 this->num_layers = num_layers;
42
43 bind();
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);
50 unbind();
51}
52
53bool Texture_2d_Array::load(const std::vector<std::string>& paths) {
54 std::vector<Image> images(paths.size());
55 for(unsigned int i = 0; i < paths.size(); i++) {
56 if(!images[i].load(paths[i]))
57 return false;
58 }
59 return load(images);
60}
61
62bool Texture_2d_Array::load(const std::vector<Image>& images) {
63 width = 0;
64 height = 0;
65 num_layers = images.size();
66
67 std::vector<SDL_Surface *> images_tmp(images.size());
68
69 for(unsigned int i = 0; i < num_layers; i++) {
70 if(!images[i].image)
71 return false;
72
73 if(width == 0 && height == 0) {
74 width = images[i].width;
75 height = images[i].height;
76 } else if(width != images[i].width || height != images[i].height) {
77 return false;
78 }
79
80 images_tmp[i] = SDL_ConvertSurfaceFormat(images[i].image,
81 SDL_PIXELFORMAT_RGBA8888, 0);
82 if(!images_tmp[i]) {
83 return false;
84 }
85 }
86
87 bind();
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,
93 width, height, images.size(), 0,
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,
97 0, i, width, height, 1, GL_RGBA,
98 GL_UNSIGNED_INT_8_8_8_8, images_tmp[i]->pixels);
99 }
100 glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
101 unbind();
102
103 for(SDL_Surface *image : images_tmp) {
104 SDL_FreeSurface(image);
105 }
106
107 return true;
108}
109
110void Texture_2d_Array::bind(unsigned int texture_unit) {
111 glActiveTexture(GL_TEXTURE0 + texture_unit);
112 glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
113}
114
115void Texture_2d_Array::unbind(unsigned int texture_unit) {
116 glActiveTexture(GL_TEXTURE0 + texture_unit);
117 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
118}
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.
Manages textures.
Definition texture.h:13
unsigned int num_layers
The number of layers.
Definition texture.h:46
unsigned int width
The width of the texture.
Definition texture.h:38
GLenum target
The texture target.
Definition texture.h:30
GLuint texture
The texture name.
Definition texture.h:34
unsigned int height
The height of the texture.
Definition texture.h:42