Example usage for org.lwjgl.opengl GL11 glTexParameteri

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

Introduction

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

Prototype

public static void glTexParameteri(@NativeType("GLenum") int target, @NativeType("GLenum") int pname,
        @NativeType("GLint") int param) 

Source Link

Document

Sets the integer value of a texture parameter, which controls how the texel array is treated when specified or changed, and when applied to a fragment.

Usage

From source file:com.grillecube.engine.opengl.object.GLTexture.java

public void parameteri(int target, int pname, int param) {
    GL11.glTexParameteri(target, pname, param);
}

From source file:com.kauridev.lunarfever.graphics.TextureLoader.java

License:Open Source License

public static Texture loadTexture(String file) {
    Texture texture = cache.get(file);/*from   w w w .ja  va  2 s  .  c o m*/

    if (texture != null && texture.getTexture().valid()) {
        return texture;
    }

    BufferedImage image = loadImage(file);

    GL11.glEnable(GL11.GL_TEXTURE_2D);
    int id = GL11.glGenTextures();

    // bind
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);

    // set filter
    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);

    // set wrap
    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);

    // set unpack alignment
    GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    // send data to gpu
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, loadBuffer(image));

    // Create texture
    texture = new Texture(id, image.getWidth(), image.getHeight());

    cache.put(file, texture);
    return texture;
}

From source file:com.kegare.caveworld.client.gui.GuiListSlot.java

License:Minecraft Mod Public

private void rotateAndBlurSkybox(float ticks) {
    mc.getTextureManager().bindTexture(panoramaBackground);
    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.glCopyTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, 0, 0, 256, 256);
    GL11.glEnable(GL11.GL_BLEND);/*  w ww . j a  v  a 2  s .c  om*/
    OpenGlHelper.glBlendFunc(770, 771, 1, 0);
    GL11.glColorMask(true, true, true, false);
    Tessellator tessellator = Tessellator.instance;
    tessellator.startDrawingQuads();
    GL11.glDisable(GL11.GL_ALPHA_TEST);
    byte b0 = 3;

    for (int i = 0; i < b0; ++i) {
        tessellator.setColorRGBA_F(1.0F, 1.0F, 1.0F, 1.0F / (i + 1));
        int j = width;
        int k = height;
        float f1 = (i - b0 / 2) / 256.0F;
        tessellator.addVertexWithUV(j, k, 0.0F, 0.0F + f1, 1.0D);
        tessellator.addVertexWithUV(j, 0.0D, 0.0F, 1.0F + f1, 1.0D);
        tessellator.addVertexWithUV(0.0D, 0.0D, 0.0F, 1.0F + f1, 0.0D);
        tessellator.addVertexWithUV(0.0D, k, 0.0F, 0.0F + f1, 0.0D);
    }

    tessellator.draw();
    GL11.glEnable(GL11.GL_ALPHA_TEST);
    GL11.glColorMask(true, true, true, true);
}

From source file:com.onkiup.minedroid.gui.betterfonts.GlyphCache.java

License:Open Source License

/**
 * Allocate a new OpenGL texture for caching pre-rendered glyph images. The new texture is initialized to fully transparent
 * white so the individual glyphs images within can have a transparent border between them. The new texture remains bound
 * after returning from the function./*  w  w w.j ava 2s. co m*/
 *
 * @todo use GL_ALPHA4 if anti-alias is turned off for even smaller textures
 */
private void allocateGlyphCacheTexture() {
    /* Initialize the background to all white but fully transparent. */
    glyphCacheGraphics.clearRect(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT);

    /* Allocate new OpenGL texure */
    singleIntBuffer.clear();
    textureName = GlStateManager.generateTexture();

    /* Load imageBuffer with pixel data ready for transfer to OpenGL texture */
    updateImageBuffer(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT);

    /*
     * Initialize texture with the now cleared BufferedImage. Using a texture with GL_ALPHA8 internal format may result in
     * faster rendering since the GPU has to only fetch 1 byte per texel instead of 4 with a regular RGBA texture.
     */
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureName);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_ALPHA8, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, imageBuffer);

    /* Explicitely disable mipmap support becuase updateTexture() will only update the base level 0 */
    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);
}

From source file:com.onkiup.minedroid.gui.betterfonts.StringCache.java

License:Open Source License

/**
 * Render a single-line string to the screen using the current OpenGL color. The (x,y) coordinates are of the uppet-left
 * corner of the string's bounding box, rather than the baseline position as is typical with fonts. This function will also
 * add the string to the cache so the next renderString() call with the same string is faster.
 *
 * @param str          the string being rendered; it can contain color codes
 * @param startX       the x coordinate to draw at
 * @param startY       the y coordinate to draw at
 * @param initialColor the initial RGBA color to use when drawing the string; embedded color codes can override the RGB component
 * @param shadowFlag   if true, color codes are replaces by a darker version used for drop shadows
 * @return the total advance (horizontal distance) of this string
 * @todo Add optional NumericShaper to replace ASCII digits with locale specific ones
 * @todo Add support for the "k" code which randomly replaces letters on each render (used only by splash screen)
 * @todo Pre-sort by texture to minimize binds; can store colors per glyph in string cache
 * @todo Optimize the underline/strikethrough drawing to draw a single line for each run
 *///from   ww  w .j  a v a 2s .  co m
public Entry renderString(String str, int startX, int startY, int initialColor, boolean shadowFlag) {
    /* Check for invalid arguments */
    if (str == null || str.isEmpty()) {
        return null;
    }
    View.resetBlending();
    GlStateManager.enableTexture2D();
    float scale = GuiManager.getDisplayScale();
    float f = GuiManager.getScale().getScaleFactor();

    /* Make sure the entire string is cached before rendering and return its glyph representation */
    Entry entry = cacheString(str);

    /* Adjust the baseline of the string because the startY coordinate in Minecraft is for the top of the string */
    startY += entry.getScaledBase();

    /* Color currently selected by color code; reapplied to Tessellator instance after glBindTexture() */
    int color = initialColor;

    /* Track which texture is currently bound to minimize the number of glBindTexture() and Tessellator.draw() calls needed */
    int boundTextureName = 0;

    /*
     * This color change will have no effect on the actual text (since colors are included in the Tessellator vertex
     * array), however GuiEditSign of all things depends on having the current color set to white when it renders its
     * "Edit sign message:" text. Otherwise, the sign which is rendered underneath would look too dark.
     */
    GL11.glColor3f(color >> 16 & 0xff, color >> 8 & 0xff, color & 0xff);

    /*
     * Enable GL_BLEND in case the font is drawn anti-aliased because Minecraft itself only enables blending for chat text
     * (so it can fade out), but not GUI text or signs. Minecraft uses multiple blend functions so it has to be specified here
     * as well for consistent blending. To reduce the overhead of OpenGL state changes and making native LWJGL calls, this
     * function doesn't try to save/restore the blending state. Hopefully everything else that depends on blending in Minecraft
     * will set its own state as needed.
     */
    if (antiAliasEnabled) {
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    }

    /* Using the Tessellator to queue up data in a vertex array and then draw all at once should be faster than immediate mode */
    Tessellator tessellator = Tessellator.getInstance();
    tessellator.getWorldRenderer().startDrawingQuads();
    tessellator.getWorldRenderer().setColorRGBA(color >> 16 & 0xff, color >> 8 & 0xff, color & 0xff,
            color >> 24 & 0xff);

    /* The currently active font syle is needed to select the proper ASCII digit style for fast replacement */
    int fontStyle = Font.PLAIN;

    for (int glyphIndex = 0, colorIndex = 0; glyphIndex < entry.glyphs.length; glyphIndex++) {
        /*
         * If the original string had a color code at this glyph's position, then change the current GL color that gets added
         * to the vertex array. Note that only the RGB component of the color is replaced by a color code; the alpha component
         * of the original color passed into this function will remain. The while loop handles multiple consecutive color codes,
         * in which case only the last such color code takes effect.
         */
        while (colorIndex < entry.colors.length
                && entry.glyphs[glyphIndex].stringIndex >= entry.colors[colorIndex].stringIndex) {
            color = applyColorCode(entry.colors[colorIndex].colorCode, initialColor, shadowFlag);
            fontStyle = entry.colors[colorIndex].fontStyle;
            colorIndex++;
        }

        /* Select the current glyph's texture information and horizontal layout position within this string */
        Glyph glyph = entry.glyphs[glyphIndex];
        GlyphCache.Entry texture = glyph.texture;
        int glyphX = glyph.x;

        /*
         * Replace ASCII digits in the string with their respective glyphs; strings differing by digits are only cached once.
         * If the new replacement glyph has a different width than the original placeholder glyph (e.g. the '1' glyph is often
         * narrower than other digits), re-center the new glyph over the placeholder's position to minimize the visual impact
         * of the width mismatch.
         */
        char c = str.charAt(glyph.stringIndex);
        if (c >= '0' && c <= '9') {
            int oldWidth = texture.width;
            texture = digitGlyphs[fontStyle][c - '0'].texture;
            int newWidth = texture.width;
            glyphX += (oldWidth - newWidth) >> 1;
        }

        /*
         * Make sure the OpenGL texture storing this glyph's image is bound (if not already bound). All pending glyphs in the
         * Tessellator's vertex array must be drawn before switching textures, otherwise they would erroneously use the new
         * texture as well.
         */
        if (boundTextureName != texture.textureName) {
            tessellator.draw();
            tessellator.getWorldRenderer().startDrawingQuads();
            tessellator.getWorldRenderer().setColorRGBA(color >> 16 & 0xff, color >> 8 & 0xff, color & 0xff,
                    color >> 24 & 0xff);

            GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.textureName);
            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);
            boundTextureName = texture.textureName;
        }

        /* The divide by 2.0F is needed to align with the scaled GUI coordinate system; startX/startY are already scaled */
        float x1 = startX + (glyphX) / f;
        float x2 = startX + (glyphX + texture.width) / f;
        float y1 = startY + (glyph.y) / f;
        float y2 = startY + (glyph.y + texture.height) / f;

        tessellator.getWorldRenderer().addVertexWithUV(x1, y1, 0, texture.u1, texture.v1);
        tessellator.getWorldRenderer().addVertexWithUV(x1, y2, 0, texture.u1, texture.v2);
        tessellator.getWorldRenderer().addVertexWithUV(x2, y2, 0, texture.u2, texture.v2);
        tessellator.getWorldRenderer().addVertexWithUV(x2, y1, 0, texture.u2, texture.v1);
    }

    /* Draw any remaining glyphs in the Tessellator's vertex array (there should be at least one glyph pending) */
    tessellator.draw();

    /* Draw strikethrough and underlines if the string uses them anywhere */
    if (entry.specialRender) {
        int renderStyle = 0;

        /* Use initial color passed to renderString(); disable texturing to draw solid color lines */
        color = initialColor;
        GL11.glDisable(GL11.GL_TEXTURE_2D);
        tessellator.getWorldRenderer().startDrawingQuads();
        tessellator.getWorldRenderer().setColorRGBA(color >> 16 & 0xff, color >> 8 & 0xff, color & 0xff,
                color >> 24 & 0xff);

        for (int glyphIndex = 0, colorIndex = 0; glyphIndex < entry.glyphs.length; glyphIndex++) {
            /*
             * If the original string had a color code at this glyph's position, then change the current GL color that gets added
             * to the vertex array. The while loop handles multiple consecutive color codes, in which case only the last such
             * color code takes effect.
             */
            while (colorIndex < entry.colors.length
                    && entry.glyphs[glyphIndex].stringIndex >= entry.colors[colorIndex].stringIndex) {
                color = applyColorCode(entry.colors[colorIndex].colorCode, initialColor, shadowFlag);
                renderStyle = entry.colors[colorIndex].renderStyle;
                colorIndex++;
            }

            /* Select the current glyph within this string for its layout position */
            Glyph glyph = entry.glyphs[glyphIndex];

            /* The strike/underlines are drawn beyond the glyph's width to include the extra space between glyphs */
            int glyphSpace = glyph.advance - glyph.texture.width;

            /* Draw underline under glyph if the style is enabled */
            if ((renderStyle & ColorCode.UNDERLINE) != 0) {
                /* The divide by 2.0F is needed to align with the scaled GUI coordinate system; startX/startY are already scaled */
                float x1 = startX + (glyph.x - glyphSpace) / f;
                float x2 = startX + (glyph.x + glyph.advance) / f;
                float y1 = startY + (UNDERLINE_OFFSET) / f;
                float y2 = startY + (UNDERLINE_OFFSET + UNDERLINE_THICKNESS) / f;

                tessellator.getWorldRenderer().addVertex(x1, y1, 0);
                tessellator.getWorldRenderer().addVertex(x1, y2, 0);
                tessellator.getWorldRenderer().addVertex(x2, y2, 0);
                tessellator.getWorldRenderer().addVertex(x2, y1, 0);
            }

            /* Draw strikethrough in the middle of glyph if the style is enabled */
            if ((renderStyle & ColorCode.STRIKETHROUGH) != 0) {
                /* The divide by 2.0F is needed to align with the scaled GUI coordinate system; startX/startY are already scaled */
                float x1 = startX + (glyph.x - glyphSpace) / f;
                float x2 = startX + (glyph.x + glyph.advance) / f;
                float y1 = startY + (STRIKETHROUGH_OFFSET) / f;
                float y2 = startY + (STRIKETHROUGH_OFFSET + STRIKETHROUGH_THICKNESS) / f;

                tessellator.getWorldRenderer().addVertex(x1, y1, 0);
                tessellator.getWorldRenderer().addVertex(x1, y2, 0);
                tessellator.getWorldRenderer().addVertex(x2, y2, 0);
                tessellator.getWorldRenderer().addVertex(x2, y1, 0);
            }
        }

        /* Finish drawing the last strikethrough/underline segments */
        tessellator.draw();
        GL11.glEnable(GL11.GL_TEXTURE_2D);
    }

    /* Return total horizontal advance (slightly wider than the bounding box, but close enough for centering strings) */
    return entry;
}

From source file:com.opengrave.og.light.Texture2DShadowMap.java

License:Open Source License

public Texture2DShadowMap(int size) {
    texture = GL11.glGenTextures();//w ww .  j  a v a 2s.  c o  m
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    Util.checkErr();
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    Util.checkErr();
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    Util.checkErr();
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
    Util.checkErr();
    // GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
    // GL14.GL_TEXTURE_COMPARE_FUNC, GL11.GL_LEQUAL);
    // GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
    // GL14.GL_TEXTURE_COMPARE_MODE,
    // GL30.GL_COMPARE_REF_TO_TEXTURE);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_TEXTURE_COMPARE_MODE, GL11.GL_NONE);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_DEPTH_TEXTURE_MODE, GL11.GL_INTENSITY);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_DEPTH_COMPONENT, size, size, 0, GL11.GL_DEPTH_COMPONENT,
            GL11.GL_FLOAT, (FloatBuffer) null);
    Util.checkErr();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

    // texture2 = GL11.glGenTextures();
    // GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture2);
    // GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,GL11.GL_RGB, size, size,
    // 0,GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (FloatBuffer)null);
    // 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.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.glBindTexture(GL11.GL_TEXTURE_2D, 0);

}

From source file:com.opengrave.og.light.TextureCubeShadowMap.java

License:Open Source License

public TextureCubeShadowMap(int size) {
    texture = GL11.glGenTextures();/*from   w  w w. ja va2s.c om*/
    GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, texture);
    GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    Util.checkErr();
    GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    Util.checkErr();
    GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    Util.checkErr();
    GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
    Util.checkErr();
    GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL12.GL_TEXTURE_WRAP_R, GL12.GL_CLAMP_TO_EDGE);
    GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL14.GL_TEXTURE_COMPARE_MODE, GL11.GL_NONE);
    GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL14.GL_DEPTH_TEXTURE_MODE, GL11.GL_INTENSITY);
    // GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP,
    // GL12.GL_TEXTURE_BASE_LEVEL, 0);
    // GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP,
    // GL12.GL_TEXTURE_MAX_LEVEL, 0);

    for (int i = 0; i < 6; i++) {
        GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL11.GL_DEPTH_COMPONENT, size, size, 0,
                GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (FloatBuffer) null);
    }
    Util.checkErr();
    GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, 0);

    // texture2 = GL11.glGenTextures();
    // GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture2);
    // GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,GL11.GL_RGB, size, size,
    // 0,GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (FloatBuffer)null);
    // 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.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.glBindTexture(GL11.GL_TEXTURE_2D, 0);

}

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

License:Open Source License

protected static TextureAtlas create(ArrayList<String> files) {
    if (files.size() == 0) {
        // WAT/*from  ww  w. java  2  s  .c  o m*/
        files.add("blank");
    }
    TextureAtlas atlas = new TextureAtlas();
    atlas.id = GL11.glGenTextures();

    GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, atlas.id);
    Util.checkErr();
    atlas.depth = getPowerOfTwo(files.size());
    while (files.size() < atlas.depth) {
        files.add("blank");
    }
    // System.out.println("Texture Atlas id : " + atlas.id + " size : "
    // + files.size() + " end size : " + atlas.depth);
    atlas.width = 1;
    atlas.height = 1;
    for (int i = 0; i < files.size(); i++) {
        if (files.get(i).equalsIgnoreCase("blank") || files.get(i).equalsIgnoreCase("none")) {
            continue;
        }
        PNGDecoder firstImage = getImageFile(files.get(i));
        if (firstImage == null) {
            return null;
        }
        atlas.width = firstImage.getWidth();
        atlas.height = firstImage.getHeight();
        break;
    }
    Util.checkErr();
    // ARBTextureStorage.glTexStorage3D(atlas.getTextureType(), 1,
    // GL11.GL_RGBA, atlas.width, atlas.height, atlas.depth); // Throws an
    // error for GL_RGBA, it wants an internal format only.
    // Inversly. No GL_RGBAx values work.
    // Util.checkErr(false);
    ByteBuffer buf = ByteBuffer.allocateDirect(4 * atlas.width * atlas.height * atlas.depth);
    for (String location : files) {
        if (location.equalsIgnoreCase("blank")) {
            byte b = (byte) 255;
            for (int count = 0; count < atlas.width * atlas.height; count++) {
                buf.put(b).put(b).put(b).put(b);
            }
            // System.out
            // .println("Added BLANK to atlas as number" + (index++));
            atlas.token = atlas.token + ":blank";
        } else if (location.equalsIgnoreCase("none")) {
            byte b = (byte) 0;
            for (int count = 0; count < atlas.width * atlas.height; count++) {
                buf.put(b).put(b).put(b).put(b);
            }
            // System.out
            // .println("Added BLANK to atlas as number" + (index++));
            atlas.token = atlas.token + ":blank";
        } else {

            PNGDecoder decoder = getImageFile(location);
            if (decoder == null) {
                byte b = (byte) 255;
                for (int count = 0; count < atlas.width * atlas.height; count++) {
                    buf.put(b).put(b).put(b).put(b);
                }
            } else {
                if (decoder.getWidth() != atlas.width || decoder.getHeight() != atlas.height) {
                    System.out.println(
                            "Non-standard image size. All images" + " in an atlas must have the same size");
                    System.out.println("Offender : " + location);
                    return null;
                }
                try {
                    decoder.decode(buf, atlas.width * 4, Format.RGBA);
                } catch (IOException e) {
                    new DebugExceptionHandler(e);
                }
            }
            // System.out.println("Added " + location +
            // " to atlas as number "
            // + (index++));
            atlas.token = atlas.token + ":" + location;
        }

    }
    buf.flip();
    GL12.glTexImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, atlas.width, atlas.height, atlas.depth, 0,
            GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
    Util.checkErr();
    GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
    Util.checkErr();
    GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
    Util.checkErr();
    GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    Util.checkErr();

    GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    Util.checkErr();
    // GL30.glGenerateMipmap(GL30.GL_TEXTURE_2D_ARRAY);
    GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0);
    Util.checkErr();
    return atlas;
}

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

License:Open Source License

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

    this.size = getPowerOfTwo(size);
    id = GL11.glGenTextures();//ww w.java 2  s .c  om
    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(GL30.GL_TEXTURE_2D_ARRAY, id);
    GL12.glTexImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, this.size, this.size, 1, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, mainBuf);
    GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
    GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
    GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0);
}

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

License:Open Source License

@Override
public void bind(int t) {

    GL13.glActiveTexture(t);//from ww  w  . j a  v  a 2s  . co m
    if (pixels != null) {
        id = GL11.glGenTextures();
        mainBuf = BufferUtils.createByteBuffer(4 * this.size * this.size);
        // for (int i = this.size - 1; i >= 0; i--) {
        for (int i = 0; i < this.size; i++) {
            for (int j = 0; j < this.size; j++) {
                float r = 1f, g = 1f, b = 1f, a = 0f;
                if (i < py && j < px) {
                    int pixel = pixels[i * px + j];
                    r = (float) ((pixel >> 16) & 0xFF) / 255f;
                    g = (float) ((pixel >> 8) & 0xFF) / 255f;
                    b = (float) (pixel & 0xFF) / 255f;
                    a = (float) ((pixel >> 24) & 0xFF) / 255f;
                }
                addColour(r, g, b, a);
            }
        }
        mainBuf.flip();
        GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, id);
        GL12.glTexImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, this.size, this.size, 1, 0, GL11.GL_RGBA,
                GL11.GL_UNSIGNED_BYTE, mainBuf);
        GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0);
        pixels = null;
    }
    synchronized (changeList) {
        if (changeList.size() < 10) {
            GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 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(GL30.GL_TEXTURE_2D_ARRAY, 0, change.getX(), change.getY(), 1, 1,
                        GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, buf);
            }
            GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 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(GL30.GL_TEXTURE_2D_ARRAY, id);
            Util.checkErr();
            GL12.glTexSubImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, size, size, 0, GL12.GL_BGRA,
                    GL11.GL_UNSIGNED_BYTE, mainBuf);
            Util.checkErr();
            GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0);
            Util.checkErr();

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