de.matthiasmann.twl.utils.PNGDecoder Java Examples

The following examples show how to use de.matthiasmann.twl.utils.PNGDecoder. 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: TextureUtils.java    From OpenGL-Animation with The Unlicense 6 votes vote down vote up
protected static TextureData decodeTextureFile(MyFile file) {
	int width = 0;
	int height = 0;
	ByteBuffer buffer = null;
	try {
		InputStream in = file.getInputStream();
		PNGDecoder decoder = new PNGDecoder(in);
		width = decoder.getWidth();
		height = decoder.getHeight();
		buffer = ByteBuffer.allocateDirect(4 * width * height);
		decoder.decode(buffer, width * 4, Format.BGRA);
		buffer.flip();
		in.close();
	} catch (Exception e) {
		e.printStackTrace();
		System.err.println("Tried to load texture " + file.getName() + " , didn't work");
		System.exit(-1);
	}
	return new TextureData(buffer, width, height);
}
 
Example #2
Source File: OpenGL3_TheQuadTextured.java    From ldparteditor with MIT License 4 votes vote down vote up
private int loadPNGTexture(String filename, int textureUnit) {
    ByteBuffer buf = null;
    int tWidth = 0;
    int tHeight = 0;
     
    try {
        // Open the PNG file as an InputStream
        InputStream in = new FileInputStream(filename);
        // Link the PNG decoder to this stream
        PNGDecoder decoder = new PNGDecoder(in);
         
        // Get the width and height of the texture
        tWidth = decoder.getWidth();
        tHeight = decoder.getHeight();
         
         
        // Decode the PNG file in a ByteBuffer
        buf = ByteBuffer.allocateDirect(
                4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
        buf.flip();
         
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }
     
    // Create a new texture object in memory and bind it
    int texId = GL11.glGenTextures();
    GL13.glActiveTexture(textureUnit);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
     
    // All RGB bytes are aligned to each other and each component is 1 byte
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
     
    // Upload the texture data and generate mip maps (for scaling)
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0, 
            GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
     
    // Setup the ST coordinate system
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
     
    // Setup what to do when the texture has to be scaled
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, 
            GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, 
            GL11.GL_LINEAR_MIPMAP_LINEAR);
     
    this.exitOnGLError("loadPNGTexture");
     
    return texId;
}