Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

PixFileTexture.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: PixFileTexture.cpp,v 1.4 2002/01/11 14:34:59 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/Graphics/PixFileTexture.h"
00026 #include "Teddy/Graphics/Device.h"
00027 #include "Teddy/SysSupport/EndianIn.h"
00028 #include "Teddy/SysSupport/Messages.h"
00029 #include "Teddy/SysSupport/StdIO.h"
00030 using namespace Teddy::Maths;
00031 using namespace Teddy::SysSupport;
00032 
00033 
00034 namespace Teddy    {
00035 namespace Graphics {
00036 
00037 
00039 PixFileTexture::PixFileTexture( const std::string &fname ):Texture(fname){
00040     EndianIn      *s       = NULL;
00041     unsigned char *filler  = NULL;
00042     unsigned char *data    = NULL;
00043     unsigned char  byte_in;
00044     unsigned char  color_table[256*4];  //  256 * RGBA
00045     unsigned char  index;
00046     int            i = 0;
00047 
00048     //  First read palette PAL
00049     try{
00050         s = new EndianIn( "Data/buda_textures/iwarflip.pal" );
00051         s->set_bigendian();
00052 
00053         //  First skip 24 byte header
00054         for( i=0; i<24; i++ ){
00055             s->read_byte();
00056         }
00057 
00058         //  Next read in color palette data
00059         for( i=0; i<256;i++ ){
00060             color_table[i*4+0] = s->read_byte();
00061             color_table[i*4+1] = s->read_byte();
00062             color_table[i*4+2] = s->read_byte();
00063             color_table[i*4+3] = s->read_byte();
00064         }       
00065     }catch( ... ){
00066         emsg( M_MAT, "Error loading iwarflip.pal, got as far as i = %d", i );
00067     }
00068     delete s;
00069 
00070     this->size = Vector2( 256, 256 );
00071 
00072     //  Then load texture PIX
00073     try{
00074         s = new EndianIn( fname );
00075         /*debug_msg(
00076             "PIX texture file %s opened, length %d bytes",
00077             fname,
00078             s->len()
00079         );*/
00080         s->set_bigendian();
00081         filler = data = new unsigned char[256*256*4];
00082 
00083         int skip_count = 0;
00084         do{
00085             byte_in = s->read_byte();
00086             skip_count++;
00087         }while( byte_in != 0x80 );
00088         //lwo_debug_msg( "Header scan %d bytes skipped", skip_count );
00089 
00090         do{
00091             byte_in = s->read_byte();
00092             skip_count++;
00093         }while( byte_in != 0x80 );
00094         //lwo_debug_msg( "Header scan %d bytes skipped", skip_count );
00095 
00096         do{
00097             byte_in = s->read_byte();
00098             skip_count++;
00099         }while( byte_in != 0x0 );
00100         //lwo_debug_msg( "Header scan %d bytes skipped", skip_count );
00101 
00102         //  Name comes after 80 00 80 
00103         //  00 terminates name
00104         for( i=0; i<16; i++ ){    //  16 values skipped after name
00105             byte_in = s->read_byte();
00106             //lwo_debug_msg( "Header scan byte in %d", byte_in );
00107             skip_count++;
00108         }
00109         //lwo_debug_msg( "Header scan %d bytes skipped", skip_count );
00110 
00111         //  Next data
00112         for( i=0; i<256*256; i++ ){
00113             index     = s->read_byte();
00114             *filler++ = color_table[index*4+0];
00115             *filler++ = color_table[index*4+1];
00116             *filler++ = color_table[index*4+2];
00117         }
00118     }catch( ... ){
00119         emsg( M_MAT, "Error reading texture %s: %d bytes missing", fname.c_str(), 256*256 - i );
00120     }
00121     s->close();
00122     dmsg( M_MAT, "%s\n", fname.c_str() );
00123 
00124     glBindTexture  ( GL_TEXTURE_2D, gl_texture_id );
00125     glPixelStorei  ( GL_UNPACK_ALIGNMENT, 1 );
00126     glTexParameteri( GL_TEXTURE_2D,  GL_TEXTURE_WRAP_S,     GL_REPEAT );
00127     glTexParameteri( GL_TEXTURE_2D,  GL_TEXTURE_WRAP_T,     GL_REPEAT );
00128     glTexParameteri( GL_TEXTURE_2D,  GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00129     glTexParameteri( GL_TEXTURE_2D,  GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00130 #   if !defined( USE_TINY_GL )
00131     glTexImage2D   ( GL_TEXTURE_2D,  0, GL_RGBA, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
00132 #   else
00133     glTexEnvi      ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,   GL_DECAL/*GL_MODULATE*/ );
00134     glTexImage2D   ( GL_TEXTURE_2D,  0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
00135 #   endif
00136     delete[] data;
00137     this->is_good = true;
00138 
00139     // 00 00 00 12  
00140     // 00 00 00 08                   bits per pixel ?
00141     // 00 00 00 02
00142     // 00 00 00 02
00143     // 00 00 00 03
00144     // 00 00 00 14/0f/11
00145     // 03 01 00 01
00146     // 00 01 00 00                   256 - width / height ?
00147     // 80 00 80  n   a  m  e  0
00148     // 00 00 00 21
00149     // 00 01 00 08
00150     // 00 01 00 00                   256 - width / height ?
00151     // 00 00 00 01   d  a  t  a
00152     // 00 00 00 00
00153     // 00 00 00 00
00154 }
00155 
00156 
00157 };  //  namespace Graphics
00158 };  //  namespace Teddy
00159