gltexture2darray.h File Reference

Go to the documentation of this file. Source: include/ffw/graphics/gltexture2darray.h

/* This file is part of FineFramework project */
#ifndef FFW_GRAPHICS_TEXTURE_2D_ARRAY
#define FFW_GRAPHICS_TEXTURE_2D_ARRAY

#include "gltexture.h"
#include "glimagetype.h"

namespace ffw {
    class FFW_API GLTexture2DArray: public GLTexture {
    public:
        GLTexture2DArray();
        GLTexture2DArray(const GLTexture2DArray& other) = delete;
        GLTexture2DArray(GLTexture2DArray&& other) NOEXCEPT;
        virtual ~GLTexture2DArray() = default;
        GLTexture2DArray(GLsizei width, GLsizei height, GLsizei layers, GLenum internalformat,
            GLenum format, GLenum pixelformat, const GLvoid* pixels = nullptr);
        void resize(GLsizei width, GLsizei height, GLsizei layers, const GLvoid* pixels = nullptr);
        void setPixels(GLint level, const GLvoid* pixels);
        void setPixels(GLint level, GLint xoffset, GLint yoffset, GLint loffset,
            GLsizei width, GLsizei height, const GLvoid* pixels);
        void getPixels(GLvoid* pixels) const;
        GLTexture2DArray& operator = (const GLTexture2DArray& second) = delete;
        GLTexture2DArray& operator = (GLTexture2DArray&& second) NOEXCEPT;
#ifdef FFW_MEDIA_IMAGE_BUFFER
        friend bool setFromBuffer(GLTexture2DArray& self, const ImageBuffer& image, const GLint layer, const bool inverse);
#endif
    private:
        void setPixelsInternal(GLint level, const GLvoid* pixels = nullptr);
        void setPixelsInternal(GLint level, GLint xoffset, GLint yoffset, GLint loffset,
            GLsizei width, GLsizei height, const GLvoid* pixels);
    };

#ifdef FFW_MEDIA_IMAGE_BUFFER
    inline bool setFromBuffer(GLTexture2DArray& self, const ImageBuffer& image, const GLint layer, const bool inverse) {

        if (!image.isAllocated())return false;
        if (image.getDepth() > 1)return false;

        const auto openglType = getOpenGLImageType(image.getImageType());
        if (!openglType) {
            return false;
        }

        if (image.isCompressed() && inverse)return false;

        if (self.isCreated()) {
            self.bind();
            for(auto m = 0; m <= image.getNumOfMipMaps()-1; m++) {
                
                if(inverse) {
                    if (m != 0)self.setPixelsInternal(m, nullptr); // Create mipmap
                    for (auto i = 0; i < image.getHeight(m); i++) {
                        const auto ptr = &image.getMipMapPtr(m)[image.getStrideSize(m) * i];
                        self.setPixelsInternal(
                            m, 0, image.getHeight(m) - i - 1,
                            layer, image.getWidth(m), 1, ptr);
                    }
                }
                else {
                    self.setPixelsInternal(
                        m, 0, 0, layer, image.getWidth(m),
                        image.getHeight(m), image.getMipMapPtr(m));
                }
            }

            if(image.getNumOfMipMaps() > 0) {
                glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
                glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, image.getNumOfMipMaps()-1);
            }
        } else {
            return false;
        }

        return true;
    }
#endif
};

inline void swap(ffw::GLTexture2DArray& first, ffw::GLTexture2DArray& second) NOEXCEPT {
    first.swap(second);
}
#endif