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
71
72
|
/*
(c) 2008 Victor "ErV" Eremin, Voronezh, Russia.
mailto: ErV2005@rambler.ru, erv@box.vsi.ru
for non-ncommercial use only
*/
#include "Textures.h"
#include <GL/glu.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
GLuint loadTexture(const char* filename_, GLenum minFilter, GLenum magFilter, bool mipmaps){
char filename[1024];
strcpy(filename, filename_+1);
while(true){
char *c = strchr(filename, ':');
if (!c) break;
*c = '/';
}
GLuint tex = 0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
SDL_Surface *surface = IMG_Load(filename);
if (!surface){
fprintf(stderr, "couldn't load file %s!\n", filename);
return 0;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLenum format = GL_RGBA;
int numColors = surface->format->BytesPerPixel;
if (surface){
switch (numColors){
case (4):{
if (surface->format->Rmask == 0x000000ff)
format = GL_RGBA;
else
format = GL_BGRA;
break;
};
case (3):{
if (surface->format->Rmask == 0x000000ff)
format = GL_RGB;
else
format = GL_BGR;
};
};
//well, our textures are upside down. Fixing it here.
Uint32 bytesPerRow = surface->format->BytesPerPixel*surface->w;
char * buf = new char[bytesPerRow];
char* p = (char*)surface->pixels;
for (Uint32 i = 0; i < surface->h/2; i++){
Uint32 offset1 = i*bytesPerRow;
Uint32 offset2 = (surface->h - i - 1)*bytesPerRow;
memcpy(buf, &p[offset1], bytesPerRow);
memcpy(&p[offset1], &p[offset2], bytesPerRow);
memcpy(&p[offset2], buf, bytesPerRow);
}
delete[] buf;
glTexImage2D(GL_TEXTURE_2D, 0, numColors, surface->w, surface->h, 0, format, GL_UNSIGNED_BYTE, surface->pixels);
if (mipmaps)
gluBuild2DMipmaps(GL_TEXTURE_2D, format, surface->w, surface->h, format, GL_UNSIGNED_BYTE, surface->pixels);
}
delete surface;
return tex;
}
|