Texture3D.cpp 7.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "GPUdefs.h"
#include "Texture3D.h"
#include "FrameBuffer.h"
#include <iostream>




bool GPU::Texture3D::Allocate()
{
19
    gpuAssert( glGenTextures( 1, &m_Id ) );
20 21 22 23 24 25
    return m_Id != 0;
}


bool GPU::Texture3D::Unallocate()
{
26
    gpuAssert( glDeleteTextures( 1, &m_Id ) );
27 28 29 30 31 32 33 34 35
    m_Id = 0;
    return true;
}


bool GPU::Texture3D::Create()
{
    if( Instantiate() )
    {
36
        gpuAssert( glBindTexture( m_Target, m_Id ) );
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        m_Width = m_Height = m_Depth = 0;
        return true;
    }
    else
        return false;
}


bool GPU::Texture3D::Create( GLenum internalFormats[],
                             GLsizei width,
                             GLsizei height,
                             GLsizei depth,
                             GLint border,
                             GLenum format,
                             GLenum type,
                             const GLvoid* data )
{
    if( Instantiate() )
    {
        if( Load( internalFormats, width, height, depth, border, format, type, data ) )
            return true;
        else
            Release();
    }

    return false;
}


bool GPU::Texture3D::Load( GLenum internalFormats[],
                           GLsizei width,
                           GLsizei height,
                           GLsizei depth,
                           GLint border,
                           GLenum format,
                           GLenum type,
                           const GLvoid* data,
                           GLint level )
{
76
    gpuAssert( glBindTexture( m_Target, m_Id ) );
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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

    int currentFormatId = -1;
    GLint isSupported;
    do
    {
        if( internalFormats[++currentFormatId] == GL_NONE )
        {
            m_Width = m_Height = m_Depth = 0;
            std::cout << "Texture3D error - specified internal format(s) not supported." << std::endl;
            for( int i=0; i<currentFormatId; ++i )
                std::cout << internalFormats[i] << "  ";
            std::cout << std::endl;
            return false;
        }

        if( glGetInternalformativ )
            glGetInternalformativ( m_Target, internalFormats[currentFormatId], GL_INTERNALFORMAT_SUPPORTED, 1, &isSupported );
        else
            switch( internalFormats[currentFormatId] )
            {
                case GL_DEPTH_COMPONENT  :
                case GL_DEPTH_COMPONENT16:
                case GL_DEPTH_COMPONENT24:
                case GL_DEPTH_COMPONENT32:  isSupported = GL_TRUE;  break;
                default:
                {
                    glGetError();
                    glTexImage3D(
                        m_Target,
                        level,
                        internalFormats[currentFormatId],
                        width,
                        height,
                        depth,
                        border,
                        format,
                        type,
                        data
                        );
                    isSupported = glGetError() == GL_NO_ERROR;
                }
            }
    } while( !isSupported );


    GLint validatedFormat;
    GLint validatedType;

    if( format == GL_NONE )
126 127 128
    {
        gpuAssert( glGetInternalformativ( m_Target, internalFormats[currentFormatId], GL_TEXTURE_IMAGE_FORMAT, 1, &validatedFormat ) );
    }
129 130 131 132
    else
        validatedFormat = (GLint) format;

    if( type == GL_NONE )
133 134 135
    {
        gpuAssert( glGetInternalformativ( m_Target, internalFormats[currentFormatId], GL_TEXTURE_IMAGE_TYPE  , 1, &validatedType   ) );
    }
136 137 138 139
    else
        validatedType = (GLint) type;


140
    gpuAssert( glTexImage3D(
141 142 143 144 145 146 147 148 149 150
        m_Target,
        level,
        internalFormats[currentFormatId],
        width,
        height,
        depth,
        border,
        validatedFormat,
        validatedType,
        data
151
    ) );
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

    m_Width  = width;
    m_Height = height;
    m_Depth  = depth;

    return true;
}


bool GPU::Texture3D::LoadSlice( GLsizei sliceDepth,
                                GLenum format,
                                GLenum type,
                                const GLvoid* data,
                                GLint level )
{
    if( m_Id )
    {
169 170
        gpuAssert( glBindTexture( m_Target, m_Id ) );
        gpuAssert( glTexSubImage3D(
171 172 173 174 175 176 177 178 179 180 181 182 183 184
            m_Target,
            level,

            0,
            0,
            sliceDepth,

            m_Width,
            m_Height,
            1,

            format,
            type,
            data
185
        ) );
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

        return true;
    }

    return false;
}


#if 0
unsigned int GPU::Texture3D::RealWidth() const
{
    GLint width = 0;
    if( m_Id )
    {
        glBindTexture( GL_TEXTURE_3D, m_Id );
        glGetTexLevelParameteriv( GL_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &width );
    }
    return width;
}


unsigned int GPU::Texture3D::RealHeight() const
{
    GLint height = 0;
    if( m_Id )
    {
        glBindTexture( GL_TEXTURE_3D, m_Id );
        glGetTexLevelParameteriv( GL_TEXTURE_3D, 0, GL_TEXTURE_HEIGHT, &height );
    }
    return height;
}


unsigned int GPU::Texture3D::RealDepth() const
{
    GLint depth = 0;
    if( m_Id )
    {
        glBindTexture( GL_TEXTURE_3D, m_Id );
        glGetTexLevelParameteriv( GL_TEXTURE_3D, 0, GL_TEXTURE_DEPTH, &depth );
    }
    return depth;
}
#endif


#if 0
bool GPU::Texture3D::DumpTo( void *buffer,
                             const GLenum format,
                             const GLenum type,
                             const GLint level ) const
{
    if( m_Id )
    {
#if glGetTextureImageEXT    // Extension GL_EXT_direct_state_access is no supported by every common computer...
        glGetTextureImageEXT( m_Id, GL_TEXTURE_2D, level, format, type, buffer );
#else
        GPU::FrameBuffer fbuffer( m_Width, m_Height );
        fbuffer.Attach( GL_COLOR_ATTACHMENT0_EXT, this );
        fbuffer.DumpTo( GL_COLOR_ATTACHMENT0_EXT, buffer, format, type );
#endif
    }

    return m_Id != 0;
}


bool GPU::Texture3D::DumpTo( void *buffer,
                             const unsigned int x,
                             const unsigned int y,
                             const unsigned int w,
                             const unsigned int h,
                             const GLenum format,
                             const GLenum type,
                             const GLint level ) const
{
    if( m_Id )
    {
#if glGetTextureImageEXT    // Extension GL_EXT_direct_state_access is no supported by every common computer...
        glGetTextureImageEXT( m_Id, GL_TEXTURE_2D, level, format, type, buffer );
#else
        GPU::FrameBuffer fbuffer( m_Width, m_Height );
        fbuffer.Attach( GL_COLOR_ATTACHMENT0_EXT, this );
        fbuffer.DumpTo( GL_COLOR_ATTACHMENT0_EXT, buffer, x, y, w, h, format, type );
#endif
    }

    return m_Id != 0;
}


GPU::Texture3D GPU::Texture3D::Clone() const
{
    GPU::Texture3D newTex;
    newTex.Create();
    newTex.m_Width = Width();
    newTex.m_Height = Height();

    if( m_Id )
    {
        GPU::FrameBuffer fbuffer( Width(), Height() );
        fbuffer.Attach( GL_COLOR_ATTACHMENT0_EXT, this );
        fbuffer.Bind();

        newTex.Bind( 0 );
        glCopyTexImage2D( GL_TEXTURE_2D, 0, InternalFormat(), 0, 0, Width(), Height(), 0 );
        newTex.Unbind();

        fbuffer.Unbind();
    }

    return newTex;
}
#endif