Example usage for org.lwjgl.opengl GL13 GL_TEXTURE_COMPRESSION_HINT

List of usage examples for org.lwjgl.opengl GL13 GL_TEXTURE_COMPRESSION_HINT

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL13 GL_TEXTURE_COMPRESSION_HINT.

Prototype

int GL_TEXTURE_COMPRESSION_HINT

To view the source code for org.lwjgl.opengl GL13 GL_TEXTURE_COMPRESSION_HINT.

Click Source Link

Document

Accepted by the target parameter of Hint and the value parameter of GetIntegerv, GetBooleanv, GetFloatv, and GetDoublev.

Usage

From source file:illarion.graphics.lwjgl.TextureAtlasLWJGL.java

License:Open Source License

/**
 * Activate the texture and prepare it for usage by OpenGL.
 * /*from  w  w w .  j  a  va2  s . c  o  m*/
 * @param resizeable true in case the texture shall be loaded with advanced
 *            rescaling methods, that are more calculation intensive but
 *            look better then the normal ones
 * @param allowCompression true if the texture is compressed at default
 *            settings, false if not. Best disallow compression for static
 *            images such as tiles, since the effects of the compression
 *            will be quite visible there
 */
@Override
@SuppressWarnings("nls")
public void activateTexture(final boolean resizeable, final boolean allowCompression) {

    if (!hasTextureData()) {
        throw new IllegalStateException("No texturedata loaded");
    }

    final int quality = Graphics.getInstance().getQuality();

    if (getTextureID() != 0) {
        removeTexture();
    }

    // generate new texture ID
    final int texID = getNewTextureID();
    setTextureID(texID);

    // bind texture ID
    DriverSettingsLWJGL.getInstance().enableTexture(texID);

    // prepare texture data
    if (resizeable) { // Textures will be resized -> smoothing would be good
        if (quality <= Graphics.QUALITY_LOW) {
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        } else if ((quality <= Graphics.QUALITY_NORMAL) || isNoMipMaps()) {
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        } else {
            if (GLContext.getCapabilities().OpenGL14) {
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
            } else {
                setNoMipMaps(true);
            }
            if (!isNoMipMaps()) {
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
                        GL11.GL_LINEAR_MIPMAP_LINEAR);
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
            } else {
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
            }
        }
    } else {
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
    }
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);

    if (GLContext.getCapabilities().OpenGL13) {
        GL11.glHint(GL13.GL_TEXTURE_COMPRESSION_HINT, GL11.GL_NICEST);
    }

    // setup texture compression
    final boolean activateCompression = GLContext.getCapabilities().OpenGL13
            && ((allowCompression && (quality < Graphics.QUALITY_MAX)) || (quality <= Graphics.QUALITY_LOW));
    if (isTextureRGBA()) {
        internalFormat = GL11.GL_RGBA;
        sourceFormat = GL11.GL_RGBA;
        if (activateCompression) {
            internalFormat = GL13.GL_COMPRESSED_RGBA;
        }
    } else if (isTextureRGB()) {
        internalFormat = GL11.GL_RGB;
        sourceFormat = GL11.GL_RGB;
        if (activateCompression) {
            internalFormat = GL13.GL_COMPRESSED_RGB;
        }
    } else if (isTextureGrey()) {
        internalFormat = GL11.GL_LUMINANCE;
        sourceFormat = GL11.GL_LUMINANCE;
        if (activateCompression) {
            internalFormat = GL13.GL_COMPRESSED_LUMINANCE;
        }
    } else if (isTextureGreyAlpha()) {
        internalFormat = GL11.GL_LUMINANCE_ALPHA;
        sourceFormat = GL11.GL_LUMINANCE_ALPHA;
        if (activateCompression) {
            internalFormat = GL13.GL_COMPRESSED_LUMINANCE_ALPHA;
        }
    }

    final ByteBuffer texData = getTextureData();

    final int texWidth = getTextureWidth();
    final int texHeight = getTextureHeight();

    // produce a texture from the byte buffer
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, internalFormat, texWidth, texHeight, 0, sourceFormat,
            GL11.GL_UNSIGNED_BYTE, texData);

    if (quality < Graphics.QUALITY_MAX) {
        texIDBuffer.rewind();
        GL11.glGetTexLevelParameter(GL11.GL_TEXTURE_2D, 0, GL13.GL_TEXTURE_COMPRESSED, texIDBuffer);

        texData.rewind();
        if (texIDBuffer.get(0) == GL11.GL_FALSE) {
            int newInternalFormat = internalFormat;
            if (internalFormat == GL13.GL_COMPRESSED_LUMINANCE_ALPHA) {
                newInternalFormat = GL13.GL_COMPRESSED_RGBA;
            } else if (internalFormat == GL13.GL_COMPRESSED_LUMINANCE) {
                newInternalFormat = GL13.GL_COMPRESSED_RGB;
            }
            final int orgSize = texData.remaining();
            if (newInternalFormat != internalFormat) {
                GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, newInternalFormat, texWidth, texHeight, 0,
                        sourceFormat, GL11.GL_UNSIGNED_BYTE, texData);
                GL11.glGetTexLevelParameter(GL11.GL_TEXTURE_2D, 0, GL13.GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
                        texIDBuffer);
                final int newSize = texIDBuffer.get(0);
                if (newSize > orgSize) {
                    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, internalFormat, texWidth, texHeight, 0,
                            sourceFormat, GL11.GL_UNSIGNED_BYTE, texData);
                }
            }
        }
    }

    texIDBuffer.rewind();
    GL11.glGetTexLevelParameter(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_INTERNAL_FORMAT, texIDBuffer);
    internalFormat = texIDBuffer.get();

    discardImageData();
}