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