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:mwisbest.openbase.opengl.TextureLoader.java

License:Open Source License

private TextureImplementation getTexture(InputStream in, String resourceName, int target, int minFilter,
        int magFilter, boolean flipped, int[] transparent) throws IOException {
    // create the texture ID for this texture
    ByteBuffer textureBuffer;// w w  w.ja v  a 2  s .  co m

    LoadableImageData imageData = ImageDataFactory.getImageDataFor(resourceName);
    textureBuffer = imageData.loadImage(new BufferedInputStream(in), flipped, transparent);

    int textureID = createTextureID();
    TextureImplementation texture = new TextureImplementation(resourceName, target, textureID);
    // bind this texture
    GL11.glEnable(target);
    GL11.glBindTexture(target, textureID);

    int width;
    int height;
    int texWidth;
    int texHeight;

    ImageData.Format format;

    width = imageData.getWidth();
    height = imageData.getHeight();
    format = imageData.getFormat();

    texture.setTextureWidth(imageData.getTexWidth());
    texture.setTextureHeight(imageData.getTexHeight());

    texWidth = texture.getTextureWidth();
    texHeight = texture.getTextureHeight();

    IntBuffer temp = BufferUtils.createIntBuffer(16);
    GL11.glGetInteger(GL11.GL_MAX_TEXTURE_SIZE, temp);
    int max = temp.get(0);
    if (texWidth > max || texHeight > max)
        throw new IOException("Attempt to allocate a texture to big for the current hardware");

    int srcPixelFormat = format.getOGLType();

    texture.setWidth(width);
    texture.setHeight(height);
    texture.setImageFormat(format);

    GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
    GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);

    ContextCapabilities capabilities = GLContext.getCapabilities();

    if (capabilities.OpenGL30 || capabilities.GL_EXT_framebuffer_object
            || capabilities.GL_ARB_framebuffer_object || capabilities.OpenGL14) {
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST_MIPMAP_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LOD, GL11.GL_POLYGON_BIT);
        if (capabilities.OpenGL30)
            GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
        else if (capabilities.GL_EXT_framebuffer_object)
            EXTFramebufferObject.glGenerateMipmapEXT(GL11.GL_TEXTURE_2D);
        else if (capabilities.GL_ARB_framebuffer_object)
            ARBFramebufferObject.glGenerateMipmap(GL11.GL_TEXTURE_2D);
        else if (capabilities.OpenGL14)
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
    }

    // produce a texture from the byte buffer
    GL11.glTexImage2D(target, 0, this.dstPixelFormat, get2Fold(width), get2Fold(height), 0, srcPixelFormat,
            GL11.GL_UNSIGNED_BYTE, textureBuffer);
    return texture;
}

From source file:mwisbest.openbase.opengl.TextureLoader.java

License:Open Source License

/**
 * Get a texture from an image file./* w  w w.j a  v  a 2 s  .c  om*/
 * 
 * @param dataSource The image data to generate the texture from
 * @param filter The filter to use when scaling the texture
 * @return The texture created
 * @throws IOException Indicates the texture is too big for the hardware
 */
public Texture getTexture(ImageData dataSource, int filter) throws IOException {
    int target = GL11.GL_TEXTURE_2D;

    ByteBuffer textureBuffer;
    textureBuffer = dataSource.getImageBufferData();

    // create the texture ID for this texture
    int textureID = createTextureID();
    TextureImplementation texture = new TextureImplementation("generated:" + dataSource, target, textureID);

    int minFilter = filter;
    int magFilter = filter;

    // bind this texture
    GL11.glEnable(target);
    GL11.glBindTexture(target, textureID);

    int width;
    int height;
    int texWidth;
    int texHeight;

    ImageData.Format format;

    width = dataSource.getWidth();
    height = dataSource.getHeight();
    format = dataSource.getFormat();

    texture.setTextureWidth(dataSource.getTexWidth());
    texture.setTextureHeight(dataSource.getTexHeight());

    texWidth = texture.getTextureWidth();
    texHeight = texture.getTextureHeight();

    int srcPixelFormat = format.getOGLType();

    texture.setWidth(width);
    texture.setHeight(height);
    texture.setImageFormat(format);

    IntBuffer temp = BufferUtils.createIntBuffer(16);
    GL11.glGetInteger(GL11.GL_MAX_TEXTURE_SIZE, temp);
    int max = temp.get(0);
    if (texWidth > max || texHeight > max)
        throw new IOException("Attempt to allocate a texture to big for the current hardware");

    GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
    GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);

    // produce a texture from the byte buffer
    GL11.glTexImage2D(target, 0, this.dstPixelFormat, get2Fold(width), get2Fold(height), 0, srcPixelFormat,
            GL11.GL_UNSIGNED_BYTE, textureBuffer);
    return texture;
}

From source file:mwisbest.openbase.opengl.TextureLoader.java

License:Open Source License

/**
 * Reload a given texture blob; used internally with setHoldTextureData. Call TextureImpl.reload instead.
 * // www  .  j  av a 2  s.  c o m
 * @param texture The texture being reloaded
 * @param srcPixelFormat The source pixel format
 * @param componentCount The component count
 * @param minFilter The minification filter
 * @param magFilter The magnification filter
 * @param textureBuffer The pixel data
 * @return The ID of the newly created texture
 */
public int reload(TextureImplementation texture, int srcPixelFormat, int componentCount, int minFilter,
        int magFilter, ByteBuffer textureBuffer) {
    int target = GL11.GL_TEXTURE_2D;
    int textureID = createTextureID();
    GL11.glEnable(target);
    GL11.glBindTexture(target, textureID);

    GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
    GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);

    // produce a texture from the byte buffer
    GL11.glTexImage2D(target, 0, this.dstPixelFormat, texture.getTextureWidth(), texture.getTextureHeight(), 0,
            srcPixelFormat, GL11.GL_UNSIGNED_BYTE, textureBuffer);
    return textureID;
}

From source file:mwisbest.openbase.opengl.UtilsGL.java

License:Open Source License

/**
 * Draws the specified texture to positions x and y on the screen and scaled with the specified width and height
 * //from  www . j  a  v a2 s. c om
 * @param texture
 * @param x
 * @param y
 * @param width
 * @param height
 */
public static void drawTexture(Texture texture, int x, int y, int width, int height) {
    GL11.glPushMatrix();
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    GL11.glDepthMask(false);
    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    if (Common.currentTextureID != texture.getTextureID()) {
        int texID = texture.getTextureID();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texID);
        Common.currentTextureID = texID;
    }
    GL11.glTranslatef(x, y, 0);
    GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0, 0);
    GL11.glVertex2f(0, 0);
    GL11.glTexCoord2f(0, texture.getHeight());
    GL11.glVertex2f(0, height);
    GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
    GL11.glVertex2f(width, height);
    GL11.glTexCoord2f(texture.getWidth(), 0);
    GL11.glVertex2f(width, 0);
    GL11.glEnd();
    GL11.glPopMatrix();
}

From source file:net.betabears.the2dlibrary.graphics.Image.java

/**
 * Binds this image and prepares rendering. Make sure to call
 * glEnable(GL_TEXTURE_2D) before./*from www. j  a v  a2  s  .  c o  m*/
 */
public void bind() {
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
    GL33.glBindSampler(0, samplerID);
}

From source file:net.BiggerOnTheInside.Binder.GameLoop.java

License:Open Source License

private void render2D() {
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    Color.white.bind();

    renderDebug();
}

From source file:net.minecraft.client.gui.fonts.GlyphCache.java

License:Open Source License

/**
 * Allocate a new OpenGL texture for caching pre-rendered glyph images. The
 * new texture is initialized to fully transparent white so the individual
 * glyphs images within can have a transparent border between them. The new
 * texture remains bound after returning from the function.
 *
 * @todo use GL_ALPHA4 if anti-alias is turned off for even smaller textures
 */// w  w  w .  j  a va  2s  . c  o m
private void allocateGlyphCacheTexture() {
    /* Initialize the background to all white but fully transparent. */
    glyphCacheGraphics.clearRect(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT);

    /* Allocate new OpenGL texure */
    singleIntBuffer.clear();
    GLAllocation.generateTextureNames(singleIntBuffer);
    textureName = singleIntBuffer.get(0);

    /* Load imageBuffer with pixel data ready for transfer to OpenGL texture */
    updateImageBuffer(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT);

    /*
     * Initialize texture with the now cleared BufferedImage. Using a texture with GL_ALPHA8 internal format may result in
     * faster rendering since the GPU has to only fetch 1 byte per texel instead of 4 with a regular RGBA texture.
     */
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureName);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_ALPHA8, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, imageBuffer);

    /* Explicitely disable mipmap support becuase updateTexture() will only update the base level 0 */
    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);
}

From source file:net.minecraft.client.gui.GuiAchievement.java

public void updateAchievementWindow() {
    if (Minecraft.hasPaidCheckTime > 0L) {
        GL11.glDisable(2929 /*GL_DEPTH_TEST*/);
        GL11.glDepthMask(false);//from w w w. j av  a2  s  .c  o m
        RenderHelper.disableStandardItemLighting();
        this.updateAchievementWindowScale();
        String var1 = "Minecraft Beta 1.7.3   Unlicensed Copy :(";
        String var2 = "(Or logged in from another location)";
        String var3 = "Purchase at minecraft.net";
        this.theGame.fontRenderer.drawStringWithShadow(var1, 2, 2, 16777215);
        this.theGame.fontRenderer.drawStringWithShadow(var2, 2, 11, 16777215);
        this.theGame.fontRenderer.drawStringWithShadow(var3, 2, 20, 16777215);
        GL11.glDepthMask(true);
        GL11.glEnable(2929 /*GL_DEPTH_TEST*/);
    }

    if (this.theAchievement != null && this.achievementTime != 0L) {
        double var8 = (double) (System.currentTimeMillis() - this.achievementTime) / 3000.0D;
        if (!this.haveAchiement && !this.haveAchiement && (var8 < 0.0D || var8 > 1.0D)) {
            this.achievementTime = 0L;
        } else {
            this.updateAchievementWindowScale();
            GL11.glDisable(2929 /*GL_DEPTH_TEST*/);
            GL11.glDepthMask(false);
            double var9 = var8 * 2.0D;
            if (var9 > 1.0D) {
                var9 = 2.0D - var9;
            }

            var9 *= 4.0D;
            var9 = 1.0D - var9;
            if (var9 < 0.0D) {
                var9 = 0.0D;
            }

            var9 *= var9;
            var9 *= var9;
            int var5 = this.achievementWindowWidth - 160;
            int var6 = 0 - (int) (var9 * 36.0D);
            int var7 = this.theGame.renderEngine.getTexture("/achievement/bg.png");
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            GL11.glEnable(3553 /*GL_TEXTURE_2D*/);
            GL11.glBindTexture(3553 /*GL_TEXTURE_2D*/, var7);
            GL11.glDisable(2896 /*GL_LIGHTING*/);
            this.drawTexturedModalRect(var5, var6, 96, 202, 160, 32);
            if (this.haveAchiement) {
                this.theGame.fontRenderer.drawSplitString(this.achievementStatName, var5 + 30, var6 + 7, 120,
                        -1);
            } else {
                this.theGame.fontRenderer.drawString(this.achievementGetLocalText, var5 + 30, var6 + 7, -256);
                this.theGame.fontRenderer.drawString(this.achievementStatName, var5 + 30, var6 + 18, -1);
            }

            GL11.glPushMatrix();
            GL11.glRotatef(180.0F, 1.0F, 0.0F, 0.0F);
            RenderHelper.enableStandardItemLighting();
            GL11.glPopMatrix();
            GL11.glDisable(2896 /*GL_LIGHTING*/);
            GL11.glEnable('\u803a');
            GL11.glEnable(2903 /*GL_COLOR_MATERIAL*/);
            GL11.glEnable(2896 /*GL_LIGHTING*/);
            this.itemRender.renderItemIntoGUI(this.theGame.fontRenderer, this.theGame.renderEngine,
                    this.theAchievement.theItemStack, var5 + 8, var6 + 8);
            GL11.glDisable(2896 /*GL_LIGHTING*/);
            GL11.glDepthMask(true);
            GL11.glEnable(2929 /*GL_DEPTH_TEST*/);
        }
    }
}

From source file:net.minecraft.src.betterfonts.GlyphCache.java

License:Open Source License

/**
 * Update a portion of the current glyph cache texture using the contents of the glyphCacheImage with glTexSubImage2D().
 *
 * @param dirty The rectangular region in glyphCacheImage that has changed and needs to be copied into the texture
 *
 * @todo Add mip-mapping support here//from w  w  w  .ja va2 s  . c o  m
 * @todo Test with bilinear texture interpolation and possibly add a 1 pixel transparent border around each glyph to avoid
 * bleedover when interpolation is active or add a small "fudge factor" to the UV coordinates like already n FontRenderer
 */
private void updateTexture(Rectangle dirty) {
    /* Only update OpenGL texture if changes were made to the texture */
    if (dirty != null) {
        /* Load imageBuffer with pixel data ready for transfer to OpenGL texture */
        updateImageBuffer(dirty.x, dirty.y, dirty.width, dirty.height);

        /*
        * NOTE: Since the text is drawn in white with full alpha, each pixel is always all 0s or all 1s
        * and there is no need to manually convert from Java's ARGB layout to OpenGLs RGBA. This avoids
        * having to copy into an extra byte array first; instead the int array is copied straight to a
        * direct int buffer.
        */

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureName);
        GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, dirty.x, dirty.y, dirty.width, dirty.height, GL11.GL_RGBA,
                GL11.GL_UNSIGNED_BYTE, imageBuffer);
    }
}

From source file:net.minecraft.src.forge.ForgeHooksClient.java

License:Minecraft Forge Public License

protected static void bindTexture(String texture, int subID) {
    Integer texID = textures.get(texture);
    if (texID == null) {
        texID = ModLoader.getMinecraftInstance().renderEngine.getTexture(texture);
        textures.put(texture, texID);// w  ww .  j ava  2  s . co m
    }
    if (!inWorld) {
        if (unbindContext != null) {
            unbindContext.afterRenderContext();
            unbindContext = null;
        }
        if (Tessellator.instance.isDrawing) {
            int mode = Tessellator.instance.drawMode;
            Tessellator.instance.draw();
            Tessellator.instance.startDrawing(mode);
        }
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texID);
        unbindContext = renderHandlers.get(new TesKey(texID, subID));
        if (unbindContext != null) {
            unbindContext.beforeRenderContext();
        }
        return;
    }
    bindTessellator(texID, subID);
}