Search in sources :

Example 6 with PNGDecoder

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

the class WorldAtlasImpl method createAtlasMipmaps.

private ByteBuffer[] createAtlasMipmaps(int numMipMaps, Color initialColor, List<BlockTile> tileImages, String screenshotName, List<BlockTile> alphaMaskTiles) {
    ByteBuffer[] data = new ByteBuffer[numMipMaps];
    for (int i = 0; i < numMipMaps; ++i) {
        BufferedImage image = generateAtlas(i, tileImages, initialColor);
        if (alphaMaskTiles.size() > 0) {
            BufferedImage alphaMask = generateAtlas(i, alphaMaskTiles, Color.BLACK);
            storeGreyscaleMapIntoAlpha(image, alphaMask);
        }
        if (i == 0) {
            try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(PathManager.getInstance().getScreenshotPath().resolve(screenshotName)))) {
                ImageIO.write(image, "png", stream);
            } catch (IOException e) {
                logger.warn("Failed to write atlas");
            }
        }
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            ImageIO.write(image, "png", bos);
            PNGDecoder decoder = new PNGDecoder(new ByteArrayInputStream(bos.toByteArray()));
            ByteBuffer buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
            decoder.decode(buf, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
            buf.flip();
            data[i] = buf;
        } catch (IOException e) {
            logger.error("Failed to create atlas texture");
        }
    }
    return data;
}
Also used : PNGDecoder(de.matthiasmann.twl.utils.PNGDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) BufferedOutputStream(java.io.BufferedOutputStream) BufferedImage(java.awt.image.BufferedImage)

Example 7 with PNGDecoder

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

the class GLFWGameWindow method displaySplashScreen.

@SuppressWarnings("unused")
private void displaySplashScreen() throws IOException {
    int texture = glGenTextures();
    InputStream is = getClass().getResourceAsStream("/splash.png");
    PNGDecoder decoder = new PNGDecoder(is);
    int width = decoder.getWidth();
    int 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();
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) temp);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    int shaderProgramId = glCreateProgram();
    int vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    int fragShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    String vertexSource = "#version 330\n\n\nin vec3 vertexIn;\nout vec2 texCoord;\nuniform float ratio;\n\nvoid main()\n{\ngl_Position = vec4(vertexIn.x*ratio, vertexIn.y, 0.0, 1.0);\ntexCoord = vertexIn.xy*0.5+0.5;\n}";
    String fragSource = "#version 330\nuniform sampler2D diffuseTexture;\n\nin vec2 texCoord;\nout vec4 fragColor;\n\nvoid main()\n{\nfragColor = texture(diffuseTexture, vec2(texCoord.x, 1.0-texCoord.y));\n}\n";
    // System.out.println(vertexSource);
    // System.out.println(fragSource);
    glShaderSource(vertexShaderId, vertexSource);
    glCompileShader(vertexShaderId);
    glBindFragDataLocation(shaderProgramId, 0, "fragColor");
    glShaderSource(fragShaderId, fragSource);
    glCompileShader(fragShaderId);
    glAttachShader(shaderProgramId, vertexShaderId);
    glAttachShader(shaderProgramId, fragShaderId);
    glLinkProgram(shaderProgramId);
    glUseProgram(shaderProgramId);
    int uniformLocation = glGetUniformLocation(shaderProgramId, "diffuseTexture");
    // glUniform2f(uniformLocation, ((Vector2fc)uniformData).x(), ((Vector2fc)uniformData).y());
    glUniform1i(uniformLocation, (Integer) 0);
    float ratio = (float) windowHeight / windowWidth;
    uniformLocation = glGetUniformLocation(shaderProgramId, "ratio");
    glUniform1f(uniformLocation, ratio);
    glValidateProgram(shaderProgramId);
    FloatBuffer fsQuadBuffer = BufferUtils.createFloatBuffer(6 * 2);
    fsQuadBuffer.put(new float[] { 1f, 1f, -1f, -1f, 1f, -1f, 1f, 1f, -1f, 1f, -1f, -1f });
    fsQuadBuffer.flip();
    int vertexBuffer = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, (FloatBuffer) fsQuadBuffer, GL_STATIC_DRAW);
    int location = glGetAttribLocation(shaderProgramId, "vertexIn");
    glEnableVertexAttribArray(location);
    glVertexAttribPointer(location, 2, GL_FLOAT, false, 0, 0L);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // while(1 - Math.floor(1) == 0 && !glfwWindowShouldClose(glfwWindowHandle))
    {
        glClearColor(0.25f, 0.25f, 0.25f, 1f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // Draw happens here
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glfwSwapBuffers(glfwWindowHandle);
        glfwPollEvents();
    }
    glDisable(GL_BLEND);
    glDisableVertexAttribArray(location);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDeleteBuffers(vertexBuffer);
    glBindTexture(GL_TEXTURE_2D, 0);
    glDeleteTextures(texture);
    glUseProgram(0);
    glDeleteProgram(shaderProgramId);
    glDeleteShader(vertexShaderId);
    glDeleteShader(fragShaderId);
    glClearColor(0.0f, 0.0f, 0.0f, 1f);
}
Also used : PNGDecoder(de.matthiasmann.twl.utils.PNGDecoder) InputStream(java.io.InputStream) FloatBuffer(java.nio.FloatBuffer) GL11.glGetString(org.lwjgl.opengl.GL11.glGetString) ByteBuffer(java.nio.ByteBuffer) GLFW.glfwWindowHint(org.lwjgl.glfw.GLFW.glfwWindowHint)

Aggregations

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