Example usage for org.lwjgl.opengl GL11 glTexImage2D

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

Introduction

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

Prototype

public static void glTexImage2D(@NativeType("GLenum") int target, @NativeType("GLint") int level,
        @NativeType("GLint") int internalformat, @NativeType("GLsizei") int width,
        @NativeType("GLsizei") int height, @NativeType("GLint") int border, @NativeType("GLenum") int format,
        @NativeType("GLenum") int type, @Nullable @NativeType("void const *") double[] pixels) 

Source Link

Document

Array version of: #glTexImage2D TexImage2D

Usage

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

License:Open Source License

public static TextureDead create(String location) {
    InputStream in = null;// www  .jav a  2s . co  m
    File f = new File(Resources.cache, location);
    try {
        in = new FileInputStream(f.getAbsolutePath());
    } catch (FileNotFoundException e1) {
        System.out.println("Cannot open file " + f.getAbsolutePath());
        return null;
    }
    PNGDecoder decoder;
    TextureDead textureObject = new TextureDead();
    int texture = -1;
    try {
        decoder = new PNGDecoder(in);
        textureObject.width = decoder.getWidth();
        textureObject.height = decoder.getHeight();
        System.out.println("Width : " + decoder.getWidth() + " Height : " + decoder.getHeight());
        ByteBuffer buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
        buf.flip();
        texture = GL11.glGenTextures();
        textureObject.id = texture;
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
        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.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.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0,
                GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
        // GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
    } catch (IOException e) {
        new DebugExceptionHandler(e, location);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
        }
    }
    return textureObject;

}

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

License:Open Source License

public TextureEditable(int size, float r, float g, float b, float a) {

    this.size = getPowerOfTwo(size);
    id = GL11.glGenTextures();//from w w  w.  ja  va  2  s .  c o  m
    mainBuf = BufferUtils.createByteBuffer(4 * this.size * this.size);
    for (int count = 0; count < this.size * this.size; count++) {
        addColour(r, g, b, a);
    }
    mainBuf.flip();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, this.size, this.size, 0, GL12.GL_BGRA,
            GL11.GL_UNSIGNED_BYTE, mainBuf);
    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.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.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());/* ww w.  ja  va  2s . co m*/
    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.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 . jav  a2  s .  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.runescape.client.revised.editor.modelviewer.Generator.java

License:Open Source License

public void draw() {
    GL11.glBegin(GL11.GL_QUADS);// ww w  .  j a v  a2s  .c o m
    for (int i = 0; i < this.particleCount; i++) {
        if (this.particles[i] != null) {
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, this.decParticle.getWidth(),
                    this.decParticle.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, this.bufParticle);
            GL11.glBindTexture(GL11.GL_ADD, GL11.GL_LOAD);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 1);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
            GL11.glPushMatrix();
            GL11.glTranslated(this.particles[i].x + (Main.getMain().getCanvas().getX() / 2),
                    this.particles[i].y + (Main.getMain().getCanvas().getY() / 2), 0);
            this.particles[i].draw();
            GL11.glPopMatrix();
        }
    }
    GL11.glEnd();
}

From source file:com.sriramramani.droid.inspector.ui.InspectorCanvas.java

License:Mozilla Public License

private int bindTexture(Node node, String imageData) {
    int textureId = GL11.glGenTextures();
    byte[] bitmap = Base64.decodeBase64(imageData);
    try {/*w ww  .  j a v a 2s .c om*/
        PNGDecoder decoder = new PNGDecoder(new ByteArrayInputStream(bitmap));
        int width = decoder.getWidth();
        int height = decoder.getHeight();
        ByteBuffer buffer = ByteBuffer.allocateDirect(4 * width * height);
        decoder.decode(buffer, width * 4, Format.RGBA);
        buffer.flip();

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
                GL11.GL_UNSIGNED_BYTE, buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return textureId;
}

From source file:com.telinc1.rpjg.texture.Texture.java

License:Apache License

/**
 * Uploads the given image data to the texture.
 * //ww  w. j a  v a 2 s.  c om
 * @param format - The byte order to use.
 * @param data - The image data to upload.
 */
public void upload(int format, ByteBuffer data) {
    this.bind();
    this.setUnpackAlignment();
    GL11.glTexImage2D(this.getTarget(), 0, GL11.GL_RGBA, this.getWidth(), this.getHeight(), 0, format,
            GL11.GL_UNSIGNED_BYTE, data);
}

From source file:com.voxelplugineering.voxelsniper.util.TextureUtilities.java

License:Open Source License

public static int loadPNGTexture(File file, int textureUnit) {
    ByteBuffer buf = null;//from w w  w . j  a  v  a2s .  c o  m
    int tWidth = 0;
    int tHeight = 0;

    try {
        BufferedImage image = ImageIO.read(file);
        tWidth = image.getWidth();
        tHeight = image.getHeight();
        buf = imageToRGBABuffer(image);
    } catch (IOException e) {
        e.printStackTrace();
        if (file.getName().endsWith("debug.png")) {
            System.exit(-1);
        } else {
            return debugTexture;
        }
    }

    // Create a new texture object in memory and bind it
    int texId = GL11.glGenTextures();
    GL13.glActiveTexture(textureUnit);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);

    // All RGB bytes are aligned to each other and each component is 1 byte
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    // Upload the texture data and generate mip maps (for scaling)
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, buf);
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);

    // Setup the ST coordinate system
    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);

    // Setup what to do when the texture has to be scaled
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST_MIPMAP_NEAREST);

    OpenGLUtilities.checkGLError("loadPNGTexture");
    if (file.getName().endsWith("debug.png")) {
        debugTexture = texId;
    }
    return texId;
}

From source file:com.xrbpowered.gl.examples.GLLife.java

License:Open Source License

private void resetBuffers(boolean fill) {
    if (buffers[0] != null)
        buffers[0].destroy();/*from   w  w  w . ja v  a 2s .  c o m*/
    if (buffers[1] != null)
        buffers[1].destroy();

    int width = getTargetWidth();
    int height = getTargetHeight();
    buffers[0] = new OffscreenBuffers(width, height, false);
    buffers[1] = new OffscreenBuffers(width, height, false);
    targetBuffer = 1;
    turn = 0;

    IntBuffer intBuffer = ByteBuffer.allocateDirect(4 * width * height).order(ByteOrder.nativeOrder())
            .asIntBuffer();
    int[] pixels = new int[width * height];
    for (int x = 1; x < width; x++)
        for (int y = 1; y < height; y++) {
            int v = fill && random.nextInt(27) == 0 ? 0xffffffff : 0xff000000;
            pixels[y * width + x] = v;
        }
    intBuffer.put(pixels);
    intBuffer.flip();

    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, buffers[0].getColorTexId());
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL12.GL_BGRA,
            GL12.GL_UNSIGNED_INT_8_8_8_8_REV, intBuffer);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, buffers[1].getColorTexId());
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL12.GL_BGRA,
            GL12.GL_UNSIGNED_INT_8_8_8_8_REV, intBuffer);
}

From source file:com.xrbpowered.gl.res.buffers.OffscreenBuffers.java

License:Open Source License

protected void create(int w, int h, boolean depthBuffer, boolean hdr) {
    colorTexId = GL11.glGenTextures();/*  w w  w.ja v  a 2 s  . c  om*/
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, colorTexId);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, hdr ? GL30.GL_RGB16F : GL11.GL_RGB, w, h, 0, GL11.GL_RGB,
            GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
    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.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.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, colorTexId,
            0);

    depthTexId = 0;
    if (depthBuffer) {
        depthTexId = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, depthTexId);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT16, w, h, 0, GL11.GL_DEPTH_COMPONENT,
                GL11.GL_FLOAT, (ByteBuffer) null);
        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.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.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D,
                depthTexId, 0);
    }
    checkStatus();
    GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
}