607d0147f73ba39255c9a4542a53643155a16b2a
[demo] / src / opengl / texture-gl.cc
1 #include <GL/glew.h>
2
3 #include "texture.h"
4 #include "opengl/texture-gl.h"
5
6 TextureGL::TextureGL()
7 {
8         tex = 0;
9         target = GL_TEXTURE_2D;
10 }
11
12 TextureGL::~TextureGL()
13 {
14         glDeleteTextures(1, &tex);
15 }
16
17 void TextureGL::update()
18 {
19         if(images.empty())
20                 return;
21
22         if(!tex) {
23                 target = is_cubemap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
24
25                 glGenTextures(1, &tex);
26                 glBindTexture(target, tex);
27                 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
28                 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
29         }
30         else {
31                 glBindTexture(target, tex);
32         }
33
34         static const unsigned int faces[] = {
35                 GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
36                 GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
37         };
38
39         for(size_t i=0; i<images.size(); i++) {
40                 int w = images[i].w;
41                 int h = images[i].h;
42
43                 unsigned char *pixels = images[i].pixels;
44                 unsigned int t = is_cubemap() ? faces[i] : GL_TEXTURE_2D;
45                 glTexImage2D(t, 0, GL_SRGB_ALPHA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
46         }
47
48         glGenerateMipmap(target);
49 }
50
51 void TextureGL::bind()
52 {
53         glBindTexture(GL_TEXTURE_2D, tex);
54 }