]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
misc. cleanup, bubble trails fixed, improved lightmap compatibility.
[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*3)
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", "1"};
47 cvar_t gl_nosubimagefragments = {"gl_nosubimagefragments", "0"};
48 cvar_t gl_nosubimage = {"gl_nosubimage", "0"};
49
50 qboolean lightmaprgba, nosubimagefragments, nosubimage;
51 int lightmapbytes;
52
53 qboolean skyisvisible;
54 extern qboolean gl_arrays;
55
56 void glrsurf_init()
57 {
58         int i;
59         for (i = 0;i < MAX_LIGHTMAPS;i++)
60                 lightmaps[i] = NULL;
61         Cvar_RegisterVariable(&gl_lightmapalign);
62         Cvar_RegisterVariable(&gl_lightmaprgba);
63         Cvar_RegisterVariable(&gl_nosubimagefragments);
64         Cvar_RegisterVariable(&gl_nosubimage);
65         // check if it's the glquake minigl driver
66         if (strnicmp(gl_vendor,"3Dfx",4)==0)
67         if (!gl_arrays)
68         {
69                 Cvar_SetValue("gl_nosubimagefragments", 1);
70 //              Cvar_SetValue("gl_nosubimage", 1);
71                 Cvar_SetValue("gl_lightmode", 0);
72         }
73 }
74
75 int dlightdivtable[8192];
76 int dlightdivtableinitialized = 0;
77
78 /*
79 ===============
80 R_AddDynamicLights
81 ===============
82 */
83 void R_AddDynamicLights (msurface_t *surf)
84 {
85         int                     sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, red, green, blue, j;
86         unsigned        *bl;
87         float           dist, f;
88         vec3_t          impact, local;
89         // use 64bit integer...  shame it's not very standardized...
90 #if _MSC_VER || __BORLANDC__
91         __int64         k; // MSVC
92 #else
93         long long       k; // GCC
94 #endif
95
96         if (!dlightdivtableinitialized)
97         {
98                 dlightdivtable[0] = 1048576 >> 7;
99                 for (s = 1;s < 8192;s++)
100                         dlightdivtable[s] = 1048576 / (s << 7);
101                 dlightdivtableinitialized = 1;
102         }
103
104         smax = (surf->extents[0]>>4)+1;
105         tmax = (surf->extents[1]>>4)+1;
106
107         for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
108         {
109                 if ( !(surf->dlightbits[lnum >> 5] & (1<<(lnum&31)) ) )
110                         continue;               // not lit by this light
111
112                 VectorSubtract(cl_dlights[lnum].origin, currententity->origin, local);
113                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
114                 for (i=0 ; i<3 ; i++)
115                         impact[i] = cl_dlights[lnum].origin[i] - surf->plane->normal[i]*dist;
116
117                 f = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
118                 i = f;
119
120                 // reduce calculations
121                 t = dist*dist;
122                 for (s = 0;s < smax;s++, i -= 16)
123                         sdtable[s] = i*i + t;
124
125                 f = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
126                 i = f;
127
128                 maxdist = (int) (cl_dlights[lnum].radius*cl_dlights[lnum].radius); // for comparisons to minimum acceptable light
129                 // clamp radius to avoid exceeding 8192 entry division table
130                 if (maxdist > 1048576)
131                         maxdist = 1048576;
132                 maxdist3 = maxdist - (int) (dist*dist);
133                 // convert to 8.8 blocklights format
134                 if (!cl_dlights[lnum].dark)
135                 {
136                         f = cl_dlights[lnum].color[0] * maxdist;red = f;
137                         f = cl_dlights[lnum].color[1] * maxdist;green = f;
138                         f = cl_dlights[lnum].color[2] * maxdist;blue = f;
139                 }
140                 else // negate for darklight
141                 {
142                         f = cl_dlights[lnum].color[0] * -maxdist;red = f;
143                         f = cl_dlights[lnum].color[1] * -maxdist;green = f;
144                         f = cl_dlights[lnum].color[2] * -maxdist;blue = f;
145                 }
146                 bl = blocklights;
147                 for (t = 0;t < tmax;t++,i -= 16)
148                 {
149                         td = i*i;
150                         if (td < maxdist3) // make sure some part of it is visible on this line
151                         {
152                                 maxdist2 = maxdist - td;
153                                 for (s = 0;s < smax;s++)
154                                 {
155                                         if (sdtable[s] < maxdist2)
156                                         {
157                                                 j = dlightdivtable[(sdtable[s]+td) >> 7];
158                                                 k = (red   * j) >> 8;bl[0] += k;
159                                                 k = (green * j) >> 8;bl[1] += k;
160                                                 k = (blue  * j) >> 8;bl[2] += k;
161                                         }
162                                         bl += 3;
163                                 }
164                         }
165                         else
166                                 bl+=smax*3; // skip line
167                 }
168         }
169 }
170
171 extern qboolean lighthalf;
172 /*
173 ===============
174 R_BuildLightMap
175
176 Combine and scale multiple lightmaps into the 8.8 format in blocklights
177 ===============
178 */
179 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
180 {
181         int                     smax, tmax;
182         int                     t;
183         int                     i, j, size;
184         byte            *lightmap;
185         int                     scale;
186         int                     maps;
187         int                     *bl;
188
189         surf->cached_dlight = (surf->dlightframe == r_framecount);
190         surf->cached_lighthalf = lighthalf;
191
192         smax = (surf->extents[0]>>4)+1;
193         tmax = (surf->extents[1]>>4)+1;
194         size = smax*tmax;
195         lightmap = surf->samples;
196
197 // set to full bright if no light data
198         if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
199         {
200                 bl = blocklights;
201                 for (i=0 ; i<size ; i++)
202                 {
203                         *bl++ = 255*256;
204                         *bl++ = 255*256;
205                         *bl++ = 255*256;
206                 }
207         }
208         else
209         {
210 // clear to no light
211                 bl = blocklights;
212                 for (i=0 ; i<size ; i++)
213                 {
214                         *bl++ = 0;
215                         *bl++ = 0;
216                         *bl++ = 0;
217                 }
218
219 // add all the lightmaps
220                 if (lightmap)
221                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
222                         {
223                                 scale = d_lightstylevalue[surf->styles[maps]];
224                                 surf->cached_light[maps] = scale;       // 8.8 fraction
225                                 bl = blocklights;
226                                 for (i=0 ; i<size ; i++)
227                                 {
228                                         *bl++ += *lightmap++ * scale;
229                                         *bl++ += *lightmap++ * scale;
230                                         *bl++ += *lightmap++ * scale;
231                                 }
232                         }
233
234 // add all the dynamic lights
235                 if (surf->dlightframe == r_framecount)
236                         R_AddDynamicLights (surf);
237         }
238         stride -= (smax*lightmapbytes);
239         bl = blocklights;
240         if (lighthalf)
241         {
242                 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
243                 // the image is brightened as a processing pass
244                 if (lightmaprgba)
245                 {
246                         for (i=0 ; i<tmax ; i++, dest += stride)
247                         {
248                                 for (j=0 ; j<smax ; j++)
249                                 {
250                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
251                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
252                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
253                                         *dest++ = 255;
254                                 }
255                         }
256                 }
257                 else
258                 {
259                         for (i=0 ; i<tmax ; i++, dest += stride)
260                         {
261                                 for (j=0 ; j<smax ; j++)
262                                 {
263                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
264                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
265                                         t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
266                                 }
267                         }
268                 }
269         }
270         else
271         {
272                 if (lightmaprgba)
273                 {
274                         for (i=0 ; i<tmax ; i++, dest += stride)
275                         {
276                                 for (j=0 ; j<smax ; j++)
277                                 {
278                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
279                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
280                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
281                                         *dest++ = 255;
282                                 }
283                         }
284                 }
285                 else
286                 {
287                         for (i=0 ; i<tmax ; i++, dest += stride)
288                         {
289                                 for (j=0 ; j<smax ; j++)
290                                 {
291                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
292                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
293                                         t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
294                                 }
295                         }
296                 }
297         }
298 }
299
300 byte templight[32*32*4];
301
302 void R_UpdateLightmap(msurface_t *s, int lnum)
303 {
304         int smax, tmax;
305         // upload the new lightmap texture fragment
306         glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
307         if (nosubimage || nosubimagefragments)
308         {
309                 if (lightmapupdate[lnum][0] > s->light_t)
310                         lightmapupdate[lnum][0] = s->light_t;
311                 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
312                         lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
313                 if (lightmaprgba)
314                         R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
315                 else
316                         R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
317         }
318         else
319         {
320                 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
321                 tmax = (s->extents[1]>>4)+1;
322                 if (lightmaprgba)
323                 {
324                         R_BuildLightMap (s, templight, smax * 4);
325                         glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
326                 }
327                 else
328                 {
329                         R_BuildLightMap (s, templight, smax * 3);
330                         glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
331                 }
332         }
333 }
334
335
336 /*
337 ===============
338 R_TextureAnimation
339
340 Returns the proper texture for a given time and base texture
341 ===============
342 */
343 texture_t *R_TextureAnimation (texture_t *base)
344 {
345         texture_t *original;
346         int             relative;
347         int             count;
348
349         if (currententity->frame)
350         {
351                 if (base->alternate_anims)
352                         base = base->alternate_anims;
353         }
354         
355         if (!base->anim_total)
356                 return base;
357
358         original = base;
359
360         relative = (int)(cl.time*10) % base->anim_total;
361
362         count = 0;      
363         while (base->anim_min > relative || base->anim_max <= relative)
364         {
365                 base = base->anim_next;
366                 if (!base)
367                 {
368                         Con_Printf("R_TextureAnimation: broken cycle");
369                         return original;
370                 }
371                 if (++count > 100)
372                 {
373                         Con_Printf("R_TextureAnimation: infinite cycle");
374                         return original;
375                 }
376         }
377
378         return base;
379 }
380
381
382 /*
383 =============================================================
384
385         BRUSH MODELS
386
387 =============================================================
388 */
389
390
391 extern  int             solidskytexture;
392 extern  int             alphaskytexture;
393 extern  float   speedscale;             // for top sky and bottom sky
394
395 qboolean mtexenabled = false;
396
397 extern char skyname[];
398
399 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
400 //extern cvar_t r_dynamicwater;
401 extern int r_dlightframecount;
402 float   turbsin[256] =
403 {
404         #include "gl_warp_sin.h"
405 };
406 #define TURBSCALE (256.0 / (2 * M_PI))
407
408
409 void UploadLightmaps()
410 {
411         int i;
412         if (nosubimage || nosubimagefragments)
413         {
414                 for (i = 0;i < MAX_LIGHTMAPS;i++)
415                 {
416                         if (lightmapupdate[i][0] < lightmapupdate[i][1])
417                         {
418                                 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
419                                 if (nosubimage)
420                                 {
421                                         if (lightmaprgba)
422                                                 glTexImage2D(GL_TEXTURE_2D, 0, 4, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
423                                         else
424                                                 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
425                                 }
426                                 else
427                                 {
428                                         if (lightmaprgba)
429                                                 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]));
430                                         else
431                                                 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]));
432                                 }
433                         }
434                         lightmapupdate[i][0] = BLOCK_HEIGHT;
435                         lightmapupdate[i][1] = 0;
436                 }
437         }
438 }
439
440 /*
441 ================
442 DrawTextureChains
443 ================
444 */
445 extern qboolean hlbsp;
446 extern void R_Sky();
447 extern char skyname[];
448 void DrawTextureChains (void)
449 {
450         int             i, j, maps;
451         msurface_t      *s;
452         texture_t       *t;
453         glpoly_t        *p;
454         float           *v;
455         float           os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
456
457         // first the sky
458         skypolyclear();
459         for (j = 0;j < cl.worldmodel->numtextures;j++)
460         {
461                 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
462                         continue;
463                 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
464                 // subdivided water surface warp
465                 if (s->flags & SURF_DRAWSKY)
466                 {
467                         cl.worldmodel->textures[j]->texturechain = NULL;
468                         t = R_TextureAnimation (cl.worldmodel->textures[j]);
469                         skyisvisible = true;
470                         if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
471                         {
472                                 for (;s;s = s->texturechain)
473                                 {
474                                         for (p=s->polys ; p ; p=p->next)
475                                         {
476                                                 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
477                                                 {
478                                                         skypoly[currentskypoly].firstvert = currentskyvert;
479                                                         skypoly[currentskypoly++].verts = p->numverts;
480                                                         for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
481                                                         {
482                                                                 skyvert[currentskyvert].v[0] = v[0];
483                                                                 skyvert[currentskyvert].v[1] = v[1];
484                                                                 skyvert[currentskyvert++].v[2] = v[2];
485                                                         }
486                                                 }
487                                         }
488                                 }
489                         }
490                 }
491         }
492         skypolyrender(); // fogged sky polys, affects depth
493
494         if (skyname[0] && skyisvisible && !fogenabled)
495                 R_Sky(); // does not affect depth, draws over the sky polys
496
497         // then walls
498         wallpolyclear();
499         for (j = 0;j < cl.worldmodel->numtextures;j++)
500         {
501                 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
502                         continue;
503                 if (!(s->flags & SURF_DRAWTURB))
504                 {
505                         cl.worldmodel->textures[j]->texturechain = NULL;
506                         t = R_TextureAnimation (cl.worldmodel->textures[j]);
507                         c_brush_polys++;
508                         for (;s;s = s->texturechain)
509                         {
510                                 if (currentwallpoly < MAX_WALLPOLYS && currentwallvert < MAX_WALLVERTS && (currentwallvert + s->polys->numverts) <= MAX_WALLVERTS)
511                                 {
512                                         // check for lightmap modification
513 //                                      if (r_dynamic.value)
514 //                                      {
515                                                 if (s->dlightframe == r_framecount || s->cached_dlight || lighthalf != s->cached_lighthalf) // dynamic this frame or previously, or lighthalf changed
516                                                         R_UpdateLightmap(s, s->lightmaptexturenum);
517                                                 else
518                                                         for (maps = 0 ; maps < MAXLIGHTMAPS && s->styles[maps] != 255 ; maps++)
519                                                                 if (d_lightstylevalue[s->styles[maps]] != s->cached_light[maps])
520                                                                 {
521                                                                         R_UpdateLightmap(s, s->lightmaptexturenum);
522                                                                         break;
523                                                                 }
524 //                                      }
525                                         wallpoly[currentwallpoly].texnum = (unsigned short) t->gl_texturenum;
526                                         wallpoly[currentwallpoly].lighttexnum = (unsigned short) lightmap_textures + s->lightmaptexturenum;
527                                         wallpoly[currentwallpoly].glowtexnum = (unsigned short) t->gl_glowtexturenum;
528                                         wallpoly[currentwallpoly].firstvert = currentwallvert;
529                                         wallpoly[currentwallpoly++].verts = s->polys->numverts;
530                                         for (i = 0,v = s->polys->verts[0];i<s->polys->numverts;i++, v += VERTEXSIZE)
531                                         {
532                                                 wallvert[currentwallvert].vert[0] = v[0];
533                                                 wallvert[currentwallvert].vert[1] = v[1];
534                                                 wallvert[currentwallvert].vert[2] = v[2];
535                                                 wallvert[currentwallvert].s = v[3];
536                                                 wallvert[currentwallvert].t = v[4];
537                                                 wallvert[currentwallvert].u = v[5];
538                                                 wallvert[currentwallvert++].v = v[6];
539                                         }
540                                 }
541                         }
542                 }
543         }
544         UploadLightmaps();
545         wallpolyrender();
546
547         // then water (water gets diverted to transpoly list)
548         for (j = 0;j < cl.worldmodel->numtextures;j++)
549         {
550                 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
551                         continue;
552                 cl.worldmodel->textures[j]->texturechain = NULL;
553                 t = R_TextureAnimation (cl.worldmodel->textures[j]);
554                 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
555                 // subdivided water surface warp
556                 if (s->flags & SURF_DRAWTURB)
557                 {
558                         int light, alpha, r, g, b;
559                         vec3_t nv, shadecolor;
560                         alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
561                         light = false;
562                         if (s->flags & SURF_DRAWFULLBRIGHT)
563                                 r = g = b = lighthalf ? 128 : 255;
564                         else if (s->dlightframe == r_dlightframecount/* && r_dynamicwater.value*/)
565                                 light = true;
566                         else
567                                 r = g = b = lighthalf ? 64 : 128;
568                         if (r_waterripple.value)
569                         {
570                                 if (lighthalf)
571                                 {
572                                         if (light)
573                                         {
574                                                 for (;s;s = s->texturechain)
575                                                 {
576                                                         for (p=s->polys ; p ; p=p->next)
577                                                         {
578                                                                 // FIXME: could be a transparent water texture
579                                                                 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
580                                                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
581                                                                 {
582                                                                         nv[0] = v[0];
583                                                                         nv[1] = v[1];
584                                                                         nv[2] = v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
585                                                                         shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
586                                                                         R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
587                                                                         transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) shadecolor[0] >> 1,(int) shadecolor[1] >> 1,(int) shadecolor[2] >> 1,alpha);
588                                                                 }
589                                                                 transpolyend();
590                                                         }
591                                                 }
592                                         }
593                                         else
594                                         {
595                                                 for (;s;s = s->texturechain)
596                                                 {
597                                                         for (p=s->polys ; p ; p=p->next)
598                                                         {
599                                                                 // FIXME: could be a transparent water texture
600                                                                 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
601                                                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
602                                                                 {
603                                                                         nv[0] = v[0];
604                                                                         nv[1] = v[1];
605                                                                         nv[2] = v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
606                                                                         transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
607                                                                 }
608                                                                 transpolyend();
609                                                         }
610                                                 }
611                                         }
612                                 }
613                                 else
614                                 {
615                                         if (light)
616                                         {
617                                                 for (;s;s = s->texturechain)
618                                                 {
619                                                         for (p=s->polys ; p ; p=p->next)
620                                                         {
621                                                                 // FIXME: could be a transparent water texture
622                                                                 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
623                                                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
624                                                                 {
625                                                                         nv[0] = v[0];
626                                                                         nv[1] = v[1];
627                                                                         nv[2] = v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
628                                                                         shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
629                                                                         R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
630                                                                         transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), shadecolor[0],shadecolor[1],shadecolor[2],alpha);
631                                                                 }
632                                                                 transpolyend();
633                                                         }
634                                                 }
635                                         }
636                                         else
637                                         {
638                                                 for (;s;s = s->texturechain)
639                                                 {
640                                                         for (p=s->polys ; p ; p=p->next)
641                                                         {
642                                                                 // FIXME: could be a transparent water texture
643                                                                 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
644                                                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
645                                                                 {
646                                                                         nv[0] = v[0];
647                                                                         nv[1] = v[1];
648                                                                         nv[2] = v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
649                                                                         transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
650                                                                 }
651                                                                 transpolyend();
652                                                         }
653                                                 }
654                                         }
655                                 }
656                         }
657                         else
658                         {
659                                 if (lighthalf)
660                                 {
661                                         if (light)
662                                         {
663                                                 for (;s;s = s->texturechain)
664                                                 {
665                                                         for (p=s->polys ; p ; p=p->next)
666                                                         {
667                                                                 // FIXME: could be a transparent water texture
668                                                                 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
669                                                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
670                                                                 {
671                                                                         shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
672                                                                         R_DynamicLightPoint(shadecolor, v, s->dlightbits);
673                                                                         transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) shadecolor[0] >> 1,(int) shadecolor[1] >> 1,(int) shadecolor[2] >> 1,alpha);
674                                                                 }
675                                                                 transpolyend();
676                                                         }
677                                                 }
678                                         }
679                                         else
680                                         {
681                                                 for (;s;s = s->texturechain)
682                                                 {
683                                                         for (p=s->polys ; p ; p=p->next)
684                                                         {
685                                                                 // FIXME: could be a transparent water texture
686                                                                 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
687                                                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
688                                                                 {
689                                                                         transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
690                                                                 }
691                                                                 transpolyend();
692                                                         }
693                                                 }
694                                         }
695                                 }
696                                 else
697                                 {
698                                         if (light)
699                                         {
700                                                 for (;s;s = s->texturechain)
701                                                 {
702                                                         for (p=s->polys ; p ; p=p->next)
703                                                         {
704                                                                 // FIXME: could be a transparent water texture
705                                                                 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
706                                                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
707                                                                 {
708                                                                         shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
709                                                                         R_DynamicLightPoint(shadecolor, v, s->dlightbits);
710                                                                         transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), shadecolor[0],shadecolor[1],shadecolor[2],alpha);
711                                                                 }
712                                                                 transpolyend();
713                                                         }
714                                                 }
715                                         }
716                                         else
717                                         {
718                                                 for (;s;s = s->texturechain)
719                                                 {
720                                                         for (p=s->polys ; p ; p=p->next)
721                                                         {
722                                                                 // FIXME: could be a transparent water texture
723                                                                 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
724                                                                 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
725                                                                 {
726                                                                         transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
727                                                                 }
728                                                                 transpolyend();
729                                                         }
730                                                 }
731                                         }
732                                 }
733                         }
734                 }
735         }
736 }
737
738 // LordHavoc: transparent brush models
739 extern int r_dlightframecount;
740 extern float modelalpha;
741 extern vec3_t shadecolor;
742 //qboolean R_CullBox (vec3_t mins, vec3_t maxs);
743 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
744 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org);
745 void EmitWaterPolys (msurface_t *fa);
746 void R_MarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, mnode_t *node);
747
748 /*
749 =================
750 R_DrawBrushModel
751 =================
752 */
753 void R_DrawBrushModel (entity_t *e)
754 {
755         int                     i, j, k, smax, tmax, size3, maps;
756         vec3_t          mins, maxs, nv;
757         msurface_t      *s;
758         mplane_t        *pplane;
759         model_t         *clmodel;
760         qboolean        rotated, vertexlit = false;
761         float           dot, *v, scale;
762         texture_t       *t;
763         byte            *lm;
764         float           os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
765
766         currententity = e;
767
768         clmodel = e->model;
769
770         if (e->angles[0] || e->angles[1] || e->angles[2])
771         {
772                 rotated = true;
773                 for (i=0 ; i<3 ; i++)
774                 {
775                         mins[i] = e->origin[i] - clmodel->radius;
776                         maxs[i] = e->origin[i] + clmodel->radius;
777                 }
778         }
779         else
780         {
781                 rotated = false;
782                 VectorAdd (e->origin, clmodel->mins, mins);
783                 VectorAdd (e->origin, clmodel->maxs, maxs);
784         }
785
786         if (R_CullBox (mins, maxs))
787                 return;
788
789         VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
790         if (rotated)
791         {
792                 vec3_t  temp;
793                 vec3_t  forward, right, up;
794
795                 VectorCopy (modelorg, temp);
796                 AngleVectors (e->angles, forward, right, up);
797                 modelorg[0] = DotProduct (temp, forward);
798                 modelorg[1] = -DotProduct (temp, right);
799                 modelorg[2] = DotProduct (temp, up);
800         }
801
802         s = &clmodel->surfaces[clmodel->firstmodelsurface];
803
804 // calculate dynamic lighting for bmodel if it's not an
805 // instanced model
806         if (modelalpha == 1 && clmodel->firstmodelsurface != 0 && !(currententity->effects & EF_FULLBRIGHT) && currententity->colormod[0] == 1 && currententity->colormod[2] == 1 && currententity->colormod[2] == 1)
807         {
808 //              if (!gl_flashblend.value)
809 //              {
810                         vec3_t org;
811                         for (k=0 ; k<MAX_DLIGHTS ; k++)
812                         {
813                                 if ((cl_dlights[k].die < cl.time) || (!cl_dlights[k].radius))
814                                         continue;
815
816                                 VectorSubtract(cl_dlights[k].origin, currententity->origin, org);
817                                 R_MarkLights (org, &cl_dlights[k], 1<<(k&31), k >> 5, clmodel->nodes + clmodel->hulls[0].firstclipnode);
818                         }
819 //              }
820         }
821         else
822                 vertexlit = true;
823
824 e->angles[0] = -e->angles[0];   // stupid quake bug
825         softwaretransformforentity (e);
826 e->angles[0] = -e->angles[0];   // stupid quake bug
827
828         // draw texture
829         for (j = 0;j < clmodel->nummodelsurfaces;j++, s++)
830         {
831         // find which side of the node we are on
832                 pplane = s->plane;
833
834                 dot = DotProduct (modelorg, pplane->normal) - pplane->dist;
835
836         // draw the polygon
837                 if (((s->flags & SURF_PLANEBACK) && (dot < -BACKFACE_EPSILON)) ||
838                         (!(s->flags & SURF_PLANEBACK) && (dot > BACKFACE_EPSILON)))
839                 {
840                         if (s->flags & SURF_DRAWSKY)
841                                 continue;
842                         if (s->flags & SURF_DRAWTURB)
843                         {
844                                 glpoly_t        *p;
845                                 int                     light, alpha, r, g, b;
846                                 vec3_t          shadecolor;
847
848                                 if (s->flags & SURF_DRAWNOALPHA)
849                                         alpha = modelalpha*255.0f;
850                                 else
851                                         alpha = r_wateralpha.value*modelalpha*255.0f;
852                                 light = false;
853                                 if (s->flags & SURF_DRAWFULLBRIGHT || currententity->effects & EF_FULLBRIGHT)
854                                 {
855                                         if (lighthalf)
856                                         {
857                                                 r = 128.0f * currententity->colormod[0];
858                                                 g = 128.0f * currententity->colormod[1];
859                                                 b = 128.0f * currententity->colormod[2];
860                                         }
861                                         else
862                                         {
863                                                 r = 255.0f * currententity->colormod[0];
864                                                 g = 255.0f * currententity->colormod[1];
865                                                 b = 255.0f * currententity->colormod[2];
866                                         }
867                                 }
868                                 else if (s->dlightframe == r_dlightframecount/* && r_dynamicwater.value*/)
869                                         light = true;
870                                 else
871                                 {
872                                         if (lighthalf)
873                                         {
874                                                 r = 64.0f * currententity->colormod[0];
875                                                 g = 64.0f * currententity->colormod[1];
876                                                 b = 64.0f * currententity->colormod[2];
877                                         }
878                                         else
879                                         {
880                                                 r = 128.0f * currententity->colormod[0];
881                                                 g = 128.0f * currententity->colormod[1];
882                                                 b = 128.0f * currententity->colormod[2];
883                                         }
884                                 }
885                                 for (p=s->polys ; p ; p=p->next)
886                                 {
887                                         // FIXME: could be a transparent water texture
888                                         transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
889                                         for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
890                                         {
891                                                 softwaretransform(v, nv);
892                                                 if (r_waterripple.value)
893                                                         nv[2] += r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
894                                                 if (light)
895                                                 {
896                                                         shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
897                                                         R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
898                                                         if (lighthalf)
899                                                         {
900                                                                 r = (int) ((float) (shadecolor[0] * currententity->colormod[0])) >> 1;
901                                                                 g = (int) ((float) (shadecolor[1] * currententity->colormod[1])) >> 1;
902                                                                 b = (int) ((float) (shadecolor[2] * currententity->colormod[2])) >> 1;
903                                                         }
904                                                         else
905                                                         {
906                                                                 r = (int) ((float) (shadecolor[0] * currententity->colormod[0]));
907                                                                 g = (int) ((float) (shadecolor[1] * currententity->colormod[1]));
908                                                                 b = (int) ((float) (shadecolor[2] * currententity->colormod[2]));
909                                                         }
910                                                 }
911                                                 transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
912                                         }
913                                         transpolyend();
914                                 }
915                                 continue;
916                         }
917                         c_brush_polys++;
918                         t = R_TextureAnimation (s->texinfo->texture);
919                         v = s->polys->verts[0];
920                         if (vertexlit || s->texinfo->texture->transparent)
921                         {
922                                 // FIXME: could be a transparent water texture
923                                 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
924                                 if ((currententity->effects & EF_FULLBRIGHT) || !s->samples)
925                                 {
926                                         for (i = 0;i < s->polys->numverts;i++, v += VERTEXSIZE)
927                                         {
928                                                 softwaretransform(v, nv);
929                                                 transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], 255,255,255,modelalpha*255.0f);
930                                         }
931                                 }
932                                 else
933                                 {
934                                         smax = (s->extents[0]>>4)+1;
935                                         tmax = (s->extents[1]>>4)+1;
936                                         size3 = smax*tmax*3; // *3 for colored lighting
937                                         for (i = 0;i < s->polys->numverts;i++, v += VERTEXSIZE)
938                                         {
939                                                 shadecolor[0] = shadecolor[1] = shadecolor[2] = 0;
940                                                 lm = (byte *)((long) s->samples + ((int) v[8] * smax + (int) v[7]) * 3); // LordHavoc: *3 for colored lighting
941                                                 for (maps = 0;maps < MAXLIGHTMAPS && s->styles[maps] != 255;maps++)
942                                                 {
943                                                         scale = d_lightstylevalue[s->styles[maps]] * (1.0 / 128.0);
944                                                         shadecolor[0] += lm[0] * scale;
945                                                         shadecolor[1] += lm[1] * scale;
946                                                         shadecolor[2] += lm[2] * scale;
947                                                         lm += size3; // LordHavoc: *3 for colored lighting
948                                                 }
949                                                 softwaretransform(v, nv);
950                                                 R_DynamicLightPointNoMask(shadecolor, nv); // LordHavoc: dynamic lighting
951                                                 if (lighthalf)
952                                                 {
953                                                         transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], (int) shadecolor[0] >> 1, (int) shadecolor[1] >> 1, (int) shadecolor[2] >> 1, modelalpha*255.0f);
954                                                 }
955                                                 else
956                                                 {
957                                                         transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], shadecolor[0], shadecolor[1], shadecolor[2], modelalpha*255.0f);
958                                                 }
959                                         }
960                                 }
961                                 transpolyend();
962                         }
963                         else
964                         {
965                                 // check for lightmap modification
966 //                              if (r_dynamic.value)
967 //                              {
968                                         if (s->dlightframe == r_framecount || s->cached_dlight || lighthalf != s->cached_lighthalf) // dynamic this frame or previously, or lighthalf changed
969                                                 R_UpdateLightmap(s, s->lightmaptexturenum);
970                                         else
971                                                 for (maps = 0 ; maps < MAXLIGHTMAPS && s->styles[maps] != 255 ; maps++)
972                                                         if (d_lightstylevalue[s->styles[maps]] != s->cached_light[maps])
973                                                         {
974                                                                 R_UpdateLightmap(s, s->lightmaptexturenum);
975                                                                 break;
976                                                         }
977 //                              }
978                                 if (currentwallpoly < MAX_WALLPOLYS && (currentwallvert + s->polys->numverts) <= MAX_WALLVERTS)
979                                 {
980                                         wallpoly[currentwallpoly].texnum = (unsigned short) t->gl_texturenum;
981                                         wallpoly[currentwallpoly].lighttexnum = (unsigned short) lightmap_textures + s->lightmaptexturenum;
982                                         wallpoly[currentwallpoly].glowtexnum = (unsigned short) t->gl_glowtexturenum;
983                                         wallpoly[currentwallpoly].firstvert = currentwallvert;
984                                         wallpoly[currentwallpoly++].verts = s->polys->numverts;
985                                         for (i = 0;i<s->polys->numverts;i++, v += VERTEXSIZE)
986                                         {
987                                                 softwaretransform(v, wallvert[currentwallvert].vert);
988                                                 wallvert[currentwallvert].s = v[3];
989                                                 wallvert[currentwallvert].t = v[4];
990                                                 wallvert[currentwallvert].u = v[5];
991                                                 wallvert[currentwallvert++].v = v[6];
992                                         }
993                                 }
994                         }
995                 }
996         }
997         UploadLightmaps();
998 }
999
1000 /*
1001 =============================================================
1002
1003         WORLD MODEL
1004
1005 =============================================================
1006 */
1007
1008 void R_StoreEfrags (efrag_t **ppefrag);
1009
1010 /*
1011 ================
1012 R_RecursiveWorldNode
1013 ================
1014 */
1015 //extern qboolean R_CullBox (vec3_t mins, vec3_t maxs);
1016 /*
1017 void R_RecursiveWorldNode (mnode_t *node)
1018 {
1019         int                     c, side;
1020         double          dot;
1021
1022 loc0:
1023 // if a leaf node, draw stuff
1024         if (node->contents < 0)
1025         {
1026                 mleaf_t         *pleaf;
1027                 pleaf = (mleaf_t *)node;
1028
1029                 if (c = pleaf->nummarksurfaces)
1030                 {
1031                         msurface_t      **mark;
1032                         mark = pleaf->firstmarksurface;
1033                         do
1034                         {
1035                                 (*mark)->visframe = r_framecount;
1036                                 mark++;
1037                         } while (--c);
1038                 }
1039
1040         // deal with model fragments in this leaf
1041                 if (pleaf->efrags)
1042                         R_StoreEfrags (&pleaf->efrags);
1043
1044                 return;
1045         }
1046
1047 // node is just a decision point, so go down the apropriate sides
1048
1049 // find which side of the node we are on
1050         dot = (node->plane->type < 3 ? modelorg[node->plane->type] : DotProduct (modelorg, node->plane->normal)) - node->plane->dist;
1051
1052 // recurse down the children, front side first
1053         side = dot < 0;
1054         // LordHavoc: save a stack frame by avoiding a call
1055 //      if (node->children[side]->contents != CONTENTS_SOLID && node->children[side]->visframe == r_visframecount && !R_CullBox (node->children[side]->minmaxs, node->children[side]->minmaxs+3))
1056         // LordHavoc: inlined further to reduce conditions
1057         if (node->children[side]->contents != CONTENTS_SOLID
1058          && node->children[side]->visframe == r_visframecount
1059          && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1060          && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1061          && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1062          && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1063                 R_RecursiveWorldNode (node->children[side]);
1064
1065         // backside
1066         side = dot >= 0;
1067 // draw stuff
1068         if (c = node->numsurfaces)
1069         {
1070                 msurface_t      *surf;
1071                 surf = cl.worldmodel->surfaces + node->firstsurface;
1072
1073                 // LordHavoc: caused a crash due to texsort (it could render twice...)
1074                 // back side
1075                 //side = dot >= -BACKFACE_EPSILON;
1076                 if (dot < 0)
1077                 {
1078                         for (;c;c--, surf++)
1079                         {
1080                                 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1081                                 {
1082                                         surf->texturechain = surf->texinfo->texture->texturechain;
1083                                         surf->texinfo->texture->texturechain = surf;
1084                                 }
1085                         }
1086                 }
1087                 else
1088                 {
1089                         for (;c;c--, surf++)
1090                         {
1091                                 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1092                                 {
1093                                         surf->texturechain = surf->texinfo->texture->texturechain;
1094                                         surf->texinfo->texture->texturechain = surf;
1095                                 }
1096                         }
1097                 }
1098         }
1099
1100 // recurse down the back side
1101         // LordHavoc: save a stack frame by avoiding a call
1102 //      if (node->children[side]->contents != CONTENTS_SOLID && node->children[side]->visframe == r_visframecount && !R_CullBox (node->children[side]->minmaxs, node->children[side]->minmaxs+3))
1103         // LordHavoc: inlined further to reduce conditions
1104         if (node->children[side]->contents != CONTENTS_SOLID
1105          && node->children[side]->visframe == r_visframecount
1106          && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1107          && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1108          && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1109          && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1110         {
1111                 node = node->children[side];
1112                 goto loc0;
1113         }
1114 //              R_RecursiveWorldNode (node->children[side]);
1115 }
1116 */
1117
1118 extern int c_nodes;
1119 void R_WorldNode ()
1120 {
1121         int             c, side;
1122         double  dot;
1123         struct
1124         {
1125                 double dot;
1126                 mnode_t *node;
1127         } nodestack[1024];
1128         int             s = 0;
1129         mnode_t *node;
1130
1131         if (!(node = cl.worldmodel->nodes))
1132                 return;
1133
1134         while(1)
1135         {
1136         // if a leaf node, draw stuff
1137                 c_nodes++;
1138                 if (node->contents < 0)
1139                 {
1140                         if (node->contents != CONTENTS_SOLID)
1141                         {
1142                                 mleaf_t         *pleaf;
1143                                 pleaf = (mleaf_t *)node;
1144
1145                                 if (c = pleaf->nummarksurfaces)
1146                                 {
1147                                         msurface_t      **mark;
1148                                         mark = pleaf->firstmarksurface;
1149                                         do
1150                                         {
1151                                                 (*mark)->visframe = r_framecount;
1152                                                 mark++;
1153                                         } while (--c);
1154                                 }
1155
1156                                 // deal with model fragments in this leaf
1157                                 if (pleaf->efrags)
1158                                         R_StoreEfrags (&pleaf->efrags);
1159                         }
1160
1161                         if (!s)
1162                                 break;
1163                         node = nodestack[--s].node;
1164                         dot = nodestack[s].dot;
1165                         goto loc0;
1166                 }
1167
1168         // node is just a decision point, so go down the apropriate sides
1169
1170         // find which side of the node we are on
1171                 dot = (node->plane->type < 3 ? modelorg[node->plane->type] : DotProduct (modelorg, node->plane->normal)) - node->plane->dist;
1172
1173         // recurse down the children, front side first
1174                 side = dot < 0;
1175                 if (node->children[side]->visframe == r_visframecount
1176                  && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1177                  && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1178                  && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1179                  && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1180                 {
1181                         nodestack[s].node = node;
1182                         nodestack[s++].dot = dot;
1183                         node = node->children[side];
1184                         continue;
1185                 }
1186 loc0:
1187
1188                 // backside
1189                 side = dot >= 0;
1190         // draw stuff
1191                 if (c = node->numsurfaces)
1192                 {
1193                         msurface_t      *surf;
1194                         surf = cl.worldmodel->surfaces + node->firstsurface;
1195
1196                         if (side)
1197                         {
1198                                 for (;c;c--, surf++)
1199                                 {
1200                                         if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1201                                         {
1202                                                 surf->texturechain = surf->texinfo->texture->texturechain;
1203                                                 surf->texinfo->texture->texturechain = surf;
1204                                         }
1205                                 }
1206                         }
1207                         else
1208                         {
1209                                 for (;c;c--, surf++)
1210                                 {
1211                                         if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1212                                         {
1213                                                 surf->texturechain = surf->texinfo->texture->texturechain;
1214                                                 surf->texinfo->texture->texturechain = surf;
1215                                         }
1216                                 }
1217                         }
1218                 }
1219
1220         // recurse down the back side
1221                 if (node->children[side]->visframe == r_visframecount
1222                  && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1223                  && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1224                  && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1225                  && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1226                 {
1227                         node = node->children[side];
1228                         continue;
1229                 }
1230
1231                 if (!s)
1232                         break;
1233                 node = nodestack[--s].node;
1234                 dot = nodestack[s].dot;
1235                 goto loc0;
1236         }
1237 }
1238
1239
1240 /*
1241 =============
1242 R_DrawWorld
1243 =============
1244 */
1245 void R_DrawWorld (void)
1246 {
1247         entity_t        ent;
1248
1249         memset (&ent, 0, sizeof(ent));
1250         ent.model = cl.worldmodel;
1251         ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
1252         modelalpha = ent.alpha = 1;
1253         ent.scale = 1;
1254
1255         VectorCopy (r_refdef.vieworg, modelorg);
1256
1257         currententity = &ent;
1258
1259         softwaretransformidentity(); // LordHavoc: clear transform
1260         skyisvisible = false;
1261
1262         if (cl.worldmodel)
1263                 R_WorldNode ();
1264
1265         glClear (GL_DEPTH_BUFFER_BIT);
1266
1267         DrawTextureChains ();
1268
1269         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1270         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1271 }
1272
1273
1274 /*
1275 ===============
1276 R_MarkLeaves
1277 ===============
1278 */
1279 void R_MarkLeaves (void)
1280 {
1281         byte    *vis;
1282         mnode_t *node;
1283         int             i;
1284
1285         if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1286                 return;
1287         
1288         r_visframecount++;
1289         r_oldviewleaf = r_viewleaf;
1290
1291         if (r_novis.value)
1292         {
1293                 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1294                 {
1295                         node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1296                         do
1297                         {
1298                                 if (node->visframe == r_visframecount)
1299                                         break;
1300                                 node->visframe = r_visframecount;
1301                                 node = node->parent;
1302                         } while (node);
1303                 }
1304         }
1305         else
1306         {
1307                 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1308                 
1309                 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1310                 {
1311                         if (vis[i>>3] & (1<<(i&7)))
1312                         {
1313                                 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1314                                 do
1315                                 {
1316                                         if (node->visframe == r_visframecount)
1317                                                 break;
1318                                         node->visframe = r_visframecount;
1319                                         node = node->parent;
1320                                 } while (node);
1321                         }
1322                 }
1323         }
1324 }
1325
1326
1327
1328 /*
1329 =============================================================================
1330
1331   LIGHTMAP ALLOCATION
1332
1333 =============================================================================
1334 */
1335
1336 // returns a texture number and the position inside it
1337 int AllocBlock (int w, int h, int *x, int *y)
1338 {
1339         int             i, j;
1340         int             best, best2;
1341         int             texnum;
1342
1343         for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1344         {
1345                 best = BLOCK_HEIGHT;
1346
1347                 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1348                 {
1349                         best2 = 0;
1350
1351                         for (j=0 ; j<w ; j++)
1352                         {
1353                                 if (allocated[texnum][i+j] >= best)
1354                                         break;
1355                                 if (allocated[texnum][i+j] > best2)
1356                                         best2 = allocated[texnum][i+j];
1357                         }
1358                         if (j == w)
1359                         {       // this is a valid spot
1360                                 *x = i;
1361                                 *y = best = best2;
1362                         }
1363                 }
1364
1365                 if (best + h > BLOCK_HEIGHT)
1366                         continue;
1367
1368                 if (nosubimagefragments || nosubimage)
1369                 {
1370                         if (!lightmaps[texnum])
1371                                 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1372                 }
1373                 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1374                 else if (!allocated[texnum][0])
1375                 {
1376                         byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1377                         memset(blank, 0, sizeof(blank));
1378                         glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1379                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1380                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1381                         if (lightmaprgba)
1382                                 glTexImage2D (GL_TEXTURE_2D, 0, 4, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1383                         else
1384                                 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1385                 }
1386
1387                 for (i=0 ; i<w ; i++)
1388                         allocated[texnum][*x + i] = best + h;
1389
1390                 return texnum;
1391         }
1392
1393         Sys_Error ("AllocBlock: full");
1394 }
1395
1396
1397 mvertex_t       *r_pcurrentvertbase;
1398 model_t         *currentmodel;
1399
1400 int     nColinElim;
1401
1402 /*
1403 ================
1404 BuildSurfaceDisplayList
1405 ================
1406 */
1407 void BuildSurfaceDisplayList (msurface_t *fa)
1408 {
1409         int                     i, lindex, lnumverts;
1410         medge_t         *pedges, *r_pedge;
1411         int                     vertpage;
1412         float           *vec;
1413         float           s, t;
1414         glpoly_t        *poly;
1415
1416 // reconstruct the polygon
1417         pedges = currentmodel->edges;
1418         lnumverts = fa->numedges;
1419         vertpage = 0;
1420
1421         //
1422         // draw texture
1423         //
1424         poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1425         poly->next = fa->polys;
1426         poly->flags = fa->flags;
1427         fa->polys = poly;
1428         poly->numverts = lnumverts;
1429
1430         for (i=0 ; i<lnumverts ; i++)
1431         {
1432                 lindex = currentmodel->surfedges[fa->firstedge + i];
1433
1434                 if (lindex > 0)
1435                 {
1436                         r_pedge = &pedges[lindex];
1437                         vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1438                 }
1439                 else
1440                 {
1441                         r_pedge = &pedges[-lindex];
1442                         vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1443                 }
1444                 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1445                 s /= fa->texinfo->texture->width;
1446
1447                 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1448                 t /= fa->texinfo->texture->height;
1449
1450                 VectorCopy (vec, poly->verts[i]);
1451                 poly->verts[i][3] = s;
1452                 poly->verts[i][4] = t;
1453
1454                 //
1455                 // lightmap texture coordinates
1456                 //
1457                 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1458                 s -= fa->texturemins[0];
1459                 poly->verts[i][7] = bound(0l, ((int)s>>4), (fa->extents[0]>>4)); // LordHavoc: raw lightmap coordinates
1460                 s += fa->light_s*16;
1461                 s += 8;
1462                 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1463
1464                 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1465                 t -= fa->texturemins[1];
1466                 poly->verts[i][8] = bound(0l, ((int)t>>4), (fa->extents[1]>>4)); // LordHavoc: raw lightmap coordinates
1467                 t += fa->light_t*16;
1468                 t += 8;
1469                 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1470
1471                 poly->verts[i][5] = s;
1472                 poly->verts[i][6] = t;
1473         }
1474
1475         //
1476         // remove co-linear points - Ed
1477         //
1478         /*
1479         if (!gl_keeptjunctions.value)
1480         {
1481                 for (i = 0 ; i < lnumverts ; ++i)
1482                 {
1483                         vec3_t v1, v2;
1484                         float *prev, *this, *next;
1485
1486                         prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1487                         this = poly->verts[i];
1488                         next = poly->verts[(i + 1) % lnumverts];
1489
1490                         VectorSubtract( this, prev, v1 );
1491                         VectorNormalize( v1 );
1492                         VectorSubtract( next, prev, v2 );
1493                         VectorNormalize( v2 );
1494
1495                         // skip co-linear points
1496                         #define COLINEAR_EPSILON 0.001
1497                         if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1498                                 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) && 
1499                                 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1500                         {
1501                                 int j;
1502                                 for (j = i + 1; j < lnumverts; ++j)
1503                                 {
1504                                         int k;
1505                                         for (k = 0; k < VERTEXSIZE; ++k)
1506                                                 poly->verts[j - 1][k] = poly->verts[j][k];
1507                                 }
1508                                 --lnumverts;
1509                                 ++nColinElim;
1510                                 // retry next vertex next time, which is now current vertex
1511                                 --i;
1512                         }
1513                 }
1514         }
1515         */
1516         poly->numverts = lnumverts;
1517
1518 }
1519
1520 /*
1521 ========================
1522 GL_CreateSurfaceLightmap
1523 ========================
1524 */
1525 void GL_CreateSurfaceLightmap (msurface_t *surf)
1526 {
1527         int             smax, tmax;
1528
1529         if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1530                 return;
1531
1532         smax = (surf->extents[0]>>4)+1;
1533         tmax = (surf->extents[1]>>4)+1;
1534
1535         surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1536         if (nosubimage || nosubimagefragments)
1537                 return;
1538         glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1539         smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1540         if (lightmaprgba)
1541         {
1542                 R_BuildLightMap (surf, templight, smax * 4);
1543                 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1544         }
1545         else
1546         {
1547                 R_BuildLightMap (surf, templight, smax * 3);
1548                 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1549         }
1550 }
1551
1552
1553 /*
1554 ==================
1555 GL_BuildLightmaps
1556
1557 Builds the lightmap texture
1558 with all the surfaces from all brush models
1559 ==================
1560 */
1561 void GL_BuildLightmaps (void)
1562 {
1563         int             i, j;
1564         model_t *m;
1565
1566         memset (allocated, 0, sizeof(allocated));
1567
1568         r_framecount = 1;               // no dlightcache
1569
1570         if (gl_nosubimagefragments.value)
1571                 nosubimagefragments = 1;
1572         else
1573                 nosubimagefragments = 0;
1574
1575         if (gl_nosubimage.value)
1576                 nosubimage = 1;
1577         else
1578                 nosubimage = 0;
1579
1580         if (gl_lightmaprgba.value)
1581         {
1582                 lightmaprgba = true;
1583                 lightmapbytes = 4;
1584         }
1585         else
1586         {
1587                 lightmaprgba = false;
1588                 lightmapbytes = 3;
1589         }
1590
1591         // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1592         //            it needs to be aligned on 4 pixel boundaries...
1593         //            so I implemented an adjustable lightmap alignment
1594         if (gl_lightmapalign.value < 1)
1595                 gl_lightmapalign.value = 1;
1596         if (gl_lightmapalign.value > 16)
1597                 gl_lightmapalign.value = 16;
1598         lightmapalign = 1;
1599         while (lightmapalign < gl_lightmapalign.value)
1600                 lightmapalign <<= 1;
1601         gl_lightmapalign.value = lightmapalign;
1602         lightmapalignmask = ~(lightmapalign - 1);
1603         if (nosubimagefragments || nosubimage)
1604         {
1605                 lightmapalign = 1;
1606                 lightmapalignmask = ~0;
1607         }
1608
1609         if (!lightmap_textures)
1610         {
1611                 lightmap_textures = texture_extension_number;
1612                 texture_extension_number += MAX_LIGHTMAPS;
1613         }
1614
1615         for (j=1 ; j<MAX_MODELS ; j++)
1616         {
1617                 m = cl.model_precache[j];
1618                 if (!m)
1619                         break;
1620                 if (m->name[0] == '*')
1621                         continue;
1622                 r_pcurrentvertbase = m->vertexes;
1623                 currentmodel = m;
1624                 for (i=0 ; i<m->numsurfaces ; i++)
1625                 {
1626                         if ( m->surfaces[i].flags & SURF_DRAWTURB )
1627                                 continue;
1628                         if ( m->surfaces[i].flags & SURF_DRAWSKY )
1629                                 continue;
1630                         GL_CreateSurfaceLightmap (m->surfaces + i);
1631                         BuildSurfaceDisplayList (m->surfaces + i);
1632                 }
1633         }
1634
1635         if (nosubimage || nosubimagefragments)
1636         {
1637                 if (gl_mtexable)
1638                         qglSelectTexture(gl_mtex_enum+1);
1639                 for (i = 0;i < MAX_LIGHTMAPS;i++)
1640                 {
1641                         if (!allocated[i][0])
1642                                 break;
1643                         lightmapupdate[i][0] = BLOCK_HEIGHT;
1644                         lightmapupdate[i][1] = 0;
1645                         glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1646                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1647                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1648                         if (lightmaprgba)
1649                                 glTexImage2D(GL_TEXTURE_2D, 0, 4, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1650                         else
1651                                 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1652                 }
1653                 if (gl_mtexable)
1654                         qglSelectTexture(gl_mtex_enum+0);
1655         }
1656 }
1657