com.jogamp.opengl.util.texture.TextureData Java Examples

The following examples show how to use com.jogamp.opengl.util.texture.TextureData. 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: BasicMercatorTiledImageLayer.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
private boolean loadTexture( MercatorTextureTile tile, java.net.URL textureURL ) {
    TextureData textureData;

    synchronized (this.fileLock) {
        textureData = readTexture(textureURL, this.isUseMipMaps());
    }

    if (textureData == null)
        return false;

    tile.setTextureData(textureData);
    if (tile.getLevelNumber() != 0 || !this.isRetainLevelZeroTiles())
        this.addTileToCache(tile);

    return true;
}
 
Example #2
Source File: GLTools.java    From constellation with Apache License 2.0 5 votes vote down vote up
public static Texture loadTexture(final GL3 gl, final InputStream in, final String ext, final int minFilter, final int magFilter, final int wrapMode) throws IOException {
        // NVS-415: Appears to be a bug in JOGL where texture provider for PNG files does not flip the texture.
//         final TextureData data = TextureIO.newTextureData(gl.getGLProfile(), in, false, ext);
        final TextureData data = TextureIO.newTextureData(gl.getGLProfile(), in, false, null);
        final Texture tex = TextureIO.newTexture(data);

        gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_WRAP_S, wrapMode);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_WRAP_T, wrapMode);

        gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_MIN_FILTER, minFilter);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_MAG_FILTER, magFilter);

        return tex;
    }
 
Example #3
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 #4
Source File: HelloGlobe.java    From CPE552-Java with GNU General Public License v3.0 5 votes vote down vote up
private void initTexture(GL4 gl) {

        try {
            URL texture = getClass().getClassLoader().getResource("images/globe.png");

            TextureData textureData = TextureIO.newTextureData(gl.getGLProfile(), texture, false, TextureIO.PNG);

            gl.glCreateTextures(GL_TEXTURE_2D, 1, textureName);

            gl.glTextureParameteri(textureName.get(0), GL_TEXTURE_BASE_LEVEL, 0);
            gl.glTextureParameteri(textureName.get(0), GL_TEXTURE_MAX_LEVEL, 0);

            gl.glTextureStorage2D(textureName.get(0),
                    1, // level
                    textureData.getInternalFormat(),
                    textureData.getWidth(), textureData.getHeight());

            gl.glTextureSubImage2D(textureName.get(0),
                    0, // level
                    0, 0, // offset
                    textureData.getWidth(), textureData.getHeight(),
                    textureData.getPixelFormat(), textureData.getPixelType(),
                    textureData.getBuffer());

        } catch (IOException ex) {
            Logger.getLogger(HelloGlobe.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
Example #5
Source File: JOGLRenderer.java    From WarpPI with Apache License 2.0 5 votes vote down vote up
@Deprecated
	static Texture importTexture(final GL gl, final String string) throws IOException {
		final FileInputStream f = new FileInputStream("test.png");
		final TextureData tx_dat = TextureIO.newTextureData(gl.getGLProfile(), f, false, TextureIO.PNG);
		final Texture tex = new Texture(gl, tx_dat);
		tex.setTexParameteri(gl, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
		tex.setTexParameteri(gl, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
//		tex.setTexParameteri(gl, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
//		tex.setTexParameteri(gl, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
		return tex;
	}
 
Example #6
Source File: HelloGlobe.java    From hello-triangle with MIT License 5 votes vote down vote up
private void initTexture(GL4 gl) {

        try {
            URL texture = getClass().getClassLoader().getResource("images/globe.png");

            TextureData textureData = TextureIO.newTextureData(gl.getGLProfile(), texture, false, TextureIO.PNG);

            gl.glCreateTextures(GL_TEXTURE_2D, 1, textureName);

            gl.glTextureParameteri(textureName.get(0), GL_TEXTURE_BASE_LEVEL, 0);
            gl.glTextureParameteri(textureName.get(0), GL_TEXTURE_MAX_LEVEL, 0);

            gl.glTextureStorage2D(textureName.get(0),
                    1, // level
                    textureData.getInternalFormat(),
                    textureData.getWidth(), textureData.getHeight());

            gl.glTextureSubImage2D(textureName.get(0),
                    0, // level
                    0, 0, // offset
                    textureData.getWidth(), textureData.getHeight(),
                    textureData.getPixelFormat(), textureData.getPixelType(),
                    textureData.getBuffer());

        } catch (IOException ex) {
            Logger.getLogger(HelloGlobe.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
Example #7
Source File: HelloTexture.java    From hello-triangle with MIT License 5 votes vote down vote up
private void initTexture(GL3 gl) {

        try {
            URL texture = getClass().getClassLoader().getResource("images/door.png");

            /* Texture data is an object containing all the relevant information about texture.    */
            TextureData data = TextureIO.newTextureData(gl.getGLProfile(), texture, false, TextureIO.PNG);

            int level = 0;

            gl.glGenTextures(1, textureName);

            gl.glBindTexture(GL_TEXTURE_2D, textureName.get(0));
            {
                gl.glTexImage2D(GL_TEXTURE_2D,
                        level,
                        data.getInternalFormat(),
                        data.getWidth(), data.getHeight(),
                        data.getBorder(),
                        data.getPixelFormat(), data.getPixelType(),
                        data.getBuffer());

                gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
                gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level);

                IntBuffer swizzle = GLBuffers.newDirectIntBuffer(new int[]{GL_RED, GL_GREEN, GL_BLUE, GL_ONE});
                gl.glTexParameterIiv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle);

                destroyBuffer(swizzle);
            }
            gl.glBindTexture(GL_TEXTURE_2D, 0);

        } catch (IOException ex) {
            Logger.getLogger(HelloTextureK.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
Example #8
Source File: TextureCache2.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
Texture buildTexture(final GL gl, final BufferedImage im) {
	try {
		final TextureData data = AWTTextureIO.newTextureData(gl.getGLProfile(), im, true);
		final Texture texture = new Texture(gl, data);
		data.flush();
		return texture;
	} catch (final GLException e) {
		e.printStackTrace();
		return null;
	}
}
 
Example #9
Source File: BasicMercatorTiledImageLayer.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private static TextureData readTexture( java.net.URL url, boolean useMipMaps ) {
        try {
            return OGLUtil.newTextureData(Configuration.getMaxCompatibleGLProfile(), url, useMipMaps);
        } catch (Exception e) {
//            String msg = Logging.getMessage("layers.TextureLayer.ExceptionAttemptingToReadTextureFile", url.toString());
//            Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
            return null;
        }
    }
 
Example #10
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];
}