Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. GLuint LoadTexture( const char * filename, int width, int height )
  2. {
  3.     GLuint texture;
  4.     unsigned char * data;
  5.     FILE * file;
  6.     //The following code will read in our RAW file
  7.     file = fopen( filename, "rb" );
  8.     if ( file == NULL ) return 0;
  9.     data = (unsigned char *)malloc( width * height * 3 );
  10.     fread( data, width * height * 3, 1, file );
  11.     fclose( file );
  12.     glGenTextures( 1, &texture ); //generate the texture with the loaded data
  13.     glBindTexture( GL_TEXTURE_2D, texture ); //bind the textureto it’s array
  14.     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); //set texture environment parameters
  15.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  16.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  17.     //Here we are setting the parameter to repeat the texture instead of clamping the texture
  18.     //to the edge of our shape.
  19.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  20.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  21.     //Generate the texture
  22.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, data);
  23.     free( data ); //free the texture
  24.     return texture; //return whether it was successfull
  25. }
  26.  
  27. void FreeTexture( GLuint texture )
  28. {
  29.   glDeleteTextures( 1, &texture );
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement