Java Code Examples for com.jogamp.opengl.GL3#glTexSubImage3D()

The following examples show how to use com.jogamp.opengl.GL3#glTexSubImage3D() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GLTools.java    From constellation with Apache License 2.0 5 votes vote down vote up
/**
 * Load a BufferedImage array into a texture array.
 * <p>
 * Each BufferedImage must be of type BufferedImage.TYPE_4BYTE_ABGR. The
 * images will be loaded at 0,0 at each level of the texture array.
 * <p>
 * It appears that the images must have a row length that is a multiple of
 * four. This is probably due to the particular format we're using, and
 * could probably be worked around, but the simple fix is to check your row
 * length.
 *
 * @param gl the current OpenGL context.
 * @param textureName The name of the texture to bind to with BindTexture().
 * @param images A List of BufferedImages of type
 * BufferedImage.TYPE_4BYTE_ABGR.
 * @param maxWidth The maximum width of the images.
 * @param maxHeight The maximum height of the images.
 * @param minFilter Texture selection with TEXTURE_MIN_FILTER.
 * @param magFilter Texture selection with TEXTURE_MAG_FILTER.
 * @param wrapMode texture wrap mode with TEXTURE_WRAP_S and TEXTURE_WRAP_T.
 */
public static void loadTextures(final GL3 gl, final int textureName, final List<BufferedImage> images, final int maxWidth, final int maxHeight, final int minFilter, final int magFilter, final int wrapMode) {
    gl.glBindTexture(GL3.GL_TEXTURE_2D_ARRAY, textureName);

    gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_WRAP_S, wrapMode);
    gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_WRAP_T, wrapMode);
    gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_MIN_FILTER, minFilter);
    gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_MAG_FILTER, magFilter);

    // Call glTexImage3D() to create the buffer here: we've assumed the internalformat and format.
    gl.glTexImage3D(GL3.GL_TEXTURE_2D_ARRAY, 0, GL.GL_RGBA, maxWidth, maxHeight, images.size(), 0, GL.GL_RGBA, GL3.GL_UNSIGNED_BYTE, null);

    int i = 0;
    for (BufferedImage image : images) {
        try {
            final TextureData data = AWTTextureIO.newTextureData(gl.getGLProfile(), image, false);

            if (data.getWidth() > maxWidth || data.getHeight() > maxHeight) {
                throw new RenderException(String.format("Image %d is too large", i));
            }

            // Images are always placed at 0,0.
            final int xoffset = 0;
            final int yoffset = 0;
            final int zoffset = i;
            gl.glTexSubImage3D(GL3.GL_TEXTURE_2D_ARRAY, 0, xoffset, yoffset, zoffset, data.getWidth(), data.getHeight(), 1, data.getPixelFormat(), GL3.GL_UNSIGNED_BYTE, data.getBuffer());
            data.destroy();
        } catch (final RuntimeException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }

        i++;
    }
}
 
Example 2
Source File: GlyphManagerOpenGLController.java    From constellation with Apache License 2.0 4 votes vote down vote up
private void updateGlyphs(GL3 gl) {

        final int width = glyphManager.getTextureWidth();
        final int height = glyphManager.getTextureHeight();
        final int pageCount = glyphManager.getGlyphPageCount();
        final int glyphCount = glyphManager.getGlyphCount();

        // If there have been new glyphs then some of then might be on the last page
        // buffered to the graphics card. We need to mark this last page as unbuffered
        // so that it gets buffered again.
        if (glyphCount > glyphsGlyphsBuffered && glyphsPagesBuffered > 0) {
            glyphsPagesBuffered--;
            glyphsPageBuffers.remove(glyphsPageBuffers.size() - 1);
        }

        glyphsGlyphsBuffered = glyphCount;

        if (pageCount > glyphsPageCapacity) {
            gl.glBindTexture(GL3.GL_TEXTURE_2D_ARRAY, glyphsTextureName[0]);
            gl.glTexImage3D(GL3.GL_TEXTURE_2D_ARRAY, 0, INTERNAL_FORMAT, width, height, pageCount, 0, EXTERNAL_FORMAT, GL3.GL_UNSIGNED_BYTE, null);
            glyphsPageCapacity = pageCount;
            glyphsPagesBuffered = 0;
        }

        while (glyphsPagesBuffered < pageCount) {

            final ByteBuffer pixelBuffer;
            if (glyphsPageBuffers.size() > glyphsPagesBuffered) {
                pixelBuffer = glyphsPageBuffers.get(glyphsPagesBuffered);
                pixelBuffer.rewind();
            } else {
                pixelBuffer = ByteBuffer.allocateDirect(width * height);
                glyphManager.readGlyphTexturePage(glyphsPagesBuffered, pixelBuffer);
                glyphsPageBuffers.add(pixelBuffer);
                pixelBuffer.flip();
            }

            gl.glBindTexture(GL3.GL_TEXTURE_2D_ARRAY, glyphsTextureName[0]);
            gl.glTexSubImage3D(GL3.GL_TEXTURE_2D_ARRAY, 0, 0, 0, glyphsPagesBuffered, width, height, 1, EXTERNAL_FORMAT, GL3.GL_UNSIGNED_BYTE, pixelBuffer);

            glyphsPagesBuffered++;
        }
    }
 
Example 3
Source File: GLTools.java    From constellation with Apache License 2.0 4 votes vote down vote up
/**
 * Load an array of icon textures.
 * <p>
 * We assume that the textures being loaded are icons, and therefore are
 * roughly the same size (with a maximum of (width,height).
 * <p>
 * The array is limited to GL_MAX_ARRAY_TEXTURE_LAYERS layers. This can be
 * fairly low (512 on low-end systems), so icons are loaded into an 8x8 icon
 * matrix in each layer, thus giving a maximum of 512x8x8=32768 icons. (This
 * assumes that GL_MAX_3D_TEXTURE_SIZE is big enough to take that many
 * pixels. With the current icon size of 256x256, then
 * GL_MAX_3D_TEXTURE_SIZE must be at least 2048.)
 * <p>
 * Icons that are smaller than (width,height) are offset so they are
 * centred, so the shader can just draw the icons without worrying about
 * where in the texture they are.
 * <p>
 * It appears that the images must have a row length that is a multiple of
 * four. This is probably due to the particular format we're using, and
 * could probably be worked around, but the simple fix is to check your row
 * length.
 *
 * @param glCurrent the current OpenGL context.
 * @param icons a list of icons that need to added to the buffer.
 * @param width the width of each icon.
 * @param height the height of each icon.
 *
 * @return the id of the texture buffer.
 */
public static int loadSharedIconTextures(final GL3 glCurrent, final List<ConstellationIcon> icons, final int width, final int height) {
    final int[] v = new int[1];
    glCurrent.glGetIntegerv(GL3.GL_MAX_ARRAY_TEXTURE_LAYERS, v, 0);
    final int maxIcons = v[0] * 64;
    if (icons.size() > maxIcons) {
        System.out.printf("****\n**** Warning: nIcons %d > GL_MAX_ARRAY_TEXTURE_LAYERS %d\n****\n", icons.size(), maxIcons);
    }

    final int nIcons = Math.min(icons.size(), maxIcons);

    glCurrent.getContext().release();
    final GL3 gl = (GL3) SharedDrawable.getSharedAutoDrawable().getGL();
    final int result = gl.getContext().makeCurrent();
    if (result == GLContext.CONTEXT_NOT_CURRENT) {
        glCurrent.getContext().makeCurrent();
        throw new RenderException("Could not make texture context current.");
    }

    final int[] textureName = new int[1];
    try {
        textureName[0] = SharedDrawable.getIconTextureName();
        gl.glBindTexture(GL3.GL_TEXTURE_2D_ARRAY, textureName[0]);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_WRAP_S, GL3.GL_CLAMP_TO_EDGE);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_WRAP_T, GL3.GL_CLAMP_TO_EDGE);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_LINEAR);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_LINEAR);
        gl.glTexImage3D(GL3.GL_TEXTURE_2D_ARRAY, 0, GL.GL_RGBA, width * 8, height * 8, (nIcons + 63) / 64, 0, GL.GL_RGBA, GL3.GL_UNSIGNED_BYTE, null);

        final Iterator<ConstellationIcon> iconIterator = icons.iterator();
        for (int i = 0; i < nIcons; i++) {
            final ConstellationIcon icon = iconIterator.next();
            try {
                BufferedImage iconImage = icon.buildBufferedImage();

                if (iconImage != null) {
                    // Appears to be a bug in JOGL where texture provider for PNG files does not flip the texture.
                    final TextureData data = AWTTextureIO.newTextureData(gl.getGLProfile(), iconImage, false);

                    if (data.getWidth() > width || data.getHeight() > height) {
                        throw new RenderException(String.format("Image %d is too large (width %d>%d, height %d>%d)", i, data.getWidth(), width, data.getHeight(), height));
                    }

                    // Offset each icon into an 8x8 matrix.
                    // There are multiple icons in each
                    // Allow for icons that are smaller than width,height.
                    final int xoffset = (width - data.getWidth()) / 2 + (width * (i & 7));
                    final int yoffset = (height - data.getHeight()) / 2 + (height * ((i >>> 3) & 7));
                    final int zoffset = i >>> 6;
                    gl.glTexSubImage3D(GL3.GL_TEXTURE_2D_ARRAY, 0, xoffset, yoffset, zoffset, data.getWidth(), data.getHeight(), 1, data.getPixelFormat(), GL3.GL_UNSIGNED_BYTE, data.getBuffer());
                    data.destroy();
                }
            } catch (final RuntimeException ex) {
                System.out.printf("##%n## GLTools.loadTextures() icon %d throwable: %s%n##%n", i, ex);
                LOGGER.log(Level.SEVERE, null, ex);
            }
        }
    } finally {
        gl.getContext().release();
        glCurrent.getContext().makeCurrent();
    }

    return textureName[0];
}