Take the 2-minute tour ×

I've been searching all around for a simple solution to add sprites to my OpenGl GLUT simple moon lander game in c++ and it appears I must use bmp's since they're easiest to load and use them as textures on a rectangle.

How exactly can I load the bmp's as textures though?

share|improve this question
add comment

3 Answers

Look my simple c implementation function to load texture.

GLuint LoadTexture( const char * filename )

  GLuint texture;

  int width, height;

  unsigned char * data;

  FILE * file;

  file = fopen( filename, "rb" );

  if ( file == NULL ) return 0;
  width = 1024;
  height = 512;
  data = (unsigned char *)malloc( width * height * 3 );
  //int size = fseek(file,);
  fread( data, width * height * 3, 1, file );
  fclose( file );

 for(int i = 0; i < width * height ; ++i)
   int index = i*3;
   unsigned char B,R;
   B = data[index];
   R = data[index+2];

   data[index] = R;
   data[index+2] = B;


glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );

gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );

return texture;

Above function returns the texture data. Store the texture data in variable

 GLuint texture
 texture= LoadTexture( "your_image_name.bmp" );

Now you can bind the texure using glBindTexture

glBindTexture (GL_TEXTURE_2D, texture);
share|improve this answer
+1 for way to implement yourself. –  Cosine May 14 '13 at 16:04
add comment

You can use library GLAUX and SOIL(Simple OpenGL Image Library) . There are also other image libriries for OpenGL.

share|improve this answer
add comment

Checkout my the TextureLoader (TextureLoader.h + TextureLoader.cpp) from OpenGL_3_2_Utils:


The two files does not depend on any other files and should also work seamless on any version of OpenGL (and any platform). Example usage can be found in the file comment.

share|improve this answer
Brilliant, thank you :) –  Patricio Jerí Sep 20 '12 at 18:01
How do I add the missing glew.h file? Posted a question about this here stackoverflow.com/questions/12518757/… –  Patricio Jerí Sep 20 '12 at 18:43
SO has a spam policy that requires disclosure when you mention tools or applications you develop. You have to actually say they're yours. –  Nicol Bolas Sep 20 '12 at 18:48
@NicolBolas No problem. I didn't know that. I updated the question to clarify it. –  Mortennobel Sep 20 '12 at 19:04
@PatricioJerí Actually you don't need to glew.h file. I have removed the dependency from the TextureLoader.h file. Thanks for pointing that out! –  Mortennobel Sep 20 '12 at 19:08
show 1 more comment

Your Answer


