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.opengrave.og.resources.TextureEditable.java

License:Open Source License

@Override
public void bind(int t) {
    GL13.glActiveTexture(t);//from w  ww. j a  va2 s  . c o m
    synchronized (changeList) {
        if (changeList.size() < 10) {
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);

            for (TextureEditableDeferedChanges change : changeList) {
                applyChangeToMainBuffer(change);
                ByteBuffer buf = BufferUtils.createByteBuffer(4);
                buf.put((byte) (255 * change.getColour().z)).put((byte) (255 * change.getColour().y))
                        .put((byte) (255 * change.getColour().x)).put((byte) (255 * change.getColour().w));
                buf.flip();
                GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, change.getX(), change.getY(), 1, 1, GL12.GL_BGRA,
                        GL11.GL_UNSIGNED_BYTE, buf);
            }
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        } else {
            // Replace the whole image. There's bound to be better ways
            // around this...
            for (TextureEditableDeferedChanges change : changeList) {
                applyChangeToMainBuffer(change);
            }
            mainBuf.position(0);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
            Util.checkErr();
            GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, size, size, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE,
                    mainBuf);
            Util.checkErr();
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
            Util.checkErr();

        }
        changeList.clear();
    }
    Util.checkErr();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
    lastTexNum = t;
}

From source file:com.opengrave.og.resources.TextureEditable.java

License:Open Source License

@Override
public void unbind() {
    GL13.glActiveTexture(lastTexNum);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
}

From source file:com.owens.oobjloader.lwjgl.TextureLoader.java

License:BSD License

public int convertToTexture(BufferedImage img) {
    int[] pixels = new int[img.getWidth() * img.getHeight()];
    PixelGrabber grabber = new PixelGrabber(img, 0, 0, img.getWidth(), img.getHeight(), pixels, 0,
            img.getWidth());// w ww  . jav  a 2s. c om
    try {
        grabber.grabPixels();
    } catch (InterruptedException e) {
        log.log(SEVERE, "InterruptedException while trying to grab pixels, e=" + e);
        e.printStackTrace();
        return -1;
    }

    int bufLen = 0;
    bufLen = pixels.length * 4;

    ByteBuffer oglPixelBuf = BufferUtils.createByteBuffer(bufLen);

    for (int y = img.getHeight() - 1; y >= 0; y--) {
        for (int x = 0; x < img.getWidth(); x++) {
            int pixel = pixels[y * img.getWidth() + x];
            oglPixelBuf.put((byte) ((pixel >> 16) & 0xFF));
            oglPixelBuf.put((byte) ((pixel >> 8) & 0xFF));
            oglPixelBuf.put((byte) ((pixel >> 0) & 0xFF));
            oglPixelBuf.put((byte) ((pixel >> 24) & 0xFF));
        }
    }

    oglPixelBuf.flip();

    ByteBuffer temp = ByteBuffer.allocateDirect(4);
    temp.order(ByteOrder.nativeOrder());
    IntBuffer textBuf = temp.asIntBuffer();
    GL11.glGenTextures(textBuf);
    int textureID = textBuf.get(0);

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
    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);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
    GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);

    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, TEXTURE_LEVEL, GL11.GL_RGBA8, img.getWidth(), img.getHeight(), 0,
            GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, oglPixelBuf);

    return textureID;
}

From source file:com.owens.oobjloader.lwjgl.VBO.java

License:BSD License

public void render() {

    GL11.glEnable(GL11.GL_TEXTURE_2D);//from  ww w.  j  a  va 2 s.c  om
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textId); // Bind The Texture

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, verticeAttributesID);

    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glVertexPointer(3, GL11.GL_FLOAT, ATTR_V_STRIDE2_BYTES, ATTR_V_OFFSET_BYTES);

    GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
    GL11.glNormalPointer(GL11.GL_FLOAT, ATTR_N_STRIDE2_BYTES, ATTR_N_OFFSET_BYTES);

    GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
    GL11.glTexCoordPointer(2, GL11.GL_FLOAT, ATTR_T_STRIDE2_BYTES, ATTR_T_OFFSET_BYTES);

    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesID);
    GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount, GL11.GL_UNSIGNED_INT, 0);

    GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
    GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
    GL11.glDisable(GL11.GL_TEXTURE_2D);
}

From source file:com.pahimar.ee3.core.handlers.DrawBlockHighlightHandler.java

License:LGPL

public static void renderPulsingQuad(int texture, float maxTransparency) {

    float pulseTransparency = getPulseValue() * maxTransparency / 3000f;

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
    Tessellator tessellator = Tessellator.instance;

    GL11.glEnable(GL12.GL_RESCALE_NORMAL);
    GL11.glEnable(GL11.GL_BLEND);//from   w w w .java  2 s.c o m
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    GL11.glColor4f(1, 1, 1, pulseTransparency);

    tessellator.startDrawingQuads();
    tessellator.setColorRGBA_F(1, 1, 1, pulseTransparency);

    tessellator.addVertexWithUV(-0.5D, 0.5D, 0F, 0, 1);
    tessellator.addVertexWithUV(0.5D, 0.5D, 0F, 1, 1);
    tessellator.addVertexWithUV(0.5D, -0.5D, 0F, 1, 0);
    tessellator.addVertexWithUV(-0.5D, -0.5D, 0F, 0, 0);

    tessellator.draw();
    GL11.glDisable(GL11.GL_BLEND);
    GL11.glDisable(GL12.GL_RESCALE_NORMAL);
}

From source file:com.redthirddivision.quad.rendering.materials.Texture.java

License:Apache License

public void bind(int samplerSlot) {
    try {/*from   w  w  w.j  av  a 2 s  .c o m*/
        assert (samplerSlot >= 0 && samplerSlot <= 31);
        GL13.glActiveTexture(GL13.GL_TEXTURE0 + samplerSlot);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, manager.getID());
    } catch (Exception e) {
        System.err.println("Could not bind texture: " + file);
        System.exit(1);
    }
}

From source file:com.redthirddivision.quad.rendering.materials.Texture.java

License:Apache License

private TextureManager loadTexture(String path, String root) {

    //        String[] splitArray = path.split("\\.");
    //        String extension = splitArray[splitArray.length - 1];
    File file = null;// www .  j  a v a2s  . c  o  m
    BufferedImage image = null;
    System.out.println("Attempting to load a new texture: <" + root + "/" + path + ">");
    file = new File(root + "/" + path + ".png");
    try {
        image = ImageIO.read(file);
    } catch (IOException e) {
        System.err.println("Could not find texture: <" + root + "/" + path + ">");
        return textures.get(MISSING_TEXTURE_FILE);
    }
    try {
        int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
        ByteBuffer buffer = Util.createByteBuffer(image.getWidth() * image.getHeight() * 4);
        boolean hasAlpha = image.getColorModel().hasAlpha();
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                int pixel = pixels[y * image.getWidth() + x];
                buffer.put((byte) ((pixel >> 16) & 0xFF));
                buffer.put((byte) ((pixel >> 8) & 0xFF));
                buffer.put((byte) ((pixel) & 0xFF));
                if (hasAlpha)
                    buffer.put((byte) ((pixel >> 24) & 0xFF));
                else
                    buffer.put((byte) 0xFF);
            }
        }
        buffer.flip();
        System.out.println("Succesfully loaded texture from file: <" + file.getPath() + ">");

        TextureManager result = new TextureManager();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, result.getID());

        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, image.getWidth(), image.getHeight(), 0,
                GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
        return result;
    } catch (Exception e) {
        System.err.println("Could not load texture: <" + path + ".png>");
        System.exit(1);
    }
    return null;

}

From source file:com.redthirddivision.quad.rendering.renderers.EntityRenderer.java

License:Apache License

protected void prepare(Model model) {
    GL30.glBindVertexArray(model.getVaoID());
    GL20.glEnableVertexAttribArray(0);//from  w w  w.ja v  a 2 s .c  om
    GL20.glEnableVertexAttribArray(1);
    GL20.glEnableVertexAttribArray(2);

    Material material = model.getMaterial();
    shader.loadNumRows(material.getNumRows());
    if (material.hasTransparency())
        Util.disableCulling();
    shader.loadFakeLighting(material.isUseingFakeLighting());
    shader.loadShine(material.getShineDamper(), material.getReflectivity());

    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getMaterial().getTextureID());
}

From source file:com.redthirddivision.quad.rendering.renderers.GuiRenderer.java

License:Apache License

@Override
public void render(ArrayList<GuiTexture> elements) {
    shader.start();/*from w ww.j a v  a  2  s . co m*/

    GL30.glBindVertexArray(quad.getVaoID());
    GL20.glEnableVertexAttribArray(0);
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    GL11.glDisable(GL11.GL_DEPTH_TEST);
    for (GuiTexture gui : elements) {
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, gui.getTexture());
        Matrix4f matrix = MathHelper.createTransformationMatrix(gui.getPosition(), gui.getScale(),
                new Vector2f(0, 0));
        shader.loadTransformation(matrix);
        GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, quad.getVertexCount());
    }

    GL11.glDisable(GL11.GL_BLEND);
    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL20.glDisableVertexAttribArray(0);
    GL30.glBindVertexArray(0);

    shader.stop();
}

From source file:com.redthirddivision.quad.rendering.renderers.TerrainRenderer.java

License:Apache License

private void bindTextures(Terrain terrain) {
    TerrainTexturePack texturePack = terrain.getTexturePack();
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getBackgroundTexture().getTextureID());

    GL13.glActiveTexture(GL13.GL_TEXTURE1);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getrTexture().getTextureID());

    GL13.glActiveTexture(GL13.GL_TEXTURE2);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getgTexture().getTextureID());

    GL13.glActiveTexture(GL13.GL_TEXTURE3);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getbTexture().getTextureID());

    GL13.glActiveTexture(GL13.GL_TEXTURE4);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, terrain.getBlendMap().getTextureID());
}