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:org.spout.engine.resources.ClientTexture.java

License:Open Source License

@Override
public void bind() {
    if (textureID == -1) {
        throw new IllegalStateException("Cannot bind an unloaded texture!");
    }//from  w  w  w .j a  v a 2 s  .c o m
    GL11.glEnable(GL11.GL_TEXTURE_2D);

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
}

From source file:org.spout.engine.resources.ClientTexture.java

License:Open Source License

@Override
public void load() {
    if (textureID != -1) {
        throw new IllegalStateException("Cannot load an already loaded texture!");
    }//from  w ww.  j a  v  a 2 s .  c o m

    GL11.glEnable(GL11.GL_TEXTURE_2D);

    textureID = GL11.glGenTextures();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);

    GL11.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);

    /*if (((Client) Spout.getEngine()).getRenderMode() != RenderMode.GL30) {
            
       //Use Mipmaps
       GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
    }*/

    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_BASE_LEVEL, 0);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, 0);

    //Bilinear Filter the closest mipmap
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);

    //Wrap the 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);

    int width = image.getWidth();
    int height = image.getHeight();

    int[] pixels = new int[width * height];
    image.getRGB(0, 0, width, height, pixels, 0, width);

    ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int pixel = pixels[y * width + x];
            buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
            buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
            buffer.put((byte) (pixel & 0xFF)); // Blue component
            buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component. Only for RGBA
        }
    }

    buffer.flip();
    //if (((Client) Spout.getEngine()).getRenderMode() == RenderMode.GL30) {
    //   GL30.glGenerateMipmap(textureID);
    //}

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

    //EXTFramebufferObject.glGenerateMipmapEXT(GL11.GL_TEXTURE_2D); //Not sure if this extension is supported on most cards. 
}

From source file:org.spout.renderer.lwjgl.gl20.GL20Texture.java

License:Open Source License

@Override
public void setImageData(ByteBuffer imageData, int width, int height) {
    checkCreated();/*w ww .  j  a v  a2 s. c  o m*/
    if (width <= 0) {
        throw new IllegalArgumentException("Width must be greater than zero");
    }
    if (height <= 0) {
        throw new IllegalArgumentException("Height must be greater than zero");
    }
    this.width = width;
    this.height = height;
    // Bind the texture
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
    // Upload the texture to the GPU
    final boolean hasInternalFormat = internalFormat != null;
    if (minFilter.needsMipMaps() && imageData != null) {
        // Build mipmaps if using mip mapped filters
        GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D,
                hasInternalFormat ? internalFormat.getGLConstant() : format.getGLConstant(), width, height,
                format.getGLConstant(), hasInternalFormat ? internalFormat.getComponentType().getGLConstant()
                        : DataType.UNSIGNED_BYTE.getGLConstant(),
                imageData);
    } else {
        // Else just make it a normal texture
        // Use byte alignment
        GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
        // Upload the image
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,
                hasInternalFormat ? internalFormat.getGLConstant() : format.getGLConstant(), width, height, 0,
                format.getGLConstant(), hasInternalFormat ? internalFormat.getComponentType().getGLConstant()
                        : DataType.UNSIGNED_BYTE.getGLConstant(),
                imageData);
    }
    // Unbind the texture
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    // Check for errors
    LWJGLUtil.checkForGLError();
}

From source file:org.spout.renderer.lwjgl.gl30.GL30Texture.java

License:Open Source License

@Override
public void setImageData(ByteBuffer imageData, int width, int height) {
    checkCreated();//from   ww w.j  a v a2s.  c o  m
    if (width <= 0) {
        throw new IllegalArgumentException("Width must be greater than zero");
    }
    if (height <= 0) {
        throw new IllegalArgumentException("Height must be greater than zero");
    }
    this.width = width;
    this.height = height;
    // Bind the texture
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
    // Use byte alignment
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
    // Upload the texture
    final boolean hasInternalFormat = internalFormat != null;
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,
            hasInternalFormat ? internalFormat.getGLConstant() : format.getGLConstant(), width, height, 0,
            format.getGLConstant(), hasInternalFormat ? internalFormat.getComponentType().getGLConstant()
                    : DataType.UNSIGNED_BYTE.getGLConstant(),
            imageData);
    // Generate mipmaps if necessary
    if (minFilter.needsMipMaps() && imageData != null) {
        GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
    }
    // Unbind the texture
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    // Check for errors
    LWJGLUtil.checkForGLError();
}

From source file:org.spoutcraft.client.special.ModelNarrowtux.java

License:Open Source License

@Override
public void render(Entity par1Entity, float par2, float par3, float par4, float par5, float par6, float par7) {
    reload(0.0f);//from  www.  j a  va 2  s .  c  o m
    this.setRotationAngles(par2, par3, par4, par5, par6, par7);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D,
            CustomTextureManager.getTextureFromPath("/Users/tux/Sites/narrowtux.png").getTextureID());
    waist.render(par7);
    leftFoot.render(par7);
    rightFoot.render(par7);
    leftWing.render(par7);
    rightWing.render(par7);
    head.render(par7);
}

From source file:org.spoutcraft.spoutcraftapi.gui.DirtBackground.java

License:Open Source License

public void render() {
    GL11.glDisable(2896 /*GL_LIGHTING*/);
    GL11.glDisable(2912 /*GL_FOG*/);
    GL11.glBindTexture(3553 /*GL_TEXTURE_2D*/, Spoutcraft.getTessellator().getMCTexture("/gui/background.png"));
    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    float var3 = 32.0F;
    Spoutcraft.getTessellator().startDrawingQuads();
    Spoutcraft.getTessellator().setColorOpaqueInt(4210752);
    Spoutcraft.getTessellator().addVertexWithUV(0.0D, this.getHeight(), 0.0D, 0.0D, this.getHeight() / var3);
    Spoutcraft.getTessellator().addVertexWithUV(this.getWidth(), this.getHeight(), 0.0D, this.getWidth() / var3,
            this.getHeight() / var3);
    Spoutcraft.getTessellator().addVertexWithUV(this.getWidth(), 0.0D, 0.0D, this.getWidth() / var3, 0.0D);
    Spoutcraft.getTessellator().addVertexWithUV(0.0D, 0.0D, 0.0D, 0.0D, 0.0D);
    Spoutcraft.getTessellator().draw();//  w w  w  .j  av  a2s.co m
}

From source file:org.terasology.logic.manager.DefaultRenderingProcess.java

License:Apache License

public FBO createFBO(String title, int width, int height, FBOType type, boolean depth, boolean normals) {
    // Make sure to delete the existing FBO before creating a new one
    deleteFBO(title);/*from w w w .  j  a  v  a 2  s .co m*/

    // Create a new FBO object
    FBO fbo = new FBO();
    fbo.width = width;
    fbo.height = height;

    // Create the color target texture
    fbo.textureId = GL11.glGenTextures();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.textureId);

    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

    if (type != FBOType.NO_COLOR) {
        if (type == FBOType.HDR) {
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, ARBTextureFloat.GL_RGBA16F_ARB, width, height, 0,
                    GL11.GL_RGBA, ARBHalfFloatPixel.GL_HALF_FLOAT_ARB, (java.nio.ByteBuffer) null);
        } else {
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
                    GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
        }
    }

    if (depth) {
        // Generate the depth texture
        fbo.depthTextureId = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.depthTextureId);

        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT24, width, height, 0,
                GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (java.nio.ByteBuffer) null);

        // Create depth render buffer object
        fbo.depthRboId = EXTFramebufferObject.glGenRenderbuffersEXT();
        EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, fbo.depthRboId);
        EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT,
                GL14.GL_DEPTH_COMPONENT24, width, height);
        EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, 0);
    }

    if (normals) {
        // Generate the normals texture
        fbo.normalsTextureId = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.normalsTextureId);

        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
                GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
    }

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

    // Create the FBO
    fbo.fboId = EXTFramebufferObject.glGenFramebuffersEXT();
    EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fbo.fboId);

    if (type != FBOType.NO_COLOR) {
        EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, fbo.textureId, 0);
    }

    if (depth) {
        // Generate the depth render buffer and depth map texture
        EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, EXTFramebufferObject.GL_RENDERBUFFER_EXT,
                fbo.depthRboId);
        EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, GL11.GL_TEXTURE_2D, fbo.depthTextureId, 0);
    }

    if (normals) {
        EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                EXTFramebufferObject.GL_COLOR_ATTACHMENT1_EXT, GL11.GL_TEXTURE_2D, fbo.normalsTextureId, 0);
    }

    IntBuffer bufferIds = BufferUtils.createIntBuffer(3);
    if (type != FBOType.NO_COLOR) {
        bufferIds.put(EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT);
    }
    if (normals) {
        bufferIds.put(EXTFramebufferObject.GL_COLOR_ATTACHMENT1_EXT);
    }
    bufferIds.flip();
    GL20.glDrawBuffers(bufferIds);

    int checkFB = EXTFramebufferObject.glCheckFramebufferStatusEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT);
    switch (checkFB) {
    case EXTFramebufferObject.GL_FRAMEBUFFER_COMPLETE_EXT:
        break;
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
        logger.error("FrameBuffer: " + title
                + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT exception");

        /*
         * On some graphics cards, FBOType.NO_COLOR can cause a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT.
         * Attempt to continue without this FBO.
         */
        if (type == FBOType.NO_COLOR) {
            logger.error("FrameBuffer: " + title
                    + ", ...but the FBOType was NO_COLOR, ignoring this error and continuing without this FBO.");

            taint("Got a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT because of FBOType.NO_COLOR.");
            return null;
        }
    default:
        throw new RuntimeException("Unexpected reply from glCheckFramebufferStatusEXT: " + checkFB);
    }

    EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);

    _FBOs.put(title, fbo);
    return fbo;
}

From source file:org.terasology.logic.manager.PostProcessingRenderer.java

License:Apache License

public FBO createFBO(String title, int width, int height, boolean hdr, boolean depth, boolean normals) {
    // Make sure to delete the existing FBO before creating a new one
    deleteFBO(title);//from  w ww. ja v a  2s  .  co  m

    // Create a new FBO object
    FBO fbo = new FBO();
    fbo._width = width;
    fbo._height = height;

    // Create the color target texture
    fbo._textureId = GL11.glGenTextures();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo._textureId);

    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

    if (hdr) {
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, ARBTextureFloat.GL_RGBA16F_ARB, width, height, 0, GL11.GL_RGBA,
                ARBHalfFloatPixel.GL_HALF_FLOAT_ARB, (java.nio.ByteBuffer) null);
    } else {
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
                GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
    }

    if (depth) {
        // Generate the depth texture
        fbo._depthTextureId = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo._depthTextureId);

        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT24, width, height, 0,
                GL11.GL_DEPTH_COMPONENT, ARBHalfFloatPixel.GL_HALF_FLOAT_ARB, (java.nio.ByteBuffer) null);

        // Create depth render buffer object
        fbo._depthRboId = EXTFramebufferObject.glGenRenderbuffersEXT();
        EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, fbo._depthRboId);
        EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT,
                GL14.GL_DEPTH_COMPONENT16, width, height);
        EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, 0);
    }

    if (normals) {
        // Generate the normals texture
        fbo._normalsTextureId = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo._normalsTextureId);

        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
                GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
    }

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

    // Create the FBO
    fbo._fboId = EXTFramebufferObject.glGenFramebuffersEXT();
    EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fbo._fboId);

    EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
            EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, fbo._textureId, 0);

    if (depth) {
        // Generate the depth render buffer and depth map texture
        EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, EXTFramebufferObject.GL_RENDERBUFFER_EXT,
                fbo._depthRboId);
        EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, GL11.GL_TEXTURE_2D, fbo._depthTextureId, 0);
    }

    if (normals) {
        EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                EXTFramebufferObject.GL_COLOR_ATTACHMENT1_EXT, GL11.GL_TEXTURE_2D, fbo._normalsTextureId, 0);
    }

    IntBuffer bufferIds = BufferUtils.createIntBuffer(3);
    bufferIds.put(EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT);
    if (normals) {
        bufferIds.put(EXTFramebufferObject.GL_COLOR_ATTACHMENT1_EXT);
    }
    bufferIds.flip();
    GL20.glDrawBuffers(bufferIds);

    int framebuffer = EXTFramebufferObject.glCheckFramebufferStatusEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT);
    switch (framebuffer) {
    case EXTFramebufferObject.GL_FRAMEBUFFER_COMPLETE_EXT:
        break;
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
        logger.error("FrameBuffer: " + title
                + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT exception");
    case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT exception");
    default:
        throw new RuntimeException("Unexpected reply from glCheckFramebufferStatusEXT: " + framebuffer);
    }

    EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);

    _FBOs.put(title, fbo);
    return fbo;
}

From source file:org.terasology.logic.manager.ShaderManager.java

License:Apache License

public void bindTexture(int slot, Texture texture) {
    if (activateMaterial != null && !activateMaterial.isDisposed()) {
        GL13.glActiveTexture(GL13.GL_TEXTURE0 + slot);
        // TODO: Need to be cubemap aware, only need to clear bind when switching from cubemap to 2D and vice versa,
        // TODO: Don't bind if already bound to the same
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getId());
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
    }//from www.j av a  2s .  c  o m
}

From source file:org.terasology.rendering.opengl.DefaultRenderingProcess.java

License:Apache License

public FBO createFBO(String title, int width, int height, FBOType type, boolean depthBuffer,
        boolean normalBuffer, boolean lightBuffer, boolean stencilBuffer) {
    // Make sure to delete the existing FBO before creating a new one
    deleteFBO(title);//from  w w  w.jav  a  2  s. c o  m

    // Create a new FBO object
    FBO fbo = new FBO();
    fbo.width = width;
    fbo.height = height;

    // Create the FBO
    fbo.fboId = glGenFramebuffersEXT();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo.fboId);

    if (type != FBOType.NO_COLOR) {
        fbo.textureId = glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.textureId);

        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

        if (type == FBOType.HDR) {
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
                    ARBHalfFloatPixel.GL_HALF_FLOAT_ARB, (ByteBuffer) null);
        } else {
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
                    GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
        }

        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D,
                fbo.textureId, 0);
    }

    if (normalBuffer) {
        fbo.normalsTextureId = glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.normalsTextureId);

        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

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

        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL11.GL_TEXTURE_2D,
                fbo.normalsTextureId, 0);
    }

    if (lightBuffer) {
        fbo.lightBufferTextureId = glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.lightBufferTextureId);

        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

        if (type == FBOType.HDR) {
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, ARBTextureFloat.GL_RGBA16F_ARB, width, height, 0,
                    GL11.GL_RGBA, ARBHalfFloatPixel.GL_HALF_FLOAT_ARB, (ByteBuffer) null);
        } else {
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
                    GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
        }

        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL11.GL_TEXTURE_2D,
                fbo.lightBufferTextureId, 0);
    }

    if (depthBuffer) {
        fbo.depthStencilTextureId = glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.depthStencilTextureId);

        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

        if (!stencilBuffer) {
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT24, width, height, 0,
                    GL11.GL_DEPTH_COMPONENT, GL11.GL_UNSIGNED_INT, (ByteBuffer) null);
        } else {
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, EXTPackedDepthStencil.GL_DEPTH24_STENCIL8_EXT, width,
                    height, 0, EXTPackedDepthStencil.GL_DEPTH_STENCIL_EXT,
                    EXTPackedDepthStencil.GL_UNSIGNED_INT_24_8_EXT, (ByteBuffer) null);
        }

        fbo.depthStencilRboId = glGenRenderbuffersEXT();
        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo.depthStencilRboId);

        if (!stencilBuffer) {
            glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL14.GL_DEPTH_COMPONENT24, width, height);
        } else {
            glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, EXTPackedDepthStencil.GL_DEPTH24_STENCIL8_EXT, width,
                    height);
        }

        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT,
                fbo.depthStencilRboId);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL11.GL_TEXTURE_2D,
                fbo.depthStencilTextureId, 0);

        if (stencilBuffer) {
            glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL11.GL_TEXTURE_2D,
                    fbo.depthStencilTextureId, 0);
        }
    }

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

    IntBuffer bufferIds = BufferUtils.createIntBuffer(3);
    if (type != FBOType.NO_COLOR) {
        bufferIds.put(GL_COLOR_ATTACHMENT0_EXT);
    }
    if (normalBuffer) {
        bufferIds.put(GL_COLOR_ATTACHMENT1_EXT);
    }
    if (lightBuffer) {
        bufferIds.put(GL_COLOR_ATTACHMENT2_EXT);
    }
    bufferIds.flip();

    if (bufferIds.limit() == 0) {
        GL11.glReadBuffer(GL11.GL_NONE);
        GL20.glDrawBuffers(GL11.GL_NONE);
    } else {
        GL20.glDrawBuffers(bufferIds);
    }

    int checkFB = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
    switch (checkFB) {
    case GL_FRAMEBUFFER_COMPLETE_EXT:
        break;
    case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT exception");
        break;
    case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
        logger.error("FrameBuffer: " + title
                + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT exception");
        break;
    case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT exception");
        break;
    case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT exception");
        break;
    case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT exception");
        break;
    case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
        logger.error("FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_UNSUPPORTED_EXT exception");
        break;
    case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
        logger.error(
                "FrameBuffer: " + title + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT exception");

        /*
         * On some graphics cards, FBOType.NO_COLOR can cause a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT.
         * Attempt to continue without this FBO.
         */
        if (type == FBOType.NO_COLOR) {
            logger.error("FrameBuffer: " + title
                    + ", ...but the FBOType was NO_COLOR, ignoring this error and continuing without this FBO.");

            return null;
        }

        break;
    default:
        logger.error("Unexpected reply from glCheckFramebufferStatusEXT: " + checkFB);
        break;
    }

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

    fboLookup.put(title, fbo);
    return fbo;
}