TexturePainter.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2005 Dave Griffiths
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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     // see if we've loaded this one already
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             // remove the old texture
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         // upload to card...
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         // upload to card...
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 

Generated on Tue Sep 4 23:22:18 2007 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1