4377edc717e039979a0205f839d5510e5d554ccf
[xonotic/darkplaces.git] / gl_rsurf.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_surf.c: surface-related refresh code
21
22 #include "quakedef.h"
23
24 int             lightmap_textures;
25
26 signed blocklights[18*18*3]; // LordHavoc: *3 for colored lighting
27
28 // LordHavoc: skinny but tall lightmaps for quicker subimage uploads
29 #define BLOCK_WIDTH             128
30 #define BLOCK_HEIGHT    128
31 // LordHavoc: increased lightmap limit from 64 to 1024
32 #define MAX_LIGHTMAPS   1024
33 #define LIGHTMAPSIZE    (BLOCK_WIDTH*BLOCK_HEIGHT*4)
34
35 int                     active_lightmaps;
36
37 short allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
38
39 byte *lightmaps[MAX_LIGHTMAPS];
40 short lightmapupdate[MAX_LIGHTMAPS][2];
41
42 int lightmapalign, lightmapalignmask; // LordHavoc: NVIDIA's broken subimage fix, see BuildLightmaps for notes
43 cvar_t gl_lightmapalign = {"gl_lightmapalign", "4"};
44 cvar_t gl_lightmaprgba = {"gl_lightmaprgba", "0"};
45 cvar_t gl_nosubimagefragments = {"gl_nosubimagefragments", "0"};
46 cvar_t gl_nosubimage = {"gl_nosubimage", "0"};
47 cvar_t r_ambient = {"r_ambient", "0"};
48 cvar_t gl_vertex = {"gl_vertex", "0"};
49 cvar_t gl_texsort = {"gl_texsort", "1"};
50 //cvar_t gl_funnywalls = {"gl_funnywalls", "0"}; // LordHavoc: see BuildSurfaceDisplayList
51
52 qboolean lightmaprgba, nosubimagefragments, nosubimage, skyisvisible;
53 int lightmapbytes;
54
55 extern qboolean gl_arrays;
56
57 extern int r_dlightframecount;
58
59 void gl_surf_start()
60 {
61 }
62
63 void gl_surf_shutdown()
64 {
65 }
66
67 void GL_Surf_Init()
68 {
69         int i;
70         for (i = 0;i < MAX_LIGHTMAPS;i++)
71                 lightmaps[i] = NULL;
72         Cvar_RegisterVariable(&gl_lightmapalign);
73         Cvar_RegisterVariable(&gl_lightmaprgba);
74         Cvar_RegisterVariable(&gl_nosubimagefragments);
75         Cvar_RegisterVariable(&gl_nosubimage);
76         Cvar_RegisterVariable(&r_ambient);
77 //      Cvar_RegisterVariable(&gl_funnywalls);
78         Cvar_RegisterVariable(&gl_vertex);
79         Cvar_RegisterVariable(&gl_texsort);
80         // check if it's the glquake minigl driver
81         if (strncasecmp(gl_vendor,"3Dfx",4)==0)
82         if (!gl_arrays)
83         {
84 //              Cvar_SetValue("gl_nosubimagefragments", 1);
85 //              Cvar_SetValue("gl_nosubimage", 1);
86                 Cvar_SetValue("gl_lightmode", 0);
87         }
88
89         R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown);
90 }
91
92 /*
93 ===============
94 R_BuildLightMap
95
96 Combine and scale multiple lightmaps into the 8.8 format in blocklights
97 ===============
98 */
99 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
100 {
101         int                     smax, tmax;
102         int                     t;
103         int                     i, j, size;
104         byte            *lightmap;
105         int                     scale;
106         int                     maps;
107         int                     *bl;
108
109         surf->cached_lighthalf = lighthalf;
110         surf->cached_ambient = r_ambient.value;
111
112         smax = (surf->extents[0]>>4)+1;
113         tmax = (surf->extents[1]>>4)+1;
114         size = smax*tmax;
115         lightmap = surf->samples;
116
117 // set to full bright if no light data
118         if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
119         {
120                 bl = blocklights;
121                 for (i=0 ; i<size ; i++)
122                 {
123                         *bl++ = 255*256;
124                         *bl++ = 255*256;
125                         *bl++ = 255*256;
126                 }
127         }
128         else
129         {
130 // clear to no light
131                 bl = blocklights;
132                 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
133                 for (i=0 ; i<size ; i++)
134                 {
135                         *bl++ = j;
136                         *bl++ = j;
137                         *bl++ = j;
138                 }
139
140 // add all the lightmaps
141                 if (lightmap)
142                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
143                         {
144                                 scale = d_lightstylevalue[surf->styles[maps]];
145                                 surf->cached_light[maps] = scale;       // 8.8 fraction
146                                 bl = blocklights;
147                                 for (i=0 ; i<size ; i++)
148                                 {
149                                         *bl++ += *lightmap++ * scale;
150                                         *bl++ += *lightmap++ * scale;
151                                         *bl++ += *lightmap++ * scale;
152                                 }
153                         }
154         }
155         stride -= (smax*lightmapbytes);
156         bl = blocklights;
157         if (lighthalf)
158         {
159                 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
160                 // the image is brightened as a processing pass
161                 if (lightmaprgba)
162                 {
163                         for (i=0 ; i<tmax ; i++, dest += stride)
164                         {
165                                 for (j=0 ; j<smax ; j++)
166                                 {
167                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
168                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
169                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
170                                         *dest++ = 255;
171                                 }
172                         }
173                 }
174                 else
175                 {
176                         for (i=0 ; i<tmax ; i++, dest += stride)
177                         {
178                                 for (j=0 ; j<smax ; j++)
179                                 {
180                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
181                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
182                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
183                                 }
184                         }
185                 }
186         }
187         else
188         {
189                 if (lightmaprgba)
190                 {
191                         for (i=0 ; i<tmax ; i++, dest += stride)
192                         {
193                                 for (j=0 ; j<smax ; j++)
194                                 {
195                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
196                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
197                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
198                                         *dest++ = 255;
199                                 }
200                         }
201                 }
202                 else
203                 {
204                         for (i=0 ; i<tmax ; i++, dest += stride)
205                         {
206                                 for (j=0 ; j<smax ; j++)
207                                 {
208                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
209                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
210                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
211                                 }
212                         }
213                 }
214         }
215 }
216
217 byte templight[32*32*4];
218
219 void R_UpdateLightmap(msurface_t *s, int lnum)
220 {
221         int smax, tmax;
222         // upload the new lightmap texture fragment
223         if(r_upload.value)
224                 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
225         if (nosubimage || nosubimagefragments)
226         {
227                 if (lightmapupdate[lnum][0] > s->light_t)
228                         lightmapupdate[lnum][0] = s->light_t;
229                 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
230                         lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
231                 if (lightmaprgba)
232                         R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
233                 else
234                         R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
235         }
236         else
237         {
238                 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
239                 tmax = (s->extents[1]>>4)+1;
240                 if (lightmaprgba)
241                 {
242                         R_BuildLightMap (s, templight, smax * 4);
243                         if(r_upload.value)
244                                 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
245                 }
246                 else
247                 {
248                         R_BuildLightMap (s, templight, smax * 3);
249                         if(r_upload.value)
250                                 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
251                 }
252         }
253 }
254
255
256 /*
257 ===============
258 R_TextureAnimation
259
260 Returns the proper texture for a given time and base texture
261 ===============
262 */
263 texture_t *R_TextureAnimation (texture_t *base)
264 {
265         texture_t *original;
266         int             relative;
267         int             count;
268
269         if (currententity->frame)
270         {
271                 if (base->alternate_anims)
272                         base = base->alternate_anims;
273         }
274         
275         if (!base->anim_total)
276                 return base;
277
278         original = base;
279
280         relative = (int)(cl.time*10) % base->anim_total;
281
282         count = 0;      
283         while (base->anim_min > relative || base->anim_max <= relative)
284         {
285                 base = base->anim_next;
286                 if (!base)
287                 {
288                         Con_Printf("R_TextureAnimation: broken cycle");
289                         return original;
290                 }
291                 if (++count > 100)
292                 {
293                         Con_Printf("R_TextureAnimation: infinite cycle");
294                         return original;
295                 }
296         }
297
298         return base;
299 }
300
301
302 /*
303 =============================================================
304
305         BRUSH MODELS
306
307 =============================================================
308 */
309
310
311 extern  int             solidskytexture;
312 extern  int             alphaskytexture;
313 extern  float   speedscale;             // for top sky and bottom sky
314
315 extern char skyname[];
316
317 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
318 float   turbsin[256] =
319 {
320         #include "gl_warp_sin.h"
321 };
322 #define TURBSCALE (256.0 / (2 * M_PI))
323
324
325 void UploadLightmaps()
326 {
327         int i;
328         if (nosubimage || nosubimagefragments)
329         {
330                 for (i = 0;i < MAX_LIGHTMAPS;i++)
331                 {
332                         if (lightmapupdate[i][0] < lightmapupdate[i][1])
333                         {
334                                 if(r_upload.value)
335                                 {
336                                         glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
337                                         if (nosubimage)
338                                         {
339                                                 if (lightmaprgba)
340                                                         glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
341                                                 else
342                                                         glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
343                                         }
344                                         else
345                                         {
346                                                 if (lightmaprgba)
347                                                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, lightmapupdate[i][0], BLOCK_WIDTH, lightmapupdate[i][1] - lightmapupdate[i][0], GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i] + (BLOCK_WIDTH * 4 * lightmapupdate[i][0]));
348                                                 else
349                                                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, lightmapupdate[i][0], BLOCK_WIDTH, lightmapupdate[i][1] - lightmapupdate[i][0], GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i] + (BLOCK_WIDTH * 3 * lightmapupdate[i][0]));
350                                         }
351                                 }
352                         }
353                         lightmapupdate[i][0] = BLOCK_HEIGHT;
354                         lightmapupdate[i][1] = 0;
355                 }
356         }
357 }
358
359 float   wvert[1024*6]; // used by the following functions
360
361 void RSurf_DrawSky(msurface_t *s, int transform)
362 {
363         glpoly_t *p;
364         int i;
365         float *v;
366         for (p=s->polys ; p ; p=p->next)
367         {
368                 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
369                 {
370                         skypoly[currentskypoly].firstvert = currentskyvert;
371                         skypoly[currentskypoly++].verts = p->numverts;
372                         if (transform)
373                         {
374                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
375                                 {
376                                         softwaretransform(v, skyvert[currentskyvert].v);
377                                         currentskyvert++;
378                                 }
379                         }
380                         else
381                         {
382                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
383                                 {
384                                         skyvert[currentskyvert].v[0] = v[0];
385                                         skyvert[currentskyvert].v[1] = v[1];
386                                         skyvert[currentskyvert++].v[2] = v[2];
387                                 }
388                         }
389                 }
390         }
391 }
392
393 int RSurf_Light(int *dlightbits, glpoly_t *polys)
394 {
395         float           cr, cg, cb, radius, radius2, f, *v, *wv;
396         int                     i, a, b, lit = false;
397         unsigned int c, d;
398         dlight_t        *light;
399         vec_t           *lightorigin;
400         glpoly_t        *p;
401         for (a = 0;a < 8;a++)
402         {
403                 if ((c = dlightbits[a]))
404                 {
405                         for (b = 0, d = 1;c;b++, d <<= 1)
406                         {
407                                 if (c & d)
408                                 {
409                                         c -= d;
410                                         light = &cl_dlights[a * 32 + b];
411                                         lightorigin = light->origin;
412                                         cr = light->color[0];
413                                         cg = light->color[1];
414                                         cb = light->color[2];
415                                         radius = light->radius*light->radius*LIGHTSCALE;
416                                         radius2 = radius * (256.0f / LIGHTSCALE);
417                                         wv = wvert;
418                                         for (p = polys;p;p = p->next)
419                                         {
420                                                 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
421                                                 {
422                                                         f = VectorDistance2(wv, lightorigin);
423                                                         if (f < radius)
424                                                         {
425                                                                 f = radius2 / (f + LIGHTOFFSET);
426                                                                 wv[3] += cr * f;
427                                                                 wv[4] += cg * f;
428                                                                 wv[5] += cb * f;
429                                                                 lit = true;
430                                                         }
431                                                         wv += 6;
432                                                 }
433                                         }
434                                 }
435                         }
436                 }
437         }
438         return lit;
439 }
440
441 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
442 {
443         int             i;
444         float   os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
445         glpoly_t *p;
446         float   *wv, *v;
447         wv = wvert;
448         for (p = s->polys;p;p = p->next)
449         {
450                 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
451                 {
452                         if (transform)
453                                 softwaretransform(v, wv);
454                         else
455                                 VectorCopy(v, wv);
456                         if (r_waterripple.value)
457                                 wv[2] += r_waterripple.value * turbsin[(int)((wv[0]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * turbsin[(int)((wv[1]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
458                         wv[3] = wv[4] = wv[5] = 128.0f;
459                         wv += 6;
460                 }
461         }
462         if (s->dlightframe == r_dlightframecount && r_dynamic.value)
463                 RSurf_Light(s->dlightbits, s->polys);
464         wv = wvert;
465         // FIXME: make fog texture if water texture is transparent?
466         for (p=s->polys ; p ; p=p->next)
467         {
468                 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
469                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
470                         transpolyvert(wv[0], wv[1], wv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), wv[3], wv[4], wv[5], alpha);
471                 transpolyend();
472         }
473 }
474
475 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
476 {
477         int             i, lit = false, polys = 0, verts = 0;
478         float   *v, *wv;
479         glpoly_t *p;
480         wallpoly_t *wp;
481         wallvert_t *out;
482         // check for lightmap modification
483         if (r_dynamic.value)
484         {
485                 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
486                 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
487                 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
488                 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
489                 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
490                         R_UpdateLightmap(s, s->lightmaptexturenum);
491         }
492         wv = wvert;
493         for (p = s->polys;p;p = p->next)
494         {
495                 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
496                 {
497                         if (transform)
498                                 softwaretransform(v, wv);
499                         else
500                                 VectorCopy(v, wv);
501                         wv[3] = wv[4] = wv[5] = 0.0f;
502                         wv += 6;
503                 }
504                 verts += p->numverts;
505                 polys++;
506         }
507         if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
508                 return;
509         if (s->dlightframe == r_dlightframecount && r_dynamic.value)
510                 lit = RSurf_Light(s->dlightbits, s->polys);
511         wv = wvert;
512         wp = &wallpoly[currentwallpoly];
513         out = &wallvert[currentwallvert];
514         currentwallpoly += polys;
515         for (p = s->polys;p;p = p->next)
516         {
517                 v = p->verts[0];
518                 wp->texnum = (unsigned short) t->gl_texturenum;
519                 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
520                 wp->glowtexnum = (unsigned short) t->gl_glowtexturenum;
521                 wp->firstvert = currentwallvert;
522                 wp->numverts = p->numverts;
523                 wp->lit = lit;
524                 wp++;
525                 currentwallvert += p->numverts;
526                 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++)
527                 {
528                         if (lit)
529                         {
530                                 if (lighthalf)
531                                 {
532                                         out->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
533                                         out->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
534                                         out->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
535                                         out->a = 255;
536                                 }
537                                 else
538                                 {
539                                         out->r = (byte) (bound(0, (int) wv[3], 255));
540                                         out->g = (byte) (bound(0, (int) wv[4], 255));
541                                         out->b = (byte) (bound(0, (int) wv[5], 255));
542                                         out->a = 255;
543                                 }
544                         }
545                         out->vert[0] = wv[0];
546                         out->vert[1] = wv[1];
547                         out->vert[2] = wv[2];
548                         out->s = v[3];
549                         out->t = v[4];
550                         out->u = v[5];
551                         out->v = v[6];
552                 }
553         }
554 }
555
556 // LordHavoc: transparent brush models
557 extern int r_dlightframecount;
558 extern float modelalpha;
559
560 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
561 {
562         int i, alpha, size3;
563         float *v, *wv, scale;
564         glpoly_t *p;
565         byte *lm;
566         alpha = (int) (modelalpha * 255.0f);
567         size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
568         wv = wvert;
569         for (p = s->polys;p;p = p->next)
570         {
571                 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
572                 {
573                         if (transform)
574                                 softwaretransform(v, wv);
575                         else
576                                 VectorCopy(v, wv);
577                         wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
578                         if (s->styles[0] != 255)
579                         {
580                                 lm = (byte *)((long) s->samples + (int) v[7]);
581                                 scale = d_lightstylevalue[s->styles[0]] * (1.0f / 128.0f);wv[3] += lm[size3*0+0] * scale;wv[4] += lm[size3*0+1] * scale;wv[5] += lm[size3*0+2] * scale;
582                                 if (s->styles[1] != 255)
583                                 {
584                                         scale = d_lightstylevalue[s->styles[1]] * (1.0f / 128.0f);wv[3] += lm[size3*1+0] * scale;wv[4] += lm[size3*1+1] * scale;wv[5] += lm[size3*1+2] * scale;
585                                         if (s->styles[2] != 255)
586                                         {
587                                                 scale = d_lightstylevalue[s->styles[2]] * (1.0f / 128.0f);wv[3] += lm[size3*2+0] * scale;wv[4] += lm[size3*2+1] * scale;wv[5] += lm[size3*2+2] * scale;
588                                                 if (s->styles[3] != 255)
589                                                 {
590                                                         scale = d_lightstylevalue[s->styles[3]] * (1.0f / 128.0f);wv[3] += lm[size3*3+0] * scale;wv[4] += lm[size3*3+1] * scale;wv[5] += lm[size3*3+2] * scale;
591                                                 }
592                                         }
593                                 }
594                         }
595                         wv += 6;
596                 }
597         }
598         if (s->dlightframe == r_dlightframecount && r_dynamic.value)
599                 RSurf_Light(s->dlightbits, s->polys);
600         wv = wvert;
601         if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
602         {
603                 for (p = s->polys;p;p = p->next)
604                 {
605                         v = p->verts[0];
606                         transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
607                         for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
608                                 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3] * currententity->colormod[0], wv[4] * currententity->colormod[1], wv[5] * currententity->colormod[2], alpha);
609                         transpolyend();
610                 }
611         }
612         else
613         {
614                 for (p = s->polys;p;p = p->next)
615                 {
616                         v = p->verts[0];
617                         transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
618                         for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
619                                 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
620                         transpolyend();
621                 }
622         }
623 }
624
625 /*
626 ================
627 DrawTextureChains
628 ================
629 */
630 extern qboolean hlbsp;
631 extern char skyname[];
632 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
633 {
634         texture_t *t;
635         if (s->flags & SURF_DRAWSKY)
636         {
637                 skyisvisible = true;
638                 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
639                         RSurf_DrawSky(s, false);
640                 return;
641         }
642         t = R_TextureAnimation (s->texinfo->texture);
643         if (s->flags & SURF_DRAWTURB)
644         {
645                 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
646                 return;
647         }
648         if (vertexlit)
649                 RSurf_DrawWallVertex(s, t, false, false);
650         else
651                 RSurf_DrawWall(s, t, false);
652 }
653
654 void DrawTextureChains (void)
655 {
656         int                     n;
657         msurface_t      *s;
658         texture_t       *t;
659
660         for (n = 0;n < cl.worldmodel->numtextures;n++)
661         {
662                 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
663                         continue;
664                 cl.worldmodel->textures[n]->texturechain = NULL;
665 //              for (;s;s = s->texturechain)
666 //                      R_DrawSurf(s, false, gl_vertex.value);
667                 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
668                 // sky
669                 if (s->flags & SURF_DRAWSKY)
670                 {
671                         skyisvisible = true;
672                         if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
673                                 for (;s;s = s->texturechain)
674                                         RSurf_DrawSky(s, false);
675                         continue;
676                 }
677                 t = R_TextureAnimation (cl.worldmodel->textures[n]);
678                 // subdivided water surface warp
679                 if (s->flags & SURF_DRAWTURB)
680                 {
681                         int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
682                         for (;s;s = s->texturechain)
683                                 RSurf_DrawWater(s, t, false, alpha);
684                         continue;
685                 }
686                 if (gl_vertex.value)
687                         for (;s;s = s->texturechain)
688                                 RSurf_DrawWallVertex(s, t, false, false);
689                 else
690                         for (;s;s = s->texturechain)
691                                 RSurf_DrawWall(s, t, false);
692         }
693 }
694
695 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
696
697 /*
698 =================
699 R_DrawBrushModel
700 =================
701 */
702 void R_DrawBrushModel (entity_t *e)
703 {
704         int                     i;
705         vec3_t          mins, maxs;
706         msurface_t      *s;
707         model_t         *clmodel;
708         int     rotated, vertexlit = false;
709         texture_t       *t;
710         vec3_t          org;
711
712         currententity = e;
713
714         clmodel = e->model;
715
716         if (e->angles[0] || e->angles[1] || e->angles[2])
717         {
718                 rotated = true;
719                 for (i=0 ; i<3 ; i++)
720                 {
721                         mins[i] = e->origin[i] - clmodel->radius;
722                         maxs[i] = e->origin[i] + clmodel->radius;
723                 }
724         }
725         else
726         {
727                 rotated = false;
728                 VectorAdd (e->origin, clmodel->mins, mins);
729                 VectorAdd (e->origin, clmodel->maxs, maxs);
730         }
731
732         if (R_CullBox (mins, maxs))
733                 return;
734
735         VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
736         if (rotated)
737         {
738                 vec3_t  temp;
739                 vec3_t  forward, right, up;
740
741                 VectorCopy (modelorg, temp);
742                 AngleVectors (e->angles, forward, right, up);
743                 modelorg[0] = DotProduct (temp, forward);
744                 modelorg[1] = -DotProduct (temp, right);
745                 modelorg[2] = DotProduct (temp, up);
746         }
747
748         s = &clmodel->surfaces[clmodel->firstmodelsurface];
749
750 // calculate dynamic lighting for bmodel if it's not an
751 // instanced model
752         for (i = 0;i < MAX_DLIGHTS;i++)
753         {
754                 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
755                         continue;
756
757                 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
758                 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
759         }
760         vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
761
762 e->angles[0] = -e->angles[0];   // stupid quake bug
763         softwaretransformforentity (e);
764 e->angles[0] = -e->angles[0];   // stupid quake bug
765
766         // draw texture
767         for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
768         {
769                 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
770                 {
771 //                      R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
772                         if (s->flags & SURF_DRAWSKY)
773                         {
774                                 RSurf_DrawSky(s, true);
775                                 continue;
776                         }
777                         t = R_TextureAnimation (s->texinfo->texture);
778                         if (s->flags & SURF_DRAWTURB)
779                         {
780                                 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
781                                 continue;
782                         }
783                         if (vertexlit || s->texinfo->texture->transparent)
784                                 RSurf_DrawWallVertex(s, t, true, true);
785                         else
786                                 RSurf_DrawWall(s, t, true);
787                 }
788         }
789         UploadLightmaps();
790 }
791
792 /*
793 =============================================================
794
795         WORLD MODEL
796
797 =============================================================
798 */
799
800 void R_StoreEfrags (efrag_t **ppefrag);
801
802 struct nodestack_s
803 {
804         int side;
805         mnode_t *node;
806 } nodestack[8192];
807
808 /*
809 ================
810 R_WorldNode
811 ================
812 */
813 void R_WorldNode ()
814 {
815         int side, texsort, vertex;
816         struct nodestack_s *nstack;
817         mnode_t *node;
818         mleaf_t *pleaf;
819         msurface_t *surf, *endsurf, **mark, **endmark;
820         nstack = nodestack;
821         texsort = gl_texsort.value;
822         vertex = gl_vertex.value;
823
824         if (!(node = cl.worldmodel->nodes))
825                 return;
826
827         while(1)
828         {
829         // if a leaf node, draw stuff
830                 if (node->contents < 0)
831                 {
832                         if (node->contents != CONTENTS_SOLID)
833                         {
834                                 pleaf = (mleaf_t *)node;
835
836                                 c_leafs++;
837                                 if (pleaf->nummarksurfaces)
838                                 {
839                                         mark = pleaf->firstmarksurface;
840                                         endmark = mark + pleaf->nummarksurfaces;
841                                         do
842                                         {
843                                                 (*mark)->visframe = r_framecount;
844                                                 mark++;
845                                         }
846                                         while (mark < endmark);
847                                 }
848
849                                 // deal with model fragments in this leaf
850                                 if (pleaf->efrags)
851                                         R_StoreEfrags (&pleaf->efrags);
852                         }
853
854                         if (nstack <= nodestack)
855                                 break;
856                         nstack--;
857                         node = nstack->node;
858                         side = nstack->side;
859                         goto loc0;
860                 }
861
862                 c_nodes++;
863
864                 // node is just a decision point, so go down the apropriate sides
865
866                 // find which side of the node we are on
867                 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
868
869                 // recurse down the children, front side first
870                 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
871                 {
872                         nstack->node = node;
873                         nstack->side = !side; // go down back side when we come back up
874                         nstack++;
875                         node = node->children[side];
876                         continue;
877                 }
878                 side = !side;
879 loc0:
880
881         // draw stuff
882                 if (node->numsurfaces)
883                 {
884                         surf = cl.worldmodel->surfaces + node->firstsurface;
885                         endsurf = surf + node->numsurfaces;
886
887                         if (texsort)
888                         {
889                                 if (side)
890                                 {
891                                         do
892                                         {
893                                                 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
894                                                 {
895                                                         surf->texturechain = surf->texinfo->texture->texturechain;
896                                                         surf->texinfo->texture->texturechain = surf;
897                                                 }
898                                                 surf++;
899                                         }
900                                         while (surf < endsurf);
901                                 }
902                                 else
903                                 {
904                                         do
905                                         {
906                                                 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
907                                                 {
908                                                         surf->texturechain = surf->texinfo->texture->texturechain;
909                                                         surf->texinfo->texture->texturechain = surf;
910                                                 }
911                                                 surf++;
912                                         }
913                                         while (surf < endsurf);
914                                 }
915                         }
916                         else
917                         {
918                                 if (side)
919                                 {
920                                         do
921                                         {
922                                                 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
923                                                         R_DrawSurf(surf, false, vertex);
924                                                 surf++;
925                                         }
926                                         while (surf < endsurf);
927                                 }
928                                 else
929                                 {
930                                         do
931                                         {
932                                                 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
933                                                         R_DrawSurf(surf, false, vertex);
934                                                 surf++;
935                                         }
936                                         while (surf < endsurf);
937                                 }
938                         }
939                 }
940
941         // recurse down the back side
942                 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
943                 {
944                         node = node->children[side];
945                         continue;
946                 }
947
948                 if (nstack <= nodestack)
949                         break;
950                 nstack--;
951                 node = nstack->node;
952                 side = nstack->side;
953                 goto loc0;
954         }
955 }
956
957
958 /*
959 =============
960 R_DrawWorld
961 =============
962 */
963 void R_DrawWorld (void)
964 {
965         entity_t        ent;
966
967         memset (&ent, 0, sizeof(ent));
968         ent.model = cl.worldmodel;
969         ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
970         modelalpha = ent.alpha = 1;
971         ent.scale = 1;
972
973         VectorCopy (r_refdef.vieworg, modelorg);
974
975         currententity = &ent;
976
977         softwaretransformidentity(); // LordHavoc: clear transform
978
979         if (cl.worldmodel)
980                 R_WorldNode ();
981
982         R_PushDlights (); // now mark the lit surfaces
983
984         DrawTextureChains ();
985 }
986
987
988 /*
989 ===============
990 R_MarkLeaves
991 ===============
992 */
993 void R_MarkLeaves (void)
994 {
995         byte    *vis;
996         mnode_t *node;
997         int             i;
998
999         if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1000                 return;
1001         
1002         r_visframecount++;
1003         r_oldviewleaf = r_viewleaf;
1004
1005         if (r_novis.value)
1006         {
1007                 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1008                 {
1009                         node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1010                         do
1011                         {
1012                                 if (node->visframe == r_visframecount)
1013                                         break;
1014                                 node->visframe = r_visframecount;
1015                                 node = node->parent;
1016                         } while (node);
1017                 }
1018         }
1019         else
1020         {
1021                 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1022                 
1023                 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1024                 {
1025                         if (vis[i>>3] & (1<<(i&7)))
1026                         {
1027                                 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1028                                 do
1029                                 {
1030                                         if (node->visframe == r_visframecount)
1031                                                 break;
1032                                         node->visframe = r_visframecount;
1033                                         node = node->parent;
1034                                 } while (node);
1035                         }
1036                 }
1037         }
1038 }
1039
1040
1041
1042 /*
1043 =============================================================================
1044
1045   LIGHTMAP ALLOCATION
1046
1047 =============================================================================
1048 */
1049
1050 // returns a texture number and the position inside it
1051 int AllocBlock (int w, int h, short *x, short *y)
1052 {
1053         int             i, j;
1054         int             best, best2;
1055         int             texnum;
1056
1057         for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1058         {
1059                 best = BLOCK_HEIGHT;
1060
1061                 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1062                 {
1063                         best2 = 0;
1064
1065                         for (j=0 ; j<w ; j++)
1066                         {
1067                                 if (allocated[texnum][i+j] >= best)
1068                                         break;
1069                                 if (allocated[texnum][i+j] > best2)
1070                                         best2 = allocated[texnum][i+j];
1071                         }
1072                         if (j == w)
1073                         {       // this is a valid spot
1074                                 *x = i;
1075                                 *y = best = best2;
1076                         }
1077                 }
1078
1079                 if (best + h > BLOCK_HEIGHT)
1080                         continue;
1081
1082                 if (nosubimagefragments || nosubimage)
1083                 {
1084                         if (!lightmaps[texnum])
1085                         {
1086                                 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1087                                 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1088                         }
1089                 }
1090                 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1091                 else if (!allocated[texnum][0])
1092                 {
1093                         byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1094                         memset(blank, 0, sizeof(blank));
1095                         if(r_upload.value)
1096                         {
1097                                 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1098                                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1099                                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1100                                 if (lightmaprgba)
1101                                         glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1102                                 else
1103                                         glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1104                         }
1105                 }
1106
1107                 for (i=0 ; i<w ; i++)
1108                         allocated[texnum][*x + i] = best + h;
1109
1110                 return texnum;
1111         }
1112
1113         Sys_Error ("AllocBlock: full");
1114         return 0;
1115 }
1116
1117
1118 mvertex_t       *r_pcurrentvertbase;
1119 model_t         *currentmodel;
1120
1121 int     nColinElim;
1122
1123 /*
1124 ================
1125 BuildSurfaceDisplayList
1126 ================
1127 */
1128 void BuildSurfaceDisplayList (msurface_t *fa)
1129 {
1130         int                     i, j, lindex, lnumverts;
1131         medge_t         *pedges, *r_pedge;
1132         int                     vertpage;
1133         float           *vec;
1134         float           s, t;
1135         glpoly_t        *poly;
1136
1137 // reconstruct the polygon
1138         pedges = currentmodel->edges;
1139         lnumverts = fa->numedges;
1140         vertpage = 0;
1141
1142         //
1143         // draw texture
1144         //
1145         poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1146         poly->next = fa->polys;
1147         poly->flags = fa->flags;
1148         fa->polys = poly;
1149         poly->numverts = lnumverts;
1150
1151         for (i=0 ; i<lnumverts ; i++)
1152         {
1153                 lindex = currentmodel->surfedges[fa->firstedge + i];
1154
1155                 if (lindex > 0)
1156                 {
1157                         r_pedge = &pedges[lindex];
1158                         vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1159                 }
1160                 else
1161                 {
1162                         r_pedge = &pedges[-lindex];
1163                         vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1164                 }
1165                 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1166                 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1167
1168                 VectorCopy (vec, poly->verts[i]);
1169                 poly->verts[i][3] = s / fa->texinfo->texture->width;
1170                 poly->verts[i][4] = t / fa->texinfo->texture->height;
1171
1172                 //
1173                 // lightmap texture coordinates
1174                 //
1175                 s -= fa->texturemins[0];
1176                 t -= fa->texturemins[1];
1177                 s += 8;
1178                 t += 8;
1179                 // LordHavoc: calc lightmap data offset
1180                 j = (bound(0l, (int)t>>4, fa->extents[1]>>4) * ((fa->extents[0]>>4)+1) + bound(0l, (int)s>>4, fa->extents[0]>>4)) * 3;
1181                 poly->verts[i][7] = j;
1182                 s += fa->light_s*16;
1183                 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1184
1185                 t += fa->light_t*16;
1186                 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1187
1188                 poly->verts[i][5] = s;
1189                 poly->verts[i][6] = t;
1190         }
1191
1192         //
1193         // remove co-linear points - Ed
1194         //
1195         /*
1196         if (!gl_keeptjunctions.value)
1197         {
1198                 for (i = 0 ; i < lnumverts ; ++i)
1199                 {
1200                         vec3_t v1, v2;
1201                         float *prev, *this, *next;
1202
1203                         prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1204                         this = poly->verts[i];
1205                         next = poly->verts[(i + 1) % lnumverts];
1206
1207                         VectorSubtract( this, prev, v1 );
1208                         VectorNormalize( v1 );
1209                         VectorSubtract( next, prev, v2 );
1210                         VectorNormalize( v2 );
1211
1212                         // skip co-linear points
1213                         #define COLINEAR_EPSILON 0.001
1214                         if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1215                                 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) && 
1216                                 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1217                         {
1218                                 int j;
1219                                 for (j = i + 1; j < lnumverts; ++j)
1220                                 {
1221                                         int k;
1222                                         for (k = 0; k < VERTEXSIZE; ++k)
1223                                                 poly->verts[j - 1][k] = poly->verts[j][k];
1224                                 }
1225                                 --lnumverts;
1226                                 ++nColinElim;
1227                                 // retry next vertex next time, which is now current vertex
1228                                 --i;
1229                         }
1230                 }
1231         }
1232         */
1233         poly->numverts = lnumverts;
1234 }
1235
1236 /*
1237 ========================
1238 GL_CreateSurfaceLightmap
1239 ========================
1240 */
1241 void GL_CreateSurfaceLightmap (msurface_t *surf)
1242 {
1243         int             smax, tmax;
1244
1245         if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1246                 return;
1247
1248         smax = (surf->extents[0]>>4)+1;
1249         tmax = (surf->extents[1]>>4)+1;
1250
1251         surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1252         if (nosubimage || nosubimagefragments)
1253                 return;
1254         glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1255         smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1256         if (lightmaprgba)
1257         {
1258                 R_BuildLightMap (surf, templight, smax * 4);
1259                 if(r_upload.value)
1260                         glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1261         }
1262         else
1263         {
1264                 R_BuildLightMap (surf, templight, smax * 3);
1265                 if(r_upload.value)
1266                         glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1267         }
1268 }
1269
1270
1271 /*
1272 ==================
1273 GL_BuildLightmaps
1274
1275 Builds the lightmap texture
1276 with all the surfaces from all brush models
1277 ==================
1278 */
1279 void GL_BuildLightmaps (void)
1280 {
1281         int             i, j;
1282         model_t *m;
1283
1284         memset (allocated, 0, sizeof(allocated));
1285
1286         r_framecount = 1;               // no dlightcache
1287
1288         if (gl_nosubimagefragments.value)
1289                 nosubimagefragments = 1;
1290         else
1291                 nosubimagefragments = 0;
1292
1293         if (gl_nosubimage.value)
1294                 nosubimage = 1;
1295         else
1296                 nosubimage = 0;
1297
1298         if (gl_lightmaprgba.value)
1299         {
1300                 lightmaprgba = true;
1301                 lightmapbytes = 4;
1302         }
1303         else
1304         {
1305                 lightmaprgba = false;
1306                 lightmapbytes = 3;
1307         }
1308
1309         // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1310         //            it needs to be aligned on 4 pixel boundaries...
1311         //            so I implemented an adjustable lightmap alignment
1312         if (gl_lightmapalign.value < 1)
1313                 gl_lightmapalign.value = 1;
1314         if (gl_lightmapalign.value > 16)
1315                 gl_lightmapalign.value = 16;
1316         lightmapalign = 1;
1317         while (lightmapalign < gl_lightmapalign.value)
1318                 lightmapalign <<= 1;
1319         gl_lightmapalign.value = lightmapalign;
1320         lightmapalignmask = ~(lightmapalign - 1);
1321         if (nosubimagefragments || nosubimage)
1322         {
1323                 lightmapalign = 1;
1324                 lightmapalignmask = ~0;
1325         }
1326
1327         if (!lightmap_textures)
1328         {
1329                 lightmap_textures = texture_extension_number;
1330                 texture_extension_number += MAX_LIGHTMAPS;
1331         }
1332
1333         for (j=1 ; j<MAX_MODELS ; j++)
1334         {
1335                 m = cl.model_precache[j];
1336                 if (!m)
1337                         break;
1338                 if (m->name[0] == '*')
1339                         continue;
1340                 r_pcurrentvertbase = m->vertexes;
1341                 currentmodel = m;
1342                 for (i=0 ; i<m->numsurfaces ; i++)
1343                 {
1344                         if ( m->surfaces[i].flags & SURF_DRAWTURB )
1345                                 continue;
1346                         if ( m->surfaces[i].flags & SURF_DRAWSKY )
1347                                 continue;
1348                         GL_CreateSurfaceLightmap (m->surfaces + i);
1349                         BuildSurfaceDisplayList (m->surfaces + i);
1350                 }
1351         }
1352
1353         if (nosubimage || nosubimagefragments)
1354         {
1355                 if(r_upload.value)
1356                         if (gl_mtexable)
1357                                 qglSelectTexture(gl_mtex_enum+1);
1358                 for (i = 0;i < MAX_LIGHTMAPS;i++)
1359                 {
1360                         if (!allocated[i][0])
1361                                 break;
1362                         lightmapupdate[i][0] = BLOCK_HEIGHT;
1363                         lightmapupdate[i][1] = 0;
1364                         if(r_upload.value)
1365                         {
1366                                 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1367                                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1368                                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1369                                 if (lightmaprgba)
1370                                         glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1371                                 else
1372                                         glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1373                         }
1374                 }
1375                 if(r_upload.value)
1376                         if (gl_mtexable)
1377                                 qglSelectTexture(gl_mtex_enum+0);
1378         }
1379 }
1380