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

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

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
void Texture::setWrapS( GLenum _wrapS ) 
{
	glBindTexture(mTarget, mObjectName);
	mWrapS = _wrapS;
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_S, mWrapS);
}

void Texture::setWrapT( GLenum _wrapT )
{
	glBindTexture(mTarget, mObjectName);
	mWrapT = _wrapT;
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_T, mWrapT);
}

void Texture::setWrapR( GLenum _wrapR )
{
	glBindTexture(mTarget, mObjectName);
	mWrapR = _wrapR;
    glTexParameteri(mTarget, GL_TEXTURE_WRAP_R, mWrapR);
}


Robert Menzel's avatar
Robert Menzel committed
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
void Texture::setWrap( GLenum _wrapS, GLenum _wrapT, GLenum _wrapR )
{
    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();
}

void Texture::setAnisotropicFilter( GLfloat _sampleCount )
{
#ifdef ACGL_USE_GLEW
    if (GLEW_EXT_texture_filter_anisotropic) {
		// anisotrophic filtering is supported:
        GLfloat maxAnisotropicSamples;
        glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropicSamples );
        _sampleCount = std::max( 1.0f, _sampleCount );
        _sampleCount = std::min( maxAnisotropicSamples, _sampleCount );
        glBindTexture( mTarget, mObjectName );
        glTexParameterf( mTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, _sampleCount );
        openGLRareError();
    } else
#endif
    {
		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;
		}
    }
}
Janis Born's avatar
Janis Born committed
78

79
SharedTextureData Texture::getImageData(GLint _lod, GLenum _type) const
Janis Born's avatar
Janis Born committed
80
{
81
    if(_type == GL_INVALID_ENUM) _type = mType;
Janis Born's avatar
Janis Born committed
82

83 84 85 86 87 88 89 90 91 92 93
    // remember the previously bound texture
    GLint prevTexture;
    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
94 95 96
    // determine the required buffer size to hold the requested LOD level
    bind();
    GLint width, height, depth;
97
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_WIDTH,  &width);
Janis Born's avatar
Janis Born committed
98
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_HEIGHT, &height);
99
    glGetTexLevelParameteriv(mTarget, _lod, GL_TEXTURE_DEPTH,  &depth);
Janis Born's avatar
Janis Born committed
100 101

    // fetch the image data
102 103 104 105 106
    int channels = 4;
    if (mFormat == GL_RGB) channels = 3; // TODO: check the other channel types

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

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

Janis Born's avatar
Janis Born committed
111 112 113 114 115 116
    // 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);
117
    dataObject->setFormat(GL_RGB);
Janis Born's avatar
Janis Born committed
118 119 120 121
    dataObject->setData(imageData); // dataObject will take care of freeing imageData

    return dataObject;
}