texture_manager.cc
Go to the documentation of this file.
1 /*
2  * texture_manager.cc
3  * Stage
4  *
5  * Created by Alex Couture-Beil on 03/06/08.
6  * $Id$
7  */
8 
9 #include <sstream>
10 #include "texture_manager.hh"
11 #include "file_manager.hh"
12 using namespace Stg;
13 
14 GLuint TextureManager::loadTexture( const char *filename )
15 {
16 // if( filename == NULL )
17  // return 0;
18 
19  GLuint texName;
20  Fl_Shared_Image *img = Fl_Shared_Image::get( filename );
21 
22  if( img == NULL) {
23  fprintf( stderr, "unable to open image: %s\n", filename );
24  //exit(-1);
25  return 0;
26  }
27 
28  //TODO display an error for incorrect depths
29  if( img->d() != 3 && img->d() != 4 ) {
30  fprintf( stderr, "unable to open image: %s - incorrect depth - should be 3 or 4\n", filename );
31  return 0;
32  }
33 
34  //TODO check for correct width/height - or convert it.
35 
36  uint8_t* pixels = (uint8_t*)(img->data()[0]);
37 
38  //vertically flip the image
39  int img_size = img->w() * img->h() * img->d();
40  uint8_t* img_flip = new uint8_t[ img_size ];
41 
42  const int row_width = img->w() * img->d();
43  for( int i = 0; i < img->h(); i++ )
44  memcpy( img_flip + ( i * row_width ), pixels + ( ( img->h() - i - 1) * row_width ), row_width );
45 
46 
47  //create room for texture
48  glGenTextures(1, &texName);
49 
50  glBindTexture(GL_TEXTURE_2D, texName);
51 
52  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
53 
54  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
55  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
56 
57  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
58  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
59 
60  //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
61 
62  gluBuild2DMipmaps (GL_TEXTURE_2D, img->d(), img->w(), img->h(), ( img->d() == 3 ? GL_RGB : GL_RGBA ),
63  GL_UNSIGNED_BYTE, img_flip );
64 
65  glBindTexture( GL_TEXTURE_2D, 0 );
66  return texName;
67  }
GLuint loadTexture(const char *filename)
load a texture on the GPU, returned value is the texture ID, or 0 for failure
The Stage library uses its own namespace.
Definition: canvas.hh:8


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 Mon Jun 10 2019 15:06:10