-
Notifications
You must be signed in to change notification settings - Fork 2
/
EImage.cpp
51 lines (47 loc) · 1.41 KB
/
EImage.cpp
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
#include "global.h"
#include "EImage.h"
namespace Eg {
EImage::EImage() {
imageWidth = imageHeight = 0;
tex = 0;
}
bool EImage::load(const char* name) {
GLint i;
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
fif = FreeImage_GetFileType(name, 0);
if (fif == FIF_UNKNOWN) {
fif = FreeImage_GetFIFFromFilename(name);
}
FIBITMAP* fib = FreeImage_Load(fif, name, 0);
if (!fib) {
return false;
}
FIBITMAP* temp = fib;
fib = FreeImage_ConvertTo32Bits(fib);
FreeImage_Unload(temp);
GLubyte* data = (GLubyte*)FreeImage_GetBits(fib);
if (!data) {
FreeImage_Unload(fib);
return false;
}
imageWidth = FreeImage_GetWidth(fib);
imageHeight = FreeImage_GetHeight(fib);
for (i = 0; i < imageWidth * imageHeight; ++i) {
GLubyte temp = data[i << 2];
data[i << 2] = data[(i << 2) | 2];
data[(i << 2) | 2] = temp;
}
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
FreeImage_Unload(fib);
return true;
}
void EImage::bind() {
glBindTexture(GL_TEXTURE_2D, tex);
}
}