
/* gl_texture.hpp - interface for simple OpenGL texture class */

#ifndef __gl_texture_hpp__
#define __gl_texture_hpp__

// XXX assumes EXT_direct_state_access

#if !defined(HAS_INCLUDED_OPENGL_HEADERS)
# include "gl_headers.h"
#endif

class GLTexture {
protected:
    GLenum target;
    GLuint texture;
    GLenum internal_format;
    GLsizei width;
    GLsizei height;
    GLsizei depth;
public:
    GLsizei Width() {
        return width;
    }
    GLsizei Height() {
        return height;
    }
    GLsizei Depth() {
        return depth;
    }
    GLenum InternalFormat() {
        return internal_format;
    }
    void bind() {
        glBindTexture(target, texture);
    }
    void bind(int unit) {
        glActiveTexture(GL_TEXTURE0 + unit);
        glBindTexture(target, texture);
    }
    void bind(GLenum unit) {
        glActiveTexture(unit);
        glBindTexture(target, texture);
    }
    GLTexture(GLenum target_)
        : target(target_)
        , texture(0)
        , internal_format(GL_NONE)
        , width(0)
        , height(0)
        , depth(1)
    {
        glGenTextures(1, &texture);
        glBindTexture(target, texture);
    }
    ~GLTexture() {
        glDeleteTextures(1, &texture);
    }
    void setParameter(GLenum pname, GLfloat f) {
        glTextureParameterfEXT(target, texture, pname, f);
    }
    void setParameter(GLenum pname, GLint i) {
        glTextureParameteriEXT(texture, target, pname, i);
    }
    void generateMipmaps() {
        glGenerateTextureMipmap(texture);
    }
    void configureMipmaps() {
        generateMipmaps();
        setParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        setParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    }
    void configureSansMipmapping() {
        setParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        setParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    }
};

class GLTexture2D : public GLTexture {
public:
    GLTexture2D()
        : GLTexture(GL_TEXTURE_2D)
    {}
    bool loadImageFromFile(const char *filename, bool sRGB_encoded);
};

#endif // __gl_texture_hpp__
