19

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?

Patricio Jerí
  • 528
  • 2
  • 5
  • 15

5 Answers5

37

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 );
  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );

  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
  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 texture using glBindTexture

glBindTexture (GL_TEXTURE_2D, texture);
ManuelJE
  • 496
  • 6
  • 15
Dinesh Subedi
  • 2,603
  • 1
  • 26
  • 36
  • 3
    although you can throw an extra fread(data, 54, 1, file) to strip the typical BMP buffer... –  Jun 05 '14 at 00:22
  • Thanks for that. I had to do this without using additional libraries. So this seems to work. Thanks! :) – Tobias Reich Dec 03 '14 at 12:10
  • 4
    I used your code to implement an image in my program .it loads the bmp file but did not display any thing on screen. – Crew HaXor Dec 06 '14 at 15:15
  • Hey, but you're calling glBindTexture() twice with the same parameters! First inside the function and then after the function returns in the call example... – ManuelJE May 08 '20 at 06:00
4

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

https://github.com/mortennobel/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.

Mortennobel
  • 3,383
  • 4
  • 29
  • 46
  • 1
    How do I add the missing glew.h file? Posted a question about this here http://stackoverflow.com/questions/12518757/installing-glew-and-using-it-with-xcode – 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
  • @Mortennobel You're welcome :P, however, this doesn't seem to be compatible with my processors architecture or something of the sort. After a lot of debugging there was nothing else I could do. Thank you though :) – Patricio Jerí Sep 21 '12 at 00:23
4

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

XiaJun
  • 1,855
  • 4
  • 24
  • 41
3

How to load a bmp on GLUT to use it as a texture?

Another very simple solution would be to use STB library, which can be found at GitHub - nothings/stb.

All what is needed is one source file, the header file "stb_image.h". It doesn't require to link any library file or to compile any additional source file.

Include the header file and enable image reading by the setting the preprocessor definition STB_IMAGE IMPLEMENTATION:

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

The image file can be read by the function stbi_load:

const char *filename = .....; // path and filename
int         req_channels = 3; // 3 color channels of BMP-file   

int width = 0, height = 0, channels = 0;
stbi_uc *image = stbi_load( filename, &width, &height, &channels, 3 ); 

When the image is loaded to a texture object, then GL_UNPACK_ALIGNMENT has to be set to 1.
By default GL_UNPACK_ALIGNMENT is 4, so each line of an image is assumed to be aligned to 4 bytes. The pixels of a BMP-file in common have a size of 3 bytes and are tightly packed, this would cause a misalignment.
After loading the image, the memory can be freed by stbi_image_free:

GLuint texture_obj = 0;
if ( image != nullptr )
{
    glGenTextures(1, &texture_obj);
    glBindTexture(GL_TEXTURE_2D, texture_obj);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // default

    stbi_image_free( image );
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
2
GLuint LoadTexture(GLuint tex, const char * filename, int width, int height)
{
//bmp 24 bit
unsigned char * data;
unsigned char R,G,B;
FILE * file;

//open .bmp
file = fopen(filename, "rb");

if(file == NULL)return 0;
//get memory for data
data =(unsigned char *)malloc(width * height * 3);
//data skip offset 
fseek(file,128,0);
//read file to data
fread(data, width * height * 3, 1, file);
//close file
fclose(file);

//transpose R,G,B values
int index;
for(int i = 0; i < width * height ; ++i)
    {
    index = i*3;
    B = data[index]; G = data[index+1]; R = data[index+2];
    data[index] = R; data[index+1] = G; data[index+2] = B;
    }

//create a texture
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data);

//texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//free memory
free(data);
return 0;
}


void init(void)
{
//texture loading, bmp 24 bit
LoadTexture(1, "01.bmp", 316, 316);
LoadTexture(2, "02.bmp", 316, 316);
LoadTexture(3, "05.bmp", 316, 316);
LoadTexture(4, "03.bmp", 316, 316);
LoadTexture(5, "06.bmp", 316, 316);
LoadTexture(6, "04.bmp", 316, 316);

. . . . . . . . . . . .

enter image description here

Linux

gcc cube.c -o cube -lglut -lGL -lGLU

Windows

tcc cube.c -o cube.exe -LC:\tcc\lib -lopengl32 -lglu32 -lglut32 -Wl,-subsystem=windows

Rabbid76
  • 202,892
  • 27
  • 131
  • 174