/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package licao14;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import static java.sql.Types.NULL;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.GL_LINEAR;
import static org.lwjgl.opengl.GL11.GL_NO_ERROR;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_REPEAT;
import static org.lwjgl.opengl.GL11.GL_RGBA;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_S;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_T;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glGetError;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glTexParameteri;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glVertex2f;
import org.newdawn.slick.opengl.PNGDecoder;
import static org.lwjgl.opengl.GL11.glDeleteTextures;
import static org.lwjgl.opengl.GL11.glGenTextures;
import static org.lwjgl.opengl.GL11.glTexImage2D;

/**
 *
 * @author nynha
 */
public class LTexture14 {
    public int mTextureID = 0;
    int DEFAULT_TEXTURE_WRAP = GL_REPEAT;

    //Initialize texture dimensions
    public int mTextureWidth = 0;
    public int mTextureHeight = 0;

    public boolean loadTextureFromFile(String caminho) throws IOException
    {        
        freeTexture();
        
        boolean textureLoaded = false;
        
        PNGDecoder decoder = new PNGDecoder(new FileInputStream(caminho));
	ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth()	* decoder.getHeight());
	decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.RGBA);
//	buffer.flip();
        
	mTextureWidth = decoder.getWidth();
        mTextureHeight = decoder.getHeight();
        
        textureLoaded = loadTextureFromPixels32(buffer, mTextureWidth, mTextureHeight);
        
        return textureLoaded;
    }
    
    public boolean loadTextureFromPixels32(ByteBuffer buffer, int width, int height)
    {
        //Free texture if it exists
        freeTexture();

        //Get texture dimensions
        mTextureWidth = width;
        mTextureHeight = height;

        //Generate texture glGenTextures( 1, &mTextureID );ID
        mTextureID = glGenTextures();

        //Bind texture ID
        glBindTexture(GL_TEXTURE_2D, mTextureID);

        //Generate texture
        buffer.flip();
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

        //Set texture parameters
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, DEFAULT_TEXTURE_WRAP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, DEFAULT_TEXTURE_WRAP);

        //Unbind texture
        glBindTexture(GL_TEXTURE_2D, NULL);

            //Checagem de erros
            int erro = glGetError();
            if(erro != GL_NO_ERROR)
            {
                System.out.println("Erro de pixels");
                return false;
            }
            return true;
    }

    public void freeTexture()
    {
        //Delete texture
        if(mTextureID != 0)
        {
            glDeleteTextures(mTextureID);
            mTextureID = 0;
        }
        
        mTextureWidth = 0;
        mTextureHeight = 0;
    }
    
    public int getTextureID()
    {
        return mTextureID;
    }

    public int textureWidth()
    {
        return mTextureWidth;
    }

    public int textureHeight()
    {
        return mTextureHeight;
    }
}
