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:rtype.WorkAroundTextureLoader.java

License:Open Source License

private Texture loadTexture(String path, int xOffSet, int yOffSet, int textWidth, int textHeight) {

    BufferedImage buffImage = null;
    try {/* w  ww  .  j a  va2s  .  co  m*/
        if (imageCache.get(path) != null)
            buffImage = (BufferedImage) imageCache.get(path);
        else {
            System.out.println("Loading image:" + path);
            buffImage = ImageIO.read(this.getClass().getResource(path));

            //February 2nd, 2011: Fix for JAVA 1.6 thanks to sonicWonder for the fix
            byte[] data = ((DataBufferByte) buffImage.getRaster().getDataBuffer()).getData();
            switch (buffImage.getType()) {
            case BufferedImage.TYPE_4BYTE_ABGR:
                convertFromARGBToBGRA(data);
                break;
            case BufferedImage.TYPE_3BYTE_BGR:
                convertFromBGRToRGB(data);
                break;
            default:
                System.out.println("Unknown type:" + buffImage.getType());
                break;
            }
            //End February 2nd, 2011: Fix for JAVA 1.6 thanks to sonicWonder for the fix
            imageCache.put(path, buffImage);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    int bytesPerPixel = buffImage.getColorModel().getPixelSize() / 8;

    ByteBuffer scratch = ByteBuffer.allocateDirect(textWidth * textHeight * bytesPerPixel)
            .order(ByteOrder.nativeOrder());

    DataBufferByte dataBufferByte = ((DataBufferByte) buffImage.getRaster().getDataBuffer());

    for (int i = 0; i < textHeight; i++)
        scratch.put(dataBufferByte.getData(), (xOffSet + (yOffSet + i) * buffImage.getWidth()) * bytesPerPixel,
                textWidth * bytesPerPixel);

    scratch.rewind();

    // Create A IntBuffer For Image Address In Memory
    IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
    GL11.glGenTextures(buf); // Create Texture In OpenGL

    // Create Nearest Filtered Texture
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
    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, textWidth, textHeight, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, scratch);

    Texture newTexture = new Texture();
    newTexture.textureId = buf.get(0); // Return Image Addresses In Memory
    newTexture.textureHeight = textHeight;
    newTexture.textureWidth = textWidth;

    return newTexture;
}

From source file:se.angergard.engine.graphics.FrameBuffer.java

License:Apache License

public FrameBuffer(int width, int height, boolean hasDepth, boolean hasStencil) {
    this.width = width;
    this.height = height;

    if ((width + height) % 4 != 0.0) {
        new Exception("Width + Height must be divisible by 4");
    }//from  w w  w  .j  a v a 2 s.  c  om

    frameBufferID = GL30.glGenFramebuffers();
    GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBufferID);

    if (hasDepth && hasStencil) {
        depthAndStencilBufferID = GL30.glGenRenderbuffers();
        GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthAndStencilBufferID);
        GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_DEPTH24_STENCIL8, width, height);
        GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_STENCIL_ATTACHMENT,
                GL30.GL_RENDERBUFFER, depthAndStencilBufferID);
        GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, 0);
    }

    else if (hasDepth) {
        depthBufferID = GL30.glGenRenderbuffers();
        GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBufferID);
        GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_DEPTH_COMPONENT32F, width, height);
        GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER,
                depthBufferID);
        GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, 0);
    }

    else if (hasStencil) {
        stencilBufferID = GL30.glGenRenderbuffers();
        GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, stencilBufferID);
        GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_STENCIL_INDEX16, width, height);
        GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_STENCIL_ATTACHMENT, GL30.GL_RENDERBUFFER,
                stencilBufferID);
        GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, 0);
    }

    colorTexture = GL11.glGenTextures();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, colorTexture);
    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_MIN_FILTER, GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);

    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);

    GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D,
            colorTexture, 0);

    int result = GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER);
    GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
    if (result != GL30.GL_FRAMEBUFFER_COMPLETE) {
        if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) {
            new Exception("Frame Buffer Error: incomplete attachment").printStackTrace();
        }
        if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER) {
            new Exception("Frame Buffer Error: incomplete draw buffer").printStackTrace();
        }
        if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT) {
            new Exception("Frame Buffer Error: missing attachment").printStackTrace();
        }
        if (result == GL30.GL_FRAMEBUFFER_UNSUPPORTED) {
            new Exception("Frame Buffer Error: unsupported combination of formats").printStackTrace();
        }
        if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE) {
            new Exception("Frame Buffer Error: incomplete multisample").printStackTrace();
        }
        if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER) {
            new Exception("Frame Buffer Error: incomplete read buffer").printStackTrace();
        }

        new Exception("frame buffer couldn't be constructed: unknown error " + result).printStackTrace();
    }
    RenderUtil.unbindFrameBuffer();
    RenderUtil.unbindTexture();

    frameBufferShader = new ShaderProgram().createDefault2DShader();
}

From source file:se.angergard.engine.graphics.Texture.java

License:Apache License

private void loadTexture(ByteBuffer buffer, int textureID, int width, int height) {
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);

    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, buffer);

    // Not all computer will support this, fix //TODO
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);

    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_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
}

From source file:shadowmage.meim.client.gui.GuiTextureElement.java

License:Open Source License

private void uploadTextureRGBAInts(IntBuffer imagedata, int width, int height) {
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA,
            GL12.GL_UNSIGNED_INT_8_8_8_8, imagedata);
}

From source file:shadowmage.meim.client.texture.TextureManager.java

License:Open Source License

/**
 * upload the imageData to openGL using the currently bound texture number
 * @param imagedata in RGBA format//from ww  w.j a v a2  s.  c o m
 * @param width
 * @param height
 */
private static void uploadTextureRGBAInts(IntBuffer imagedata, int width, int height) {
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA,
            GL12.GL_UNSIGNED_INT_8_8_8_8, imagedata);
}

From source file:src.graphics.common.Texture.java

License:Open Source License

private void cacheTex() {
    if (glID == -1) {
        tmpID.rewind();//from  www  .j  a  va2s.  com
        GL11.glGenTextures(tmpID);
        glID = tmpID.get(0);
    }
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, glID);
    buffer.flip();
    setDefaultTexParams(GL11.GL_TEXTURE_2D);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, trueSize, trueSize, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, buffer);
    cached = true;
}

From source file:src.graphics.widgets.UINode.java

License:Open Source License

final public static void drawPixels(Box2D area, ByteBuffer pixels) {
    if (!cached) {
        final IntBuffer tmpID = BufferUtils.createIntBuffer(1);
        GL11.glGenTextures(tmpID);/*from w w w.  ja  v  a 2  s. co m*/
        glID = tmpID.get(0);
        cached = true;
    }

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, glID);
    Texture.setDefaultTexParams(GL11.GL_TEXTURE_2D);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, 4, (int) area.xdim(), (int) area.ydim(), 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, pixels);

    GL11.glBegin(GL11.GL_QUADS);
    UINode.drawQuad((int) area.xpos(), (int) area.ypos(), (int) area.xmax(), (int) area.ymax(), 0, 1, 1, 0, 0);
    GL11.glEnd();
}

From source file:tectonicus.rasteriser.lwjgl.LwjglTextureUtils.java

License:BSD License

public static int createTexture(BufferedImage imageData, TextureFilter filterMode) {
    imageData = convertToGlFormat(imageData);

    IntBuffer buff = BufferUtils.createIntBuffer(16);
    buff.limit(1);/*from   w w w  . ja  v  a 2s. co m*/
    GL11.glGenTextures(buff);

    int textureId = buff.get();

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
    if (filterMode == TextureFilter.NEAREST) {
        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);
    } else {
        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.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);

    ByteBuffer scratch = ByteBuffer.allocateDirect(4 * imageData.getWidth() * imageData.getHeight());

    Raster raster = imageData.getRaster();
    byte data[] = (byte[]) raster.getDataElements(0, 0, imageData.getWidth(), imageData.getHeight(), null);
    scratch.clear();
    scratch.put(data);
    scratch.rewind();

    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, // Mip level & Internal format
            imageData.getWidth(), imageData.getHeight(), 0, // width, height, border
            GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, // pixel data format
            scratch); // pixel data

    GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, imageData.getWidth(), imageData.getHeight(), GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, // format, type
            scratch);

    return textureId;
}

From source file:tectonicus.rasteriser.lwjgl.LwjglTextureUtils.java

License:BSD License

public static int createTexture(BufferedImage[] mips, TextureFilter filterMode) {
    IntBuffer buff = BufferUtils.createIntBuffer(16);
    buff.limit(1);//ww  w  .ja v a 2  s .  c o m
    GL11.glGenTextures(buff);

    int textureId = buff.get();

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
    if (filterMode == TextureFilter.NEAREST) {
        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_LINEAR);
    } else {
        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_MIPMAP_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);

    for (int mip = 0; mip < mips.length; mip++) {
        BufferedImage imageData = mips[mip];
        imageData = convertToGlFormat(imageData);

        ByteBuffer scratch = ByteBuffer.allocateDirect(4 * imageData.getWidth() * imageData.getHeight());

        Raster raster = imageData.getRaster();
        byte data[] = (byte[]) raster.getDataElements(0, 0, imageData.getWidth(), imageData.getHeight(), null);
        scratch.clear();
        scratch.put(data);
        scratch.rewind();

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, mip, GL11.GL_RGBA, // Mip level & Internal format
                imageData.getWidth(), imageData.getHeight(), 0, // width, height, border
                GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, // pixel data format
                scratch); // pixel data

        //   GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0,
        //         0, 0,
        //         imageData.getWidth(), imageData.getHeight(),
        //         GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE,         // format, type
        //         scratch);
    }

    return textureId;
}

From source file:tk.ivybits.engine.gl.GL.java

License:Open Source License

public static void glTexImage2D(int a, int b, int c, int d, int e, int f, int g, int h, IntBuffer i) {
    GL11.glTexImage2D(a, b, c, d, e, f, g, h, i);
}