Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

texture_gl.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: texture_gl.cpp,v 1.5 2001/03/06 18:51:06 japj Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #include "Core/precomp.h"
00016 #include "texture_gl.h"
00017 #include "Display/Display/Generic/pixeldata.h"
00018 
00019 CL_Texture *CL_Texture::create(CL_SurfaceProvider *prov, bool delete_prov)
00020 {
00021         return new CL_Texture_OpenGL(prov, delete_prov);
00022 }
00023 
00024 CL_Texture *CL_Texture::load(const char *id, CL_ResourceManager *manager)
00025 {
00026         return new CL_Texture_OpenGL(
00027                 CL_SurfaceProvider::load(id, manager),
00028                 false); // this should be true, but...
00029 }
00030 
00031 CL_Texture_OpenGL::CL_Texture_OpenGL(CL_SurfaceProvider *provider, bool delete_provider)
00032 {
00033         this->provider = provider;
00034         this->delete_provider = delete_provider;
00035 
00036         handles = NULL;
00037         num_textures = 0;
00038         reload();
00039 }
00040 
00041 CL_Texture_OpenGL::~CL_Texture_OpenGL()
00042 {
00043         if (delete_provider) delete provider;
00044 
00045         flush();
00046 }
00047 
00048 void CL_Texture_OpenGL::bind(int texture_no)
00049 {
00050         if (texture_no < 0 || texture_no >= num_textures)
00051         {
00052                 throw CL_Error("Illegal subtexture selected");
00053         }
00054 
00055         if (handles == NULL) reload();
00056 
00057         glBindTexture(GL_TEXTURE_2D, handles[texture_no]);
00058 }
00059 
00060 CL_SurfaceProvider *CL_Texture_OpenGL::get_provider() const
00061 {
00062         return provider;
00063 }
00064 
00065 void CL_Texture_OpenGL::reload()
00066 {
00067         provider->lock();
00068 
00069         if (handles != NULL && provider->get_num_frames() != (unsigned int) num_textures)
00070         {
00071                 glDeleteTextures(num_textures, handles);
00072                 delete[] handles;
00073                 handles = NULL;
00074         }
00075 
00076         num_textures = provider->get_num_frames();
00077 
00078         if (handles == NULL)
00079         {
00080                 handles = new GLuint[num_textures];
00081                 glGenTextures(num_textures, handles);
00082         }
00083 
00084         int width = provider->get_width();
00085         int height = provider->get_height();
00086 
00087         int texture_width = 1;
00088         int texture_height = 1;
00089         while (texture_width < width) texture_width *= 2;
00090         while (texture_height < height * num_textures) texture_height *= 2;
00091 
00092         cl_assert(width <= texture_width);
00093         cl_assert(height <= texture_height);
00094 
00095         CL_PixelData pixeldata(
00096                 255,
00097                 255 << 8,
00098                 255 << 16,
00099                 255 << 24,
00100                 provider,
00101                 4);
00102 
00103         unsigned int *texture_data = new unsigned int[texture_width * texture_height];
00104         memset(texture_data, 0, texture_width * texture_height * sizeof(int));
00105 
00106         // Convert texture to correct pixelformat before passing it to OpenGL:  
00107         for (int i = 0; i < num_textures; i++)
00108         {
00109                 if (provider->get_alpha_mask() == 0)
00110                 {
00111                         for (int y = 0; y < height * num_textures; y++)
00112                         {
00113                                 unsigned int* src = (unsigned int*) pixeldata.get_line_pixel(y + height * i);
00114                                 unsigned int* dst = (unsigned int*) &texture_data[texture_width * y];
00115                                 for (int x = 0; x < width; x++)
00116                                 {
00117                                         *(dst++) = (*src & 0x00FFFFFF) | (~(*src) & 0xFF000000);
00118                                         src++;
00119                                 }
00120                         }
00121                 }
00122                 else
00123                 {
00124                         for (int y = 0; y < height * num_textures; y++)
00125                         {       
00126                                 memcpy(
00127                                         &texture_data[texture_width * y],
00128                                         pixeldata.get_line_pixel(y + height * i),
00129                                         width * sizeof(int));
00130                         }
00131                 }
00132                 
00133                 glBindTexture(GL_TEXTURE_2D, handles[i]);
00134 /*      
00135                 glTexImage2D(
00136                         GL_TEXTURE_2D,
00137                         0,
00138                         4,
00139                         width,
00140                         height,
00141                         0,
00142                         GL_RGBA,
00143                         GL_UNSIGNED_BYTE,
00144                         texture_data);
00145 */
00146                 gluBuild2DMipmaps(
00147                         GL_TEXTURE_2D,
00148                         GL_RGBA,
00149                         texture_width,
00150                         texture_height,
00151                         GL_RGBA,
00152                         GL_UNSIGNED_BYTE,
00153                         texture_data);
00154         }
00155 
00156         delete[] texture_data;
00157 
00158         provider->unlock();
00159 }
00160 
00161 /*void CL_Texture_OpenGL::reload()
00162 {
00163         provider->lock();
00164 
00165         if (handles != NULL && provider->get_num_frames() != (unsigned int) num_textures)
00166         {
00167                 glDeleteTextures(num_textures, handles);
00168                 delete[] handles;
00169                 handles = NULL;
00170         }
00171 
00172         num_textures = provider->get_num_frames();
00173 
00174         if (handles == NULL)
00175         {
00176                 handles = new GLuint[num_textures];
00177                 glGenTextures(num_textures, handles);
00178         }
00179 
00180         int width = provider->get_width();
00181         int height = provider->get_height();
00182 
00183         int texture_width = 1;
00184         int texture_height = 1;
00185         while (texture_width < width) texture_width *= 2;
00186         while (texture_height < height * no_sprs) texture_height *= 2;
00187 
00188         cl_assert(width <= texture_width);
00189         cl_assert(height <= texture_height);
00190 
00191         CL_PixelData pixeldata(
00192                 255,
00193                 255 << 8,
00194                 255 << 16,
00195                 255 << 24,
00196                 provider,
00197                 4);
00198 
00199         unsigned int *texture_data = new unsigned int[width*height];
00200 
00201         // Convert texture to correct pixelformat before passing it to OpenGL:  
00202         for (int i=0;i<num_textures;i++)
00203         {
00204                 if (provider->get_alpha_mask() == 0)
00205                 {
00206                         for (int y=0; y<height; y++)
00207                         {
00208                                 unsigned int* src = (unsigned int*) pixeldata.get_line_pixel(y+height*i);
00209                                 unsigned int* dst = (unsigned int*) &texture_data[width*y];
00210                                 for (int x=0; x< width; x++)
00211                                 {
00212                                         *(dst++) = (*src & 0x00FFFFFF) | (~(*src) & 0xFF000000);
00213                                         src++;
00214                                 }
00215                         }
00216                 }
00217                 else
00218                 {
00219                         for (int y=0; y<height; y++)
00220                         {       
00221                                 memcpy(
00222                                         &texture_data[width*y],
00223                                         pixeldata.get_line_pixel(y+height*i),
00224                                         width * sizeof(int));
00225                         }
00226                 }
00227                 
00228                 glBindTexture(GL_TEXTURE_2D, handles[i]);
00229 
00230                 gluBuild2DMipmaps(
00231                         GL_TEXTURE_2D,
00232                         4,
00233                         width,
00234                         height,
00235                         GL_RGBA,
00236                         GL_UNSIGNED_BYTE,
00237                         texture_data);
00238 
00239         }
00240 
00241         delete[] texture_data;
00242 
00243         provider->unlock();
00244 }
00245 */
00246 int CL_Texture_OpenGL::get_width() const
00247 {
00248         return provider->get_width();
00249 }
00250 
00251 int CL_Texture_OpenGL::get_height() const
00252 {
00253         return provider->get_height();
00254 }
00255 
00256 int CL_Texture_OpenGL::get_num_frames() const
00257 {
00258         return provider->get_num_frames();
00259 }
00260 
00261 int CL_Texture_OpenGL::get_num_textures() const
00262 {
00263         return num_textures; 
00264 }
00265 
00266 void CL_Texture_OpenGL::flush(CL_DisplayCard *card)
00267 {
00268         if (handles != NULL)
00269         {
00270                 glDeleteTextures(num_textures, handles);
00271                 delete[] handles;
00272                 handles = NULL;
00273         }
00274 }

Generated at Wed Apr 4 19:54:04 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001