#include <GL/glew.h>
+#include <stdlib.h>
#include "texture.h"
#include "opengl/texture-gl.h"
TextureGL::TextureGL()
{
- w = 0;
- h = 0;
-
- pixels = 0;
tex = 0;
+ target = GL_TEXTURE_2D;
}
TextureGL::~TextureGL()
void TextureGL::update()
{
+ if(images.empty())
+ return;
+
if(!tex) {
+ target = is_cubemap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
+
glGenTextures(1, &tex);
- glBindTexture(GL_TEXTURE_2D, tex);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glBindTexture(target, tex);
+ glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else {
- glBindTexture(GL_TEXTURE_2D, tex);
+ glBindTexture(target, tex);
+ }
+
+ static const unsigned int faces[] = {
+ GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
+ GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
+ };
+
+ for(size_t i=0; i<images.size(); i++) {
+ int w = images[i].w;
+ int h = images[i].h;
+
+ /* target */
+ unsigned int t = is_cubemap() ? faces[i] : GL_TEXTURE_2D;
+
+ /* internal format */
+ unsigned int ifmt = images[i].is_float ? GL_RGBA16F : GL_SRGB_ALPHA;
+
+ /* data type of pixel data */
+ unsigned int type = images[i].is_float ? GL_FLOAT : GL_UNSIGNED_BYTE;
+
+ glTexImage2D(t, 0, ifmt, w, h, 0, GL_RGBA, type, images[i].pixels);
}
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
- glGenerateMipmap(GL_TEXTURE_2D);
+ glGenerateMipmap(target);
+}
+
+void TextureGL::bind(int texture_unit)
+{
+ glActiveTexture(GL_TEXTURE0 + texture_unit);
+
+ unsigned int target = is_cubemap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
+ glBindTexture(target, tex);
+
+ //TODO: (not needed for now) if I ever use textures outside the Texture class
+ //glActiveTexture(GL_TEXTURE0);
+}
+
+void TextureGL::unbind()
+{
+ unsigned int target = is_cubemap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
+ glBindTexture(target, 0);
}
\ No newline at end of file