-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLoader.cpp
More file actions
157 lines (146 loc) · 4.95 KB
/
ImageLoader.cpp
File metadata and controls
157 lines (146 loc) · 4.95 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <apollo/Graphics/ImageLoader.h>
#include <apollo/Utilities/ResourceManager.h>
namespace Graphics
{
namespace ImageLoader
{
SDL_Surface* Zip(SDL_Surface* colour, SDL_Surface* alpha)
{
if (alpha)
{
assert(colour->w == alpha->w);
assert(colour->h == alpha->h);
}
SDL_Surface* sfc = SDL_CreateRGBSurface(SDL_SWSURFACE, colour->w, colour->h, 32,
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000);
const char* cpixels = static_cast<const char*>(colour->pixels);
const char* apixels = alpha ? static_cast<const char*>(alpha->pixels) : NULL;
char* dpixels = static_cast<char*>(sfc->pixels);
int pixcount = colour->w * colour->h;
for (int i = 0; i < pixcount; ++i)
{
uint8_t cR, cG, cB, aR = 0x00, dead;
uint32_t res;
SDL_GetRGBA(*(uint32_t*)cpixels, colour->format, &cR, &cG, &cB, &dead);
if (alpha)
SDL_GetRGBA(*(uint32_t*)apixels, alpha->format, &aR, &dead, &dead, &dead);
res = SDL_MapRGBA(sfc->format, cR, cG, cB, aR);
memcpy(dpixels, &res, sfc->format->BytesPerPixel);
cpixels += colour->format->BytesPerPixel;
if (alpha)
apixels += alpha->format->BytesPerPixel;
dpixels += sfc->format->BytesPerPixel;
}
return sfc;
}
SDL_Surface* LoadImage ( const std::string& path )
{
SDL_RWops* rwops = ResourceManager::OpenFile(path);
if (!rwops)
return NULL;
SDL_Surface* surface = IMG_LoadTyped_RW(rwops, 1, const_cast<char*>("PNG"));
return surface;
}
static SDL_Surface* InvertSurface(SDL_Surface* surface)
{
int scanlines = surface->h, pitch = surface->pitch;
SDL_Surface* copy = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, surface->format->BitsPerPixel,
surface->format->Rmask,
surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask);
assert(pitch == copy->pitch);
for (int i = 0; i < scanlines; ++i)
{
int sourceLine = i;
int destLine = scanlines - i - 1;
char* dstBase = (char*)(copy->pixels) + (pitch * destLine);
const char* srcBase = (const char*)(surface->pixels) + (pitch * sourceLine);
memcpy(dstBase, srcBase, pitch);
}
return copy;
}
GLuint CreateTexture ( SDL_Surface* surface, bool autofree, bool rectangle, bool invert )
{
GLuint texID;
if (invert)
{
SDL_Surface* inverted = InvertSurface(surface);
texID = CreateTexture(inverted, true, rectangle, false);
}
else
{
glGenTextures(1, &texID);
GLenum target = rectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D;
glBindTexture(target, texID);
bool mipmaps = !rectangle && strstr((const char*)glGetString(GL_EXTENSIONS), "GL_SGIS_generate_mipmap");
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (mipmaps)
glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
if (surface->format->BytesPerPixel == 3)
{
GLenum format = (surface->format->Rmask) != 0xFF ? GL_BGR_EXT : GL_RGB;
glTexImage2D(target, 0, GL_RGB,
surface->w, surface->h, 0,
format, GL_UNSIGNED_BYTE, surface->pixels);
}
else
{
GLenum format = (surface->format->Rmask) != 0xFF ? GL_BGRA_EXT : GL_RGBA;
glTexImage2D(target, 0, GL_RGBA,
surface->w, surface->h, 0,
format, GL_UNSIGNED_BYTE, surface->pixels);
}
}
if (autofree)
SDL_FreeSurface(surface);
return texID;
}
static inline uint8_t heightAt(SDL_Surface* surface, int x, int y)
{
if (!surface) return 0;
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= surface->w) x = surface->w - 1;
if (y >= surface->h) y = surface->h - 1;
return *((unsigned char*)surface->pixels + surface->format->BytesPerPixel*(surface->w*y + x));
}
#define HEIGHTAT(x, y) *((unsigned char*)heightMap->pixels + bpp*(width*(y) + x))
SDL_Surface* CreateBumpMap(SDL_Surface* heightMap)
{
int width = heightMap ? heightMap->w : 1;
int height = heightMap ? heightMap->h : 1;
SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
short Vdiff, Hdiff;
float VdiffF, HdiffF;
float TC;
unsigned char L, R, U, D;
Uint32 rgba;
L = heightAt(heightMap, x - 1, y);
R = heightAt(heightMap, x + 1, y);
U = heightAt(heightMap, x, y - 1);
D = heightAt(heightMap, x, y + 1);
Vdiff = (short)D - (short)U;
Hdiff = (short)R - (short)L;
VdiffF = Vdiff / 255.0f;
HdiffF = Hdiff / 255.0f;
TC = sqrtf(VdiffF*VdiffF + HdiffF*HdiffF - 1.0f);
VdiffF += 1.0f; VdiffF *= 0.5f;
HdiffF += 1.0f; HdiffF *= 0.5f;
TC += 1.0f; TC *= 0.5f;
rgba = SDL_MapRGBA(surface->format, HdiffF, VdiffF, TC, heightAt(heightMap, x, y));
*((Uint32*)surface->pixels + y*width + x) = rgba;
}
}
return surface;
}
}
}