forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture.cpp
260 lines (217 loc) · 8.16 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
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file texture.c
* This is where we do texture atlas generation.
*/
#include "lib/framework/frame.h"
#include "lib/framework/opengl.h"
#include <string.h>
#include <physfs.h>
#include "lib/framework/file.h"
#include "lib/framework/string_ext.h"
#include "lib/ivis_opengl/pietypes.h"
#include "lib/ivis_opengl/piestate.h"
#include "lib/ivis_opengl/tex.h"
#include "lib/ivis_opengl/piepalette.h"
#include "lib/ivis_opengl/screen.h"
#include "display3ddef.h"
#include "texture.h"
#include "radar.h"
#include "map.h"
#define MIPMAP_LEVELS 4
#define MIPMAP_MIN 16
#define MIPMAP_NORMAL 64
#define MIPMAP_MAX 128
/* Texture page and coordinates for each tile */
TILE_TEX_INFO tileTexInfo[MAX_TILES];
static int firstPage; // the last used page before we start adding terrain textures
int terrainPage; // texture ID of the terrain page
static int mipmap_max, mipmap_levels;
static int maxTextureSize = 2048; ///< the maximum size texture we will create
void setTextureSize(int texSize)
{
if (texSize < 16 || texSize % 16 != 0)
{
debug(LOG_ERROR, "Attempted to set bad texture size %d! Ignored.", texSize);
return;
}
maxTextureSize = texSize;
debug(LOG_TEXTURE, "texture size set to %d", texSize);
}
int getTextureSize()
{
return maxTextureSize;
}
// Generate a new texture page both in the texture page table, and on the graphics card
static int newPage(const char *name, int level, int width, int height, int count)
{
int texPage = firstPage + ((count + 1) / TILES_IN_PAGE);
// debug(LOG_TEXTURE, "newPage: texPage=%d firstPage=%d %s %d (%d,%d) (count %d + 1) / %d _TEX_INDEX=%u",
// texPage, firstPage, name, level, width, height, count, TILES_IN_PAGE, _TEX_INDEX);
if (texPage == _TEX_INDEX)
{
// We need to create a new texture page; create it and increase texture table to store it
glGenTextures(1, &_TEX_PAGE[texPage].id);
_TEX_INDEX++;
}
terrainPage = texPage;
ASSERT(_TEX_INDEX > texPage, "newPage: Index too low (%d > %d)", _TEX_INDEX, texPage);
ASSERT(_TEX_INDEX < iV_TEX_MAX, "Too many texture pages used");
sstrcpy(_TEX_PAGE[texPage].name, name);
pie_SetTexturePage(texPage);
// Specify first and last mipmap level to be used
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mipmap_levels - 1);
// debug(LOG_TEXTURE, "newPage: glTexImage2D(page=%d, level=%d) opengl id=%u", texPage, level, _TEX_PAGE[texPage].id);
glTexImage2D(GL_TEXTURE_2D, level, wz_texture_compression, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Use anisotropic filtering, if available, but only max 4.0 to reduce processor burden
if (GLEW_EXT_texture_filter_anisotropic)
{
GLfloat max;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MIN(4.0f, max));
}
return texPage;
}
bool texLoad(const char *fileName)
{
char fullPath[PATH_MAX], partialPath[PATH_MAX], *buffer;
unsigned int i, j, k, size;
int texPage;
GLint glval;
firstPage = _TEX_INDEX;
ASSERT_OR_RETURN(false, _TEX_INDEX < iV_TEX_MAX, "Too many texture pages used");
ASSERT_OR_RETURN(false, MIPMAP_MAX == TILE_WIDTH && MIPMAP_MAX == TILE_HEIGHT, "Bad tile sizes");
// store the filename so we can later determine which tileset we are using
if (tileset) free(tileset);
tileset = strdup(fileName);
// reset defaults
mipmap_max = MIPMAP_MAX;
mipmap_levels = MIPMAP_LEVELS;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glval);
while (glval < mipmap_max * TILES_IN_PAGE_COLUMN)
{
mipmap_max /= 2;
mipmap_levels--;
debug(LOG_ERROR, "Max supported texture size %dx%d is too low - reducing texture detail to %dx%d.",
(int)glval, (int)glval, mipmap_max, mipmap_max);
ASSERT(mipmap_levels > 0, "Supported texture size %d is too low to load any mipmap levels!",
(int)glval);
if (mipmap_levels == 0)
{
exit(1);
}
}
while (maxTextureSize < mipmap_max)
{
mipmap_max /= 2;
mipmap_levels--;
debug(LOG_TEXTURE, "Downgrading texture quality to %d due to user setting %d", mipmap_max, maxTextureSize);
}
/* Get and set radar colours */
sprintf(fullPath, "%s.radar", fileName);
if (!loadFile(fullPath, &buffer, &size))
{
debug(LOG_FATAL, "texLoad: Could not find radar colours at %s", fullPath);
abort(); // cannot recover; we could possibly generate a random palette?
}
i = 0; // tile
j = 0; // place in buffer
do {
unsigned int r, g, b;
int cnt = 0;
k = sscanf(buffer + j, "%2x%2x%2x%n", &r, &g, &b, &cnt);
j += cnt;
if (k >= 3)
{
radarColour(i, r, g, b);
}
i++; // next tile
} while (k >= 3 && j + 6 < size);
free(buffer);
/* Now load the actual tiles */
i = mipmap_max; // i is used to keep track of the tile dimensions
for (j = 0; j < mipmap_levels; j++)
{
int xOffset = 0, yOffset = 0; // offsets into the texture atlas
int xSize = 1;
int ySize = 1;
const int xLimit = TILES_IN_PAGE_COLUMN * i;
const int yLimit = TILES_IN_PAGE_ROW * i;
// pad width and height into ^2 values
while (xLimit > (xSize *= 2)) {}
while (yLimit > (ySize *= 2)) {}
// Generate the empty texture buffer in VRAM
texPage = newPage(fileName, j, xSize, ySize, 0);
sprintf(partialPath, "%s-%d", fileName, i);
// Load until we cannot find anymore of them
for (k = 0; k < MAX_TILES; k++)
{
iV_Image tile;
sprintf(fullPath, "%s/tile-%02d.png", partialPath, k);
if (PHYSFS_exists(fullPath)) // avoid dire warning
{
bool retval = iV_loadImage_PNG(fullPath, &tile);
ASSERT_OR_RETURN(false, retval, "Could not load %s!", fullPath);
}
else
{
// no more textures in this set
ASSERT_OR_RETURN(false, k > 0, "Could not find %s", fullPath);
break;
}
// Insert into texture page
glTexSubImage2D(GL_TEXTURE_2D, j, xOffset, yOffset, tile.width, tile.height,
GL_RGBA, GL_UNSIGNED_BYTE, tile.bmp);
free(tile.bmp);
if (i == mipmap_max) // dealing with main texture page; so register coordinates
{
tileTexInfo[k].uOffset = (float)xOffset / (float)xSize;
tileTexInfo[k].vOffset = (float)yOffset / (float)ySize;
tileTexInfo[k].texPage = texPage;
debug(LOG_TEXTURE, " texLoad: Registering k=%d i=%d u=%f v=%f xoff=%d yoff=%d xsize=%d ysize=%d tex=%d (%s)",
k, i, tileTexInfo[k].uOffset, tileTexInfo[k].vOffset, xOffset, yOffset, xSize, ySize, texPage, fullPath);
}
xOffset += i; // i is width of tile
if (xOffset + i > xLimit)
{
yOffset += i; // i is also height of tile
xOffset = 0;
}
if (yOffset + i > yLimit)
{
/* Change to next texture page */
xOffset = 0;
yOffset = 0;
debug(LOG_TEXTURE, "texLoad: Extra page added at %d for %s, was page %d, opengl id %u",
k, partialPath, texPage, (unsigned int)_TEX_PAGE[texPage].id);
texPage = newPage(fileName, j, xSize, ySize, k);
}
}
debug(LOG_TEXTURE, "texLoad: Found %d textures for %s mipmap level %d, added to page %d, opengl id %u",
k, partialPath, i, texPage, (unsigned int)_TEX_PAGE[texPage].id);
i /= 2; // halve the dimensions for the next series; OpenGL mipmaps start with largest at level zero
}
return true;
}