00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "State.h"
00018 #include "TexturePainter.h"
00019 #include "PNGLoader.h"
00020 #include "SearchPaths.h"
00021
00022 using namespace Fluxus;
00023
00024 TexturePainter *TexturePainter::m_Singleton=NULL;
00025
00026 TexturePainter::TexturePainter()
00027 {
00028 }
00029
00030 TexturePainter::~TexturePainter()
00031 {
00033 }
00034
00035 void TexturePainter::Initialise()
00036 {
00037 for (int c=0; c<MAX_TEXTURES; c++)
00038 {
00039 #ifdef ENABLE_MULTITEXTURE
00040 glActiveTexture(GL_TEXTURE0+c);
00041 glClientActiveTexture(GL_TEXTURE0+c);
00042 #endif
00043 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00044
00045 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00046 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00047 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00048 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00049
00050 glMatrixMode(GL_TEXTURE);
00051 glLoadIdentity();
00052 }
00053 #ifdef ENABLE_MULTITEXTURE
00054 glClientActiveTexture(GL_TEXTURE0);
00055 #endif
00056 }
00057
00058 void TexturePainter::ClearCache()
00059 {
00060 m_TextureMap.clear();
00061 m_LoadedMap.clear();
00062 }
00063
00064 unsigned int TexturePainter::LoadTexture(const string &Filename, bool ignorecache)
00065 {
00066 string Fullpath = SearchPaths::Get()->GetFullPath(Filename);
00067
00068
00069 map<string,int>::iterator i=m_LoadedMap.find(Fullpath);
00070 if (i!=m_LoadedMap.end())
00071 {
00072 if (!ignorecache)
00073 {
00074 return i->second;
00075 }
00076 else
00077 {
00078
00079 glDeleteTextures(1,(GLuint*)&i->second);
00080 delete m_TextureMap[i->second];
00081 }
00082 }
00083
00084 GLuint ID=0;
00085
00086 unsigned char *ImageData;
00087 TextureDesc *desc = new TextureDesc;
00088 ImageData=PNGLoader::Load(Fullpath,desc->Width,desc->Height,desc->Format);
00089
00090 if (ImageData!=NULL)
00091 {
00092
00093 glGenTextures(1,&ID);
00094 glBindTexture(GL_TEXTURE_2D,ID);
00095 if (desc->Format==RGB)
00096 {
00097 gluBuild2DMipmaps(GL_TEXTURE_2D,3,desc->Width, desc->Height,GL_RGB,GL_UNSIGNED_BYTE,ImageData);
00098 }
00099 else
00100 {
00101 gluBuild2DMipmaps(GL_TEXTURE_2D,4,desc->Width, desc->Height,GL_RGBA,GL_UNSIGNED_BYTE,ImageData);
00102 }
00103 delete[] ImageData;
00104
00105 m_TextureMap[ID]=desc;
00106 m_LoadedMap[Fullpath]=ID;
00107 return ID;
00108 }
00109
00110 m_LoadedMap[Fullpath]=0;
00111 return 0;
00112 }
00113
00114 bool TexturePainter::LoadPData(const string &Filename, unsigned int &w, unsigned int &h, TypedPData<dColour> &pixels)
00115 {
00116 string Fullpath = SearchPaths::Get()->GetFullPath(Filename);
00117
00118 unsigned char *ImageData;
00119 TextureDesc desc;
00120 ImageData=PNGLoader::Load(Fullpath,desc.Width,desc.Height,desc.Format);
00121
00122 if (ImageData!=NULL)
00123 {
00124 pixels.Resize(desc.Width*desc.Height);
00125 w=desc.Width;
00126 h=desc.Height;
00127
00128 if (desc.Format==RGB)
00129 {
00130 for (int n=0; n<desc.Width*desc.Height; n++)
00131 {
00132 pixels.m_Data[n]=dColour(ImageData[n*3]/255.0f,
00133 ImageData[n*3+1]/255.0f,
00134 ImageData[n*3+2]/255.0f,1.0f);
00135
00136 }
00137 }
00138 else if (desc.Format==RGBA)
00139 {
00140 for (int n=0; n<desc.Width*desc.Height; n++)
00141 {
00142 pixels.m_Data[n]=dColour(ImageData[n*4]/255.0f,
00143 ImageData[n*4+1]/255.0f,
00144 ImageData[n*4+2]/255.0f,
00145 ImageData[n*4+3]/255.0f);
00146
00147 }
00148 }
00149 else
00150 {
00151 return false;
00152 }
00153
00154 return true;
00155 }
00156
00157 return false;
00158 }
00159
00160 unsigned int TexturePainter::MakeTexture(unsigned int w, unsigned int h, PData *data)
00161 {
00162 GLuint ID=0;
00163 TypedPData<dColour> *pixels = dynamic_cast<TypedPData<dColour>*>(data);
00164 if (pixels)
00165 {
00166
00167 glGenTextures(1,&ID);
00168 glBindTexture(GL_TEXTURE_2D,ID);
00169 gluBuild2DMipmaps(GL_TEXTURE_2D,4,w,h,GL_RGBA,GL_FLOAT,&pixels->m_Data[0]);
00170 return ID;
00171 }
00172 return 0;
00173 }
00174
00175 bool TexturePainter::SetCurrent(unsigned int *ids)
00176 {
00177 bool ret=false;
00178
00179 for (int c=0; c<MAX_TEXTURES; c++)
00180 {
00181 #ifdef ENABLE_MULTITEXTURE
00182 glActiveTexture(GL_TEXTURE0+c);
00183 #endif
00184
00185 if (ids[c]!=0)
00186 {
00187 glEnable(GL_TEXTURE_2D);
00188 glBindTexture(GL_TEXTURE_2D,ids[c]);
00189 if (c==0) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00190 else glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00191 ret=true;
00192 }
00193 else
00194 {
00195 glDisable(GL_TEXTURE_2D);
00196 }
00197 }
00198
00199 #ifdef ENABLE_MULTITEXTURE
00200 glActiveTexture(GL_TEXTURE0);
00201 #endif
00202
00203 return ret;
00204 }
00205
00206 void TexturePainter::DisableAll()
00207 {
00208 #ifdef ENABLE_MULTITEXTURE
00209 for (int c=0; c<MAX_TEXTURES; c++)
00210 {
00211 glActiveTexture(GL_TEXTURE0+c);
00212 glDisable(GL_TEXTURE_2D);
00213 }
00214 glClientActiveTexture(GL_TEXTURE0);
00215 #else
00216 glDisable(GL_TEXTURE_2D);
00217 #endif
00218 }
00219
00220
00221
00222
00223
00224
00225