Texture.cc 24.4 KB
Newer Older
1 2 3 4 5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
Robert Menzel's avatar
Robert Menzel committed
6

Robert Menzel's avatar
Robert Menzel committed
7
#include <ACGL/OpenGL/Objects/Texture.hh>
8
#include <ACGL/OpenGL/glloaders/extensions.hh> // for anisotrophic filtering
9
#include <ACGL/Math/Math.hh>
Robert Menzel's avatar
Robert Menzel committed
10

Robert Menzel's avatar
Robert Menzel committed
11
using namespace ACGL::OpenGL;
Robert Menzel's avatar
Robert Menzel committed
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26
void TextureBase::setMinFilter(GLint _value)
{
    GLuint prevTexture = bindAndGetOldTexture();
    glTexParameteri(mTarget, GL_TEXTURE_MIN_FILTER, _value);
    glBindTexture(mTarget, prevTexture);
}

void TextureBase::setMagFilter(GLint _value)
{
    GLuint prevTexture = bindAndGetOldTexture();
    glTexParameteri(mTarget, GL_TEXTURE_MAG_FILTER, _value);
    glBindTexture(mTarget, prevTexture);
}

27
#ifndef ACGL_OPENGLES_VERSION_20
Robert Menzel's avatar
Robert Menzel committed
28
void TextureBase::setWrapS( GLenum _wrapS )
29
{
30 31 32
    GLuint prevTexture = bindAndGetOldTexture();
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_S, _wrapS);
    glBindTexture(mTarget, prevTexture);
33 34
}

Robert Menzel's avatar
Robert Menzel committed
35
void TextureBase::setWrapT( GLenum _wrapT )
36
{
37 38 39
    GLuint prevTexture = bindAndGetOldTexture();
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_T, _wrapT);
    glBindTexture(mTarget, prevTexture);
40 41
}

Robert Menzel's avatar
Robert Menzel committed
42
void TextureBase::setWrapR( GLenum _wrapR )
43
{
44 45 46
    GLuint prevTexture = bindAndGetOldTexture();
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_R, _wrapR);
    glBindTexture(mTarget, prevTexture);
47 48
}

Robert Menzel's avatar
Robert Menzel committed
49
void TextureBase::setWrap( GLenum _wrapS, GLenum _wrapT, GLenum _wrapR )
Robert Menzel's avatar
Robert Menzel committed
50
{
51
    setWrapS(_wrapS);
Robert Menzel's avatar
Robert Menzel committed
52 53 54

    if(_wrapT != 0)
    {
55
        setWrapT(_wrapT);
Robert Menzel's avatar
Robert Menzel committed
56 57 58 59
    }

    if(_wrapR != 0)
    {
60
        setWrapR(_wrapR);
Robert Menzel's avatar
Robert Menzel committed
61 62
    }
}
63
#endif // ACGL_OPENGLES_VERSION_20
Robert Menzel's avatar
Robert Menzel committed
64

65 66
void TextureBase::setMinLOD(  GLint _lod )
{
67
    GLuint prevTexture = bindAndGetOldTexture();
68
    glTexParameteri( mTarget, GL_TEXTURE_MIN_LOD, _lod);
69
    glBindTexture(mTarget, prevTexture);
70 71 72 73
}

void TextureBase::setMaxLOD(  GLint _lod )
{
74
    GLuint prevTexture = bindAndGetOldTexture();
75
    glTexParameteri( mTarget, GL_TEXTURE_MAX_LOD, _lod);
76
    glBindTexture(mTarget, prevTexture);
77 78 79 80
}

void TextureBase::setLODBias( GLfloat _bias )
{
81
    GLuint prevTexture = bindAndGetOldTexture();
82
    glTexParameterf( mTarget, GL_TEXTURE_LOD_BIAS, _bias);
83
    glBindTexture(mTarget, prevTexture);
84 85 86 87
}

void TextureBase::setCompareMode( GLenum _mode )
{
88
    GLuint prevTexture = bindAndGetOldTexture();
89
    glTexParameteri( mTarget, GL_TEXTURE_COMPARE_MODE, _mode);
90
    glBindTexture(mTarget, prevTexture);
91 92 93 94
}

void TextureBase::setCompareFunc( GLenum _func )
{
95
    GLuint prevTexture = bindAndGetOldTexture();
96
    glTexParameteri( mTarget, GL_TEXTURE_COMPARE_FUNC, _func);
97
    glBindTexture(mTarget, prevTexture);
98 99 100
}


Robert Menzel's avatar
Robert Menzel committed
101
void TextureBase::setAnisotropicFilter( GLfloat _sampleCount )
Robert Menzel's avatar
Robert Menzel committed
102
{
103
    if ( ACGL_EXT_texture_filter_anisotrophic() ) {
Robert Menzel's avatar
Robert Menzel committed
104
        // anisotrophic filtering is supported:
105 106
        GLuint prevTexture = bindAndGetOldTexture();

107 108
        _sampleCount = std::max( _sampleCount, 1.0f );
        _sampleCount = std::min( _sampleCount, ACGL_MAX_TEXTURE_MAX_ANISOTROPY );
Robert Menzel's avatar
Robert Menzel committed
109
        glTexParameterf( mTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, _sampleCount );
110 111

        glBindTexture(mTarget, prevTexture);
Robert Menzel's avatar
Robert Menzel committed
112 113 114
        openGLRareError();
    } else
    {
Robert Menzel's avatar
Robert Menzel committed
115 116 117 118 119 120
        if (_sampleCount != 1.0f ) {
            // anisotropic filtering will just increase the image quality, so this is just
            // a warning and no error, 1 sample is equal to no anisotrophic filtering, so ignore
            // that case entirely!
            ACGL::Utils::warning() << "Anisotropic filtering is not supported, ignored" << std::endl;
        }
Robert Menzel's avatar
Robert Menzel committed
121 122
    }
}
Janis Born's avatar
Janis Born committed
123

Robert Menzel's avatar
Robert Menzel committed
124 125
void TextureBase::generateMipmaps(void)
{
126 127
    GLuint prevTexture = bindAndGetOldTexture();

Robert Menzel's avatar
Robert Menzel committed
128 129 130 131 132 133 134 135 136 137 138 139 140
#if (!defined ACGL_OPENGL_PROFILE_CORE)
    // on some ATI systems texturing has to be enabled to generate MipMaps
    // this is not needed by the spec an deprecated on core profiles (generates
    // an error on MacOS X Lion)
    glEnable(mTarget);
    openGLRareError();
#endif
#ifdef ACGL_OPENGL_VERSION_21
    // OpenGL 2 way to generate MipMaps
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
#else
    glGenerateMipmap(mTarget);
#endif
141 142

    glBindTexture(mTarget, prevTexture);
Robert Menzel's avatar
Robert Menzel committed
143 144 145
    openGLRareError();
}

146 147 148 149 150 151
GLint   TextureBase::getParameterI( GLenum _name ) const
{
    GLuint prevTexture = bindAndGetOldTexture();
    GLint param;
    glGetTexParameteriv( mTarget, _name, &param );
    glBindTexture(mTarget, prevTexture);
152
    openGLRareError();
153 154 155 156 157 158 159 160 161
    return param;
}

GLfloat TextureBase::getParameterF( GLenum _name ) const
{
    GLuint prevTexture = bindAndGetOldTexture();
    GLfloat param;
    glGetTexParameterfv( mTarget, _name, &param );
    glBindTexture(mTarget, prevTexture);
162
    openGLRareError();
163 164 165 166 167 168 169
    return param;
}

GLuint TextureBase::bindAndGetOldTexture() const
{
    GLint prevTexture = 0;

170 171 172 173 174 175 176 177 178
    if     (mTarget == GL_TEXTURE_1D)                   glGetIntegerv(GL_TEXTURE_BINDING_1D,                   &prevTexture);
    else if(mTarget == GL_TEXTURE_2D)                   glGetIntegerv(GL_TEXTURE_BINDING_2D,                   &prevTexture);
    else if(mTarget == GL_TEXTURE_3D)                   glGetIntegerv(GL_TEXTURE_BINDING_3D,                   &prevTexture);
    else if(mTarget == GL_TEXTURE_1D_ARRAY)             glGetIntegerv(GL_TEXTURE_BINDING_1D_ARRAY,             &prevTexture);
    else if(mTarget == GL_TEXTURE_2D_ARRAY)             glGetIntegerv(GL_TEXTURE_BINDING_2D_ARRAY,             &prevTexture);
    else if(mTarget == GL_TEXTURE_RECTANGLE)            glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE,            &prevTexture);
    else if(mTarget == GL_TEXTURE_2D_MULTISAMPLE)       glGetIntegerv(GL_TEXTURE_BINDING_2D_MULTISAMPLE,       &prevTexture);
    else if(mTarget == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) glGetIntegerv(GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY, &prevTexture);
    else if(mTarget == GL_TEXTURE_CUBE_MAP)             glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP,             &prevTexture);
Robert Menzel's avatar
Robert Menzel committed
179
#if (ACGL_OPENGL_VERSION >= 40)
180
    else if(mTarget == GL_TEXTURE_CUBE_MAP_ARRAY)       glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARRAY,       &prevTexture);
Robert Menzel's avatar
Robert Menzel committed
181
#endif
182
    else if(mTarget == GL_TEXTURE_BUFFER)               glGetIntegerv(GL_TEXTURE_BINDING_BUFFER,               &prevTexture);
183 184 185 186 187 188 189 190 191
    else {
        ACGL::Utils::error() << "Unknown texture target, will create sideeffecs as old bound texture can not get restored!" << std::endl;
    }

    glBindTexture(mTarget, mObjectName);

    return (GLuint) prevTexture;
}

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
GLenum TextureBase::getCompatibleFormat( GLenum _internalFormat )
{
    if (   _internalFormat == GL_DEPTH_COMPONENT
        || _internalFormat == GL_DEPTH_COMPONENT16
        || _internalFormat == GL_DEPTH_COMPONENT24
        || _internalFormat == GL_DEPTH_COMPONENT32F
        || _internalFormat == GL_DEPTH24_STENCIL8) {
        return GL_DEPTH_COMPONENT;
    }

    if (   _internalFormat == GL_R8I
        || _internalFormat == GL_R8UI
        || _internalFormat == GL_R16I
        || _internalFormat == GL_R16UI
        || _internalFormat == GL_R32I
        || _internalFormat == GL_R32UI
        || _internalFormat == GL_RG8I
        || _internalFormat == GL_RG8UI
        || _internalFormat == GL_RG16I
        || _internalFormat == GL_RG16UI
        || _internalFormat == GL_RG32I
        || _internalFormat == GL_RG32UI
        || _internalFormat == GL_RGB8I
        || _internalFormat == GL_RGB8UI
        || _internalFormat == GL_RGB16I
        || _internalFormat == GL_RGB16UI
        || _internalFormat == GL_RGB32I
        || _internalFormat == GL_RGB32UI
        || _internalFormat == GL_RGBA8I
        || _internalFormat == GL_RGBA8UI
        || _internalFormat == GL_RGBA16I
        || _internalFormat == GL_RGBA16UI
        || _internalFormat == GL_RGBA32I
        || _internalFormat == GL_RGBA32UI ) {
        return GL_RGBA_INTEGER;
    }

    // there are probably some cases missing that don't work with RGBA
    return GL_RGBA;
}

GLenum TextureBase::getCompatibleType(   GLenum _internalFormat )
{
    if (   _internalFormat == GL_R8I
        || _internalFormat == GL_R8UI
        || _internalFormat == GL_R16I
        || _internalFormat == GL_R16UI
        || _internalFormat == GL_R32I
        || _internalFormat == GL_R32UI
        || _internalFormat == GL_RG8I
        || _internalFormat == GL_RG8UI
        || _internalFormat == GL_RG16I
        || _internalFormat == GL_RG16UI
        || _internalFormat == GL_RG32I
        || _internalFormat == GL_RG32UI
        || _internalFormat == GL_RGB8I
        || _internalFormat == GL_RGB8UI
        || _internalFormat == GL_RGB16I
        || _internalFormat == GL_RGB16UI
        || _internalFormat == GL_RGB32I
        || _internalFormat == GL_RGB32UI
        || _internalFormat == GL_RGBA8I
        || _internalFormat == GL_RGBA8UI
        || _internalFormat == GL_RGBA16I
        || _internalFormat == GL_RGBA16UI
        || _internalFormat == GL_RGBA32I
        || _internalFormat == GL_RGBA32UI ) {
        return GL_INT;
    }
    // not sure if this works for all formats:
    return GL_BYTE;
}

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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

#ifndef ACGL_OPENGLES_VERSION_20
TextureData *TextureBase::getTextureImageRAW( const Image &_image, GLenum _format, GLenum _type ) const
{
    GLenum target = mTarget;
    if (target == GL_TEXTURE_CUBE_MAP || target == GL_TEXTURE_CUBE_MAP_ARRAY) {
        target = _image.cubeMapFace;
    }

    // remember the previously bound texture and bind this one
    GLuint prevTexture = bindAndGetOldTexture();

    // determine the required buffer size to hold the requested LOD level
    GLint width, height, depth;
    glGetTexLevelParameteriv(mTarget, _image.mipmapLevel, GL_TEXTURE_WIDTH,  &width);
    glGetTexLevelParameteriv(mTarget, _image.mipmapLevel, GL_TEXTURE_HEIGHT, &height);
    glGetTexLevelParameteriv(mTarget, _image.mipmapLevel, GL_TEXTURE_DEPTH,  &depth);

    // fetch the image data
    int channels = 4;
    if      (_format == GL_DEPTH_COMPONENT) { channels = 1; } // TODO: test
    else if (_format == GL_DEPTH_STENCIL)   { channels = 2; } // TODO: test
    else if (_format == GL_RED)   { channels = 1; }
    else if (_format == GL_GREEN) { channels = 1; }
    else if (_format == GL_BLUE)  { channels = 1; }
    else if (_format == GL_RG)    { channels = 2; }
    else if (_format == GL_RGB)   { channels = 3; }
    else if (_format == GL_RGBA)  { channels = 4; }
    else if (_format == GL_BGR)   { channels = 3; }
    else if (_format == GL_BGRA)  { channels = 4; }
    else if (_format == GL_RED_INTEGER)   { channels = 1; }
    else if (_format == GL_GREEN_INTEGER) { channels = 1; }
    else if (_format == GL_BLUE_INTEGER)  { channels = 1; }
    else if (_format == GL_RG_INTEGER)    { channels = 2; }
    else if (_format == GL_RGB_INTEGER)   { channels = 3; }
    else if (_format == GL_RGBA_INTEGER)  { channels = 4; }
    else if (_format == GL_BGR_INTEGER)   { channels = 3; }
    else if (_format == GL_BGRA_INTEGER)  { channels = 4; }
    else {
        std::cerr << "unknown format: will guess the number of channels" << std::endl;
        channels = 4;
    }

    GLubyte *imageData = new GLubyte[ width * height * getGLTypeSize(_type) * channels ];
    glGetTexImage(target, _image.mipmapLevel, _format, _type, (GLvoid*)imageData);

    // revert to the previously bound texture
    glBindTexture(mTarget, prevTexture);

    // store the image data and meta information in a TextureData object
    TextureData *dataObject = new TextureData();
    dataObject->setWidth(  width  );
    dataObject->setHeight( height );
    dataObject->setDepth(  depth  );
    dataObject->setType(  _type   );
    dataObject->setFormat(_format );
    dataObject->setData( imageData); // dataObject will take care of freeing imageData

    return dataObject;
}
#endif // ES 2


328 329 330
void TextureRectangle::resize( const glm::uvec2 &_newSize )
{
    if (_newSize.x != (unsigned int)mWidth || _newSize.y != (unsigned int)mHeight) {
331 332 333 334 335 336 337
        SharedTextureData sTexData( new TextureData() );
        sTexData->setData  (NULL);
        sTexData->setWidth (_newSize.x);
        sTexData->setHeight(_newSize.y);
        sTexData->setFormat(getCompatibleFormat( mInternalFormat ));
        sTexData->setType  (getCompatibleType(   mInternalFormat ));
        setImageData( sTexData );
338 339 340
    }
}

341 342 343 344 345
void TextureRectangle::setImageData( const SharedTextureData &_data )
{
    mWidth  = _data->getWidth();
    mHeight = _data->getHeight();

346
    openGLCriticalError();
347 348 349 350 351 352 353 354 355 356
    bind();
    glTexImage2D(
        mTarget,
        0, // there are no mipmaps for Rect Textures!
        mInternalFormat,
        mWidth, mHeight,
        0, // no border
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
357
    openGLCriticalError();
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
}


void Texture1D::setImageData( const SharedTextureData &_data, uint32_t _mipmapLayer )
{
    if (!textureStorageIsAllocated()) {
        texImage1D( _data, _mipmapLayer );
    } else {
        texSubImage1D( _data, _mipmapLayer );
    }
}

void Texture1D::resize( const uint32_t _newSize )
{
    if (_newSize != (unsigned int)mWidth) {
        SharedTextureData sTexData( new TextureData() );
        sTexData->setData  (NULL);
        sTexData->setWidth (_newSize);
        sTexData->setFormat(getCompatibleFormat( mInternalFormat ));
        sTexData->setType  (getCompatibleType(   mInternalFormat ));

        texImage1D( sTexData, 0 );
380 381 382 383
    }
}


384 385 386 387 388 389 390 391
void Texture2D::setImageData( const SharedTextureData &_data, uint32_t _mipmapLayer )
{
    if (!textureStorageIsAllocated()) {
        texImage2D( _data, _mipmapLayer );
    } else {
        texSubImage2D( _data, _mipmapLayer );
    }
}
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

void Texture2D::resize( const glm::uvec2 &_newSize )
{
    if (_newSize.x != (unsigned int)mWidth || _newSize.y != (unsigned int)mHeight) {
        SharedTextureData sTexData( new TextureData() );
        sTexData->setData  (NULL);
        sTexData->setWidth (_newSize.x);
        sTexData->setHeight(_newSize.y);
        sTexData->setFormat(getCompatibleFormat( mInternalFormat ));
        sTexData->setType  (getCompatibleType(   mInternalFormat ));

        texImage2D( sTexData, 0 );
    }
}


Robert Menzel's avatar
Robert Menzel committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
void Texture3D::setImageData( const SharedTextureData &_data, uint32_t _slice, uint32_t _mipmapLayer )
{
    if (!textureStorageIsAllocated()) {
        glm::uvec3 newSize = _data->getSize();
        newSize.z = _slice; // _data holds just one slice, so z = 1, resize the texture enough to hold at least _slice number of slices
        newSize = newSize * ((unsigned int)1<<_mipmapLayer);
        resize(newSize);
    }

    glm::uvec3 offset = glm::uvec3(0,0,_slice);
    texSubImage3D( _data, _mipmapLayer, offset );
}

void Texture3D::setImageData( const SharedTextureData &_data, uint32_t _mipmapLayer )
{
    if (!textureStorageIsAllocated()) {
        glm::uvec3 newSize = _data->getSize();
        newSize = newSize * ((unsigned int)1<<_mipmapLayer);
        resize(newSize);
    }

    glm::uvec3 offset = glm::uvec3(0,0,0);
    texSubImage3D( _data, _mipmapLayer, offset );
}

void Texture3D::resize( const glm::uvec3 &_newSize )
{
    if (_newSize.x != (unsigned int)mWidth || _newSize.y != (unsigned int)mHeight || _newSize.z != (unsigned int)mDepth) {
        SharedTextureData sTexData( new TextureData() );
        sTexData->setData  (NULL);
        sTexData->setWidth (_newSize.x);
        sTexData->setHeight(_newSize.y);
        sTexData->setDepth (_newSize.z);
        sTexData->setFormat(getCompatibleFormat( mInternalFormat ));
        sTexData->setType  (getCompatibleType(   mInternalFormat ));

        texImage3D( sTexData, 0 );
    }
}




void Texture1DArray::setImageData( const SharedTextureData &_data, uint32_t _layer, uint32_t _mipmapLayer )
{
    if (!textureStorageIsAllocated()) {
        glm::uvec2 newSize;
        newSize.x = _data->getWidth();
        newSize.y = _layer;
        newSize = newSize * ((unsigned int)1<<_mipmapLayer);
        resize(newSize);
    }

    glm::uvec2 offset = glm::uvec2(0, _layer );
    texSubImage2D( _data, _mipmapLayer, offset );
}

void Texture1DArray::setImageData( const SharedTextureData &_data, uint32_t _mipmapLayer )
{
    if (!textureStorageIsAllocated()) {
        glm::uvec2 newSize;
        newSize.x = _data->getWidth();
        newSize.y = _data->getHeight();
        newSize = newSize * ((unsigned int)1<<_mipmapLayer);
        resize(newSize);
    }

    texSubImage2D( _data, _mipmapLayer );
}

void Texture1DArray::resize( const glm::uvec2 &_newSize )
{
    if (_newSize.x != (unsigned int)mWidth || _newSize.y != (unsigned int)mHeight) {
        SharedTextureData sTexData( new TextureData() );
        sTexData->setData  (NULL);
        sTexData->setWidth (_newSize.x);
        sTexData->setHeight(_newSize.y);
        sTexData->setFormat(getCompatibleFormat( mInternalFormat ));
        sTexData->setType  (getCompatibleType(   mInternalFormat ));

        texImage2D( sTexData, 0 );
    }
}




495 496 497 498 499
void Texture2DArray::setImageData( const SharedTextureData &_data, uint32_t _arrayLayer, uint32_t _mipmapLayer )
{
    if (!textureStorageIsAllocated()) {
        texImage3D( _data, _mipmapLayer );
    } else {
Robert Menzel's avatar
Robert Menzel committed
500
        glm::uvec3 offset = glm::uvec3(0,0,_arrayLayer); // the array layer is the offset in Z in a 3D texture
501 502 503 504
        texSubImage3D( _data, _mipmapLayer, offset );
    }
}

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
void Texture2DArray::resize( const glm::uvec3 &_newSize )
{
    if (_newSize.x != (unsigned int)mWidth || _newSize.y != (unsigned int)mHeight || _newSize.z != (unsigned int)mDepth) {
        SharedTextureData sTexData( new TextureData() );
        sTexData->setData  (NULL);
        sTexData->setWidth (_newSize.x);
        sTexData->setHeight(_newSize.y);
        sTexData->setDepth (_newSize.z);
        sTexData->setFormat(getCompatibleFormat( mInternalFormat ));
        sTexData->setType  (getCompatibleType(   mInternalFormat ));

        texImage3D( sTexData, 0 );
    }
}


521 522 523 524 525 526
void TextureCubeMap::setImageData( const SharedTextureData &_data, GLenum _cubeSide, uint32_t _mipmapLayer )
{
    if (!cubeSideIsValid(_cubeSide)) {
        std::cerr << "can't set cubemap side as provided enum does not name a side!" << std::endl;
        return;
    }
Robert Menzel's avatar
Robert Menzel committed
527 528 529
    if (!_data) {
        std::cerr << "can't set cubemap side as data is invalid" << std::endl;
        return;
530
    }
Robert Menzel's avatar
Robert Menzel committed
531 532 533 534 535
    //if (!textureStorageIsAllocated()) {
        texImage2DCube( _data, _cubeSide, _mipmapLayer );
    ///} else {
    //    texSubImage2DCube( _data, _cubeSide, _mipmapLayer );
    //}
536 537
}

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
void TextureCubeMap::resize( const glm::uvec2 &_newSize )
{
    if (_newSize.x != (unsigned int)mWidth || _newSize.y != (unsigned int)mHeight) {
        SharedTextureData sTexData( new TextureData() );
        sTexData->setData  (NULL);
        sTexData->setWidth (_newSize.x);
        sTexData->setHeight(_newSize.y);
        sTexData->setFormat(getCompatibleFormat( mInternalFormat ));
        sTexData->setType  (getCompatibleType(   mInternalFormat ));

        texImage2DCube( sTexData, GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0 );
        texImage2DCube( sTexData, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0 );
        texImage2DCube( sTexData, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0 );
        texImage2DCube( sTexData, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0 );
        texImage2DCube( sTexData, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0 );
        texImage2DCube( sTexData, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0 );
    }
}

bool TextureCubeMap::cubeSideIsValid( const GLenum _cubeSide ) const
{
    if (   _cubeSide == GL_TEXTURE_CUBE_MAP_POSITIVE_X || _cubeSide == GL_TEXTURE_CUBE_MAP_NEGATIVE_X
        || _cubeSide == GL_TEXTURE_CUBE_MAP_POSITIVE_Y || _cubeSide == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
        || _cubeSide == GL_TEXTURE_CUBE_MAP_POSITIVE_Z || _cubeSide == GL_TEXTURE_CUBE_MAP_NEGATIVE_Z ) return true;
    return false;
}

void TextureCubeMap::texImage2DCube( const SharedTextureData &_data, GLenum _cubeSide, GLint _mipmapLevel )
{
    mWidth  = _data->getWidth();
    mHeight = _data->getHeight();

    bind();
    glTexImage2D(
        _cubeSide,
        _mipmapLevel,
        mInternalFormat,
        mWidth, mHeight,
        0, // no border
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
580
    openGLRareError();
581 582 583 584 585 586 587 588 589 590 591 592 593
}

void TextureCubeMap::texSubImage2DCube( const SharedTextureData &_data, GLenum _cubeSide, GLint _mipmapLevel, glm::ivec2 _offset )
{
    bind();
    glTexSubImage2D(
        _cubeSide,
        _mipmapLevel,
        _offset.x, _offset.y,
        mWidth, mHeight,
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
594
    openGLRareError();
595 596 597
}


598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

void TextureBase::texImage1D( const SharedTextureData &_data, GLint _mipmapLevel )
{
    mWidth  = _data->getWidth();

    bind();
    glTexImage1D(
        mTarget,
        _mipmapLevel,
        mInternalFormat,
        mWidth,
        0, // no border
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
613
    openGLRareError();
614 615 616 617 618
}

void TextureBase::texSubImage1D( const SharedTextureData &_data, GLint _mipmapLevel, uint32_t _offset )
{
    bind();
619 620
    GLsizei w = std::min( mWidth,  _data->getWidth()  );

621 622 623 624
    glTexSubImage1D(
        mTarget,
        _mipmapLevel,
        _offset,
625
        w,
626 627 628
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
629
    openGLRareError();
630 631
}

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
void TextureBase::texImage2D( const SharedTextureData &_data, GLint _mipmapLevel )
{
    mWidth  = _data->getWidth();
    mHeight = _data->getHeight();

    bind();
    glTexImage2D(
        mTarget,
        _mipmapLevel,
        mInternalFormat,
        mWidth, mHeight,
        0, // no border
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
647
    openGLRareError();
648 649
}

Robert Menzel's avatar
Robert Menzel committed
650
void TextureBase::texSubImage2D( const SharedTextureData &_data, GLint _mipmapLevel, glm::uvec2 _offset )
651
{
652 653
    GLsizei w =           std::min( mWidth,  _data->getWidth()  );
    GLsizei h = std::max( std::min( mHeight, _data->getHeight() ), 1);
654 655 656 657 658 659

    bind();
    glTexSubImage2D(
        mTarget,
        _mipmapLevel,
        _offset.x, _offset.y,
660
        w,h,
661 662 663
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
664
    openGLRareError();
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
}


void TextureBase::texImage3D( const SharedTextureData &_data, GLint _mipmapLevel )
{
    mWidth  = _data->getWidth();
    mHeight = _data->getHeight();
    mDepth  = _data->getDepth();

    bind();
    glTexImage3D(
        mTarget,
        _mipmapLevel,
        mInternalFormat,
        mWidth, mHeight, mDepth,
        0, // no border
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
684
    openGLRareError();
685 686
}

Robert Menzel's avatar
Robert Menzel committed
687
void TextureBase::texSubImage3D( const SharedTextureData &_data, GLint _mipmapLevel, glm::uvec3 _offset )
688 689
{
    bind();
690 691 692 693
    GLsizei w =           std::min( mWidth,  _data->getWidth()  );
    GLsizei h = std::max( std::min( mHeight, _data->getHeight() ), 1);
    GLsizei d = std::max( std::min( mDepth,  _data->getDepth()  ), 1);

694 695 696 697
    glTexSubImage3D(
        mTarget,
        _mipmapLevel,
        _offset.x, _offset.y, _offset.z,
698
        w,h,d,
699 700 701
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
702
    openGLRareError();
703
}
704

705 706

#ifdef ACGL_INCLUDE_DEPRECATED_FUNCTIONALITY
Robert Menzel's avatar
Robert Menzel committed
707 708 709 710
//////////////////////////////////////////////////////////////////////////
//
// old Texture class:

711
#ifndef ACGL_OPENGLES_VERSION_20
712
SharedTextureData Texture::getImageData(GLint _lod, GLenum _type) const
Janis Born's avatar
Janis Born committed
713
{
714 715 716 717
    Image img;
    img.mipmapLevel = _lod;
    return getTextureImage( img, mFormat, _type );
    /*
718
    if(_type == GL_INVALID_ENUM) _type = mType;
Janis Born's avatar
Janis Born committed
719

720 721
    // remember the previously bound texture and bind this one
    GLuint prevTexture = bindAndGetOldTexture();
722
    
Janis Born's avatar
Janis Born committed
723 724
    // determine the required buffer size to hold the requested LOD level
    GLint width, height, depth;
725
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_WIDTH,  &width);
Janis Born's avatar
Janis Born committed
726
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_HEIGHT, &height);
727
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_DEPTH,  &depth);
Janis Born's avatar
Janis Born committed
728 729

    // fetch the image data
730 731 732 733
    int channels = 4;
    if (mFormat == GL_RGB) channels = 3; // TODO: check the other channel types

    GLubyte* imageData = new GLubyte[width * height * getGLTypeSize(_type) * channels];
734
    glGetTexImage(mTarget, _lod, mFormat, _type, (GLvoid*)imageData);
Janis Born's avatar
Janis Born committed
735

736 737 738
    // revert to the previously bound texture
    glBindTexture(mTarget, prevTexture);

Janis Born's avatar
Janis Born committed
739 740 741 742 743 744
    // store the image data and meta information in a TextureData object
    SharedTextureData dataObject = SharedTextureData(new TextureData());
    dataObject->setWidth(width);
    dataObject->setHeight(height);
    dataObject->setDepth(depth);
    dataObject->setType(_type);
745
    dataObject->setFormat(mFormat);
Janis Born's avatar
Janis Born committed
746 747 748
    dataObject->setData(imageData); // dataObject will take care of freeing imageData

    return dataObject;
749
    */
Janis Born's avatar
Janis Born committed
750
}
751
#endif // ES 2.0
752
#endif