#include "TextureObject.h"

TextureObject::TextureObject(std::string path)
{
	// Handle holen
	ilGenImages(1, &handler);

	// Handle Binden
	ilBindImage(handler); 

	// File Laden
	ILboolean success = ilLoadImage((wchar_t*)path.c_str());
	if (!success)
	{
		printf("Texture: %s not loaded.\n", path.c_str());
		return;
	}
		
	// Konvertieren
	success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); 
	if (!success)
	{
		printf("Texture: %s not not converted.\n", path.c_str());
		
	}
		
	// Textur generieren
	glGenTextures(1, &imageHandle);
	
	// Textur binden
	glBindTexture(GL_TEXTURE_2D, imageHandle);
		
	// Texturinterpolation einstellen
	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, GL_REPEAT);
	//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);

	glTexImage2D(	GL_TEXTURE_2D, 0, 
					ilGetInteger(IL_IMAGE_BPP), 
					ilGetInteger(IL_IMAGE_WIDTH),
					ilGetInteger(IL_IMAGE_HEIGHT), 
					0,													// border
					ilGetInteger(IL_IMAGE_FORMAT), 
					GL_UNSIGNED_BYTE,
					ilGetData());
	

	// Speicher des Files wieder freigeben
	ilDeleteImages(1, &handler); 
}

TextureObject::~TextureObject()
{
	glDeleteTextures(1, &imageHandle);
}

void TextureObject::activate()
{
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, imageHandle);
}

void TextureObject::deactivate()
{
	glDisable(GL_TEXTURE_2D);
}