Example usage for org.lwjgl.opengl GL11 glBindTexture

List of usage examples for org.lwjgl.opengl GL11 glBindTexture

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL11 glBindTexture.

Prototype

public static void glBindTexture(@NativeType("GLenum") int target, @NativeType("GLuint") int texture) 

Source Link

Document

Binds the a texture to a texture target.

Usage

From source file:com.adavr.player.globjects.Texture.java

License:Open Source License

public static Texture create(int internalFormat, int width, int height, int format, int type,
        ByteBuffer pixels) {//from  ww  w.  j  ava2 s . c om
    int id = GL11.glGenTextures();
    int target = GL11.GL_TEXTURE_2D;

    GL11.glBindTexture(target, id);

    GL11.glTexImage2D(target, 0, internalFormat, width, height, 0, format, type, pixels);

    GL11.glTexParameteri(target, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    GL11.glTexParameteri(target, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

    GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);

    return new Texture(id, target);
}

From source file:com.ardor3d.scene.state.lwjgl.LwjglTextureStateUtil.java

License:Open Source License

public static void apply(final TextureState state) {
    // ask for the current state record
    final RenderContext context = ContextManager.getCurrentContext();
    final ContextCapabilities caps = context.getCapabilities();
    final TextureStateRecord record = (TextureStateRecord) context.getStateRecord(StateType.Texture);
    context.setCurrentState(StateType.Texture, state);

    if (state.isEnabled()) {

        Texture texture;//from  ww  w  .  j av  a 2  s  .  c o m
        Texture.Type type;
        TextureUnitRecord unitRecord;
        TextureRecord texRecord;

        final int glHint = LwjglTextureUtil.getPerspHint(state.getCorrectionType());
        if (!record.isValid() || record.hint != glHint) {
            // set up correction mode
            GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, glHint);
            record.hint = glHint;
        }

        // loop through all available texture units...
        for (int i = 0; i < caps.getNumberOfTotalTextureUnits(); i++) {
            unitRecord = record.units[i];

            // grab a texture for this unit, if available
            texture = state.getTexture(i);

            // pull our texture id for this texture, for this context.
            int textureId = texture != null ? texture.getTextureIdForContext(context.getGlContextRep()) : 0;

            // check for invalid textures - ones that have no opengl id and
            // no image data
            if (texture != null && textureId == 0 && texture.getImage() == null) {
                texture = null;
            }

            // null textures above fixed limit do not need to be disabled
            // since they are not really part of the pipeline.
            if (texture == null) {
                if (i >= caps.getNumberOfFixedTextureUnits()) {
                    continue;
                } else {
                    // a null texture indicates no texturing at this unit
                    // Disable texturing on this unit if enabled.
                    disableTexturing(unitRecord, record, i, caps);

                    if (i < state._keyCache.length) {
                        state._keyCache[i] = null;
                    }

                    // next texture!
                    continue;
                }
            }

            type = texture.getType();

            // disable other texturing types for this unit, if enabled.
            disableTexturing(unitRecord, record, i, type, caps);

            // Time to bind the texture, so see if we need to load in image
            // data for this texture.
            if (textureId == 0) {
                // texture not yet loaded.
                // this will load and bind and set the records...
                load(texture, i);
                textureId = texture.getTextureIdForContext(context.getGlContextRep());
                if (textureId == 0) {
                    continue;
                }
            } else if (texture.isDirty(context.getGlContextRep())) {
                update(texture, i);
                textureId = texture.getTextureIdForContext(context.getGlContextRep());
                if (textureId == 0) {
                    continue;
                }
            } else {
                // texture already exists in OpenGL, just bind it if needed
                if (!unitRecord.isValid() || unitRecord.boundTexture != textureId) {
                    checkAndSetUnit(i, record, caps);
                    GL11.glBindTexture(getGLType(type), textureId);
                    if (Constants.stats) {
                        StatCollector.addStat(StatType.STAT_TEXTURE_BINDS, 1);
                    }
                    unitRecord.boundTexture = textureId;
                }
            }

            // Use the Java Integer object for the getTextureRecord call to avoid
            // boxing/unboxing ints for map lookups.
            final Integer textureIdInteger = texture.getTextureIdForContextAsInteger(context.getGlContextRep());

            // Grab our record for this texture
            texRecord = record.getTextureRecord(textureIdInteger, texture.getType());

            // Set the keyCache value for this unit of this texture state
            // This is done so during state comparison we don't have to
            // spend a lot of time pulling out classes and finding field
            // data.
            state._keyCache[i] = texture.getTextureKey();

            // Some texture things only apply to fixed function pipeline
            if (i < caps.getNumberOfFixedTextureUnits()) {

                // Enable 2D texturing on this unit if not enabled.
                if (!unitRecord.isValid() || !unitRecord.enabled[type.ordinal()]) {
                    checkAndSetUnit(i, record, caps);
                    GL11.glEnable(getGLType(type));
                    unitRecord.enabled[type.ordinal()] = true;
                }

                // Set our blend color, if needed.
                applyBlendColor(texture, unitRecord, i, record, caps);

                // Set the texture environment mode if this unit isn't
                // already set properly
                applyEnvMode(texture.getApply(), unitRecord, i, record, caps);

                // If our mode is combine, and we support multitexturing
                // apply combine settings.
                if (texture.getApply() == ApplyMode.Combine && caps.isMultitextureSupported()
                        && caps.isEnvCombineSupported()) {
                    applyCombineFactors(texture, unitRecord, i, record, caps);
                }
            }

            // Other items only apply to textures below the frag unit limit
            if (i < caps.getNumberOfFragmentTextureUnits()) {

                // texture specific params
                applyFilter(texture, texRecord, i, record, caps);
                applyWrap(texture, texRecord, i, record, caps);
                applyShadow(texture, texRecord, i, record, caps);

                // Set our border color, if needed.
                applyBorderColor(texture, texRecord, i, record);

                // all states have now been applied for a tex record, so we
                // can safely make it valid
                if (!texRecord.isValid()) {
                    texRecord.validate();
                }

            }

            // Other items only apply to textures below the frag tex coord
            // unit limit
            if (i < caps.getNumberOfFragmentTexCoordUnits()) {

                // Now time to play with texture matrices
                // Determine which transforms to do.
                applyTextureTransforms(texture, i, record, caps);

                // Now let's look at automatic texture coordinate
                // generation.
                applyTexCoordGeneration(texture, unitRecord, i, record, caps);

                // Set our texture lod bias, if needed.
                applyLodBias(texture, unitRecord, i, record, caps);
            }

        }

    } else {
        // turn off texturing
        TextureUnitRecord unitRecord;

        if (caps.isMultitextureSupported()) {
            for (int i = 0; i < caps.getNumberOfFixedTextureUnits(); i++) {
                unitRecord = record.units[i];
                disableTexturing(unitRecord, record, i, caps);
            }
        } else {
            unitRecord = record.units[0];
            disableTexturing(unitRecord, record, 0, caps);
        }
    }

    if (!record.isValid()) {
        record.validate();
    }
}

From source file:com.ardor3d.scene.state.lwjgl.LwjglTextureStateUtil.java

License:Open Source License

/**
 * Useful for external lwjgl based classes that need to safely set the current texture.
 *//* www.j av a2  s  . c o  m*/
public static void doTextureBind(final Texture texture, final int unit, final boolean invalidateState) {
    // ask for the current state record
    final RenderContext context = ContextManager.getCurrentContext();
    final ContextCapabilities caps = context.getCapabilities();
    final TextureStateRecord record = (TextureStateRecord) context.getStateRecord(StateType.Texture);
    if (invalidateState) {
        // Set this to null because no current state really matches anymore
        context.setCurrentState(StateType.Texture, null);
    }
    checkAndSetUnit(unit, record, caps);

    final int id = texture.getTextureIdForContext(context.getGlContextRep());
    GL11.glBindTexture(getGLType(texture.getType()), id);
    if (Constants.stats) {
        StatCollector.addStat(StatType.STAT_TEXTURE_BINDS, 1);
    }
    if (record != null) {
        record.units[unit].boundTexture = id;
    }
}

From source file:com.ardor3d.util.lwjgl.LwjglTextureUpdater.java

License:Open Source License

public static void updateTexture(final Texture texture, final ByteBuffer data, final int w, final int h,
        final Format format) {
    final int dataFormat = LwjglTextureUtil.getGLDataFormat(format);
    final int pixelFormat = LwjglTextureUtil.getGLPixelFormat(format);

    idBuff.clear();//from   w  w w .  j ava 2 s .com
    GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D, idBuff);
    final int oldTex = idBuff.get();

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureId());
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    if (glTexSubImage2DSupported) {
        GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, w, h, pixelFormat, GL11.GL_UNSIGNED_BYTE, data);

        try {
            Util.checkGLError();
        } catch (final OpenGLException e) {
            glTexSubImage2DSupported = false;
            updateTexture(texture, data, w, h, format);
        }
    } else {
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, dataFormat, w, h, 0, pixelFormat, GL11.GL_UNSIGNED_BYTE, data);
    }

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, oldTex);
}

From source file:com.auroraengine.opengl.GLTexture.java

License:Open Source License

@Override
public void update() throws GLException {
    if (tex_ref != 0) {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex_ref);

        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, min_filter);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, mag_filter);

        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, wrap_s);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, wrap_t);

        if (image_modified) {
            ByteBuffer bb = ByteBuffer.allocateDirect(4 * img.getWidth() * img.getHeight());
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, img.getWidth(), img.getHeight(), 0,
                    GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, bb);
            // TODO: Redirect images to buffers
        }/*from   w w  w.ja va 2s  .c  o m*/
    }
}

From source file:com.badlogic.gdx.backends.jglfw.JglfwGL20.java

License:Apache License

public void glBindTexture(int target, int texture) {
    GL11.glBindTexture(target, texture);
}

From source file:com.badlogic.gdx.backends.lwjgl.LwjglGL10.java

License:Apache License

public final void glBindTexture(int target, int texture) {
    GL11.glBindTexture(target, texture);
}

From source file:com.damagedearth.Utilities.Components.TrueTypeFont.java

License:Open Source License

public static int loadImage(BufferedImage bufferedImage) {
    try {/*from www  .  ja v a 2s .c  om*/
        short width = (short) bufferedImage.getWidth();
        short height = (short) bufferedImage.getHeight();
        //textureLoader.bpp = bufferedImage.getColorModel().hasAlpha() ? (byte)32 : (byte)24;
        int bpp = (byte) bufferedImage.getColorModel().getPixelSize();
        ByteBuffer byteBuffer;
        DataBuffer db = bufferedImage.getData().getDataBuffer();
        if (db instanceof DataBufferInt) {
            int intI[] = ((DataBufferInt) (bufferedImage.getData().getDataBuffer())).getData();
            byte newI[] = new byte[intI.length * 4];
            for (int i = 0; i < intI.length; i++) {
                byte b[] = intToByteArray(intI[i]);
                int newIndex = i * 4;

                newI[newIndex] = b[1];
                newI[newIndex + 1] = b[2];
                newI[newIndex + 2] = b[3];
                newI[newIndex + 3] = b[0];
            }

            byteBuffer = ByteBuffer.allocateDirect(width * height * (bpp / 8)).order(ByteOrder.nativeOrder())
                    .put(newI);
        } else {
            byteBuffer = ByteBuffer.allocateDirect(width * height * (bpp / 8)).order(ByteOrder.nativeOrder())
                    .put(((DataBufferByte) (bufferedImage.getData().getDataBuffer())).getData());
        }
        byteBuffer.flip();

        int internalFormat = GL11.GL_RGBA8, format = GL11.GL_RGBA;
        IntBuffer textureId = BufferUtils.createIntBuffer(1);
        ;
        GL11.glGenTextures(textureId);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId.get(0));

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

        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);

        GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);

        GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, internalFormat, width, height, format, GL11.GL_UNSIGNED_BYTE,
                byteBuffer);
        return textureId.get(0);

    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }

    return -1;
}

From source file:com.damagedearth.Utilities.Components.TrueTypeFont.java

License:Open Source License

public void drawString(float x, float y, String whatchars, int startIndex, int endIndex, float scaleX,
        float scaleY, int format) {

    IntObject intObject = null;/*from   w  ww . j av  a2s. co m*/
    int charCurrent;

    int totalwidth = 0;
    int i = startIndex, d, c;
    float startY = 0;

    switch (format) {
    case ALIGN_RIGHT: {
        d = -1;
        c = correctR;

        while (i < endIndex) {
            if (whatchars.charAt(i) == '\n')
                startY -= fontHeight;
            i++;
        }
        break;
    }
    case ALIGN_CENTER: {
        for (int l = startIndex; l <= endIndex; l++) {
            charCurrent = whatchars.charAt(l);
            if (charCurrent == '\n')
                break;
            if (charCurrent < 256) {
                intObject = charArray[charCurrent];
            } else {
                intObject = (IntObject) customChars.get(new Character((char) charCurrent));
            }
            totalwidth += intObject.width - correctL;
        }
        totalwidth /= -2;
    }
    case ALIGN_LEFT:
    default: {
        d = 1;
        c = correctL;
        break;
    }

    }

    GL11.glEnable(GL11.GL_BLEND); // Enabled blending
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); // selects blending method
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, fontTextureID);
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glBegin(GL11.GL_QUADS);

    while (i >= startIndex && i <= endIndex) {

        charCurrent = whatchars.charAt(i);
        if (charCurrent < 256) {
            intObject = charArray[charCurrent];
        } else {
            intObject = (IntObject) customChars.get(new Character((char) charCurrent));
        }

        if (intObject != null) {
            if (d < 0)
                totalwidth += (intObject.width - c) * d;
            if (charCurrent == '\n') {
                startY -= fontHeight * d;
                totalwidth = 0;
                if (format == ALIGN_CENTER) {
                    for (int l = i + 1; l <= endIndex; l++) {
                        charCurrent = whatchars.charAt(l);
                        if (charCurrent == '\n')
                            break;
                        if (charCurrent < 256) {
                            intObject = charArray[charCurrent];
                        } else {
                            intObject = (IntObject) customChars.get(new Character((char) charCurrent));
                        }
                        totalwidth += intObject.width - correctL;
                    }
                    totalwidth /= -2;
                }
                //if center get next lines total width/2;
            } else {
                drawQuad((totalwidth + intObject.width) * scaleX + x, startY * scaleY + y,
                        totalwidth * scaleX + x, (startY + intObject.height) * scaleY + y,
                        intObject.storedX + intObject.width, intObject.storedY + intObject.height,
                        intObject.storedX, intObject.storedY);
                if (d > 0)
                    totalwidth += (intObject.width - c) * d;
            }
            i += d;

        }
    }
    GL11.glEnd();

    GL11.glDisable(GL11.GL_TEXTURE_2D);
    GL11.glDisable(GL11.GL_BLEND);
}

From source file:com.damagedearth.Utilities.Components.TrueTypeFont.java

License:Open Source License

public void destroy() {
    IntBuffer scratch = BufferUtils.createIntBuffer(1);
    scratch.put(0, fontTextureID);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    GL11.glDeleteTextures(scratch);
}