Example usage for org.lwjgl.opengl GL11 glGenTextures

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

Introduction

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

Prototype

@NativeType("void")
public static int glGenTextures() 

Source Link

Document

Returns n previously unused texture names in textures.

Usage

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 ww.j a  v a 2 s .c o m*/

    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

/** Only constructor for Texture, final texture id, can't be changed. BROKEN BROKEN BROKEN
 * @param TEXTURE_ID *//* www .j  av  a2 s .c  om*/
public Texture(String path) {

    int texID = -1;
    InputStream in = null;
    PNGDecoder decoder = null;
    try {
        in = new FileInputStream(MainLoop.getPathToRes() + path);

        decoder = new PNGDecoder(in);

        ByteBuffer buf = BufferUtils.createByteBuffer(decoder.getWidth() * decoder.getHeight() * 4);
        decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
        buf.flip();

        texID = GL11.glGenTextures();
        loadTexture(buf, texID, decoder.getWidth(), decoder.getHeight());

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    if (texID == -1) {
        new Exception("Failed to create texture with path = " + path).printStackTrace();
    }

    this.TEXTURE_ID = texID;
    this.WIDTH = decoder.getWidth();
    this.HEIGHT = decoder.getHeight();
}

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

License:Apache License

public Texture(int width, int height) {
    this.TEXTURE_ID = GL11.glGenTextures();
    this.WIDTH = width;
    this.HEIGHT = height;
    loadTexture(null, TEXTURE_ID, width, height);
}

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

License:Open Source License

public void allocateTexture() {
    if (openGLTextureNumber > 0) {
        GL11.glDeleteTextures(openGLTextureNumber);
    }/*from  w  w  w .  j  a va 2 s.  c  o  m*/
    openGLTextureNumber = GL11.glGenTextures();
    this.updateTextureContents(image);
}

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

License:Open Source License

public static void allocateTexture() {
    if (texNum >= 0) {
        GL11.glDeleteTextures(texNum);//www .j ava 2 s.  co  m
    }
    texNum = GL11.glGenTextures();
    bindTexture();
    for (int i = 0; i < 256 * 256; i++) {
        dataBuffer.put(i, 0xfffffff);
    }
    dataBuffer.rewind();
    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_NEAREST); //GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); //GL11.GL_NEAREST);
    uploadTextureRGBAInts(dataBuffer, 256, 256);//upload empty data to texture so that it is 'valid'?
    resetBoundTexture();
}

From source file:volpes.ldk.client.opengl.LWJGLRender.java

License:Open Source License

private int loadPNGTexture(String filename, int textureUnit) {
    ByteBuffer buf = null;/* w  w  w.  j  a v a 2 s. c  o m*/
    int tWidth = 0;
    int tHeight = 0;

    try {
        InputStream in = new FileInputStream(filename);

        PNGDecoder decoder = new PNGDecoder(in);

        tWidth = decoder.getWidth();
        tHeight = decoder.getHeight();
        buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buf, 4 * decoder.getWidth(), PNGDecoder.Format.RGBA);
        if (!decoder.hasAlpha()) {
            System.out.println("Wrong PNG format. Missing alpha channel");
        }
        buf.flip();
        in.close();
    } catch (IOException e) {
        System.err.println("Could not load textures. Doing a totally overkill app crash now ;)");
        e.printStackTrace();
        System.exit(-1);
    }

    int texId = GL11.glGenTextures();
    glActiveTexture(textureUnit);
    glBindTexture(GL_TEXTURE_2D, texId);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tWidth, tHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
    glGenerateMipmap(GL_TEXTURE_2D);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

    return texId;

}

From source file:wrath.client.ClientUtils.java

License:Open Source License

/**
 * Loads a LWJGL Texture from an image./*from   w ww .j  a  v  a2  s  .  c  o m*/
 * @param image The {@link java.awt.image.BufferedImage} version of the Texture.
 * @return Returns the LWJGL texture id.
 */
public static int getTexture(BufferedImage image) {
    if (image == null)
        return 0;

    int[] pixels = new int[image.getWidth() * image.getHeight()];
    image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

    ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);

    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));
            buffer.put((byte) ((pixel >> 24) & 0xFF));
        }
    }
    buffer.flip();

    int id = GL11.glGenTextures();
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
    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.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, buffer);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    return id;
}