1 // License information.
2 // The Software "SAF BENCH" was written by Patrik.A, also known as Nitton Attiofyra
3 // on YouTube. (https://www.youtube.com/@NittonAttiofyra)
5 // The software is released in to the Public Domain and comes with no warranty and is used
8 // Simple 32bit BGRA BMP loader and converter to RGBA (compatible with Mac OS 9)
10 #include <stdlib.h> // VS 2005 wont compile it if its noth placed here, need to investigate
28 // borrowed code that will load BMP file to be used as texture
30 GLuint LoadTexture( const char * filename, int width, int height )
36 //The following code will read in our RAW file
37 file = fopen( filename, "rb" );
39 fprintf(stderr, "failed to load texture: %s\n", filename);
42 data = (unsigned char *)malloc( width * height * 4 );
44 fread(data, 138, 1, file); // strip BMP buffer and Color information befor reading 32bit BMP BGRA data
45 fread( data,1, width * height * 4, file ); // read BMP data
48 // GL_BGR nor GL_BGR_EXT worked on my system, no texturs rendered so I have to make sure the textur data is in RGB format
49 // *.bmp files are BGR formated, this converts the data to RGB
50 for(i = 0; i < width * height * 4; i += 4)
52 // flip the order of every 3 bytes
53 unsigned char tmp = data[i];
59 glGenTextures( 1, &texture );
60 glBindTexture( GL_TEXTURE_2D, texture );
61 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
64 //even better quality, but this will do for now.
65 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR );
66 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
69 //to the edge of our shape.
70 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
71 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
73 //Generate the texture
74 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
76 free( data ); //free the texture
77 return texture; //return whether it was successful