Skip to content
This repository
tag: v4.0.0
Fetching contributors…

Cannot retrieve contributors at this time

file 72 lines (52 sloc) 1.955 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*
* texture_manager.cc
* Stage
*
* Created by Alex Couture-Beil on 03/06/08.
* $Id$
*/

#include <sstream>

#include "texture_manager.hh"
#include "file_manager.hh"
using namespace Stg;

Fl_Shared_Image* TextureManager::loadImage( const char* filename )
{
Fl_Shared_Image *img = NULL;

img = Fl_Shared_Image::get( filename );

return img;
}

GLuint TextureManager::loadTexture( const char *filename )
{
GLuint texName;
Fl_Shared_Image *img = loadImage( filename );
if( img == NULL ) {
fprintf( stderr, "unable to open image: %s\n", filename );
return 0;
}

//TODO display an error for incorrect depths
if( img->d() != 3 && img->d() != 4 ) {
fprintf( stderr, "unable to open image: %s - incorrect depth - should be 3 or 4\n", filename );
return 0;
}

//TODO check for correct width/height - or convert it.

uint8_t* pixels = (uint8_t*)(img->data()[0]);

//vertically flip the image
int img_size = img->w() * img->h() * img->d();
uint8_t* img_flip = new uint8_t[ img_size ];

const int row_width = img->w() * img->d();
for( int i = 0; i < img->h(); i++ )
memcpy( img_flip + ( i * row_width ), pixels + ( ( img->h() - i - 1) * row_width ), row_width );


//create room for texture
glGenTextures(1, &texName);

glBindTexture(GL_TEXTURE_2D, texName);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

//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

gluBuild2DMipmaps (GL_TEXTURE_2D, img->d(), img->w(), img->h(), ( img->d() == 3 ? GL_RGB : GL_RGBA ),
GL_UNSIGNED_BYTE, img_flip );

glBindTexture( GL_TEXTURE_2D, 0 );
return texName;
}
Something went wrong with that request. Please try again.