Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <sstream>
00010 #include "texture_manager.hh"
00011 #include "file_manager.hh"
00012 using namespace Stg;
00013
00014 GLuint TextureManager::loadTexture( const char *filename )
00015 {
00016
00017
00018
00019 GLuint texName;
00020 Fl_Shared_Image *img = Fl_Shared_Image::get( filename );
00021
00022 if( img == NULL) {
00023 fprintf( stderr, "unable to open image: %s\n", filename );
00024
00025 return 0;
00026 }
00027
00028
00029 if( img->d() != 3 && img->d() != 4 ) {
00030 fprintf( stderr, "unable to open image: %s - incorrect depth - should be 3 or 4\n", filename );
00031 return 0;
00032 }
00033
00034
00035
00036 uint8_t* pixels = (uint8_t*)(img->data()[0]);
00037
00038
00039 int img_size = img->w() * img->h() * img->d();
00040 uint8_t* img_flip = new uint8_t[ img_size ];
00041
00042 const int row_width = img->w() * img->d();
00043 for( int i = 0; i < img->h(); i++ )
00044 memcpy( img_flip + ( i * row_width ), pixels + ( ( img->h() - i - 1) * row_width ), row_width );
00045
00046
00047
00048 glGenTextures(1, &texName);
00049
00050 glBindTexture(GL_TEXTURE_2D, texName);
00051
00052 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00053
00054 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00055 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00056
00057 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00058 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00059
00060
00061
00062 gluBuild2DMipmaps (GL_TEXTURE_2D, img->d(), img->w(), img->h(), ( img->d() == 3 ? GL_RGB : GL_RGBA ),
00063 GL_UNSIGNED_BYTE, img_flip );
00064
00065 glBindTexture( GL_TEXTURE_2D, 0 );
00066 return texName;
00067 }