Texture.cc 5.34 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
#ifndef ACGL_OPENGLES_VERSION_20
Robert Menzel's avatar
Robert Menzel committed
13
void TextureBase::setWrapS( GLenum _wrapS )
14
{
Robert Menzel's avatar
Robert Menzel committed
15 16
    glBindTexture(mTarget, mObjectName);
    mWrapS = _wrapS;
17 18 19
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_S, mWrapS);
}

Robert Menzel's avatar
Robert Menzel committed
20
void TextureBase::setWrapT( GLenum _wrapT )
21
{
Robert Menzel's avatar
Robert Menzel committed
22 23
    glBindTexture(mTarget, mObjectName);
    mWrapT = _wrapT;
24 25 26
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_T, mWrapT);
}

Robert Menzel's avatar
Robert Menzel committed
27
void TextureBase::setWrapR( GLenum _wrapR )
28
{
Robert Menzel's avatar
Robert Menzel committed
29 30
    glBindTexture(mTarget, mObjectName);
    mWrapR = _wrapR;
31 32 33
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_R, mWrapR);
}

Robert Menzel's avatar
Robert Menzel committed
34
void TextureBase::setWrap( GLenum _wrapS, GLenum _wrapT, GLenum _wrapR )
Robert Menzel's avatar
Robert Menzel committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
{
    glBindTexture(mTarget, mObjectName);

    mWrapS = _wrapS;
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_S, mWrapS);

    if(_wrapT != 0)
    {
        mWrapT = _wrapT;
        glTexParameteri(mTarget, GL_TEXTURE_WRAP_T, mWrapT);
    }

    if(_wrapR != 0)
    {
        mWrapR = _wrapR;
        glTexParameteri(mTarget, GL_TEXTURE_WRAP_R, mWrapR);
    }

    openGLRareError();
}
55
#endif // ACGL_OPENGLES_VERSION_20
Robert Menzel's avatar
Robert Menzel committed
56

Robert Menzel's avatar
Robert Menzel committed
57
void TextureBase::setAnisotropicFilter( GLfloat _sampleCount )
Robert Menzel's avatar
Robert Menzel committed
58
{
59
    if ( ACGL_EXT_texture_filter_anisotrophic() ) {
Robert Menzel's avatar
Robert Menzel committed
60
        // anisotrophic filtering is supported:
61 62
        _sampleCount = std::max( _sampleCount, 1.0f );
        _sampleCount = std::min( _sampleCount, ACGL_MAX_TEXTURE_MAX_ANISOTROPY );
Robert Menzel's avatar
Robert Menzel committed
63 64 65 66 67
        glBindTexture( mTarget, mObjectName );
        glTexParameterf( mTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, _sampleCount );
        openGLRareError();
    } else
    {
Robert Menzel's avatar
Robert Menzel committed
68 69 70 71 72 73
        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
74 75
    }
}
Janis Born's avatar
Janis Born committed
76

Robert Menzel's avatar
Robert Menzel committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
void TextureBase::generateMipmaps(void)
{
    glBindTexture(mTarget, mObjectName);
#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
    openGLRareError();
}

//////////////////////////////////////////////////////////////////////////
//
// old Texture class:

100
#ifndef ACGL_OPENGLES_VERSION_20
101
SharedTextureData Texture::getImageData(GLint _lod, GLenum _type) const
Janis Born's avatar
Janis Born committed
102
{
103
    if(_type == GL_INVALID_ENUM) _type = mType;
Janis Born's avatar
Janis Born committed
104

105 106
    // remember the previously bound texture
    GLint prevTexture;
107
    
108 109 110 111 112 113 114 115
    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);
Janis Born's avatar
Janis Born committed
116 117 118
    // determine the required buffer size to hold the requested LOD level
    bind();
    GLint width, height, depth;
119
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_WIDTH,  &width);
Janis Born's avatar
Janis Born committed
120
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_HEIGHT, &height);
121
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_DEPTH,  &depth);
Janis Born's avatar
Janis Born committed
122 123

    // fetch the image data
124 125 126 127
    int channels = 4;
    if (mFormat == GL_RGB) channels = 3; // TODO: check the other channel types

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

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

Janis Born's avatar
Janis Born committed
133 134 135 136 137 138
    // 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);
139
    dataObject->setFormat(mFormat);
Janis Born's avatar
Janis Born committed
140 141 142 143
    dataObject->setData(imageData); // dataObject will take care of freeing imageData

    return dataObject;
}
144
#endif // ES 2.0