-
Notifications
You must be signed in to change notification settings - Fork 2
/
Texture.cpp
269 lines (222 loc) · 7.4 KB
/
Texture.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "Texture.h"
#include "Image.h"
#include <GL/glu.h>
Texture::Texture(const char *ident) {
this->identifier = (char *)MemoryObj::ZAlloc(strlen(ident) + 1);
strcpy(this->identifier, ident);
this->textureType = GL_TEXTURE_2D;
this->data = NULL;
this->dataHash = 0;
this->width = 0;
this->height = 0;
this->bytesPerPixel = 0;
this->textureId = 0;
this->mipmap = true;
this->grayscale = false;
}
Texture::~Texture() {
if (textureId != 0)
TextureManager::delTextureId(textureId);
if (data)
free(data);
if (identifier)
MemoryObj::ZFree(identifier);
}
bool Texture::operator ==(const Texture& other) const {
return strcmp(this->identifier, other.identifier) == 0;
}
bool Texture::operator !=(const Texture& other) const {
return !(*this == other);
}
bool Texture::isMissMatch(const Texture *other) {
return (this->dataHash != other->dataHash || this->width != other->width || this->height != other->height);
}
void Texture::calculateHash() {
static int lhcsumtable[256];
dataHash = 0;
if (!this->data) {
// Setup table
for (int i = 0; i < 256; i++)
lhcsumtable[i] = i + 1;
// Calculate hash
int size = width * height * bytesPerPixel;
for (int i = 0; i < size; i++)
dataHash += (lhcsumtable[data[i] & 255]++);
}
}
__inline unsigned RGBAtoGrayscale(unsigned rgba) {
unsigned char *rgb, value;
unsigned output;
double shift;
output = rgba;
rgb = ((unsigned char *) &output);
value = min(255, rgb[0] * 0.2125 + rgb[1] * 0.7154 + rgb[2] * 0.0721);
//shift = sqrt(max(0,(rgb[0])-(rgb[1]+rgb[2])/2))/16;
//value = min(255,rgb[0] * 0.299 + rgb[1] * 0.587 + rgb[2] * 0.114);
#if 1
rgb[0] = value;
rgb[1] = value;
rgb[2] = value;
#else
rgb[0] = rgb[0] + (value - rgb[0]) * shift;
rgb[1] = rgb[1] + (value - rgb[1]) * shift;
rgb[2] = rgb[2] + (value - rgb[2]) * shift;
#endif
return output;
}
void PrintErrorMessage(const char *function, const char *message) {
Con_SafePrintf("&c500%s:&r %s\n");
}
void Texture::convertToGrayscale() {
if (bytesPerPixel != 4) {
PrintErrorMessage("convertToGrayscale", "Requires 32bit image to convert...");
return;
}
//go through data and convert
int size = width * height;
for (int i = 0; i < size; i++) {
data[i] = RGBAtoGrayscale(data[i]);
}
}
bool Texture::convert8To32Fullbright() {
if (bytesPerPixel != 1) {
PrintErrorMessage("convert8To32Fullbright", "Requires 8bit image to convert...");
return false;
}
bool hasFullbright = false;
unsigned *newData = (unsigned *)malloc(width * height * 4);
int size = width * height;
for (int i=0; i<size; i++) {
unsigned char p = data[i];
if (p < 224)
newData[i] = d_8to24table[255]; // transparent
else {
newData[i] = d_8to24table[p]; // fullbright
hasFullbright = true;
}
}
free(this->data);
data = (unsigned char *)newData;
bytesPerPixel = 4;
return hasFullbright;
}
void Texture::convert8To32() {
if (bytesPerPixel != 1) {
PrintErrorMessage("convert8To32", "Requires 8bit image to convert...");
return;
}
unsigned *newData = (unsigned *)malloc(width * height * 4);
int size = width * height;
for (int i=0; i<size; i++) {
newData[i] = d_8to24table[data[i]];
}
free(this->data);
this->data = (unsigned char *)newData;
this->bytesPerPixel = 4;
}
void Texture::convert24To32() {
if (bytesPerPixel != 3) {
PrintErrorMessage("convert8To32", "Requires 24bit image to convert...");
return;
}
unsigned char *newData = (unsigned char *)malloc(width * height * 4);
int size = width * height;
int input = 0;
int output = 0;
for (int i=0; i<size; i++) {
newData[output++] = data[input++];
newData[output++] = data[input++];
newData[output++] = data[input++];
newData[output++] = 0xff;
}
free(this->data);
this->data = newData;
this->bytesPerPixel = 4;
}
void Texture::fixSize() {
int scaled_width, scaled_height;
if (gl_texture_non_power_of_two) {
scaled_width = width;
scaled_height = height;
//this seems buggered (will squash really large textures, but then again, not may huge textures around)
if (scaled_width > gl_max_size.getInt()) scaled_width = gl_max_size.getInt(); //make sure its not bigger than the max size
if (scaled_height > gl_max_size.getInt()) scaled_height = gl_max_size.getInt();//make sure its not bigger than the max size
} else {
scaled_width = 1 << (int) ceil(log(width) / log(2.0));
scaled_height = 1 << (int) ceil(log(height) / log(2.0));
//this seems buggered (will squash really large textures, but then again, not may huge textures around)
scaled_width = min(scaled_width, gl_max_size.getInt()); //make sure its not bigger than the max size
scaled_height = min(scaled_height, gl_max_size.getInt()); //make sure its not bigger than the max size
}
if (scaled_width != width || scaled_height != height) {
resample(scaled_height, scaled_width);
width = scaled_width;
height = scaled_height;
}
}
void Texture::resample(const int scaledHeight, const int scaledWidth) {
unsigned char *scaled = (unsigned char *)malloc(scaledHeight * scaledWidth * this->bytesPerPixel);
Image::resample((void *)this->data, this->width, this->height, (void *)scaled, scaledWidth, scaledHeight, this->bytesPerPixel, 1);
free(this->data);
this->data = scaled;
}
void Texture::upload() {
// Ensure we have a 32bit image to upload
if (bytesPerPixel == 1)
convert8To32();
else if (bytesPerPixel == 3)
convert24To32();
// If we are grayscaling things, do it now
if (grayscale && gl_sincity.getBool())
convertToGrayscale();
// Resize the image if we need to (non-power of 2 or over max size)
fixSize();
if (textureId == 0)
textureId = TextureManager::getTextureId();
if (this->textureType == GL_TEXTURE_2D) {
glBindTexture(GL_TEXTURE_2D, textureId);
if (!mipmap) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
} else { //else build mipmaps for it
if (gl_sgis_mipmap) {
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_FALSE);
} else {
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
}
}
if (mipmap)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, TextureManager::glFilterMin);
else
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, TextureManager::glFilterMax);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, TextureManager::glFilterMax);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, gl_anisotropic.getInt());
} else if (this->textureType == GL_TEXTURE_1D) {
glBindTexture(GL_TEXTURE_1D, textureId);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Upload
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, width, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
} else {
Con_Printf("Unknown texture type for texture: %s", identifier);
}
}
void Texture::setData(unsigned char *data) {
setData(data, false);
}
void Texture::setData(unsigned char *data, bool copy) {
if (width == 0 || height == 0 || bytesPerPixel == 0) {
Sys_Printf("Error texture has a zero width, height or bbp: %s\n", this->identifier);
} else {
if (copy) {
this->data = (unsigned char*)malloc(width * height * bytesPerPixel);
memcpy(this->data, data, width * height * bytesPerPixel);
} else {
this->data = data;
}
}
}
bool Texture::hasData() {
return this->data != NULL;
}