automatic download of data files
[safbench] / bmp_alpha.cpp
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)
4
5 // The software is released in to the Public Domain and comes with no warranty and is used
6 // at your own risk!
7
8 // Simple 32bit BGRA BMP loader and converter to RGBA (compatible with Mac OS 9)
9
10 #include <stdlib.h> // VS 2005 wont compile it if its noth placed here, need to investigate
11 #ifdef macintosh
12 #include <glut.h>
13 #include <gl.h>
14 #include <glu.h>
15 #else
16 #include <GL/glut.h>
17 #include <GL/gl.h>
18 #include <GL/glu.h>
19 #endif
20 #include <iostream>
21 #include <stdio.h>
22 #include <string>
23
24
25
26 static int i;
27
28 // borrowed code that will load BMP file to be used as texture
29 // load BMP texture
30 GLuint LoadTexture( const char * filename, int width, int height )
31 {
32         GLuint texture;
33         unsigned char * data;
34         FILE * file;
35
36         //The following code will read in our RAW file
37         file = fopen( filename, "rb" );
38         if ( file == NULL ) {
39                 fprintf(stderr, "failed to load texture: %s\n", filename);
40                 return 0;
41         }
42         data = (unsigned char *)malloc( width * height * 4 );
43
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
46         fclose( file );
47
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)
51         {
52                 // flip the order of every 3 bytes
53                 unsigned char tmp = data[i];
54                 data[i] = data[i+2];
55                 data[i+2] = tmp;
56         }
57
58
59         glGenTextures( 1, &texture );
60         glBindTexture( GL_TEXTURE_2D, texture );
61         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
62
63
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 );
67
68
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 );
72
73         //Generate the texture
74         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
75
76         free( data ); //free the texture
77         return texture; //return whether it was successful
78 }