TextureLoadStoreDDS.cc 7.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/

#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Data/TextureLoadStore.hh>
#include <nv_dds/nv_dds.h>

#include <fstream>

using namespace ACGL;
using namespace ACGL::OpenGL;
using namespace ACGL::Utils;

using namespace nv_dds;

namespace {

Janis Born's avatar
Janis Born committed
21
GLenum getDDSInternalFormat(bool compressed, GLenum format, ColorSpace _colorSpace = ColorSpace::AUTO_DETECT)
22 23 24 25 26 27 28 29 30
{
    GLenum internal_format = format;

    // Internal format for uncompressed color formats
    // For compressed textures, internal_format == image.get_format(), i.e. no conversion
    if(format == GL_BGRA) internal_format = GL_RGBA;
    if(format == GL_BGR)  internal_format = GL_RGB;
    if(format == GL_RED)  internal_format = GL_RED;

31
    // Transform internal format according to desired color space
32
    internal_format = recommendedInternalFormat(internal_format, _colorSpace);
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    return internal_format;
}

// To prevent code duplication, wrap glTexImage calls to take a compressed parameter

void texImage2D(bool compressed, GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const GLvoid* data)
{
    if(compressed)
        glCompressedTexImage2D(target, level, internalformat, width, height, 0, imageSize, data);
    else
        glTexImage2D(target, level, internalformat, width, height, 0, format, GL_UNSIGNED_BYTE, data);
}

void texImage3D(bool compressed, GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid* data)
{
    if(compressed)
        glCompressedTexImage3D(target, level, internalformat, width, height, depth, 0, imageSize, data);
    else
        glTexImage3D(target, level, internalformat, width, height, depth, 0, format, GL_UNSIGNED_BYTE, data);
}

}

namespace ACGL {
namespace OpenGL {

60
SharedTexture2D loadTexture2DFromDDS(const std::string& _filename, ColorSpace _colorSpace)
61 62 63 64 65 66
{
    CDDSImage image;
    SharedTexture2D texture;

    if(image.load(_filename))
    {
67
        GLenum internal_format = getDDSInternalFormat(image.is_compressed(), image.get_format(), _colorSpace);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

        if(image.is_volume() || image.is_cubemap())
        {
            ACGL::Utils::error() << _filename << " is not a 2D texture" << std::endl;
        }
        else
        {
            // It is a 2D texture
            texture = SharedTexture2D(new Texture2D(glm::uvec2(image.get_width(), image.get_height()), internal_format));
            texture->bind();
            texImage2D(image.is_compressed(), texture->getTarget(), 0, internal_format, image.get_width(), image.get_height(), image.get_format(), image.get_size(), image);

            for(unsigned int i = 0; i < image.get_num_mipmaps(); i++)
            {
                const CSurface& mipmap = image.get_mipmap(i);
                texImage2D(image.is_compressed(), texture->getTarget(), i+1, internal_format, mipmap.get_width(), mipmap.get_height(), image.get_format(), mipmap.get_size(), mipmap);
            }

            texture->setMaxLevel(image.get_num_mipmaps());
        }
    }
    else
    {
        ACGL::Utils::error() << "could not open " << _filename << std::endl;
    }

    return texture;
}

97
SharedTexture3D loadTexture3DFromDDS(const std::string& _filename, ColorSpace _colorSpace)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
{
    CDDSImage image;
    SharedTexture3D texture;

    if(image.load(_filename))
    {
        GLenum internal_format = getDDSInternalFormat(image.is_compressed(), image.get_format());

        if(image.is_volume())
        {
            // It is a 3D texture
            texture = SharedTexture3D(new Texture3D(glm::uvec3(image.get_width(), image.get_height(), image.get_depth()), internal_format));
            texture->bind();
            texImage3D(image.is_compressed(), texture->getTarget(), 0, internal_format, image.get_width(), image.get_height(), image.get_depth(), image.get_format(), image.get_size(), image);

            for(unsigned int i = 0; i < image.get_num_mipmaps(); i++)
            {
                const CSurface& mipmap = image.get_mipmap(i);
                texImage3D(image.is_compressed(), texture->getTarget(), i+1, internal_format, mipmap.get_width(), mipmap.get_height(), mipmap.get_depth(), image.get_format(), mipmap.get_size(), mipmap);
            }

            texture->setMaxLevel(image.get_num_mipmaps());
        }
        else
        {
            ACGL::Utils::error() << _filename << " is not a 3D texture" << std::endl;
        }
    }
    else
    {
        ACGL::Utils::error() << "could not open " << _filename << std::endl;
    }

    return texture;
}

134
SharedTextureCubeMap loadTextureCubeMapFromDDS(const std::string& _filename, ColorSpace _colorSpace)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
{
    CDDSImage image;
    SharedTextureCubeMap texture;

    if(image.load(_filename))
    {
        GLenum internal_format = getDDSInternalFormat(image.is_compressed(), image.get_format());

        if(image.is_cubemap())
        {
            // It is a cube map texture
            texture = SharedTextureCubeMap(new TextureCubeMap(glm::uvec2(image.get_width(), image.get_height()), internal_format));
            texture->bind();

            unsigned int numMipmapLevels = 1000;
            for(unsigned int i = 0; i < 6; ++i)
            {
                GLenum target = GL_INVALID_ENUM;
                switch(i)
                {
                    case 0: target = GL_TEXTURE_CUBE_MAP_POSITIVE_X; break;
                    case 1: target = GL_TEXTURE_CUBE_MAP_NEGATIVE_X; break;
                    case 2: target = GL_TEXTURE_CUBE_MAP_POSITIVE_Y; break;
                    case 3: target = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; break;
                    case 4: target = GL_TEXTURE_CUBE_MAP_POSITIVE_Z; break;
                    case 5: target = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; break;
                }

                const CTexture& face = image.get_cubemap_face(i);
                texImage2D(image.is_compressed(), target, 0, internal_format, face.get_width(), face.get_height(), image.get_format(), face.get_size(), face);

                numMipmapLevels = std::min(numMipmapLevels, face.get_num_mipmaps());
                for(unsigned int j = 0; j < face.get_num_mipmaps(); j++)
                {
                    const CSurface& mipmap = face.get_mipmap(j);
                    texImage2D(image.is_compressed(), texture->getTarget(), j+1, internal_format, mipmap.get_width(), mipmap.get_height(), image.get_format(), mipmap.get_size(), mipmap);
                }
            }

            texture->setMaxLevel(numMipmapLevels);
        }
        else
        {
            ACGL::Utils::error() << _filename << " is not a cube map texture" << std::endl;
        }
    }
    else
    {
        ACGL::Utils::error() << "could not open " << _filename << std::endl;
    }

    return texture;
}

}
}