Advertisement
Guest User

Hkon

a guest
Dec 25th, 2008
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. int LoadBitmap(std::string filename)
  2. {
  3.     unsigned char *l_texture; // The pointer to the memory zone in which we will load the texture
  4.     int width, height, size; //Storage for bitmap-info
  5.     num_texture++; // A variable which keeps track of the number of textures
  6.  
  7.     FILE *mf;
  8.     mf = fopen(filename.c_str(), "rb"); //Open file
  9.     if (mf == NULL)                        //Check if the file opened
  10.         exit(13);
  11.    
  12.     fseek(mf, 18, SEEK_SET);
  13.     fread(&width, sizeof(int), 1, mf);    //Read in the width of the image
  14.    
  15.     fseek(mf, 22, SEEK_SET);
  16.     fread(&height, sizeof(int), 1, mf);    //Read in the height of the image
  17.        
  18.     size = height * width * 4;        //The size for all the important bytes
  19.  
  20.     // Now we need to allocate the memory for our image (width * height * color deep)
  21.     l_texture = (unsigned char *) malloc(size);
  22.     // And fill it with zeros
  23.     memset(l_texture, 0, size);
  24.  
  25.     // At this point we can read every pixel of the image
  26.     for (int i=0; i < size; i += 4)
  27.     {            
  28.             // We load the red, green and blue values
  29.             char r, g, b;
  30.             fread(&r, 1, 1, mf);
  31.             fread(&g, 1, 1, mf);
  32.             fread(&b, 1, 1, mf);
  33.                        
  34.             // And store it
  35.             l_texture[i+0] = r; // Red component
  36.             l_texture[i+1] = g; // Green component
  37.             l_texture[i+2] = b; // Blue component
  38.             l_texture[i+3] = 255; // Alpha value
  39.     }
  40.  
  41.     fclose(mf); // Closes the file stream
  42.      
  43.     glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter
  44.  
  45.     // The next commands sets the texture parameters
  46.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
  47.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  48.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
  49.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //The minifying function
  50.  
  51.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // We don't combine the color with the original surface color, use only the texture map.
  52.  
  53.     // Finally we define the 2d texture
  54.     glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
  55.  
  56.     // And create 2d mipmaps for the minifying function
  57.     gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
  58.  
  59.     free(l_texture); // Free the memory we used to load the texture
  60.  
  61.     return num_texture; // Returns the current texture OpenGL ID
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement