sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
texture_1d_array.cpp
1#include "texture.h"
2
3using namespace sgltk;
4
5Texture_1d_Array::Texture_1d_Array() : Texture() {
6 target = GL_TEXTURE_1D_ARRAY;
7}
8
9Texture_1d_Array::Texture_1d_Array(unsigned int res,
10 unsigned int num_layers,
11 GLenum internal_format,
12 GLenum type,
13 GLenum format) : Texture() {
14
15 target = GL_TEXTURE_1D_ARRAY;
16 create_empty(res, num_layers, internal_format, type, format);
17}
18
19Texture_1d_Array::Texture_1d_Array(const std::vector<Image>& images) : Texture() {
20 target = GL_TEXTURE_1D_ARRAY;
21 load(images);
22}
23
24Texture_1d_Array::Texture_1d_Array(const std::vector<std::string>& paths) : Texture() {
25 target = GL_TEXTURE_1D_ARRAY;
26 load(paths);
27}
28
29Texture_1d_Array::~Texture_1d_Array() {
30}
31
32void Texture_1d_Array::create_empty(unsigned int res,
33 unsigned int num_layers,
34 GLenum internal_format,
35 GLenum type, GLenum format) {
36
37 width = res;
38 height = 1;
39 this->num_layers = num_layers;
40
41 bind();
42 glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, internal_format,
43 res, num_layers, 0, format, type, nullptr);
44 glTexParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
45 glTexParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
46 glTexParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
47 unbind();
48}
49
50bool Texture_1d_Array::load(const std::vector<std::string>& paths) {
51 std::vector<Image> images(paths.size());
52 for(unsigned int i = 0; i < paths.size(); i++) {
53 if(!images[i].load(paths[i]))
54 return false;
55 }
56 return load(images);
57}
58
59bool Texture_1d_Array::load(const std::vector<Image>& images) {
60 width = 0;
61 height = 1;
62 num_layers = images.size();
63
64 std::vector<SDL_Surface *> images_tmp(images.size());
65
66 for(unsigned int i = 0; i < images.size(); i++) {
67 if(!images[i].image)
68 return false;
69
70 if(width == 0) {
71 width = images[i].width;
72 } else if(width != images[i].width) {
73 return false;
74 }
75
76 images_tmp[i] = SDL_ConvertSurfaceFormat(images[i].image,
77 SDL_PIXELFORMAT_RGBA8888, 0);
78 if(!images_tmp[i]) {
79 return false;
80 }
81 }
82
83 bind();
84 glTexParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
85 glTexParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
86 glTexParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
87 glTexParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
88 glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA,
89 width, images.size(), 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, nullptr);
90 for(unsigned int i = 0; num_layers; i++) {
91 glTexSubImage2D(GL_TEXTURE_1D_ARRAY, 0, 0, i, width, 1,
92 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, images_tmp[i]->pixels);
93 }
94 glGenerateMipmap(GL_TEXTURE_1D_ARRAY);
95 unbind();
96
97 for(SDL_Surface *image : images_tmp) {
98 SDL_FreeSurface(image);
99 }
100
101 return true;
102}
103
104void Texture_1d_Array::bind(unsigned int texture_unit) {
105 glActiveTexture(GL_TEXTURE0 + texture_unit);
106 glBindTexture(GL_TEXTURE_1D_ARRAY, texture);
107}
108
109void Texture_1d_Array::unbind(unsigned int texture_unit) {
110 glActiveTexture(GL_TEXTURE0 + texture_unit);
111 glBindTexture(GL_TEXTURE_1D_ARRAY, 0);
112}
void unbind(unsigned int texture_unit=0)
Unbind the texture.
void create_empty(unsigned int res, unsigned int num_layers, GLenum internal_format, GLenum type, GLenum format)
Creates an empty texture.
bool load(const std::vector< sgltk::Image > &images)
Load a new image as texture.
void bind(unsigned int texture_unit=0)
Bind the texture to be used by the shader.
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