-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBMP.h
70 lines (54 loc) · 1.92 KB
/
BMP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/#loading-bmp-images-yourself
#pragma once
#include "Texture.h"
class BMP : virtual public Texture
{
public:
BMP(std::string file) : Texture() {
m_file = file;
load();
};
protected:
void load() {
unsigned char header[54]; // Each BMP file begins by a 54-bytes header
unsigned int dataPos; // Position in the file where the actual data begins
unsigned int width, height;
unsigned int imageSize; // = width*height*3
// Actual RGB data
unsigned char* data;
FILE* image;
image = fopen(m_file.c_str(), "rb");
if (!image)
{
printf("Error creating texture %s. %s\n", m_file.c_str(), strerror(errno));
return;
}
size_t headerSize = fread(header, 1, 54, image);
if (headerSize != sizeof(header) || header[0] != 'B' || header[1] != 'M') {
printf("Not a correct BMP file\n");
return;
}
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (imageSize == 0) imageSize = width * height * 3; // 3 : one byte for each Red, Green and Blue component
if (dataPos == 0) dataPos = 54; // The BMP header is done that way
data = new unsigned char[imageSize];
// read file contents
fread(data, 1, imageSize, image);
fclose(image);
// bind texture into texture unit
Activate();
// set sampling to GL_NEAREST to make integer texture work
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// make data into texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, (void*)data);
glGenerateMipmap(GL_TEXTURE_2D);
// free data
delete[] data;
}
private:
std::string m_file;
};