]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
speedups to R_WorldNode, and some shrinkage on the surface struct
[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 struct nodestack_s
825 {
826         int side;
827         mnode_t *node;
828 } nodestack[8192];
829
830 /*
831 ================
832 R_WorldNode
833 ================
834 */
835 void R_WorldNode ()
836 {
837         int side, texsort, vertex;
838         struct nodestack_s *nstack;
839         mnode_t *node;
840         mleaf_t *pleaf;
841         msurface_t *surf, *endsurf, **mark, **endmark;
842         nstack = nodestack;
843         texsort = gl_texsort.value;
844         vertex = gl_vertex.value;
845
846         if (!(node = cl.worldmodel->nodes))
847                 return;
848
849         while(1)
850         {
851         // if a leaf node, draw stuff
852                 if (node->contents < 0)
853                 {
854                         if (node->contents != CONTENTS_SOLID)
855                         {
856                                 pleaf = (mleaf_t *)node;
857
858                                 c_leafs++;
859                                 if (pleaf->nummarksurfaces)
860                                 {
861                                         mark = pleaf->firstmarksurface;
862                                         endmark = mark + pleaf->nummarksurfaces;
863                                         do
864                                         {
865                                                 (*mark)->visframe = r_framecount;
866                                                 mark++;
867                                         }
868                                         while (mark < endmark);
869                                 }
870
871                                 // deal with model fragments in this leaf
872                                 if (pleaf->efrags)
873                                         R_StoreEfrags (&pleaf->efrags);
874                         }
875
876                         if (nstack <= nodestack)
877                                 break;
878                         nstack--;
879                         node = nstack->node;
880                         side = nstack->side;
881                         goto loc0;
882                 }
883
884                 c_nodes++;
885
886                 // node is just a decision point, so go down the apropriate sides
887
888                 // find which side of the node we are on
889                 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
890
891                 // recurse down the children, front side first
892                 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
893                 {
894                         nstack->node = node;
895                         nstack->side = !side; // go down back side when we come back up
896                         nstack++;
897                         node = node->children[side];
898                         continue;
899                 }
900                 side = !side;
901 loc0:
902
903         // draw stuff
904                 if (node->numsurfaces)
905                 {
906                         surf = cl.worldmodel->surfaces + node->firstsurface;
907                         endsurf = surf + node->numsurfaces;
908
909                         if (texsort)
910                         {
911                                 if (side)
912                                 {
913                                         do
914                                         {
915                                                 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
916                                                 {
917                                                         surf->texturechain = surf->texinfo->texture->texturechain;
918                                                         surf->texinfo->texture->texturechain = surf;
919                                                 }
920                                                 surf++;
921                                         }
922                                         while (surf < endsurf);
923                                 }
924                                 else
925                                 {
926                                         do
927                                         {
928                                                 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
929                                                 {
930                                                         surf->texturechain = surf->texinfo->texture->texturechain;
931                                                         surf->texinfo->texture->texturechain = surf;
932                                                 }
933                                                 surf++;
934                                         }
935                                         while (surf < endsurf);
936                                 }
937                         }
938                         else
939                         {
940                                 if (side)
941                                 {
942                                         do
943                                         {
944                                                 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
945                                                         R_DrawSurf(surf, false, vertex);
946                                                 surf++;
947                                         }
948                                         while (surf < endsurf);
949                                 }
950                                 else
951                                 {
952                                         do
953                                         {
954                                                 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
955                                                         R_DrawSurf(surf, false, vertex);
956                                                 surf++;
957                                         }
958                                         while (surf < endsurf);
959                                 }
960                         }
961                 }
962
963         // recurse down the back side
964                 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
965                 {
966                         node = node->children[side];
967                         continue;
968                 }
969
970                 if (nstack <= nodestack)
971                         break;
972                 nstack--;
973                 node = nstack->node;
974                 side = nstack->side;
975                 goto loc0;
976         }
977 }
978
979
980 /*
981 =============
982 R_DrawWorld
983 =============
984 */
985 void R_DrawWorld (void)
986 {
987         entity_t        ent;
988
989         memset (&ent, 0, sizeof(ent));
990         ent.model = cl.worldmodel;
991         ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
992         modelalpha = ent.alpha = 1;
993         ent.scale = 1;
994
995         VectorCopy (r_refdef.vieworg, modelorg);
996
997         currententity = &ent;
998
999         softwaretransformidentity(); // LordHavoc: clear transform
1000
1001         if (cl.worldmodel)
1002                 R_WorldNode ();
1003
1004         glClear (GL_DEPTH_BUFFER_BIT);
1005
1006         R_PushDlights (); // now mark the lit surfaces
1007
1008         DrawTextureChains ();
1009
1010         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1011         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1012 }
1013
1014
1015 /*
1016 ===============
1017 R_MarkLeaves
1018 ===============
1019 */
1020 void R_MarkLeaves (void)
1021 {
1022         byte    *vis;
1023         mnode_t *node;
1024         int             i;
1025
1026         if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1027                 return;
1028         
1029         r_visframecount++;
1030         r_oldviewleaf = r_viewleaf;
1031
1032         if (r_novis.value)
1033         {
1034                 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1035                 {
1036                         node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1037                         do
1038                         {
1039                                 if (node->visframe == r_visframecount)
1040                                         break;
1041                                 node->visframe = r_visframecount;
1042                                 node = node->parent;
1043                         } while (node);
1044                 }
1045         }
1046         else
1047         {
1048                 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1049                 
1050                 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1051                 {
1052                         if (vis[i>>3] & (1<<(i&7)))
1053                         {
1054                                 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1055                                 do
1056                                 {
1057                                         if (node->visframe == r_visframecount)
1058                                                 break;
1059                                         node->visframe = r_visframecount;
1060                                         node = node->parent;
1061                                 } while (node);
1062                         }
1063                 }
1064         }
1065 }
1066
1067
1068
1069 /*
1070 =============================================================================
1071
1072   LIGHTMAP ALLOCATION
1073
1074 =============================================================================
1075 */
1076
1077 // returns a texture number and the position inside it
1078 int AllocBlock (int w, int h, short *x, short *y)
1079 {
1080         int             i, j;
1081         int             best, best2;
1082         int             texnum;
1083
1084         for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1085         {
1086                 best = BLOCK_HEIGHT;
1087
1088                 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1089                 {
1090                         best2 = 0;
1091
1092                         for (j=0 ; j<w ; j++)
1093                         {
1094                                 if (allocated[texnum][i+j] >= best)
1095                                         break;
1096                                 if (allocated[texnum][i+j] > best2)
1097                                         best2 = allocated[texnum][i+j];
1098                         }
1099                         if (j == w)
1100                         {       // this is a valid spot
1101                                 *x = i;
1102                                 *y = best = best2;
1103                         }
1104                 }
1105
1106                 if (best + h > BLOCK_HEIGHT)
1107                         continue;
1108
1109                 if (nosubimagefragments || nosubimage)
1110                 {
1111                         if (!lightmaps[texnum])
1112                                 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1113                 }
1114                 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1115                 else if (!allocated[texnum][0])
1116                 {
1117                         byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1118                         memset(blank, 0, sizeof(blank));
1119                         glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1120                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1121                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1122                         if (lightmaprgba)
1123                                 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1124                         else
1125                                 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1126                 }
1127
1128                 for (i=0 ; i<w ; i++)
1129                         allocated[texnum][*x + i] = best + h;
1130
1131                 return texnum;
1132         }
1133
1134         Sys_Error ("AllocBlock: full");
1135         return 0;
1136 }
1137
1138
1139 mvertex_t       *r_pcurrentvertbase;
1140 model_t         *currentmodel;
1141
1142 int     nColinElim;
1143
1144 /*
1145 ================
1146 BuildSurfaceDisplayList
1147 ================
1148 */
1149 void BuildSurfaceDisplayList (msurface_t *fa)
1150 {
1151         int                     i, j, lindex, lnumverts;
1152         medge_t         *pedges, *r_pedge;
1153         int                     vertpage;
1154         float           *vec;
1155         float           s, t;
1156         glpoly_t        *poly;
1157
1158 // reconstruct the polygon
1159         pedges = currentmodel->edges;
1160         lnumverts = fa->numedges;
1161         vertpage = 0;
1162
1163         //
1164         // draw texture
1165         //
1166         poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1167         poly->next = fa->polys;
1168         poly->flags = fa->flags;
1169         fa->polys = poly;
1170         poly->numverts = lnumverts;
1171
1172         for (i=0 ; i<lnumverts ; i++)
1173         {
1174                 lindex = currentmodel->surfedges[fa->firstedge + i];
1175
1176                 if (lindex > 0)
1177                 {
1178                         r_pedge = &pedges[lindex];
1179                         vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1180                 }
1181                 else
1182                 {
1183                         r_pedge = &pedges[-lindex];
1184                         vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1185                 }
1186                 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1187                 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1188
1189                 VectorCopy (vec, poly->verts[i]);
1190                 poly->verts[i][3] = s / fa->texinfo->texture->width;
1191                 poly->verts[i][4] = t / fa->texinfo->texture->height;
1192
1193                 //
1194                 // lightmap texture coordinates
1195                 //
1196                 s -= fa->texturemins[0];
1197                 t -= fa->texturemins[1];
1198                 s += 8;
1199                 t += 8;
1200                 // LordHavoc: calc lightmap data offset
1201                 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;
1202                 poly->verts[i][7] = j;
1203                 s += fa->light_s*16;
1204                 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1205
1206                 t += fa->light_t*16;
1207                 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1208
1209                 poly->verts[i][5] = s;
1210                 poly->verts[i][6] = t;
1211         }
1212
1213         //
1214         // remove co-linear points - Ed
1215         //
1216         /*
1217         if (!gl_keeptjunctions.value)
1218         {
1219                 for (i = 0 ; i < lnumverts ; ++i)
1220                 {
1221                         vec3_t v1, v2;
1222                         float *prev, *this, *next;
1223
1224                         prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1225                         this = poly->verts[i];
1226                         next = poly->verts[(i + 1) % lnumverts];
1227
1228                         VectorSubtract( this, prev, v1 );
1229                         VectorNormalize( v1 );
1230                         VectorSubtract( next, prev, v2 );
1231                         VectorNormalize( v2 );
1232
1233                         // skip co-linear points
1234                         #define COLINEAR_EPSILON 0.001
1235                         if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1236                                 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) && 
1237                                 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1238                         {
1239                                 int j;
1240                                 for (j = i + 1; j < lnumverts; ++j)
1241                                 {
1242                                         int k;
1243                                         for (k = 0; k < VERTEXSIZE; ++k)
1244                                                 poly->verts[j - 1][k] = poly->verts[j][k];
1245                                 }
1246                                 --lnumverts;
1247                                 ++nColinElim;
1248                                 // retry next vertex next time, which is now current vertex
1249                                 --i;
1250                         }
1251                 }
1252         }
1253         */
1254         poly->numverts = lnumverts;
1255 }
1256
1257 /*
1258 ========================
1259 GL_CreateSurfaceLightmap
1260 ========================
1261 */
1262 void GL_CreateSurfaceLightmap (msurface_t *surf)
1263 {
1264         int             smax, tmax;
1265
1266         if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1267                 return;
1268
1269         smax = (surf->extents[0]>>4)+1;
1270         tmax = (surf->extents[1]>>4)+1;
1271
1272         surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1273         if (nosubimage || nosubimagefragments)
1274                 return;
1275         glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1276         smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1277         if (lightmaprgba)
1278         {
1279                 R_BuildLightMap (surf, templight, smax * 4);
1280                 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1281         }
1282         else
1283         {
1284                 R_BuildLightMap (surf, templight, smax * 3);
1285                 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1286         }
1287 }
1288
1289
1290 /*
1291 ==================
1292 GL_BuildLightmaps
1293
1294 Builds the lightmap texture
1295 with all the surfaces from all brush models
1296 ==================
1297 */
1298 void GL_BuildLightmaps (void)
1299 {
1300         int             i, j;
1301         model_t *m;
1302
1303         memset (allocated, 0, sizeof(allocated));
1304
1305         r_framecount = 1;               // no dlightcache
1306
1307         if (gl_nosubimagefragments.value)
1308                 nosubimagefragments = 1;
1309         else
1310                 nosubimagefragments = 0;
1311
1312         if (gl_nosubimage.value)
1313                 nosubimage = 1;
1314         else
1315                 nosubimage = 0;
1316
1317         if (gl_lightmaprgba.value)
1318         {
1319                 lightmaprgba = true;
1320                 lightmapbytes = 4;
1321         }
1322         else
1323         {
1324                 lightmaprgba = false;
1325                 lightmapbytes = 3;
1326         }
1327
1328         // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1329         //            it needs to be aligned on 4 pixel boundaries...
1330         //            so I implemented an adjustable lightmap alignment
1331         if (gl_lightmapalign.value < 1)
1332                 gl_lightmapalign.value = 1;
1333         if (gl_lightmapalign.value > 16)
1334                 gl_lightmapalign.value = 16;
1335         lightmapalign = 1;
1336         while (lightmapalign < gl_lightmapalign.value)
1337                 lightmapalign <<= 1;
1338         gl_lightmapalign.value = lightmapalign;
1339         lightmapalignmask = ~(lightmapalign - 1);
1340         if (nosubimagefragments || nosubimage)
1341         {
1342                 lightmapalign = 1;
1343                 lightmapalignmask = ~0;
1344         }
1345
1346         if (!lightmap_textures)
1347         {
1348                 lightmap_textures = texture_extension_number;
1349                 texture_extension_number += MAX_LIGHTMAPS;
1350         }
1351
1352         for (j=1 ; j<MAX_MODELS ; j++)
1353         {
1354                 m = cl.model_precache[j];
1355                 if (!m)
1356                         break;
1357                 if (m->name[0] == '*')
1358                         continue;
1359                 r_pcurrentvertbase = m->vertexes;
1360                 currentmodel = m;
1361                 for (i=0 ; i<m->numsurfaces ; i++)
1362                 {
1363                         if ( m->surfaces[i].flags & SURF_DRAWTURB )
1364                                 continue;
1365                         if ( m->surfaces[i].flags & SURF_DRAWSKY )
1366                                 continue;
1367                         GL_CreateSurfaceLightmap (m->surfaces + i);
1368                         BuildSurfaceDisplayList (m->surfaces + i);
1369                 }
1370         }
1371
1372         if (nosubimage || nosubimagefragments)
1373         {
1374                 if (gl_mtexable)
1375                         qglSelectTexture(gl_mtex_enum+1);
1376                 for (i = 0;i < MAX_LIGHTMAPS;i++)
1377                 {
1378                         if (!allocated[i][0])
1379                                 break;
1380                         lightmapupdate[i][0] = BLOCK_HEIGHT;
1381                         lightmapupdate[i][1] = 0;
1382                         glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1383                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1384                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1385                         if (lightmaprgba)
1386                                 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1387                         else
1388                                 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1389                 }
1390                 if (gl_mtexable)
1391                         qglSelectTexture(gl_mtex_enum+0);
1392         }
1393 }
1394