Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. void make_tga_texture()
  2. {
  3.   // open the file
  4.   FILE *file = fopen("assets/ship3.tga","rb");
  5.   assert( file != NULL && "could not open file" );
  6.  
  7.   // find length and allocate buffer
  8.   fseek(file, 0, SEEK_END);
  9.   int length = ftell(file);
  10.   uchar *tmp = new uchar[length];
  11.  
  12.   // read the file
  13.   fseek(file, 0, SEEK_SET);
  14.   fread(tmp, 1, length, file);
  15.   fclose(file);
  16.  
  17.   // convert the data
  18.   TgaHeader *header = (TgaHeader*)tmp;
  19.   uchar *data = tmp + sizeof(TgaHeader);
  20.  
  21.   int width = le2(header->width);
  22.   int height = le2(header->height);
  23.  
  24.   // make sure this is the GIMP flavour  
  25.   assert(header->identsize == 0);
  26.   assert(header->colourmaptype == 0);
  27.   assert(header->imagetype == 2);
  28.   assert(header->bits == 32);
  29.   assert(header->descriptor == 8);
  30.  
  31.   // swap red and blue!
  32.  
  33.   /*
  34.   for (int y = 0; y != width; ++y)
  35.   {
  36.     for (int x = 0; x != height; ++x)
  37.     {
  38.       uchar red = data[y * width * 3 + x*3 + 2];
  39.       uchar blue = data[y * width * 3 + x*3 + 0];
  40.       data[y * width * 3 + x*3 + 0] = red;
  41.       data[y * width * 3 + x*3 + 2] = blue;
  42.     }
  43.   }
  44.   */
  45.  
  46.   // build the texture
  47.   glBindTexture(GL_TEXTURE_2D, state.textures[1]);
  48.   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)data);
  49.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  50.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  51.  
  52.   delete [] tmp;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement