Texture.cc 24.1 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
Robert Menzel's avatar
Robert Menzel committed
9

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

12 13 14 15 16 17 18 19 20 21 22 23 24 25
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);
}

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

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

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

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

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

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

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

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

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

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

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


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

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

        glBindTexture(mTarget, prevTexture);
Robert Menzel's avatar
Robert Menzel committed
111 112 113
        openGLRareError();
    } else
    {
Robert Menzel's avatar
Robert Menzel committed
114 115 116 117 118 119
        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
120 121
    }
}
Janis Born's avatar
Janis Born committed
122

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

Robert Menzel's avatar
Robert Menzel committed
127 128 129 130 131 132 133 134 135 136 137 138 139
#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
140 141

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

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

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

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

169 170 171 172 173 174 175 176 177
    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
178
#if (ACGL_OPENGL_VERSION >= 40)
179
    else if(mTarget == GL_TEXTURE_CUBE_MAP_ARRAY)       glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARRAY,       &prevTexture);
Robert Menzel's avatar
Robert Menzel committed
180
#endif
181
    else if(mTarget == GL_TEXTURE_BUFFER)               glGetIntegerv(GL_TEXTURE_BINDING_BUFFER,               &prevTexture);
182 183 184 185 186 187 188 189 190
    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;
}

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
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;
}

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 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

#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


327 328 329
void TextureRectangle::resize( const glm::uvec2 &_newSize )
{
    if (_newSize.x != (unsigned int)mWidth || _newSize.y != (unsigned int)mHeight) {
330 331 332 333 334 335 336
        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 );
337 338 339
    }
}

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
void TextureRectangle::setImageData( const SharedTextureData &_data )
{
    mWidth  = _data->getWidth();
    mHeight = _data->getHeight();

    openGLCriticalErrorOccured();
    bind();
    glTexImage2D(
        mTarget,
        0, // there are no mipmaps for Rect Textures!
        mInternalFormat,
        mWidth, mHeight,
        0, // no border
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    openGLCriticalErrorOccured();
}


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 );
379 380 381 382
    }
}


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

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
407 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
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 );
    }
}




494 495 496 497 498
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
499
        glm::uvec3 offset = glm::uvec3(0,0,_arrayLayer); // the array layer is the offset in Z in a 3D texture
500 501 502 503
        texSubImage3D( _data, _mipmapLayer, offset );
    }
}

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
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 );
    }
}


520 521 522 523 524 525
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
526 527 528
    if (!_data) {
        std::cerr << "can't set cubemap side as data is invalid" << std::endl;
        return;
529
    }
Robert Menzel's avatar
Robert Menzel committed
530 531 532 533 534
    //if (!textureStorageIsAllocated()) {
        texImage2DCube( _data, _cubeSide, _mipmapLayer );
    ///} else {
    //    texSubImage2DCube( _data, _cubeSide, _mipmapLayer );
    //}
535 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 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
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() );
    openGLRareErrorOccured();
}

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() );
    openGLRareErrorOccured();
}


597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628

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() );
    openGLRareErrorOccured();
}

void TextureBase::texSubImage1D( const SharedTextureData &_data, GLint _mipmapLevel, uint32_t _offset )
{
    bind();
    glTexSubImage1D(
        mTarget,
        _mipmapLevel,
        _offset,
        mWidth,
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
    openGLRareErrorOccured();
}

629 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() );
    openGLRareErrorOccured();
}

Robert Menzel's avatar
Robert Menzel committed
647
void TextureBase::texSubImage2D( const SharedTextureData &_data, GLint _mipmapLevel, glm::uvec2 _offset )
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
{
    // TODO: test if the size is correct?

    bind();
    glTexSubImage2D(
        mTarget,
        _mipmapLevel,
        _offset.x, _offset.y,
        mWidth, mHeight,
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
    openGLRareErrorOccured();
}


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() );
    openGLRareErrorOccured();
}

Robert Menzel's avatar
Robert Menzel committed
683
void TextureBase::texSubImage3D( const SharedTextureData &_data, GLint _mipmapLevel, glm::uvec3 _offset )
684 685 686 687 688 689 690 691 692 693 694 695
{
    bind();
    glTexSubImage3D(
        mTarget,
        _mipmapLevel,
        _offset.x, _offset.y, _offset.z,
        mWidth, mHeight, mDepth,
        _data->getFormat(),
        _data->getType(),
        _data->getData() );
    openGLRareErrorOccured();
}
696

697 698

#ifdef ACGL_INCLUDE_DEPRECATED_FUNCTIONALITY
Robert Menzel's avatar
Robert Menzel committed
699 700 701 702
//////////////////////////////////////////////////////////////////////////
//
// old Texture class:

703
#ifndef ACGL_OPENGLES_VERSION_20
704
SharedTextureData Texture::getImageData(GLint _lod, GLenum _type) const
Janis Born's avatar
Janis Born committed
705
{
706 707 708 709
    Image img;
    img.mipmapLevel = _lod;
    return getTextureImage( img, mFormat, _type );
    /*
710
    if(_type == GL_INVALID_ENUM) _type = mType;
Janis Born's avatar
Janis Born committed
711

712 713
    // remember the previously bound texture and bind this one
    GLuint prevTexture = bindAndGetOldTexture();
714
    
Janis Born's avatar
Janis Born committed
715 716
    // determine the required buffer size to hold the requested LOD level
    GLint width, height, depth;
717
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_WIDTH,  &width);
Janis Born's avatar
Janis Born committed
718
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_HEIGHT, &height);
719
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_DEPTH,  &depth);
Janis Born's avatar
Janis Born committed
720 721

    // fetch the image data
722 723 724 725
    int channels = 4;
    if (mFormat == GL_RGB) channels = 3; // TODO: check the other channel types

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

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

Janis Born's avatar
Janis Born committed
731 732 733 734 735 736
    // 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);
737
    dataObject->setFormat(mFormat);
Janis Born's avatar
Janis Born committed
738 739 740
    dataObject->setData(imageData); // dataObject will take care of freeing imageData

    return dataObject;
741
    */
Janis Born's avatar
Janis Born committed
742
}
743
#endif // ES 2.0
744
#endif