-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadTexture.cpp
70 lines (53 loc) · 2.49 KB
/
LoadTexture.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "LoadTexture.h"
#include "FreeImage.h"
GLuint LoadTexture(const char* fname)
{
GLuint tex_id;
FIBITMAP* tempImg = FreeImage_Load(FreeImage_GetFileType(fname, 0), fname);
FIBITMAP* img = FreeImage_ConvertTo32Bits(tempImg);
FreeImage_Unload(tempImg);
GLuint w = FreeImage_GetWidth(img);
GLuint h = FreeImage_GetHeight(img);
GLuint scanW = FreeImage_GetPitch(img);
GLubyte* byteImg = new GLubyte[h*scanW];
FreeImage_ConvertToRawBits(byteImg, img, scanW, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
FreeImage_Unload(img);
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, byteImg);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
delete byteImg;
return tex_id;
}
GLuint LoadCube(const std::string& fname)
{
GLuint tex_id;
const std::string faces[6] = { "posx.png", "negx.png", "posy.png", "negy.png", "posz.png", "negz.png" };
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex_id);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
for (int i = 0; i<6; i++)
{
std::string cube_face = fname + "\\" + faces[i];
FIBITMAP* tempImg = FreeImage_Load(FreeImage_GetFileType(cube_face.c_str(), 0), cube_face.c_str());
FIBITMAP* img = FreeImage_ConvertTo32Bits(tempImg);
FreeImage_Unload(tempImg);
GLuint w = FreeImage_GetWidth(img);
GLuint h = FreeImage_GetHeight(img);
GLuint scanW = FreeImage_GetPitch(img);
GLubyte* byteImg = new GLubyte[h*scanW];
FreeImage_ConvertToRawBits(byteImg, img, scanW, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE); //last arg FALSE set origin of the texture to the lower left
FreeImage_Unload(img);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, byteImg);
delete byteImg;
}
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
return tex_id;
}