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