Debian Codegraph @

prboom2/src/gl_texture.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 *
31 *---------------------------------------------------------------------
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <assert.h>
39
40 #include "gl_opengl.h"
41
42 #include "z_zone.h"
43 #ifdef _WIN32
44 #ifndef WIN32_LEAN_AND_MEAN
45 #define WIN32_LEAN_AND_MEAN 1
46 #endif
47 #include <windows.h>
48 #endif
49 #ifndef CALLBACK
50 #define CALLBACK
51 #endif
52 #include <stdio.h>
53 #include <string.h>
54 #include <SDL.h>
55 #include "doomtype.h"
56 #include "w_wad.h"
57 #include "d_event.h"
58 #include "v_video.h"
59 #include "doomstat.h"
60 #include "r_bsp.h"
61 #include "r_main.h"
62 #include "r_draw.h"
63 #include "r_sky.h"
64 #include "r_plane.h"
65 #include "r_data.h"
66 #include "p_maputl.h"
67 #include "p_tick.h"
68 #include "m_bbox.h"
69 #include "lprintf.h"
70 #include "gl_intern.h"
71 #include "gl_struct.h"
72 #include "p_spec.h"
73 #include "e6y.h"
74
75 #include "dsda/mapinfo.h"
76
77 int imageformats[5] = {0, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA};
78
79 /* TEXTURES */
80 static GLTexture **gld_GLTextures=NULL;
81 /* PATCHES FLATS SPRITES */
82 static GLTexture **gld_GLPatchTextures=NULL;
83 static GLTexture **gld_GLStaticPatchTextures = NULL;
84 /* [XA] Indexed textures, for indexed lightmode */
85 static GLTexture **gld_GLIndexedTextures = NULL;
86 static GLTexture **gld_GLIndexedPatchTextures = NULL;
87 static GLTexture **gld_GLIndexedStaticPatchTextures = NULL;
88 /* [XA] Colormap textures, for indexed lightmode */
89 static GLTexture **gld_GLColormapTextures = NULL;
90 static GLTexture **gld_GLFullbrightColormapTextures = NULL; // yeah, a "fullbright colormap" is really just a palette, but it matches the name elsewhere...
91 int gld_numGLColormaps = -1;
92 int gld_paletteIndex = 0;
93 /* [XA] Sky textures for indexed lightmode -- name is sort
94 of a misnomer since the textures themselves aren't indexed,
95 but rather have the GL colormaps pre-applied, but meh. */
96 static GLTexture **gld_GLIndexedSkyTextures = NULL;
97
98 GLuint* last_glTexID = NULL;
99
100 void gld_ResetLastTexture(void)
101 {
102 last_glTexID = NULL;
103 }
104
105 int gld_GetTexDimension(int value)
106 {
107 if (value > gl_max_texture_size)
108 value = gl_max_texture_size;
109
110 return value;
111 }
112
113 // e6y
114 // Creates TIntDynArray
115 void* NewIntDynArray(int dimCount, int *dims)
116 {
117 int i, dim;
118 int tableOffset;
119 int bufferSize = 0;
120 int tableSize = 1;
121 void* buffer;
122
123 for(dim = 0; dim < dimCount - 1; dim++)
124 {
125 tableSize *= dims[dim];
126 bufferSize += sizeof(int*) * tableSize;
127 }
128
129 bufferSize += sizeof(int) * tableSize * dims[dimCount - 1];
130
131 buffer = Z_Calloc(1, bufferSize);
132 if(!buffer)
133 {
134 return 0;
135 }
136
137 tableOffset = 0;
138 tableSize = 1;
139
140 for(dim = 0; dim < dimCount - 1; dim++)
141 {
142 tableSize *= dims[dim];
143 tableOffset += tableSize;
144
145 for(i = 0; i < tableSize; i++)
146 {
147 if(dim < dimCount - 2)
148 {
149 *((int***)buffer + tableOffset - tableSize + i) =
150 (((int**)buffer + tableOffset + i*dims[dim + 1]));
151 }
152 else
153 {
154 *((int**)buffer + tableOffset - tableSize + i) =
155 ((int*)((int**)buffer + tableOffset) + i*dims[dim + 1]);
156 }
157 }
158 }
159
160 return buffer;
161 }
162
163 #define INVULN_PLAYER_CM 8
164
165 // e6y
166 // Get index of player->fixedcolormap for GLTexture().glTexExID array
167 // There are 9 known values for player->fixedcolormap:
168 // 0 (normal) -> 0; 1 (pw_infrared) -> 1; 2..7 -> 2..7 (heretic torch flicker);
169 // 32 (pw_invulnerability) -> 8
170 static void gld_GetTextureTexID(GLTexture *gltexture, int cm)
171 {
172 static int data[NUMCOLORMAPS+1] = {
173 0, 1, 2, 3, 4, 5, 6, 7,
174 -1, -1, -1, -1, -1, -1, -1, -1,
175 -1, -1, -1, -1, -1, -1, -1, -1,
176 -1, -1, -1, -1, -1, -1, -1, -1,
177 INVULN_PLAYER_CM
178 };
179 int player_cm;
180 int bcm = boom_cm;
181
182 gltexture->cm = cm;
183 gltexture->player_cm = player_cm = data[frame_fixedcolormap];
184 assert(gltexture->player_cm != -1);
185
186 if (gltexture->flags & GLTEXTURE_INDEXED)
187 {
188 // Collapse indices that share the same texture contents in indexed mode
189 if (cm == CR_LIMIT)
190 cm = CR_DEFAULT;
191 if (player_cm != INVULN_PLAYER_CM)
192 player_cm = 0;
193 bcm = 0;
194 }
195 gltexture->texflags_p = &gltexture->texflags[cm][gltexture->player_cm];
196 gltexture->texid_p = &gltexture->glTexExID[cm][gltexture->player_cm][bcm];
197 return;
198 }
199
200 // e6y
201 // The common function for adding textures and patches
202 // Used by gld_AddNewGLTexture and gld_AddNewGLPatchTexture
203 static GLTexture *gld_AddNewGLTexItem(int num, int count, GLTexture ***items)
204 {
205 if (num<0)
206 return NULL;
207 if (num>=count)
208 return NULL;
209 if (!(*items))
210 {
211 (*items)=Z_Calloc(count, sizeof(GLTexture *));
212 }
213 if (!(*items)[num])
214 {
215 (*items)[num]=Z_Calloc(1, sizeof(GLTexture));
216 (*items)[num]->textype=GLDT_UNREGISTERED;
217
218 {
219 GLTexture *texture = (*items)[num];
220 int dims[3] = {(CR_LIMIT+MAX_MAXPLAYERS), (PLAYERCOLORMAP_COUNT), numcolormaps};
221 texture->glTexExID = NewIntDynArray(3, dims);
222 }
223 }
224 return (*items)[num];
225 }
226
227 static GLTexture *gld_AddNewGLTexture(int texture_num, dboolean indexed)
228 {
229 return gld_AddNewGLTexItem(texture_num, numtextures, indexed ? &gld_GLIndexedTextures : &gld_GLTextures);
230 }
231
232 static GLTexture *gld_AddNewGLPatchTexture(int lump, dboolean indexed)
233 {
234 if (lumpinfo[lump].flags & LUMP_STATIC)
235 return gld_AddNewGLTexItem(lump, numlumps, indexed ? &gld_GLIndexedStaticPatchTextures : &gld_GLStaticPatchTextures);
236 else
237 return gld_AddNewGLTexItem(lump, numlumps, indexed ? &gld_GLIndexedPatchTextures : &gld_GLPatchTextures);
238 }
239
240 static GLTexture *gld_AddNewGLColormapTexture(int palette_index, int gamma_level, dboolean fullbright)
241 {
242 return gld_AddNewGLTexItem(palette_index + (gamma_level * V_GetPlaypalCount()), gld_numGLColormaps, fullbright ? &gld_GLFullbrightColormapTextures : &gld_GLColormapTextures);
243 }
244
245 // [XA] adds a memory-contiguous batch of sky textures,
246 // one for each GL colormap, and returns the first one
247 static GLTexture *gld_AddNewGLIndexedSkyTextures(int texture_num)
248 {
249 int numIndexedSkyTextures = numtextures * gld_numGLColormaps;
250 int baseTextureNum = texture_num * gld_numGLColormaps;
251 int i;
252
253 for (i = 0; i < gld_numGLColormaps; i++) {
254 gld_AddNewGLTexItem(baseTextureNum + i, numIndexedSkyTextures, &gld_GLIndexedSkyTextures);
255 }
256
257 return gld_GLIndexedSkyTextures[baseTextureNum];
258 }
259
260 static GLTexture *gld_AddNewGLSkyTexture(int texture_num)
261 {
262 int numIndexedSkyTextures = numtextures * gld_numGLColormaps;
263 int baseTextureNum = texture_num * gld_numGLColormaps;
264
265 gld_AddNewGLTexItem(baseTextureNum, numIndexedSkyTextures, &gld_GLIndexedSkyTextures);
266
267 return gld_GLIndexedSkyTextures[baseTextureNum];
268 }
269
270 // [XA] returns a specific sky texture from a set.
271 static GLTexture *gld_GetGLIndexedSkyTexture(int texture_num, int index)
272 {
273 if (texture_num < 0 || texture_num >= numtextures)
274 return NULL;
275
276 return gld_GLIndexedSkyTextures[texture_num * gld_numGLColormaps + index];
277 }
278
279 // Translate from base texture reference to sky texture
280 static GLTexture *gld_GetGLSkyTexture(GLTexture *basetexture)
281 {
282 int index = 0;
283
284 if (!basetexture)
285 return NULL;
286
287 // [XA] returns the indexed sky texture for the current palette & gamma level.
288 index = gld_paletteIndex * NUM_GAMMA_LEVELS + usegamma;
289
290 return gld_GetGLIndexedSkyTexture(basetexture->index, index);
291 }
292
293 void gld_SetIndexedPalette(int palette_index)
294 {
295 gld_paletteIndex = palette_index;
296 }
297
298 static void gld_AddPatchToTexture_UnTranslated(GLTexture *gltexture, unsigned char *buffer, const rpatch_t *patch, int originx, int originy)
299 {
300 int x,y,j;
301 int xs,xe;
302 int js,je;
303 const rcolumn_t *column;
304 const byte *source;
305 int i, pos;
306 int bpp;
307 const unsigned char *playpal;
308 const lighttable_t *colormap = (fixedcolormap ? fixedcolormap : fullcolormap);
309
310 if (!gltexture)
311 return;
312 if (!patch)
313 return;
314 playpal = V_GetPlaypal();
315 xs=0;
316 xe=patch->width;
317 if ((xs+originx)>=gltexture->realtexwidth)
318 return;
319 if ((xe+originx)<=0)
320 return;
321 if ((xs+originx)<0)
322 xs=-originx;
323 if ((xe+originx)>gltexture->realtexwidth)
324 xe+=(gltexture->realtexwidth-(xe+originx));
325
326 //e6y
327 if (patch->flags&PATCH_HASHOLES)
328 gltexture->flags |= GLTEXTURE_HASHOLES;
329
330 bpp = gltexture->flags & GLTEXTURE_INDEXED ? 2 : 4;
331
332 for (x=xs;x<xe;x++)
333 {
334 #ifdef RANGECHECK
335 if (x>=patch->width)
336 {
337 lprintf(LO_ERROR,"gld_AddPatchToTexture_UnTranslated x>=patch->width (%i >= %i)\n",x,patch->width);
338 return;
339 }
340 #endif
341 column = &patch->columns[x];
342 for (i=0; i<column->numPosts; i++) {
343 const rpost_t *post = &column->posts[i];
344 y=(post->topdelta+originy);
345 js=0;
346 je=post->length;
347 if ((js+y)>=gltexture->realtexheight)
348 continue;
349 if ((je+y)<=0)
350 continue;
351 if ((js+y)<0)
352 js=-y;
353 if ((je+y)>gltexture->realtexheight)
354 je+=(gltexture->realtexheight-(je+y));
355 source = column->pixels + post->topdelta;
356 pos=bpp*(((js+y)*gltexture->buffer_width)+x+originx);
357 for (j=js;j<je;j++,pos+=(bpp*gltexture->buffer_width))
358 {
359 #ifdef RANGECHECK
360 if ((pos+bpp)>gltexture->buffer_size)
361 {
362 lprintf(LO_ERROR,"gld_AddPatchToTexture_UnTranslated pos+bpp>size (%i > %i)\n",pos+bpp,gltexture->buffer_size);
363 return;
364 }
365 #endif
366 if (gltexture->flags & GLTEXTURE_INDEXED)
367 {
368 // [XA] new indexed color mode: store the palette index in
369 // the R channel and get the colormap index in the shader.
370 buffer[pos] = gltexture->player_cm == INVULN_PLAYER_CM ? colormap[source[j]] : source[j];
371 buffer[pos+1] = 255;
372 }
373 else if (use_boom_cm && !(comp[comp_skymap] && (gltexture->flags&GLTEXTURE_SKY)))
374 {
375 //e6y: Boom's color maps
376 buffer[pos+0]=playpal[colormap[source[j]]*3+0];
377 buffer[pos+1]=playpal[colormap[source[j]]*3+1];
378 buffer[pos+2]=playpal[colormap[source[j]]*3+2];
379 buffer[pos+3]=255;
380 }
381 else
382 {
383 buffer[pos+0]=playpal[source[j]*3+0];
384 buffer[pos+1]=playpal[source[j]*3+1];
385 buffer[pos+2]=playpal[source[j]*3+2];
386 buffer[pos+3]=255;
387 }
388 }
389 }
390 }
391 }
392
393 void gld_AddPatchToTexture(GLTexture *gltexture, unsigned char *buffer, const rpatch_t *patch, int originx, int originy, int cm)
394 {
395 int x,y,j;
396 int xs,xe;
397 int js,je;
398 const rcolumn_t *column;
399 const byte *source;
400 int i, pos;
401 const unsigned char *playpal;
402 const unsigned char *outr;
403 const lighttable_t *colormap = (fixedcolormap ? fixedcolormap : fullcolormap);
404 int bpp;
405
406 if (!gltexture)
407 return;
408 if (!patch)
409 return;
410
411 if ((cm==CR_DEFAULT) || (cm==CR_LIMIT))
412 outr=&colormaps[0][0];
413 else if (cm==CR_DARKEN)
414 outr=&colormaps[0][256 * 15];
415 else if (cm<CR_LIMIT)
416 outr=colrngs[cm];
417 else
418 outr=translationtables + 256*((cm-CR_LIMIT)-1);
419
420 playpal = V_GetPlaypal();
421 xs=0;
422 xe=patch->width;
423 if ((xs+originx)>=gltexture->realtexwidth)
424 return;
425 if ((xe+originx)<=0)
426 return;
427 if ((xs+originx)<0)
428 xs=-originx;
429 if ((xe+originx)>gltexture->realtexwidth)
430 xe+=(gltexture->realtexwidth-(xe+originx));
431
432 //e6y
433 if (patch->flags&PATCH_HASHOLES)
434 gltexture->flags |= GLTEXTURE_HASHOLES;
435
436 bpp = gltexture->flags & GLTEXTURE_INDEXED ? 2 : 4;
437
438 for (x=xs;x<xe;x++)
439 {
440 #ifdef RANGECHECK
441 if (x>=patch->width)
442 {
443 lprintf(LO_ERROR,"gld_AddPatchToTexture x>=patch->width (%i >= %i)\n",x,patch->width);
444 return;
445 }
446 #endif
447 column = &patch->columns[x];
448 for (i=0; i<column->numPosts; i++) {
449 const rpost_t *post = &column->posts[i];
450 y=(post->topdelta+originy);
451 js=0;
452 je=post->length;
453 if ((js+y)>=gltexture->realtexheight)
454 continue;
455 if ((je+y)<=0)
456 continue;
457 if ((js+y)<0)
458 js=-y;
459 if ((je+y)>gltexture->realtexheight)
460 je+=(gltexture->realtexheight-(je+y));
461 source = column->pixels + post->topdelta;
462 pos=bpp*(((js+y)*gltexture->buffer_width)+x+originx);
463 for (j=js;j<je;j++,pos+=(bpp*gltexture->buffer_width))
464 {
465 #ifdef RANGECHECK
466 if ((pos+bpp)>gltexture->buffer_size)
467 {
468 lprintf(LO_ERROR,"gld_AddPatchToTexture pos+bpp>size (%i > %i)\n",pos+bpp,gltexture->buffer_size);
469 return;
470 }
471 #endif
472 //e6y: Boom's color maps
473 if (gltexture->flags & GLTEXTURE_INDEXED)
474 {
475 // [XA] new indexed color mode
476 buffer[pos]=gltexture->player_cm == INVULN_PLAYER_CM ? colormap[outr[source[j]]] : outr[source[j]];
477 buffer[pos+1]=255;
478 }
479 else if (use_boom_cm)
480 {
481 //e6y: Boom's color maps
482 buffer[pos+0]=playpal[colormap[outr[source[j]]]*3+0];
483 buffer[pos+1]=playpal[colormap[outr[source[j]]]*3+1];
484 buffer[pos+2]=playpal[colormap[outr[source[j]]]*3+2];
485 buffer[pos+3]=255;
486 }
487 else
488 {
489 buffer[pos+0]=playpal[outr[source[j]]*3+0];
490 buffer[pos+1]=playpal[outr[source[j]]*3+1];
491 buffer[pos+2]=playpal[outr[source[j]]*3+2];
492 buffer[pos+3]=255;
493 }
494 }
495 }
496 }
497 }
498
499 static void gld_AddRawToTexture(GLTexture *gltexture, unsigned char *buffer, const unsigned char *raw)
500 {
501 int x,y,w,pos,bpp;
502 const unsigned char *playpal;
503 const lighttable_t *colormap = (fixedcolormap ? fixedcolormap : fullcolormap);
504
505 if (!gltexture)
506 return;
507 if (!raw)
508 return;
509 w = gltexture->realtexwidth;
510 playpal = V_GetPlaypal();
511 bpp = gltexture->flags & GLTEXTURE_INDEXED ? 2 : 4;
512 for (y=0;y<gltexture->realtexheight;y++)
513 {
514 pos=bpp*(y*gltexture->buffer_width);
515 for (x=0;x<gltexture->realtexwidth;x++,pos+=bpp)
516 {
517 #ifdef RANGECHECK
518 if ((pos+bpp)>=gltexture->buffer_size)
519 {
520 lprintf(LO_ERROR,"gld_AddRawToTexture pos+bpp>size (%i > %i)\n",pos+bpp,gltexture->buffer_size);
521 return;
522 }
523 #endif
524 if (gltexture->flags & GLTEXTURE_INDEXED)
525 {
526 // [XA] new indexed color mode
527 buffer[pos]=gltexture->player_cm == INVULN_PLAYER_CM ? colormap[raw[y*w+x]] : raw[y*w+x];
528 buffer[pos+1] = 255;
529 }
530 else if (use_boom_cm)
531 {
532 //e6y: Boom's color maps
533 buffer[pos+0]=playpal[colormap[raw[y*w+x]]*3+0];
534 buffer[pos+1]=playpal[colormap[raw[y*w+x]]*3+1];
535 buffer[pos+2]=playpal[colormap[raw[y*w+x]]*3+2];
536 buffer[pos+3]=255;
537 }
538 else
539 {
540 buffer[pos+0]=playpal[raw[y*w+x]*3+0];
541 buffer[pos+1]=playpal[raw[y*w+x]*3+1];
542 buffer[pos+2]=playpal[raw[y*w+x]*3+2];
543 buffer[pos+3]=255;
544 }
545 }
546 }
547 }
548
549 static void gld_AddColormapToTexture(GLTexture *gltexture, unsigned char *buffer, int palette_index, int gamma_level, dboolean fullbright)
550 {
551 int x,y,pos;
552 const unsigned char *playpal;
553 const lighttable_t *colormap;
554 const byte * gtable;
555 int gtlump;
556
557 if (!gltexture)
558 return;
559 if (gltexture->realtexwidth<256)
560 return;
561
562 // figure out which palette variant to use
563 // (e.g. normal, pain flash, item flash, etc).
564 playpal = V_GetPlaypal() + (palette_index*PALETTE_SIZE);
565 colormap = fullcolormap;
566
567 // fallback in case the current colormap hasn't been set
568 // yet; this occurs when rendering the main menu for the
569 // first time if the game is launched in indexed lightmode
570 if (colormap == NULL)
571 colormap = colormaps[0];
572
573 // also yoink the gamma table and apply
574 // software gamma emulation to the texture.
575 gtlump = W_CheckNumForName2("GAMMATBL", ns_prboom);
576 gtable = (const byte*) W_LumpByNum(gtlump) + 256 * gamma_level;
577
578 // construct a colormap texture using the selected
579 // palette variant & gamma, for shader lookup.
580 for (y=0;y<gltexture->realtexheight;y++)
581 {
582 pos=4*(y*gltexture->buffer_width);
583 for (x=0;x<gltexture->realtexwidth;x++,pos+=4)
584 {
585 #ifdef RANGECHECK
586 if ((pos+3)>=gltexture->buffer_size)
587 {
588 lprintf(LO_ERROR,"gld_AddColormapToTexture pos+3>=size (%i >= %i)\n",pos+3,gltexture->buffer_size);
589 return;
590 }
591 #endif
592 // [XA] also build a set of "fullbright" colormaps for UI.
593 // this is needed because the first row of the colormap
594 // isn't necessarily identical to the palette itself (e.g.
595 // in Heretic it's slightly off for a few colors), and UI
596 // needs to be drawn with exact palette colors, no lighting.
597 if (fullbright)
598 {
599 buffer[pos+0]=gtable[playpal[x*3+0]];
600 buffer[pos+1]=gtable[playpal[x*3+1]];
601 buffer[pos+2]=gtable[playpal[x*3+2]];
602 buffer[pos+3]=255;
603 }
604 else
605 {
606 buffer[pos+0]=gtable[playpal[colormap[y*256+x]*3+0]];
607 buffer[pos+1]=gtable[playpal[colormap[y*256+x]*3+1]];
608 buffer[pos+2]=gtable[playpal[colormap[y*256+x]*3+2]];
609 buffer[pos+3]=255;
610 }
611 }
612 }
613 }
614
615 // [XA] indexed lightmode sky support -- this basically creates
616 // a "pre-baked" version of the sky texture with the given
617 // palette (i.e. normal, pain flash, etc.) and gamma applied.
618 // the game selects the correct texture to use on the fly.
619 static void gld_AddIndexedSkyToTexture(GLTexture *gltexture, unsigned char *buffer, const rpatch_t *patch, int palette_index, int gamma_level)
620 {
621 int x,y,j;
622 int xs,xe;
623 int js,je;
624 const rcolumn_t *column;
625 const byte *source;
626 int i, pos;
627 const unsigned char *playpal;
628 const byte * gtable;
629 int gtlump;
630
631 if (!gltexture || !patch)
632 return;
633
634 // get palette & gamma table for the given args
635 playpal = V_GetPlaypal() + (palette_index*PALETTE_SIZE);
636 gtlump = W_CheckNumForName2("GAMMATBL", ns_prboom);
637 gtable = (const byte*)W_LumpByNum(gtlump) + 256 * gamma_level;
638
639 xs=0;
640 xe=patch->width;
641 if (xs>=gltexture->realtexwidth)
642 return;
643 if (xe<=0)
644 return;
645 if (xs<0)
646 xs=0;
647 if (xe>gltexture->realtexwidth)
648 xe+=(gltexture->realtexwidth-xe);
649
650 //e6y
651 if (patch->flags&PATCH_HASHOLES)
652 gltexture->flags |= GLTEXTURE_HASHOLES;
653
654 for (x=xs;x<xe;x++)
655 {
656 #ifdef RANGECHECK
657 if (x>=patch->width)
658 {
659 lprintf(LO_ERROR,"gld_AddIndexedSkyToTexture x>=patch->width (%i >= %i)\n",x,patch->width);
660 return;
661 }
662 #endif
663 column = &patch->columns[x];
664 for (i=0; i<column->numPosts; i++) {
665 const rpost_t *post = &column->posts[i];
666 y=(post->topdelta);
667 js=0;
668 je=post->length;
669 if ((js+y)>=gltexture->realtexheight)
670 continue;
671 if ((je+y)<=0)
672 continue;
673 if ((js+y)<0)
674 js=-y;
675 if ((je+y)>gltexture->realtexheight)
676 je+=(gltexture->realtexheight-(je+y));
677 source = column->pixels + post->topdelta;
678 pos=4*(((js+y)*gltexture->buffer_width)+x);
679 for (j=js;j<je;j++,pos+=(4*gltexture->buffer_width))
680 {
681 #ifdef RANGECHECK
682 if ((pos+3)>=gltexture->buffer_size)
683 {
684 lprintf(LO_ERROR,"gld_AddIndexedSkyToTexture pos+3>=size (%i >= %i)\n",pos+3,gltexture->buffer_size);
685 return;
686 }
687 #endif
688 //e6y: Boom's color maps
689 if (use_boom_cm && !comp[comp_skymap])
690 {
691 const lighttable_t *colormap = (fixedcolormap ? fixedcolormap : fullcolormap);
692 buffer[pos+0]=gtable[playpal[colormap[source[j]]*3+0]];
693 buffer[pos+1]=gtable[playpal[colormap[source[j]]*3+1]];
694 buffer[pos+2]=gtable[playpal[colormap[source[j]]*3+2]];
695 }
696 else
697 {
698 buffer[pos+0]=gtable[playpal[source[j]*3+0]];
699 buffer[pos+1]=gtable[playpal[source[j]*3+1]];
700 buffer[pos+2]=gtable[playpal[source[j]*3+2]];
701 }
702 buffer[pos+3]=255;
703 }
704 }
705 }
706 }
707
708 static GLTexture *gld_InitUnregisteredTexture(int texture_num, GLTexture *gltexture, dboolean indexed, dboolean sky)
709 {
710 texture_t *texture = NULL;
711
712 if (texture_num >= 0 || texture_num < numtextures)
713 texture = textures[texture_num];
714
715 if (!texture)
716 return NULL;
717
718 gltexture->textype = GLDT_BROKEN;
719 gltexture->index = texture_num;
720
721 //e6y
722 gltexture->flags = 0;
723
724 if (indexed && !sky)
725 gltexture->flags |= GLTEXTURE_INDEXED;
726
727 gltexture->realtexwidth = texture->width;
728 gltexture->realtexheight = texture->height;
729
730 if (sky)
731 {
732 const rpatch_t *patch;
733
734 patch = R_HackedSkyPatch(texture);
735
736 if (patch)
737 {
738 gltexture->patch_index = texture->patches[0].patch;
739 gltexture->flags |= GLTEXTURE_SKYHACK;
740 gltexture->realtexheight=patch->height;
741 }
742 }
743
744 gltexture->leftoffset = 0;
745 gltexture->topoffset = 0;
746 gltexture->tex_width = gld_GetTexDimension(gltexture->realtexwidth);
747 gltexture->tex_height = gld_GetTexDimension(gltexture->realtexheight);
748 gltexture->width = MIN(gltexture->realtexwidth, gltexture->tex_width);
749 gltexture->height = MIN(gltexture->realtexheight, gltexture->tex_height);
750 gltexture->buffer_width = gltexture->tex_width;
751 gltexture->buffer_height = gltexture->tex_height;
752 gltexture->width = gltexture->tex_width;
753 gltexture->height = gltexture->tex_height;
754 gltexture->buffer_width = gltexture->realtexwidth;
755 gltexture->buffer_height = gltexture->realtexheight;
756
757 //e6y: right/bottom UV coordinates for texture drawing
758 gltexture->scalexfac = (float) gltexture->width / (float) gltexture->tex_width;
759 gltexture->scaleyfac = (float) gltexture->height / (float) gltexture->tex_height;
760
761 gltexture->buffer_size = gltexture->buffer_width * gltexture->buffer_height * ((indexed && !sky) ? 2 : 4);
762
763 if (gltexture->realtexwidth > gltexture->buffer_width)
764 return gltexture;
765
766 if (gltexture->realtexheight > gltexture->buffer_height)
767 return gltexture;
768
769 gltexture->textype = GLDT_TEXTURE;
770
771 return gltexture;
772 }
773
774 //e6y: "force" flag for loading texture with zero index
775 // [XA] "indexed" flag for new indexed lightmode
776 GLTexture *gld_RegisterTexture(int texture_num, dboolean mipmap, dboolean force, dboolean indexed, dboolean sky)
777 {
778 GLTexture *gltexture;
779
780 //e6y: textures with zero index should be loaded sometimes
781 if (texture_num == NO_TEXTURE && !force)
782 return NULL;
783
784 if (sky)
785 gltexture = gld_AddNewGLSkyTexture(texture_num);
786 else
787 gltexture = gld_AddNewGLTexture(texture_num, indexed);
788
789 if (!gltexture)
790 return NULL;
791
792 if (gltexture->textype == GLDT_UNREGISTERED)
793 gltexture = gld_InitUnregisteredTexture(texture_num, gltexture, indexed, sky);
794
795 return gltexture;
796 }
797
798 unsigned char* gld_GetTextureBuffer(GLuint texid, int miplevel, int *width, int *height)
799 {
800 int w, h;
801 static unsigned char *buf = NULL;
802 static int buf_size = 512 * 256 * 4;
803
804 if (!buf)
805 {
806 buf = Z_Malloc(buf_size);
807 }
808
809 if (texid)
810 {
811 glBindTexture(GL_TEXTURE_2D, texid);
812 }
813
814 glGetTexLevelParameteriv(GL_TEXTURE_2D, miplevel, GL_TEXTURE_WIDTH, &w);
815 glGetTexLevelParameteriv(GL_TEXTURE_2D, miplevel, GL_TEXTURE_HEIGHT, &h);
816 if (w * h * 4 > buf_size)
817 {
818 Z_Free(buf);
819 buf_size = w * h * 4;
820 buf = Z_Malloc(buf_size);
821 }
822 glGetTexImage(GL_TEXTURE_2D, miplevel, GL_RGBA, GL_UNSIGNED_BYTE, buf);
823
824 if (width)
825 *width = w;
826 if (height)
827 *height = h;
828
829 return buf;
830 }
831
832 void gld_SetTexFilters(GLTexture *gltexture)
833 {
834 float aniso_filter = 0.0f;
835
836 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
837 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
838 if (aniso_filter > 0.0f)
839 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso_filter);
840 }
841
842 void gld_SetTexClamp(GLTexture *gltexture, unsigned int flags)
843 {
844 //if ((gltexture->flags & GLTEXTURE_CLAMPXY) != (flags & GLTEXTURE_CLAMPXY))
845 /* sp1n0za 05/2010: simplify */
846 if ((*gltexture->texflags_p ^ flags) & GLTEXTURE_CLAMPXY)
847 {
848 int need_clamp_x = (flags & GLTEXTURE_CLAMPX);
849 int need_clamp_y = (flags & GLTEXTURE_CLAMPY);
850 int has_clamp_x = (*gltexture->texflags_p & GLTEXTURE_CLAMPX);
851 int has_clamp_y = (*gltexture->texflags_p & GLTEXTURE_CLAMPY);
852
853 if (need_clamp_x)
854 {
855 if (!has_clamp_x)
856 {
857 *gltexture->texflags_p |= GLTEXTURE_CLAMPX;
858 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
859 }
860 }
861 else
862 {
863 if (has_clamp_x)
864 {
865 *gltexture->texflags_p &= ~GLTEXTURE_CLAMPX;
866 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
867 }
868 }
869
870 if (need_clamp_y)
871 {
872 if (!has_clamp_y)
873 {
874 *gltexture->texflags_p |= GLTEXTURE_CLAMPY;
875 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
876 }
877 }
878 else
879 {
880 if (has_clamp_y)
881 {
882 *gltexture->texflags_p &= ~GLTEXTURE_CLAMPY;
883 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
884 }
885 }
886 }
887 }
888
889 int gld_BuildTexture(GLTexture *gltexture, void *data, dboolean readonly, int width, int height)
890 {
891 int tex_width, tex_height, tex_format;
892
893 tex_format = gltexture->flags & GLTEXTURE_INDEXED ? GL_RG : GL_RGBA;
894 tex_width = gld_GetTexDimension(width);
895 tex_height = gld_GetTexDimension(height);
896
897 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
898
899 glTexImage2D( GL_TEXTURE_2D, 0, tex_format,
900 tex_width, tex_height,
901 0, tex_format, GL_UNSIGNED_BYTE, data);
902
903 gld_SetTexFilters(gltexture);
904
905 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
906 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
907
908 if (!readonly)
909 {
910 Z_Free(data);
911 data = NULL;
912 }
913
914 return true;
915 }
916
917 void gld_BindTexture(GLTexture *gltexture, unsigned int flags, dboolean sky)
918 {
919 const rpatch_t *patch;
920 unsigned char *buffer;
921
922 // resolve the actual texture for the related sky
923 if (sky)
924 {
925 gltexture = gld_GetGLSkyTexture(gltexture);
926 }
927
928 if (!gltexture || gltexture->textype != GLDT_TEXTURE)
929 {
930 glBindTexture(GL_TEXTURE_2D, 0);
931 last_glTexID = NULL;
932 return;
933 }
934
935 gld_GetTextureTexID(gltexture, CR_DEFAULT);
936
937 if (last_glTexID == gltexture->texid_p)
938 {
939 gld_SetTexClamp(gltexture, flags);
940 return;
941 }
942
943 last_glTexID = gltexture->texid_p;
944
945 if (*gltexture->texid_p != 0)
946 {
947 glBindTexture(GL_TEXTURE_2D, *gltexture->texid_p);
948 gld_SetTexClamp(gltexture, flags);
949 glsl_SetTextureDims(0, gltexture->realtexwidth, gltexture->realtexheight);
950 return;
951 }
952
953 buffer=(unsigned char*)Z_Malloc(gltexture->buffer_size);
954 memset(buffer,0,gltexture->buffer_size);
955
956 if (gltexture->flags & GLTEXTURE_SKYHACK)
957 {
958 patch=R_PatchByNum(gltexture->patch_index);
959 }
960 else
961 {
962 patch=R_TextureCompositePatchByNum(gltexture->index);
963 }
964
965 if (sky)
966 {
967 gld_AddIndexedSkyToTexture(gltexture, buffer, patch, gld_paletteIndex, usegamma);
968 }
969 else
970 {
971 gld_AddPatchToTexture(gltexture, buffer, patch, 0, 0, CR_DEFAULT);
972 }
973
974 if (*gltexture->texid_p == 0)
975 glGenTextures(1, gltexture->texid_p);
976 glBindTexture(GL_TEXTURE_2D, *gltexture->texid_p);
977
978 gld_BuildTexture(gltexture, buffer, false, gltexture->buffer_width, gltexture->buffer_height);
979
980 gld_SetTexClamp(gltexture, flags);
981 glsl_SetTextureDims(0, gltexture->realtexwidth, gltexture->realtexheight);
982 }
983
984 GLTexture *gld_RegisterPatch(int lump, int cm, dboolean is_sprite, dboolean indexed)
985 {
986 const rpatch_t *patch;
987 GLTexture *gltexture;
988
989 gltexture=gld_AddNewGLPatchTexture(lump, indexed);
990 if (!gltexture)
991 return NULL;
992 if (gltexture->textype==GLDT_UNREGISTERED)
993 {
994 patch=R_PatchByNum(lump);
995 if (!patch)
996 return NULL;
997 gltexture->textype=GLDT_BROKEN;
998 gltexture->index=lump;
999
1000 //e6y
1001 gltexture->flags = 0;
1002 if (is_sprite)
1003 {
1004 gltexture->flags |= GLTEXTURE_SPRITE;
1005 }
1006
1007 if (indexed)
1008 gltexture->flags |= GLTEXTURE_INDEXED;
1009
1010 gltexture->realtexwidth=patch->width;
1011 gltexture->realtexheight=patch->height;
1012 gltexture->leftoffset=patch->leftoffset;
1013 gltexture->topoffset=patch->topoffset;
1014 gltexture->tex_width=gld_GetTexDimension(gltexture->realtexwidth);
1015 gltexture->tex_height=gld_GetTexDimension(gltexture->realtexheight);
1016 gltexture->width=MIN(gltexture->realtexwidth, gltexture->tex_width);
1017 gltexture->height=MIN(gltexture->realtexheight, gltexture->tex_height);
1018 gltexture->buffer_width=gltexture->tex_width;
1019 gltexture->buffer_height=gltexture->tex_height;
1020 gltexture->width=MIN(gltexture->realtexwidth, gltexture->tex_width);
1021 gltexture->height=MIN(gltexture->realtexheight, gltexture->tex_height);
1022 gltexture->buffer_width=MAX(gltexture->realtexwidth, gltexture->tex_width);
1023 gltexture->buffer_height=MAX(gltexture->realtexheight, gltexture->tex_height);
1024
1025 //e6y: right/bottom UV coordinates for patch drawing
1026 gltexture->scalexfac=(float)gltexture->width/(float)gltexture->tex_width;
1027 gltexture->scaleyfac=(float)gltexture->height/(float)gltexture->tex_height;
1028
1029 gltexture->buffer_size=gltexture->buffer_width*gltexture->buffer_height*(indexed ? 2 : 4);
1030 if (gltexture->realtexwidth>gltexture->buffer_width)
1031 return gltexture;
1032 if (gltexture->realtexheight>gltexture->buffer_height)
1033 return gltexture;
1034 gltexture->textype=GLDT_PATCH;
1035 }
1036 return gltexture;
1037 }
1038
1039 void gld_BindPatch(GLTexture *gltexture, int cm)
1040 {
1041 const rpatch_t *patch;
1042 unsigned char *buffer;
1043
1044 if (!gltexture || gltexture->textype != GLDT_PATCH)
1045 {
1046 glBindTexture(GL_TEXTURE_2D, 0);
1047 last_glTexID = NULL;
1048 return;
1049 }
1050
1051 gld_GetTextureTexID(gltexture, cm);
1052
1053 if (last_glTexID == gltexture->texid_p)
1054 {
1055 gld_SetTexClamp(gltexture, GLTEXTURE_CLAMPXY);
1056 return;
1057 }
1058
1059 last_glTexID = gltexture->texid_p;
1060
1061 if (*gltexture->texid_p != 0)
1062 {
1063 glBindTexture(GL_TEXTURE_2D, *gltexture->texid_p);
1064 gld_SetTexClamp(gltexture, GLTEXTURE_CLAMPXY);
1065 glsl_SetTextureDims(0, gltexture->realtexwidth, gltexture->realtexheight);
1066 return;
1067 }
1068
1069 patch=R_PatchByNum(gltexture->index);
1070 buffer=(unsigned char*)Z_Malloc(gltexture->buffer_size);
1071 memset(buffer,0,gltexture->buffer_size);
1072 gld_AddPatchToTexture(gltexture, buffer, patch, 0, 0, cm);
1073
1074 if (*gltexture->texid_p == 0)
1075 glGenTextures(1, gltexture->texid_p);
1076 glBindTexture(GL_TEXTURE_2D, *gltexture->texid_p);
1077
1078 gld_BuildTexture(gltexture, buffer, false, gltexture->buffer_width, gltexture->buffer_height);
1079
1080 gld_SetTexClamp(gltexture, GLTEXTURE_CLAMPXY);
1081 glsl_SetTextureDims(0, gltexture->realtexwidth, gltexture->realtexheight);
1082 }
1083
1084 GLTexture *gld_RegisterRaw(int lump, int width, int height, dboolean mipmap, dboolean indexed)
1085 {
1086 GLTexture *gltexture;
1087
1088 gltexture=gld_AddNewGLPatchTexture(lump, indexed);
1089 if (!gltexture)
1090 return NULL;
1091 if (gltexture->textype==GLDT_UNREGISTERED)
1092 {
1093 gltexture->textype=GLDT_BROKEN;
1094 gltexture->index=lump;
1095
1096 //e6y
1097 gltexture->flags = 0;
1098
1099 if (indexed)
1100 gltexture->flags |= GLTEXTURE_INDEXED;
1101
1102 gltexture->realtexwidth= width;
1103 gltexture->realtexheight= height;
1104 gltexture->leftoffset=0;
1105 gltexture->topoffset=0;
1106 gltexture->tex_width=gld_GetTexDimension(gltexture->realtexwidth);
1107 gltexture->tex_height=gld_GetTexDimension(gltexture->realtexheight);
1108 gltexture->width=MIN(gltexture->realtexwidth, gltexture->tex_width);
1109 gltexture->height=MIN(gltexture->realtexheight, gltexture->tex_height);
1110 gltexture->buffer_width=gltexture->tex_width;
1111 gltexture->buffer_height=gltexture->tex_height;
1112 gltexture->width=gltexture->tex_width;
1113 gltexture->height=gltexture->tex_height;
1114 gltexture->buffer_width=gltexture->realtexwidth;
1115 gltexture->buffer_height=gltexture->realtexheight;
1116
1117 //e6y: right/bottom UV coordinates for flat drawing
1118 gltexture->scalexfac=(float)gltexture->width/(float)gltexture->tex_width;
1119 gltexture->scaleyfac=(float)gltexture->height/(float)gltexture->tex_height;
1120
1121 gltexture->buffer_size=gltexture->buffer_width*gltexture->buffer_height*(indexed ? 2 : 4);
1122 if (gltexture->realtexwidth>gltexture->buffer_width)
1123 return gltexture;
1124 if (gltexture->realtexheight>gltexture->buffer_height)
1125 return gltexture;
1126
1127 gltexture->textype=GLDT_FLAT;
1128 }
1129 return gltexture;
1130 }
1131
1132 void gld_BindRaw(GLTexture *gltexture, unsigned int flags)
1133 {
1134 const unsigned char *raw;
1135 unsigned char *buffer;
1136
1137 if (!gltexture || gltexture->textype != GLDT_FLAT)
1138 {
1139 glBindTexture(GL_TEXTURE_2D, 0);
1140 last_glTexID = NULL;
1141 return;
1142 }
1143
1144 gld_GetTextureTexID(gltexture, CR_DEFAULT);
1145
1146 if (last_glTexID == gltexture->texid_p)
1147 {
1148 gld_SetTexClamp(gltexture, flags);
1149 return;
1150 }
1151
1152 last_glTexID = gltexture->texid_p;
1153
1154 if (*gltexture->texid_p != 0)
1155 {
1156 glBindTexture(GL_TEXTURE_2D, *gltexture->texid_p);
1157 gld_SetTexClamp(gltexture, flags);
1158 glsl_SetTextureDims(0, gltexture->realtexwidth, gltexture->realtexheight);
1159 return;
1160 }
1161
1162 raw=W_LumpByNum(gltexture->index);
1163 buffer=(unsigned char*)Z_Malloc(gltexture->buffer_size);
1164 memset(buffer,0,gltexture->buffer_size);
1165 gld_AddRawToTexture(gltexture, buffer, raw);
1166 if (*gltexture->texid_p == 0)
1167 glGenTextures(1, gltexture->texid_p);
1168 glBindTexture(GL_TEXTURE_2D, *gltexture->texid_p);
1169
1170 gld_BuildTexture(gltexture, buffer, false, gltexture->buffer_width, gltexture->buffer_height);
1171
1172 gld_SetTexClamp(gltexture, flags);
1173 glsl_SetTextureDims(0, gltexture->realtexwidth, gltexture->realtexheight);
1174 }
1175
1176
1177 // [XA] new functions for registering & binding sky textures.
1178 // for truecolor lightmodes, these just call into the regular
1179 // RegisterTexture function; for indexed lightmode, there's
1180 // a special set of sky textures with palettes pre-applied
1181 // that gets selected based on current palette, and we need
1182 // to generate & bind those here.
1183 // NOTE: since these only differer in behavior in indexed
1184 // lightmode, and indexed lightmode enforces gld_DrawStripsSky,
1185 // only that sky function bothers to call these, because lazy. ;)
1186 GLTexture *gld_RegisterSkyTexture(int texture_num, dboolean force)
1187 {
1188 GLTexture *basetexture;
1189 GLTexture *gltexture;
1190 int i;
1191
1192 if (texture_num == NO_TEXTURE && !force)
1193 return NULL;
1194
1195 basetexture = gld_AddNewGLIndexedSkyTextures(texture_num);
1196 if (!basetexture)
1197 return NULL;
1198
1199 // [XA] initialize all textures in the set.
1200 for(i = 0; i < gld_numGLColormaps; i++)
1201 {
1202 gltexture = gld_GetGLIndexedSkyTexture(texture_num, i);
1203 if (gltexture->textype == GLDT_UNREGISTERED)
1204 gld_InitUnregisteredTexture(texture_num, gltexture, true, true);
1205 }
1206
1207 return basetexture;
1208 }
1209
1210 void gld_BindSkyTexture(GLTexture *gltexture)
1211 {
1212 gld_BindTexture(gltexture, 0, true);
1213 }
1214
1215 GLTexture *gld_RegisterColormapTexture(int palette_index, int gamma_level, dboolean fullbright)
1216 {
1217 GLTexture *gltexture;
1218
1219 gltexture=gld_AddNewGLColormapTexture(palette_index, gamma_level, fullbright);
1220 if (!gltexture)
1221 return NULL;
1222 if (gltexture->textype==GLDT_UNREGISTERED)
1223 {
1224 gltexture->textype=GLDT_BROKEN;
1225 gltexture->index=palette_index;
1226
1227 gltexture->realtexwidth=256;
1228 gltexture->realtexheight=(fullbright ? 1 : NUMCOLORMAPS+1); // it's +1 'cause of the invuln palette
1229 gltexture->leftoffset=0;
1230 gltexture->topoffset=0;
1231 gltexture->tex_width=gld_GetTexDimension(gltexture->realtexwidth);
1232 gltexture->tex_height=gld_GetTexDimension(gltexture->realtexheight);
1233 gltexture->width=MIN(gltexture->realtexwidth, gltexture->tex_width);
1234 gltexture->height=MIN(gltexture->realtexheight, gltexture->tex_height);
1235 gltexture->buffer_width=gltexture->tex_width;
1236 gltexture->buffer_height=gltexture->tex_height;
1237
1238 //e6y: right/bottom UV coordinates for patch drawing
1239 gltexture->scalexfac=(float)gltexture->width/(float)gltexture->tex_width;
1240 gltexture->scaleyfac=(float)gltexture->height/(float)gltexture->tex_height;
1241
1242 gltexture->buffer_size=gltexture->buffer_width*gltexture->buffer_height*4;
1243
1244 if (gltexture->realtexwidth>gltexture->buffer_width)
1245 return gltexture;
1246 if (gltexture->realtexheight>gltexture->buffer_height)
1247 return gltexture;
1248 gltexture->textype=GLDT_COLORMAP;
1249 }
1250 return gltexture;
1251 }
1252
1253 void gld_BindColormapTexture(GLTexture *gltexture, int palette_index, int gamma_level, dboolean fullbright)
1254 {
1255 unsigned char *buffer;
1256
1257 // abort if we're trying to bind the wrong texture type
1258 if (!gltexture || gltexture->textype != GLDT_COLORMAP)
1259 {
1260 glBindTexture(GL_TEXTURE_2D, 0);
1261 last_glTexID = NULL;
1262 return;
1263 }
1264
1265 // 'push' the active texture -- use texture2 for
1266 // colormaps since texture1 is already in use.
1267 GLEXT_glActiveTextureARB(GL_TEXTURE2_ARB);
1268
1269 gld_GetTextureTexID(gltexture, CR_DEFAULT);
1270
1271 if (last_glTexID == gltexture->texid_p)
1272 {
1273 GLEXT_glActiveTextureARB(GL_TEXTURE0_ARB);
1274 return;
1275 }
1276 last_glTexID = gltexture->texid_p;
1277
1278 // texure data already initialized; use it
1279 if (*gltexture->texid_p != 0)
1280 {
1281 glBindTexture(GL_TEXTURE_2D, *gltexture->texid_p);
1282 GLEXT_glActiveTextureARB(GL_TEXTURE0_ARB);
1283 glsl_SetTextureDims(2, gltexture->realtexwidth, gltexture->realtexheight);
1284 return;
1285 }
1286
1287 // collect the actual texture data
1288 buffer = (unsigned char*) Z_Malloc(gltexture->buffer_size);
1289 memset(buffer, 0, gltexture->buffer_size);
1290
1291 gld_AddColormapToTexture(gltexture, buffer, palette_index, gamma_level, fullbright);
1292
1293 // bind it, finally :P
1294 if (*gltexture->texid_p == 0)
1295 glGenTextures(1, gltexture->texid_p);
1296 glBindTexture(GL_TEXTURE_2D, *gltexture->texid_p);
1297
1298 gld_BuildTexture(gltexture, buffer, false, gltexture->buffer_width, gltexture->buffer_height);
1299
1300 // 'pop' the active texture back to the default.
1301 GLEXT_glActiveTextureARB(GL_TEXTURE0_ARB);
1302 glsl_SetTextureDims(2, gltexture->realtexwidth, gltexture->realtexheight);
1303 }
1304
1305 void gld_InitColormapTextures(dboolean fullbright)
1306 {
1307 GLTexture *gltexture;
1308
1309 // figure out how many palette variants are in the
1310 // PLAYPAL lump and create a colormap texture for
1311 // each palette at each gamma level. word.
1312 gld_numGLColormaps = V_GetPlaypalCount() * NUM_GAMMA_LEVELS;
1313
1314 // abort if we're trying to show an out-of-bounds palette.
1315 if (gld_paletteIndex < 0 || gld_paletteIndex >= gld_numGLColormaps)
1316 return;
1317
1318 // lazy-init and bind the colormap texture for
1319 // the current palette index. since colormaps
1320 // won't change during the frame, we can go
1321 // ahead and bind them now and be done with it.
1322 gltexture = gld_RegisterColormapTexture(gld_paletteIndex, usegamma, fullbright);
1323 if (gltexture)
1324 gld_BindColormapTexture(gltexture, gld_paletteIndex, usegamma, fullbright);
1325 }
1326
1327 // Darkness values for 1 to 5 iterations of fuzz darkening, derived from
1328 // dcolors.c by lovely847
1329 #define FUZZ1 0.188235294117647058824
1330 #define FUZZ2 0.341176470588235294118
1331 #define FUZZ3 0.462745098039215686275
1332 #define FUZZ4 0.564705882352941176471
1333 #define FUZZ5 0.647058823529411764706
1334
1335 // Darkness values for each fuzz pixel in a column, derived from software fuzz
1336 // offset table by counting consecutive negative offsets
1337 static const float fuzz[50] =
1338 {
1339 FUZZ1, FUZZ2, FUZZ1, FUZZ2, FUZZ1, FUZZ1, FUZZ2, FUZZ1, FUZZ1, FUZZ2,
1340 FUZZ1, FUZZ1, FUZZ1, FUZZ2, FUZZ1, FUZZ1, FUZZ1, FUZZ2, FUZZ3, FUZZ4,
1341 FUZZ5, FUZZ1, FUZZ2, FUZZ3, FUZZ1, FUZZ1, FUZZ1, FUZZ1, FUZZ2, FUZZ1,
1342 FUZZ2, FUZZ1, FUZZ1, FUZZ2, FUZZ3, FUZZ1, FUZZ1, FUZZ2, FUZZ3, FUZZ4,
1343 FUZZ5, FUZZ1, FUZZ1, FUZZ1, FUZZ1, FUZZ2, FUZZ1, FUZZ1, FUZZ2, FUZZ1
1344 };
1345
1346 static GLuint fuzz_texid = 0;
1347
1348 void gld_InitFuzzTexture(void)
1349 {
1350 if (fuzz_texid == 0)
1351 {
1352 GLEXT_glActiveTextureARB(GL_TEXTURE1_ARB);
1353
1354 glGenTextures(1, &fuzz_texid);
1355 glBindTexture(GL_TEXTURE_2D, fuzz_texid);
1356
1357 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
1358
1359 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED,
1360 sizeof(fuzz) / sizeof(*fuzz), 1, 0, GL_RED,
1361 GL_FLOAT, fuzz);
1362
1363 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1364 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1365 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1366 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1367
1368 GLEXT_glActiveTextureARB(GL_TEXTURE0_ARB);
1369
1370 glsl_SetTextureDims(1, sizeof(fuzz) / sizeof(*fuzz), 1);
1371 }
1372 }
1373
1374 // e6y
1375 // The common function for cleaning textures and patches
1376 // gld_CleanTextures and gld_CleanPatchTextures are replaced with that
1377 static void gld_CleanTexItems(int count, GLTexture ***items)
1378 {
1379 int i, j;
1380
1381 if (!(*items))
1382 return;
1383
1384 for (i=0; i<count; i++)
1385 {
1386 if ((*items)[i])
1387 {
1388 int cm, n;
1389 for (j=0; j<(CR_LIMIT+MAX_MAXPLAYERS); j++)
1390 {
1391 for (n=0; n<PLAYERCOLORMAP_COUNT; n++)
1392 {
1393 for (cm=0; cm<numcolormaps; cm++)
1394 {
1395 if ((*items) && (*items)[i]->glTexExID[j][n][cm])
1396 {
1397 glDeleteTextures(1,&((*items)[i]->glTexExID[j][n][cm]));
1398 }
1399 }
1400 }
1401 }
1402
1403 Z_Free((*items)[i]->glTexExID);
1404 (*items)[i]->glTexExID = NULL;
1405
1406 Z_Free((*items)[i]);
1407 }
1408 }
1409 memset((*items),0,count*sizeof(GLTexture *));
1410 }
1411
1412 void gld_FlushTextures(void)
1413 {
1414 gld_CleanTexItems(numtextures, &gld_GLTextures);
1415 gld_CleanTexItems(numlumps, &gld_GLPatchTextures);
1416 gld_CleanTexItems(numlumps, &gld_GLStaticPatchTextures);
1417 gld_CleanTexItems(numtextures, &gld_GLIndexedTextures);
1418 gld_CleanTexItems(numlumps, &gld_GLIndexedPatchTextures);
1419 gld_CleanTexItems(numlumps, &gld_GLIndexedStaticPatchTextures);
1420 gld_CleanTexItems(gld_numGLColormaps, &gld_GLColormapTextures);
1421 gld_CleanTexItems(gld_numGLColormaps, &gld_GLFullbrightColormapTextures);
1422 gld_CleanTexItems(numtextures * gld_numGLColormaps, &gld_GLIndexedSkyTextures);
1423
1424 if (fuzz_texid)
1425 {
1426 glDeleteTextures(1, &fuzz_texid);
1427 fuzz_texid = 0;
1428 }
1429
1430 gld_ResetLastTexture();
1431
1432 gld_InitSky();
1433 gld_InitColormapTextures(V_IsUILightmodeIndexed() || V_IsAutomapLightmodeIndexed() || V_IsMenuLightmodeIndexed());
1434
1435 // do not draw anything in current frame after flushing
1436 gld_ResetDrawInfo();
1437 }
1438
1439 static void CalcHitsCount(const byte *hitlist, int size, int *hit, int*hitcount)
1440 {
1441 int i;
1442
1443 if (!hitlist || !hit || !hitcount)
1444 return;
1445
1446 *hit = 0;
1447 *hitcount = 0;
1448 for (i = 0; i < size; i++)
1449 {
1450 if (hitlist[i])
1451 (*hitcount)++;
1452 }
1453 }
1454
1455 void gld_Precache(void)
1456 {
1457 int i;
1458 byte *hitlist;
1459 int hit, hitcount = 0;
1460 GLTexture *gltexture;
1461
1462 unsigned int tics = SDL_GetTicks();
1463
1464 if (nodrawers)
1465 return;
1466
1467 if (timingdemo)
1468 return;
1469
1470 gld_ProgressStart();
1471
1472 {
1473 size_t size = numflats > num_sprites ? numflats : num_sprites;
1474 hitlist = Z_Malloc((size_t)numtextures > size ? (size_t)numtextures : size);
1475 }
1476
1477 // Precache flats.
1478
1479 memset(hitlist, 0, numflats);
1480
1481 for (i = numsectors; --i >= 0; )
1482 {
1483 int j,k;
1484
1485 int floorpic = sectors[i].floorpic;
1486 int ceilingpic = sectors[i].ceilingpic;
1487
1488 anim_t *flatanims[2] = {
1489 anim_flats[floorpic].anim,
1490 anim_flats[ceilingpic].anim
1491 };
1492
1493 hitlist[floorpic] = hitlist[ceilingpic] = 1;
1494
1495 //e6y: animated flats
1496 for (k = 0; k < 2; k++)
1497 {
1498 if (flatanims[k] && !flatanims[k]->istexture)
1499 {
1500 for (j = flatanims[k]->basepic; j < flatanims[k]->basepic + flatanims[k]->numpics; j++)
1501 hitlist[j] = 1;
1502 }
1503 }
1504 }
1505
1506 CalcHitsCount(hitlist, numflats, &hit, &hitcount);
1507
1508 for (i = numflats; --i >= 0; )
1509 if (hitlist[i])
1510 {
1511 gld_ProgressUpdate("Loading Flats...", ++hit, hitcount);
1512 gltexture = gld_RegisterFlat(i, true, true);
1513 if (gltexture)
1514 {
1515 gld_BindFlat(gltexture, 0);
1516 }
1517 }
1518
1519 // Precache textures.
1520
1521 memset(hitlist, 0, numtextures);
1522
1523 for (i = numsides; --i >= 0;)
1524 {
1525 int j, k;
1526 int bottomtexture, toptexture, midtexture;
1527 anim_t *textureanims[3];
1528
1529 bottomtexture = sides[i].bottomtexture;
1530 toptexture = sides[i].toptexture;
1531 midtexture = sides[i].midtexture;
1532
1533 textureanims[0] = anim_textures[bottomtexture].anim;
1534 textureanims[1] = anim_textures[toptexture].anim;
1535 textureanims[2] = anim_textures[midtexture].anim;
1536
1537 hitlist[bottomtexture] =
1538 hitlist[toptexture] =
1539 hitlist[midtexture] = 1;
1540
1541 //e6y: animated textures
1542 for (k = 0; k < 3; k++)
1543 {
1544 if (textureanims[k] && textureanims[k]->istexture)
1545 {
1546 for (j = textureanims[k]->basepic; j < textureanims[k]->basepic + textureanims[k]->numpics; j++)
1547 hitlist[j] = 1;
1548 }
1549 }
1550
1551 //e6y: swithes
1552 {
1553 int GetPairForSwitchTexture(side_t *side);
1554 int pair = GetPairForSwitchTexture(&sides[i]);
1555 if (pair != -1)
1556 hitlist[pair] = 1;
1557 }
1558 }
1559
1560 // Sky texture is always present.
1561 // Note that F_SKY1 is the name used to
1562 // indicate a sky floor/ceiling as a flat,
1563 // while the sky texture is stored like
1564 // a wall texture, with an episode dependend
1565 // name.
1566
1567 if (hitlist)
1568 hitlist[skytexture] = 0;
1569
1570 CalcHitsCount(hitlist, numtextures, &hit, &hitcount);
1571
1572 for (i = numtextures; --i >= 0; )
1573 if (hitlist[i])
1574 {
1575 gld_ProgressUpdate("Loading Textures...", ++hit, hitcount);
1576 gltexture = gld_RegisterTexture(i, i != skytexture, false, true, false);
1577 if (gltexture)
1578 {
1579 gld_BindTexture(gltexture, 0, false);
1580 }
1581 }
1582
1583 // Precache sprites.
1584 memset(hitlist, 0, num_sprites);
1585
1586 if (thinkercap.next)
1587 {
1588 thinker_t *th;
1589 for (th = thinkercap.next ; th != &thinkercap ; th=th->next)
1590 {
1591 if (th->function == P_MobjThinker)
1592 hitlist[((mobj_t *)th)->sprite] = 1;
1593 }
1594 }
1595
1596 hit = 0;
1597 hitcount = 0;
1598 for (i = 0; i < num_sprites; i++)
1599 {
1600 if (hitlist[i])
1601 hitcount += 7 * sprites[i].numframes;
1602 }
1603
1604 for (i=num_sprites; --i >= 0;)
1605 if (hitlist[i])
1606 {
1607 int j = sprites[i].numframes;
1608 while (--j >= 0)
1609 {
1610 short *sflump = sprites[i].spriteframes[j].lump;
1611 int k = 7;
1612 do
1613 {
1614 gld_ProgressUpdate("Loading Sprites...", ++hit, hitcount);
1615 gltexture = gld_RegisterPatch(firstspritelump + sflump[k], CR_LIMIT, true, true);
1616 if (gltexture)
1617 {
1618 gld_BindPatch(gltexture, CR_LIMIT);
1619 }
1620 }
1621 while (--k >= 0);
1622 }
1623 }
1624 Z_Free(hitlist);
1625
1626 gld_ProgressEnd();
1627
1628 gld_InitFBO();
1629
1630 // e6y: some statistics
1631 {
1632 char map[9];
1633
1634 snprintf(map, sizeof(map), "%s", dsda_MapLumpName(gameepisode, gamemap));
1635
1636 lprintf(LO_DEBUG, "gld_Precache: %s done in %d ms\n", map, SDL_GetTicks() - tics);
1637 }
1638 }
1639
1640 void gld_CleanMemory(void)
1641 {
1642 gld_CleanVertexData();
1643 gld_CleanTexItems(numtextures, &gld_GLTextures);
1644 gld_CleanTexItems(numlumps, &gld_GLPatchTextures);
1645 gld_CleanTexItems(numtextures, &gld_GLIndexedTextures);
1646 gld_CleanTexItems(numlumps, &gld_GLIndexedPatchTextures);
1647 gld_CleanTexItems(numtextures * gld_numGLColormaps, &gld_GLIndexedSkyTextures);
1648 gl_preprocessed = false;
1649 }
1650
1651 void gld_CleanStaticMemory(void)
1652 {
1653 gld_CleanTexItems(numlumps, &gld_GLStaticPatchTextures);
1654 gld_CleanTexItems(numlumps, &gld_GLIndexedStaticPatchTextures);
1655 gld_CleanTexItems(gld_numGLColormaps, &gld_GLColormapTextures);
1656 gld_CleanTexItems(gld_numGLColormaps, &gld_GLFullbrightColormapTextures);
1657 }