00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "TextureInterface.h"
00022
00023 #include "system.h"
00024 #include <GL/gl.h>
00025 #include <GL/glu.h>
00026 #include <IL/il.h>
00027
00028 int TextureInterface::s_handlerIndex = 1;
00029
00030 TextureInterface::TextureInterface()
00031 {
00032 ilInit();
00033 }
00034
00035 TextureInterface::~TextureInterface()
00036 {
00037 }
00038
00039 TextureData* TextureInterface::loadBMP(std::string p_filename)
00040 {
00041 TextureData* ret = 0;
00042 int i;
00043
00044 int j=0;
00045 FILE *l_file;
00046
00047 if( fopen_s(&l_file, p_filename.c_str(), "rb") == 0)
00048 {
00049 ret = new TextureData;
00050
00051 fread(&(ret->fileheader), sizeof(ret->fileheader), 1, l_file);
00052
00053 fseek(l_file, sizeof(ret->fileheader), SEEK_SET);
00054 fread(&(ret->infoheader), sizeof(ret->infoheader), 1, l_file);
00055
00056
00057 ret->l_texture = (unsigned char *) malloc(ret->infoheader.biWidth * ret->infoheader.biHeight * 4);
00058
00059 memset(ret->l_texture, 0, ret->infoheader.biWidth * ret->infoheader.biHeight * 4);
00060
00061
00062 for (i=0; i < (ret->infoheader.biWidth) * (ret->infoheader.biHeight); i++)
00063 {
00064
00065 fread(&(ret->rgb), sizeof(ret->rgb), 1, l_file);
00066
00067
00068 ret->l_texture[j+0] = ret->rgb.rgbtRed;
00069 ret->l_texture[j+1] = ret->rgb.rgbtGreen;
00070 ret->l_texture[j+2] = ret->rgb.rgbtBlue;
00071 ret->l_texture[j+3] = 255;
00072 j += 4;
00073 }
00074
00075 fclose(l_file);
00076 }
00077
00078 return ret;
00079 }
00080
00081 int TextureInterface::bind(TextureData* data)
00082 {
00083 int ret = 0;
00084
00085 s_handlerIndex++;
00086 glBindTexture(GL_TEXTURE_2D, ret=s_handlerIndex);
00087
00088
00089 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00090 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00091 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00092 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
00093
00094 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00095
00096
00097 glTexImage2D(GL_TEXTURE_2D, 0, 4, data->infoheader.biWidth, data->infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data->l_texture);
00098
00099
00100 gluBuild2DMipmaps(GL_TEXTURE_2D, 4, data->infoheader.biWidth, data->infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, data->l_texture);
00101
00102 glBindTexture(GL_TEXTURE_2D, 0);
00103
00104 return ret;
00105 }
00106
00107 TextureData* TextureInterface::loadPNG(std::string p_filename)
00108 {
00109 TextureData *ret = 0;
00110 ilBindImage(0);
00111 if (ilLoadImage(p_filename.c_str()) == IL_TRUE)
00112 {
00113 ret = new TextureData();
00114 ret->infoheader.biWidth = ilGetInteger(IL_IMAGE_WIDTH);
00115 ret->infoheader.biHeight = ilGetInteger(IL_IMAGE_HEIGHT);
00116 ret->l_texture = (unsigned char*)malloc(ret->infoheader.biWidth * ret->infoheader.biHeight * sizeof(unsigned char) * 4);
00117 ilCopyPixels(0, 0, 0, ret->infoheader.biWidth, ret->infoheader.biHeight, 1, IL_RGBA, IL_UNSIGNED_BYTE, ret->l_texture);
00118 while (ilGetError() != IL_NO_ERROR)
00119 int stop = 1;
00120 }
00121 int stop;
00122 while (stop = ilGetError() != IL_NO_ERROR)
00123 stop = 1;
00124 return ret;
00125 }