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