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