#include "Texture.h"
CTexture::CTexture()
{
ID = 0;
ilutRenderer(ILUT_OPENGL);
ilInit();
iluInit();
ilutInit();
}
CTexture::CTexture(const char* filename)
{
ID = 0;
ilutRenderer(ILUT_OPENGL);
ilInit();
iluInit();
ilutInit();
load(filename);
}
CTexture::~CTexture()
{
if (ID != 0)
{
glDeleteTextures(1, &ID);
ID = 0;
}
}
GLuint CTexture::load(const char *filename)
{
ILuint imageID;
ilGenImages(1, &imageID);
ilBindImage(imageID);
if (!ilLoadImage( (const char *)filename ))
{
printf("Loading image (%s) failed\n", filename);
}
else
{
printf("Textur %s loaded\n", filename);
}
switch (ilGetError())
{
case IL_INVALID_ENUM:
printf("An unacceptable enumerated value was passed to a function.");
break;
case IL_OUT_OF_MEMORY:
printf("Could not allocate enough memory in an operation. ");
break;
case IL_FORMAT_NOT_SUPPORTED:
printf("The format a function tried to use was not able to be used by that function. ");
break;
case IL_INTERNAL_ERROR:
printf("A serious error has occurred. Please e-mail DooMWiz with the conditions leading up to this error being reported. ");
break;
case IL_INVALID_VALUE:
printf("An invalid value was passed to a function or was in a file. ");
break;
case IL_ILLEGAL_OPERATION:
printf("The operation attempted is not allowable in the current state. The function returns with no ill side effects. ");
break;
case IL_ILLEGAL_FILE_VALUE:
printf("An illegal value was found in a file trying to be loaded. ");
break;
case IL_INVALID_FILE_HEADER:
printf("A file's header was incorrect. ");
break;
case IL_INVALID_PARAM:
printf("An invalid parameter was passed to a function, such as a NULL pointer. ");
break;
case IL_COULD_NOT_OPEN_FILE:
printf("Could not open the file specified. The file may already be open by another app or may not exist. ");
break;
case IL_INVALID_EXTENSION:
printf("The extension of the specified filename was not correct for the type of image-loading function. ");
break;
case IL_FILE_ALREADY_EXISTS:
printf("The filename specified already belongs to another file. To overwrite files by default, call ilEnable with the IL_FILE_OVERWRITE parameter. ");
break;
case IL_OUT_FORMAT_SAME:
printf("Tried to convert an image from its format to the same format. ");
break;
case IL_STACK_OVERFLOW:
printf("One of the internal stacks was already filled, and the user tried to add on to the full stack. ");
break;
case IL_STACK_UNDERFLOW:
printf("One of the internal stacks was empty, and the user tried to empty the already empty stack. ");
break;
case IL_INVALID_CONVERSION:
printf("An invalid conversion attempt was tried. ");
break;
case IL_LIB_JPEG_ERROR:
printf("An error occurred in the libjpeg library. ");
break;
case IL_LIB_PNG_ERROR:
printf("An error occurred in the libpng library. ");
break;
case ILUT_NOT_SUPPORTED:
printf("A type is valid but not supported in the current build.");
break;
case IL_UNKNOWN_ERROR:
printf("Unknown Error.");
break;
default:
break;
}
if (ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE) == 0)
{
return -1;
}
size.X = ilGetInteger(IL_IMAGE_WIDTH);
size.Y = ilGetInteger(IL_IMAGE_HEIGHT);
glGenTextures(1, &ID);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, size.X, size.Y, GL_RGBA, GL_UNSIGNED_BYTE, ilGetData());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.X, size.Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, ilGetData());
ilDeleteImages(1, &imageID);
return 0;
}
void CTexture::bind()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ID);
}
void CTexture::unbind()
{
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}