Example usage for org.lwjgl.opengl GL12 glTexImage3D

List of usage examples for org.lwjgl.opengl GL12 glTexImage3D

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL12 glTexImage3D.

Prototype

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

Source Link

Document

Array version of: #glTexImage3D TexImage3D

Usage

From source file:ar.com.quark.backend.lwjgl.opengl.DesktopGLES30.java

License:Apache License

/**
 * {@inheritDoc}/*from  www .j  av a  2 s. com*/
 */
@Override
public void glTexImage3D(int target, int level, int internal, int width, int height, int depth, int border,
        int format, int type, Int8Array data) {
    GL12.glTexImage3D(target, level, internal, width, height, depth, border, format, type,
            data.<ByteBuffer>data());
}

From source file:com.ardor3d.scene.state.lwjgl.LwjglTextureStateUtil.java

License:Open Source License

/**
 * bind texture and upload image data to card
 *//*from  www  .j  a v a 2s  .  c o  m*/
public static void update(final Texture texture, final int unit) {
    final RenderContext context = ContextManager.getCurrentContext();
    final ContextCapabilities caps = context.getCapabilities();

    texture.getTextureKey().setClean(context.getGlContextRep());

    // our texture type:
    final Texture.Type type = texture.getType();

    // bind our texture id to this unit.
    doTextureBind(texture, unit, false);

    // pass image data to OpenGL
    final Image image = texture.getImage();
    final boolean hasBorder = texture.hasBorder();
    if (image == null) {
        logger.warning("Image data for texture is null.");
    }

    // set alignment to support images with width % 4 != 0, as images are
    // not aligned
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    // Get texture image data. Not all textures have image data.
    // For example, ApplyMode.Combine modes can use primary colors,
    // texture output, and constants to modify fragments via the
    // texture units.
    if (image != null) {
        final int maxSize = caps.getMaxTextureSize();
        final int actualWidth = image.getWidth();
        final int actualHeight = image.getHeight();

        final boolean needsPowerOfTwo = !caps.isNonPowerOfTwoTextureSupported()
                && (!MathUtils.isPowerOfTwo(image.getWidth()) || !MathUtils.isPowerOfTwo(image.getHeight()));
        if (actualWidth > maxSize || actualHeight > maxSize || needsPowerOfTwo) {
            if (needsPowerOfTwo) {
                logger.warning(
                        "(card unsupported) Attempted to apply texture with size that is not power of 2: "
                                + image.getWidth() + " x " + image.getHeight());
            }
            if (actualWidth > maxSize || actualHeight > maxSize) {
                logger.warning(
                        "(card unsupported) Attempted to apply texture with size bigger than max texture size ["
                                + maxSize + "]: " + image.getWidth() + " x " + image.getHeight());
            }

            int w = actualWidth;
            if (needsPowerOfTwo) {
                w = MathUtils.nearestPowerOfTwo(actualWidth);
            }
            if (w > maxSize) {
                w = maxSize;
            }

            int h = actualHeight;
            if (needsPowerOfTwo) {
                h = MathUtils.nearestPowerOfTwo(actualHeight);
            }
            if (h > maxSize) {
                h = maxSize;
            }
            logger.warning("Rescaling image to " + w + " x " + h + " !!!");

            // must rescale image to get "top" mipmap texture image
            final int pixFormat = LwjglTextureUtil.getGLPixelFormat(image.getDataFormat());
            final int pixDataType = LwjglTextureUtil.getGLPixelDataType(image.getDataType());
            final int bpp = ImageUtils.getPixelByteSize(image.getDataFormat(), image.getDataType());
            final ByteBuffer scaledImage = BufferUtils.createByteBuffer((w + 4) * h * bpp);
            final int error = MipMap.gluScaleImage(pixFormat, actualWidth, actualHeight, pixDataType,
                    image.getData(0), w, h, pixDataType, scaledImage);
            if (error != 0) {
                Util.checkGLError();
            }

            image.setWidth(w);
            image.setHeight(h);
            image.setData(scaledImage);
        }

        if (!texture.getMinificationFilter().usesMipMapLevels()
                && !texture.getTextureStoreFormat().isCompressed()) {

            // Load textures which do not need mipmap auto-generating and
            // which aren't using compressed images.

            switch (texture.getType()) {
            case TwoDimensional:
                // ensure the buffer is ready for reading
                image.getData(0).rewind();
                // send top level to card
                GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,
                        LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(),
                        image.getHeight(), hasBorder ? 1 : 0,
                        LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                        LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0));
                break;
            case OneDimensional:
                // ensure the buffer is ready for reading
                image.getData(0).rewind();
                // send top level to card
                GL11.glTexImage1D(GL11.GL_TEXTURE_1D, 0,
                        LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(),
                        hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                        LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0));
                break;
            case ThreeDimensional:
                if (caps.isTexture3DSupported()) {
                    // concat data into single buffer:
                    int dSize = 0;
                    int count = 0;
                    ByteBuffer data = null;
                    for (int x = 0; x < image.getData().size(); x++) {
                        if (image.getData(x) != null) {
                            data = image.getData(x);
                            dSize += data.limit();
                            count++;
                        }
                    }
                    // reuse buffer if we can.
                    if (count != 1) {
                        data = BufferUtils.createByteBuffer(dSize);
                        for (int x = 0; x < image.getData().size(); x++) {
                            if (image.getData(x) != null) {
                                data.put(image.getData(x));
                            }
                        }
                        // ensure the buffer is ready for reading
                        data.flip();
                    }
                    // send top level to card
                    GL12.glTexImage3D(GL12.GL_TEXTURE_3D, 0,
                            LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                            image.getWidth(), image.getHeight(), image.getDepth(), hasBorder ? 1 : 0,
                            LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                            LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data);
                } else {
                    logger.warning("This card does not support Texture3D.");
                }
                break;
            case CubeMap:
                // NOTE: Cubemaps MUST be square, so height is ignored
                // on purpose.
                if (caps.isTextureCubeMapSupported()) {
                    for (final TextureCubeMap.Face face : TextureCubeMap.Face.values()) {
                        // ensure the buffer is ready for reading
                        image.getData(face.ordinal()).rewind();
                        // send top level to card
                        GL11.glTexImage2D(getGLCubeMapFace(face), 0,
                                LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                image.getWidth(), image.getWidth(), hasBorder ? 1 : 0,
                                LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                                LwjglTextureUtil.getGLPixelDataType(image.getDataType()),
                                image.getData(face.ordinal()));
                    }
                } else {
                    logger.warning("This card does not support Cubemaps.");
                }
                break;
            }
        } else if (texture.getMinificationFilter().usesMipMapLevels() && !image.hasMipmaps()
                && !texture.getTextureStoreFormat().isCompressed()) {

            // For textures which need mipmaps auto-generating and which
            // aren't using compressed images, generate the mipmaps.
            // A new mipmap builder may be needed to build mipmaps for
            // compressed textures.

            if (caps.isAutomaticMipmapsSupported()) {
                // Flag the card to generate mipmaps
                GL11.glTexParameteri(getGLType(type), SGISGenerateMipmap.GL_GENERATE_MIPMAP_SGIS, GL11.GL_TRUE);
            }

            switch (type) {
            case TwoDimensional:
                // ensure the buffer is ready for reading
                image.getData(0).rewind();
                if (caps.isAutomaticMipmapsSupported()) {
                    // send top level to card
                    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,
                            LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                            image.getWidth(), image.getHeight(), hasBorder ? 1 : 0,
                            LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                            LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0));
                } else {
                    // send to card
                    GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D,
                            LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                            image.getWidth(), image.getHeight(),
                            LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                            LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0));
                }
                break;
            case OneDimensional:
                // ensure the buffer is ready for reading
                image.getData(0).rewind();
                if (caps.isAutomaticMipmapsSupported()) {
                    // send top level to card
                    GL11.glTexImage1D(GL11.GL_TEXTURE_1D, 0,
                            LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                            image.getWidth(), hasBorder ? 1 : 0,
                            LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                            LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0));
                } else {
                    // Note: LWJGL's GLU class does not support
                    // gluBuild1DMipmaps.
                    logger.warning(
                            "non-fbo 1d mipmap generation is not currently supported.  Use DDS or a non-mipmap minification filter.");
                    return;
                }
                break;
            case ThreeDimensional:
                if (caps.isTexture3DSupported()) {
                    if (caps.isAutomaticMipmapsSupported()) {
                        // concat data into single buffer:
                        int dSize = 0;
                        int count = 0;
                        ByteBuffer data = null;
                        for (int x = 0; x < image.getData().size(); x++) {
                            if (image.getData(x) != null) {
                                data = image.getData(x);
                                dSize += data.limit();
                                count++;
                            }
                        }
                        // reuse buffer if we can.
                        if (count != 1) {
                            data = BufferUtils.createByteBuffer(dSize);
                            for (int x = 0; x < image.getData().size(); x++) {
                                if (image.getData(x) != null) {
                                    data.put(image.getData(x));
                                }
                            }
                            // ensure the buffer is ready for reading
                            data.flip();
                        }
                        // send top level to card
                        GL12.glTexImage3D(GL12.GL_TEXTURE_3D, 0,
                                LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                image.getWidth(), image.getHeight(), image.getDepth(), hasBorder ? 1 : 0,
                                LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                                LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data);
                    } else {
                        // Note: LWJGL's GLU class does not support
                        // gluBuild3DMipmaps.
                        logger.warning(
                                "non-fbo 3d mipmap generation is not currently supported.  Use DDS or a non-mipmap minification filter.");
                        return;
                    }
                } else {
                    logger.warning("This card does not support Texture3D.");
                    return;
                }
                break;
            case CubeMap:
                // NOTE: Cubemaps MUST be square, so height is ignored
                // on purpose.
                if (caps.isTextureCubeMapSupported()) {
                    if (caps.isAutomaticMipmapsSupported()) {
                        for (final TextureCubeMap.Face face : TextureCubeMap.Face.values()) {
                            // ensure the buffer is ready for reading
                            image.getData(face.ordinal()).rewind();
                            // send top level to card
                            GL11.glTexImage2D(getGLCubeMapFace(face), 0,
                                    LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                    image.getWidth(), image.getWidth(), hasBorder ? 1 : 0,
                                    LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                                    LwjglTextureUtil.getGLPixelDataType(image.getDataType()),
                                    image.getData(face.ordinal()));
                        }
                    } else {
                        for (final TextureCubeMap.Face face : TextureCubeMap.Face.values()) {
                            // ensure the buffer is ready for reading
                            image.getData(face.ordinal()).rewind();
                            // send to card
                            GLU.gluBuild2DMipmaps(getGLCubeMapFace(face),
                                    LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                    image.getWidth(), image.getWidth(),
                                    LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                                    LwjglTextureUtil.getGLPixelDataType(image.getDataType()),
                                    image.getData(face.ordinal()));
                        }
                    }
                } else {
                    logger.warning("This card does not support Cubemaps.");
                    return;
                }
                break;
            }

            if (texture.getTextureMaxLevel() >= 0) {
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL,
                        texture.getTextureMaxLevel());
            }
        } else {
            // Here we handle textures that are either compressed or have predefined mipmaps.
            // Get mipmap data sizes and amount of mipmaps to send to opengl. Then loop through all mipmaps and send
            // them.
            int[] mipSizes = image.getMipMapByteSizes();
            ByteBuffer data = null;

            if (type == Type.CubeMap) {
                if (caps.isTextureCubeMapSupported()) {
                    for (final TextureCubeMap.Face face : TextureCubeMap.Face.values()) {
                        data = image.getData(face.ordinal());
                        int pos = 0;
                        int max = 1;

                        if (mipSizes == null) {
                            mipSizes = new int[] { data.capacity() };
                        } else if (texture.getMinificationFilter().usesMipMapLevels()) {
                            max = mipSizes.length;
                        }

                        // set max mip level
                        GL11.glTexParameteri(getGLCubeMapFace(face), GL12.GL_TEXTURE_MAX_LEVEL, max - 1);

                        for (int m = 0; m < max; m++) {
                            final int width = Math.max(1, image.getWidth() >> m);
                            final int height = Math.max(1, image.getHeight() >> m);

                            data.position(pos);
                            data.limit(pos + mipSizes[m]);

                            if (texture.getTextureStoreFormat().isCompressed()) {
                                ARBTextureCompression.glCompressedTexImage2DARB(getGLCubeMapFace(face), m,
                                        LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                        width, height, hasBorder ? 1 : 0, data);
                            } else {
                                GL11.glTexImage2D(getGLCubeMapFace(face), m,
                                        LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                        width, height, hasBorder ? 1 : 0,
                                        LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                                        LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data);
                            }
                            pos += mipSizes[m];
                        }
                    }
                } else {
                    logger.warning("This card does not support CubeMaps.");
                    return;
                }
            } else {
                data = image.getData(0);
                int pos = 0;
                int max = 1;

                if (mipSizes == null) {
                    mipSizes = new int[] { data.capacity() };
                } else if (texture.getMinificationFilter().usesMipMapLevels()) {
                    max = mipSizes.length;
                }

                // Set max mip level
                switch (type) {
                case TwoDimensional:
                    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, max - 1);
                    break;
                case ThreeDimensional:
                    GL11.glTexParameteri(GL12.GL_TEXTURE_3D, GL12.GL_TEXTURE_MAX_LEVEL, max - 1);
                    break;
                case OneDimensional:
                    GL11.glTexParameteri(GL11.GL_TEXTURE_1D, GL12.GL_TEXTURE_MAX_LEVEL, max - 1);
                    break;
                }

                if (type == Type.ThreeDimensional) {
                    if (caps.isTexture3DSupported()) {
                        // concat data into single buffer:
                        int dSize = 0;
                        int count = 0;
                        for (int x = 0; x < image.getData().size(); x++) {
                            if (image.getData(x) != null) {
                                data = image.getData(x);
                                dSize += data.limit();
                                count++;
                            }
                        }
                        // reuse buffer if we can.
                        if (count != 1) {
                            data = BufferUtils.createByteBuffer(dSize);
                            for (int x = 0; x < image.getData().size(); x++) {
                                if (image.getData(x) != null) {
                                    data.put(image.getData(x));
                                }
                            }
                            // ensure the buffer is ready for reading
                            data.flip();
                        }
                    } else {
                        logger.warning("This card does not support Texture3D.");
                        return;
                    }
                }

                for (int m = 0; m < max; m++) {
                    final int width = Math.max(1, image.getWidth() >> m);
                    final int height = Math.max(1, image.getHeight() >> m);

                    data.position(pos);
                    data.limit(pos + mipSizes[m]);

                    switch (type) {
                    case TwoDimensional:
                        if (texture.getTextureStoreFormat().isCompressed()) {
                            ARBTextureCompression.glCompressedTexImage2DARB(GL11.GL_TEXTURE_2D, m,
                                    LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                    width, height, hasBorder ? 1 : 0, data);
                        } else {
                            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, m,
                                    LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                    width, height, hasBorder ? 1 : 0,
                                    LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                                    LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data);
                        }
                        break;
                    case OneDimensional:
                        if (texture.getTextureStoreFormat().isCompressed()) {
                            ARBTextureCompression.glCompressedTexImage1DARB(GL11.GL_TEXTURE_1D, m,
                                    LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                    width, hasBorder ? 1 : 0, data);
                        } else {
                            GL11.glTexImage1D(GL11.GL_TEXTURE_1D, m,
                                    LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                    width, hasBorder ? 1 : 0,
                                    LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                                    LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data);
                        }
                        break;
                    case ThreeDimensional:
                        final int depth = Math.max(1, image.getDepth() >> m);
                        // already checked for support above...
                        if (texture.getTextureStoreFormat().isCompressed()) {
                            ARBTextureCompression.glCompressedTexImage3DARB(GL12.GL_TEXTURE_3D, m,
                                    LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                    width, height, depth, hasBorder ? 1 : 0, data);
                        } else {
                            GL12.glTexImage3D(GL12.GL_TEXTURE_3D, m,
                                    LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()),
                                    width, height, depth, hasBorder ? 1 : 0,
                                    LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()),
                                    LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data);
                        }
                        break;
                    }
                    pos += mipSizes[m];
                }
            }
            if (data != null) {
                data.clear();
            }
        }
    }
}

From source file:com.badlogic.gdx.backends.jglfw.JglfwGL30.java

License:Apache License

@Override
public void glTexImage3D(int target, int level, int internalformat, int width, int height, int depth,
        int border, int format, int type, Buffer pixels) {
    if (pixels instanceof ByteBuffer)
        GL12.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type,
                (ByteBuffer) pixels);
    else if (pixels instanceof ShortBuffer)
        GL12.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type,
                (ShortBuffer) pixels);
    else if (pixels instanceof IntBuffer)
        GL12.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type,
                (IntBuffer) pixels);
    else if (pixels instanceof FloatBuffer)
        GL12.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type,
                (FloatBuffer) pixels);
    else if (pixels instanceof DoubleBuffer)
        GL12.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type,
                (DoubleBuffer) pixels);
    else//from  ww w .  j  ava  2  s.  c  o m
        throw new GdxRuntimeException(
                "pixels must be ByteBuffer, ShortBuffer, IntBuffer, FloatBuffer or DoubleBuffer");
}

From source file:com.badlogic.gdx.backends.jglfw.JglfwGL30.java

License:Apache License

@Override
public void glTexImage3D(int target, int level, int internalformat, int width, int height, int depth,
        int border, int format, int type, int offset) {
    GL12.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type, offset);
}

From source file:com.badlogic.gdx.backends.lwjgl.LwjglGL30.java

License:Apache License

@Override
public void glTexImage3D(int target, int level, int internalformat, int width, int height, int depth,
        int border, int format, int type, Buffer pixels) {
    if (pixels instanceof ByteBuffer)
        GL12.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type,
                (ByteBuffer) pixels);
    else//from   w w  w  .  j  ava2 s  . c  o m
        throw new GdxRuntimeException("pixels must be byte buffer");
}

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   w  w  w .j a v  a  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();//w  ww. j a  v  a2  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);/*  ww w .  j  a  v a  2s  .  c o 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;
}

From source file:com.xrbpowered.gl.res.textures.ArrayTexture.java

License:Open Source License

public ArrayTexture finish(boolean wrap, boolean filter) {
    intBuffer.flip();/*from   w  ww . j  a  v a2s  .c  om*/
    GL12.glTexImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, width, height, layers, 0, GL12.GL_BGRA,
            GL12.GL_UNSIGNED_INT_8_8_8_8_REV, intBuffer);
    intBuffer = null;
    Texture.setProperties(GL30.GL_TEXTURE_2D_ARRAY, wrap, filter, Client.settings.anisotropy);
    GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0);
    Client.checkError();
    return this;
}

From source file:de.ikosa.mars.viewer.glviewer.engine.GLTextureArrayBuilder.java

License:Open Source License

@Override
public GLTextureArray createTexture() {
    try {//www  .ja v a 2 s . c om
        int files = filePaths.length;
        byte[][] imageData = new byte[files][];
        int totalSize = 0;
        int imageWidth = 0;
        int imageHeight = 0;
        // read in files
        for (int file = 0; file < files; file++) {
            InputStream inputStream = new FileInputStream(filePaths[file]);
            BufferedImage image = ImageIO.read(inputStream);

            if (file > 0)
                if (imageWidth != image.getWidth() | imageHeight != image.getHeight())
                    ML.f("Incompatible images in 3D texture...");

            imageWidth = image.getWidth();
            imageHeight = image.getHeight();

            imageData[file] = GLPNGLoader.loadPNG(image);
            totalSize += imageData[file].length;
        }

        // store in consecutive buffer
        ByteBuffer buffer = ByteBuffer.allocateDirect(totalSize);
        for (int file = 0; file < files; file++) {
            byte[] singleImageData = imageData[file];
            for (int i = 0; i < singleImageData.length; i++)
                buffer.put(singleImageData[i]);
        }

        buffer.flip();

        int textureId = GL11.glGenTextures();
        GLRenderer2Stage.errorCheck("generating texture id");

        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GLRenderer2Stage.errorCheck("activating texture image unit");

        GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, textureId);
        GLRenderer2Stage.errorCheck("binding 2d texture array");

        GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
        GLRenderer2Stage.errorCheck("setting unpack aligment");

        GL12.glTexImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, imageWidth, imageHeight, files, 0,
                GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
        GLRenderer2Stage.errorCheck("storing 2d texture array data");

        GL30.glGenerateMipmap(GL30.GL_TEXTURE_2D_ARRAY);
        GLRenderer2Stage.errorCheck("generating 2d texture array mipmaps");

        GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0);
        GLRenderer2Stage.errorCheck("unbinding 2d texture array");

        return new GLTextureArray(name, textureId);
    } catch (Exception e) {
        ML.f(e);
    }
    return null;
}