texture_manager.cc
Go to the documentation of this file.
00001 /*
00002  *  texture_manager.cc
00003  *  Stage
00004  *
00005  *  Created by Alex Couture-Beil on 03/06/08.
00006  *  $Id$
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 //    if( filename == NULL )
00017   //      return 0;
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                 //exit(-1);
00025                 return 0;
00026         }
00027         
00028         //TODO display an error for incorrect depths
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         //TODO check for correct width/height - or convert it.
00035         
00036         uint8_t* pixels = (uint8_t*)(img->data()[0]);
00037         
00038         //vertically flip the image
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         //create room for texture
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         //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //or use GL_DECAL or GL_MODULATE instead of DECAL to mix colours and textures
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         }


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Thu Aug 27 2015 15:20:57