-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture_loader.cc
50 lines (42 loc) · 1.27 KB
/
texture_loader.cc
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
#include "texture_loader.h"
TextureLoader::TextureLoader() {
}
TextureInfo TextureLoader::GetTexture(const char *file_name) {
TextureInfo texture_info;
GLuint texture;
SDL_Surface *surface;
GLint num_colors;
int width;
int height;
if((surface = SDL_LoadBMP(file_name))) {
num_colors = surface->format->BytesPerPixel;
GLenum texture_format;
if (num_colors == 4) {
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
} else if (num_colors == 3) {
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
} else {
exit(0);
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, num_colors, surface->w, surface->h, 0,
texture_format, GL_UNSIGNED_BYTE, surface->pixels);
}
texture_info.texture = texture;
texture_info.colors = num_colors;
texture_info.width = surface->w;
texture_info.height = surface->h;
if ( surface ) {
SDL_FreeSurface( surface );
}
return texture_info;
}