Search in sources :

Example 1 with PNGDecoder

use of de.matthiasmann.twl.utils.PNGDecoder in project chunkstories by Hugobros3.

the class CubemapGL method loadCubemapFromDisk.

public int loadCubemapFromDisk() {
    bind();
    ByteBuffer temp;
    String[] names = { "right", "left", "top", "bottom", "front", "back" };
    if (Client.getInstance().getContent().getAsset((name + "/front.png")) == null) {
        logger().info("Can't find front.png from CS-format skybox, trying MC format.");
        names = new String[] { "panorama_1", "panorama_3", "panorama_4", "panorama_5", "panorama_0", "panorama_2" };
    }
    try {
        for (int i = 0; i < 6; i++) {
            Asset pngFile = Client.getInstance().getContent().getAsset(name + "/" + names[i] + ".png");
            if (pngFile == null)
                throw new FileNotFoundException(name + "/" + names[i] + ".png");
            PNGDecoder decoder = new PNGDecoder(pngFile.read());
            temp = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
            decoder.decode(temp, decoder.getWidth() * 4, Format.RGBA);
            temp.flip();
            this.size = decoder.getHeight();
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, type.getInternalFormat(), decoder.getWidth(), decoder.getHeight(), 0, type.getFormat(), type.getType(), temp);
            // Anti alias
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            // Anti seam
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        }
    } catch (FileNotFoundException e) {
        logger().warn("Couldn't find file : " + e.getMessage());
    } catch (IOException e) {
        logger().error("Failed to load properly cubemap : " + name);
    }
    return glId;
}
Also used : PNGDecoder(de.matthiasmann.twl.utils.PNGDecoder) FileNotFoundException(java.io.FileNotFoundException) Asset(io.xol.chunkstories.api.content.Asset) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with PNGDecoder

use of de.matthiasmann.twl.utils.PNGDecoder in project chunkstories by Hugobros3.

the class Texture2DAsset method loadTextureFromAsset.

public int loadTextureFromAsset() {
    if (!Client.getInstance().getGameWindow().isMainGLWindow()) {
        System.out.println("isn't main thread, scheduling load");
        scheduledForLoad = true;
        return -1;
    }
    scheduledForLoad = false;
    // TODO we probably don't need half this shit
    bind();
    try {
        InputStream is = asset.read();
        PNGDecoder decoder = new PNGDecoder(is);
        width = decoder.getWidth();
        height = decoder.getHeight();
        ByteBuffer temp = ByteBuffer.allocateDirect(4 * width * height);
        decoder.decode(temp, width * 4, Format.RGBA);
        is.close();
        // ChunkStoriesLogger.getInstance().log("decoded " + width + " by " + height + " pixels (" + name + ")", ChunkStoriesLogger.LogType.RENDERING, ChunkStoriesLogger.LogLevel.DEBUG);
        temp.flip();
        bind();
        glTexImage2D(GL_TEXTURE_2D, 0, type.getInternalFormat(), width, height, 0, type.getFormat(), type.getType(), (ByteBuffer) temp);
        this.applyTextureWrapping();
        this.applyFiltering();
        this.computeMipmaps();
    } catch (FileNotFoundException e) {
        logger().warn("Couldn't find file : " + e.getMessage());
    } catch (IOException e) {
        logger().error("Error loading file : " + e.getMessage());
        e.printStackTrace();
    }
    mipmapsUpToDate = false;
    return glId;
}
Also used : PNGDecoder(de.matthiasmann.twl.utils.PNGDecoder) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with PNGDecoder

use of de.matthiasmann.twl.utils.PNGDecoder in project chunkstories by Hugobros3.

the class Texture2DFile method loadTextureFromFile.

public int loadTextureFromFile() {
    if (!Client.getInstance().getGameWindow().isMainGLWindow()) {
        System.out.println("isn't main thread, scheduling load");
        scheduledForLoad = true;
        return -1;
    }
    scheduledForLoad = false;
    // TODO we probably don't need half this shit
    bind();
    try {
        InputStream is = new FileInputStream(file);
        PNGDecoder decoder = new PNGDecoder(is);
        width = decoder.getWidth();
        height = decoder.getHeight();
        ByteBuffer temp = ByteBuffer.allocateDirect(4 * width * height);
        decoder.decode(temp, width * 4, Format.RGBA);
        is.close();
        // ChunkStoriesLogger.getInstance().log("decoded " + width + " by " + height + " pixels (" + name + ")", ChunkStoriesLogger.LogType.RENDERING, ChunkStoriesLogger.LogLevel.DEBUG);
        temp.flip();
        bind();
        glTexImage2D(GL_TEXTURE_2D, 0, type.getInternalFormat(), width, height, 0, type.getFormat(), type.getType(), (ByteBuffer) temp);
        this.applyTextureWrapping();
        this.applyFiltering();
        this.computeMipmaps();
    } catch (FileNotFoundException e) {
        logger().warn("Couldn't find file : " + e.getMessage());
    } catch (IOException e) {
        logger().error("Error loading file : " + e.getMessage());
        e.printStackTrace();
    }
    mipmapsUpToDate = false;
    return glId;
}
Also used : PNGDecoder(de.matthiasmann.twl.utils.PNGDecoder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 4 with PNGDecoder

use of de.matthiasmann.twl.utils.PNGDecoder in project chunkstories by Hugobros3.

the class IconLoader method getByteBufferData.

ByteBuffer getByteBufferData(String name) {
    try {
        PNGDecoder decoder = new PNGDecoder(getClass().getResourceAsStream(name));
        int width = decoder.getWidth();
        int height = decoder.getHeight();
        ByteBuffer temp = ByteBuffer.allocateDirect(4 * width * height);
        decoder.decode(temp, width * 4, Format.RGBA);
        temp.flip();
        return temp;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : PNGDecoder(de.matthiasmann.twl.utils.PNGDecoder) ByteBuffer(java.nio.ByteBuffer)

Example 5 with PNGDecoder

use of de.matthiasmann.twl.utils.PNGDecoder in project Terasology by MovingBlocks.

the class PNGTextureFormat method load.

@Override
public TextureData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    try (InputStream pngStream = inputs.get(0).openStream()) {
        PNGDecoder decoder = new PNGDecoder(pngStream);
        ByteBuffer buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buf, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
        buf.flip();
        int height = decoder.getHeight();
        int width = decoder.getWidth();
        Texture.FilterMode filterMode = defaultFilterMode;
        Texture.WrapMode wrapMode = Texture.WrapMode.CLAMP;
        return new TextureData(width, height, new ByteBuffer[] { buf }, wrapMode, filterMode);
    } catch (UnsupportedOperationException e) {
        throw new IOException(e);
    }
}
Also used : PNGDecoder(de.matthiasmann.twl.utils.PNGDecoder) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

PNGDecoder (de.matthiasmann.twl.utils.PNGDecoder)9 ByteBuffer (java.nio.ByteBuffer)9 IOException (java.io.IOException)7 InputStream (java.io.InputStream)5 FileNotFoundException (java.io.FileNotFoundException)3 BufferedImage (java.awt.image.BufferedImage)2 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 Asset (io.xol.chunkstories.api.content.Asset)1 FileInputStream (java.io.FileInputStream)1 FloatBuffer (java.nio.FloatBuffer)1 GLFW.glfwWindowHint (org.lwjgl.glfw.GLFW.glfwWindowHint)1 GL11.glGetString (org.lwjgl.opengl.GL11.glGetString)1