added back r_speeds2, with masses of information (6 lines high), and made it print...
[xonotic/darkplaces.git] / gl_poly.c
1 #include "quakedef.h"
2
3 transvert_t *transvert;
4 transpoly_t *transpoly;
5 unsigned short *transpolyindex;
6 wallvert_t *wallvert;
7 wallpoly_t *wallpoly;
8 skyvert_t *skyvert;
9 skypoly_t *skypoly;
10
11 unsigned short currenttranspoly;
12 unsigned short currenttransvert;
13 unsigned short currentwallpoly;
14 unsigned short currentwallvert;
15 unsigned short currentskypoly;
16 unsigned short currentskyvert;
17
18 cvar_t gl_multitexture = {"gl_multitexture", "1"};
19 cvar_t gl_vertexarrays = {"gl_vertexarrays", "1"};
20
21 typedef struct translistitem_s
22 {
23         transpoly_t *poly;
24         struct translistitem_s *next;
25 }
26 translistitem;
27
28 translistitem translist[MAX_TRANSPOLYS];
29 translistitem *currenttranslist;
30
31 translistitem *translisthash[4096];
32
33 float transviewdist; // distance of view origin along the view normal
34
35 float transreciptable[256];
36
37 void gl_poly_start()
38 {
39         int i;
40         transvert = qmalloc(MAX_TRANSVERTS * sizeof(transvert_t));
41         transpoly = qmalloc(MAX_TRANSPOLYS * sizeof(transpoly_t));
42         transpolyindex = qmalloc(MAX_TRANSPOLYS * sizeof(unsigned short));
43         wallvert = qmalloc(MAX_WALLVERTS * sizeof(wallvert_t));
44         wallpoly = qmalloc(MAX_WALLPOLYS * sizeof(wallpoly_t));
45         skyvert = qmalloc(MAX_SKYVERTS * sizeof(skyvert_t));
46         skypoly = qmalloc(MAX_SKYPOLYS * sizeof(skypoly_t));
47         transreciptable[0] = 0.0f;
48         for (i = 1;i < 256;i++)
49                 transreciptable[i] = 1.0f / i;
50 }
51 void gl_poly_shutdown()
52 {
53         qfree(transvert);
54         qfree(transpoly);
55         qfree(transpolyindex);
56         qfree(wallvert);
57         qfree(wallpoly);
58         qfree(skyvert);
59         qfree(skypoly);
60 }
61
62 void GL_Poly_Init()
63 {
64         Cvar_RegisterVariable (&gl_multitexture);
65         Cvar_RegisterVariable (&gl_vertexarrays);
66         R_RegisterModule("GL_Poly", gl_poly_start, gl_poly_shutdown);
67 }
68
69 void transpolyclear()
70 {
71         currenttranspoly = currenttransvert = 0;
72         currenttranslist = translist;
73         memset(translisthash, 0, sizeof(translisthash));
74         transviewdist = DotProduct(r_refdef.vieworg, vpn);
75 }
76
77 // turned into a #define
78 /*
79 void transpolybegin(int texnum, int glowtexnum, int fogtexnum, int transpolytype)
80 {
81         if (currenttranspoly >= MAX_TRANSPOLYS || currenttransvert >= MAX_TRANSVERTS)
82                 return;
83         transpoly[currenttranspoly].texnum = (unsigned short) texnum;
84         transpoly[currenttranspoly].glowtexnum = (unsigned short) glowtexnum;
85         transpoly[currenttranspoly].fogtexnum = (unsigned short) fogtexnum;
86         transpoly[currenttranspoly].transpolytype = (unsigned short) transpolytype;
87         transpoly[currenttranspoly].firstvert = currenttransvert;
88         transpoly[currenttranspoly].verts = 0;
89 //      transpoly[currenttranspoly].ndist = 0; // clear the normal
90 }
91 */
92
93 // turned into a #define
94 /*
95 void transpolyvert(float x, float y, float z, float s, float t, int r, int g, int b, int a)
96 {
97         int i;
98         if (currenttranspoly >= MAX_TRANSPOLYS || currenttransvert >= MAX_TRANSVERTS)
99                 return;
100         transvert[currenttransvert].s = s;
101         transvert[currenttransvert].t = t;
102         transvert[currenttransvert].r = bound(0, r, 255);
103         transvert[currenttransvert].g = bound(0, g, 255);
104         transvert[currenttransvert].b = bound(0, b, 255);
105         transvert[currenttransvert].a = bound(0, a, 255);
106         transvert[currenttransvert].v[0] = x;
107         transvert[currenttransvert].v[1] = y;
108         transvert[currenttransvert].v[2] = z;
109         currenttransvert++;
110         transpoly[currenttranspoly].verts++;
111 }
112 */
113
114 void transpolyend()
115 {
116         float center, d, maxdist;
117         int i;
118         transvert_t *v;
119         if (currenttranspoly >= MAX_TRANSPOLYS || currenttransvert >= MAX_TRANSVERTS)
120                 return;
121         if (transpoly[currenttranspoly].verts < 3) // skip invalid polygons
122         {
123                 currenttransvert = transpoly[currenttranspoly].firstvert; // reset vert pointer
124                 return;
125         }
126         center = 0;
127         maxdist = -1000000000000000.0f; // eh, it's definitely behind it, so...
128         for (i = 0,v = &transvert[transpoly[currenttranspoly].firstvert];i < transpoly[currenttranspoly].verts;i++, v++)
129         {
130                 d = DotProduct(v->v, vpn);
131                 center += d;
132                 if (d > maxdist)
133                         maxdist = d;
134         }
135         maxdist -= transviewdist;
136         if (maxdist < 4.0f) // behind view
137         {
138                 currenttransvert = transpoly[currenttranspoly].firstvert; // reset vert pointer
139                 return;
140         }
141         center *= transreciptable[transpoly[currenttranspoly].verts];
142         center -= transviewdist;
143         i = bound(0, (int) center, 4095);
144         currenttranslist->next = translisthash[i];
145         currenttranslist->poly = transpoly + currenttranspoly;
146         translisthash[i] = currenttranslist;
147         currenttranslist++;
148         currenttranspoly++;
149 }
150
151 int transpolyindices;
152
153 /*
154 void transpolyrenderminmax()
155 {
156         int i, j, k, lastvert;
157         vec_t d, min, max, viewdist, s, average;
158         //vec_t ndist;
159         //vec3_t v1, v2, n;
160         transpolyindices = 0;
161         viewdist = DotProduct(r_refdef.vieworg, vpn);
162         for (i = 0;i < currenttranspoly;i++)
163         {
164                 if (transpoly[i].verts < 3) // only process valid polygons
165                         continue;
166                 min = 1000000;max = -1000000;
167                 s = 1.0f / transpoly[i].verts;
168                 lastvert = transpoly[i].firstvert + transpoly[i].verts;
169                 average = 0;
170                 for (j = transpoly[i].firstvert;j < lastvert;j++)
171                 {
172                         d = DotProduct(transvert[j].v, vpn)-viewdist;
173                         if (d < min) min = d;
174                         if (d > max) max = d;
175                         average += d * s;
176                 }
177                 if (max < 4) // free to check here, so skip polys behind the view
178                         continue;
179                 transpoly[i].distance = average;
180 */
181                 /*
182                 transpoly[i].mindistance = min;
183                 transpoly[i].maxdistance = max;
184                 // calculate normal (eek)
185                 VectorSubtract(transvert[transpoly[i].firstvert  ].v, transvert[transpoly[i].firstvert+1].v, v1);
186                 VectorSubtract(transvert[transpoly[i].firstvert+2].v, transvert[transpoly[i].firstvert+1].v, v2);
187                 VectorNormalize(v1);
188                 VectorNormalize(v2);
189                 if (transpoly[i].verts > 3 && fabs(DotProduct(v1, v2)) >= (1.0f - (1.0f / 256.0f))) // colinear edges, find a better triple
190                 {
191                         VectorSubtract(transvert[transpoly[i].firstvert + transpoly[i].verts - 1].v, transvert[transpoly[i].firstvert].v, v1);
192                         VectorSubtract(transvert[transpoly[i].firstvert + 1].v, transvert[transpoly[i].firstvert].v, v2);
193                         VectorNormalize(v1);
194                         VectorNormalize(v2);
195                         if (fabs(DotProduct(v1, v2)) < (1.0f - (1.0f / 256.0f))) // found a good triple
196                                 goto foundtriple;
197                         for (k = transpoly[i].firstvert + 2;k < (transpoly[i].firstvert + transpoly[i].verts - 1);k++)
198                         {
199                                 VectorSubtract(transvert[k-1].v, transvert[k].v, v1);
200                                 VectorSubtract(transvert[k+1].v, transvert[k].v, v2);
201                                 VectorNormalize(v1);
202                                 VectorNormalize(v2);
203                                 if (fabs(DotProduct(v1, v2)) < (1.0f - (1.0f / 256.0f))) // found a good triple
204                                         goto foundtriple;
205                         }
206                         VectorSubtract(transvert[k-1].v, transvert[k].v, v1);
207                         VectorSubtract(transvert[transpoly[i].firstvert].v, transvert[k].v, v2);
208                         VectorNormalize(v1);
209                         VectorNormalize(v2);
210                         if (fabs(DotProduct(v1, v2)) >= (1.0f - (1.0f / 256.0f))) // no good triples; the polygon is a line, skip it
211                                 continue;
212                 }
213 foundtriple:
214                 CrossProduct(v1, v2, n);
215                 VectorNormalize(n);
216                 ndist = DotProduct(transvert[transpoly[i].firstvert+1].v, n);
217                 // sorted insert
218                 for (j = 0;j < transpolyindices;j++)
219                 {
220                         // easy cases
221                         if (transpoly[transpolyindex[j]].mindistance > max)
222                                 continue;
223                         if (transpoly[transpolyindex[j]].maxdistance < min)
224                                 break;
225                         // hard case, check side
226                         for (k = transpoly[transpolyindex[j]].firstvert;k < (transpoly[transpolyindex[j]].firstvert + transpoly[transpolyindex[j]].verts);k++)
227                                 if (DotProduct(transvert[k].v, n) < ndist)
228                                         goto skip;
229                         break;
230 skip:
231                         ;
232                 }
233                 */
234 /*
235                 // sorted insert
236                 for (j = 0;j < transpolyindices;j++)
237                         if (transpoly[transpolyindex[j]].distance < average)
238                                 break;
239                 for (k = transpolyindices;k > j;k--)
240                         transpolyindex[k] = transpolyindex[k-1];
241                 transpolyindices++;
242                 transpolyindex[j] = i;
243         }
244 }
245 */
246 /*
247 // LordHavoc: qsort compare function
248 int transpolyqsort(const void *ia, const void *ib)
249 {
250         transpoly_t *a, *b;
251         int i, j;
252         a = &transpoly[*((unsigned short *)ia)];
253         b = &transpoly[*((unsigned short *)ib)];
254         // easy cases
255         if (a->mindistance > b->mindistance && a->maxdistance > b->maxdistance)
256                 return -1; // behind
257         if (a->mindistance < b->mindistance && a->maxdistance < b->maxdistance)
258                 return 1; // infront
259         // hard case
260         if (!a->ndist)
261         {
262                 // calculate normal (eek)
263                 vec3_t v1, v2;
264                 VectorSubtract(transvert[a->firstvert  ].v, transvert[a->firstvert+1].v, v1);
265                 VectorSubtract(transvert[a->firstvert+2].v, transvert[a->firstvert+1].v, v2);
266                 CrossProduct(v1, v2, a->n);
267                 VectorNormalize(a->n);
268                 a->ndist = DotProduct(transvert[a->firstvert  ].v, a->n);
269         }
270         // check side
271         for (i = b->firstvert, j = 0;i < (b->firstvert + b->verts);i++)
272                 j += DotProduct(transvert[i].v, a->n) < a->ndist; // (1) b is infront of a
273         if (j == 0)
274                 return -1; // (-1) a is behind b
275         return j == b->verts; // (1) a is infront of b    (0) a and b intersect
276 //      return (transpoly[*((unsigned short *)ib)].mindistance + transpoly[*((unsigned short *)ib)].maxdistance) - (transpoly[*((unsigned short *)ia)].mindistance + transpoly[*((unsigned short *)ia)].maxdistance);
277         */
278 /*
279         return ((transpoly_t*)ia)->distance - ((transpoly_t*)ib)->distance;
280 }
281 */
282
283 /*
284 int transpolyqsort(const void *ia, const void *ib)
285 {
286         return (transpoly[*((unsigned short *)ib)].distance - transpoly[*((unsigned short *)ia)].distance);
287 }
288 */
289
290 /*
291 void transpolyrenderminmax()
292 {
293         int i, j, lastvert;
294         vec_t d, max, viewdist, average;
295         transpolyindices = 0;
296         viewdist = DotProduct(r_refdef.vieworg, vpn);
297         for (i = 0;i < currenttranspoly;i++)
298         {
299                 if (transpoly[i].verts < 3) // only process valid polygons
300                         continue;
301                 max = -1000000;
302                 lastvert = transpoly[i].firstvert + transpoly[i].verts;
303                 average = 0;
304                 for (j = transpoly[i].firstvert;j < lastvert;j++)
305                 {
306                         d = DotProduct(transvert[j].v, vpn)-viewdist;
307                         average += d;
308                         if (d > max)
309                                 max = d;
310                 }
311                 if (max < 4) // free to check here, so skip polys behind the view
312                         continue;
313                 transpoly[i].distance = average / transpoly[i].verts;
314                 transpolyindex[transpolyindices++] = i;
315         }
316         qsort(&transpolyindex[0], transpolyindices, sizeof(unsigned short), transpolyqsort);
317 }
318 */
319 /*
320         int i, j, a;
321         a = true;
322         while(a)
323         {
324                 a = false;
325                 for (i = 1;i < transpolyindices;i++)
326                 {
327                         // easy cases
328                         if (transpoly[transpolyindex[i - 1]].mindistance > transpoly[transpolyindex[i]].mindistance && transpoly[transpolyindex[i - 1]].maxdistance > transpoly[transpolyindex[i]].maxdistance)
329                                 continue; // previous is behind (no swap)
330                         if (transpoly[transpolyindex[i - 1]].mindistance < transpoly[transpolyindex[i]].mindistance && transpoly[transpolyindex[i - 1]].maxdistance < transpoly[transpolyindex[i]].maxdistance)
331                                 goto swap; // previous is infront (swap)
332                         // hard case
333 */
334                         /*
335                         if (!transpoly[transpolyindex[i - 1]].ndist)
336                         {
337                                 // calculate normal (eek)
338                                 vec3_t v1, v2;
339                                 VectorSubtract(transvert[transpoly[transpolyindex[i - 1]].firstvert  ].v, transvert[transpoly[transpolyindex[i - 1]].firstvert+1].v, v1);
340                                 VectorSubtract(transvert[transpoly[transpolyindex[i - 1]].firstvert+2].v, transvert[transpoly[transpolyindex[i - 1]].firstvert+1].v, v2);
341                                 CrossProduct(v1, v2, transpoly[transpolyindex[i - 1]].n);
342                                 VectorNormalize(transpoly[transpolyindex[i - 1]].n);
343                                 transpoly[transpolyindex[i - 1]].ndist = DotProduct(transvert[transpoly[transpolyindex[i - 1]].firstvert  ].v, transpoly[transpolyindex[i - 1]].n);
344                         }
345                         if (DotProduct(transpoly[transpolyindex[i - 1]].n, vpn) >= 0.0f) // backface
346                                 continue;
347                         */
348 /*
349                         // check side
350                         for (i = transpoly[transpolyindex[i]].firstvert;i < (transpoly[transpolyindex[i]].firstvert + transpoly[transpolyindex[i]].verts);i++)
351                                 if (DotProduct(transvert[i].v, transpoly[transpolyindex[i - 1]].n) >= transpoly[transpolyindex[i - 1]].ndist)
352                                         goto noswap; // previous is behind or they intersect
353 swap:
354                         // previous is infront (swap)
355                         j = transpolyindex[i];
356                         transpolyindex[i] = transpolyindex[i - 1];
357                         transpolyindex[i - 1] = j;
358                         a = true;
359 noswap:
360                         ;
361                 }
362         }
363 }
364 */
365
366 void transpolyrender()
367 {
368         int i, j, tpolytype, texnum;
369         transpoly_t *p;
370         if (!r_render.value)
371                 return;
372         if (currenttranspoly < 1)
373                 return;
374 //      transpolyrenderminmax();
375 //      if (transpolyindices < 1)
376 //              return;
377         // testing
378 //      Con_DPrintf("transpolyrender: %i polys %i infront %i vertices\n", currenttranspoly, transpolyindices, currenttransvert);
379 //      if (transpolyindices >= 2)
380 //              transpolysort();
381         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
382         glEnable(GL_BLEND);
383         glShadeModel(GL_SMOOTH);
384         glDepthMask(0); // disable zbuffer updates
385         if (isG200) // Matrox G200 cards can't handle per pixel alpha
386                 glEnable(GL_ALPHA_TEST);
387         else
388                 glDisable(GL_ALPHA_TEST);
389         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
390         tpolytype = TPOLYTYPE_ALPHA;
391         texnum = -1;
392         /*
393         if (gl_vertexarrays.value)
394         {
395                 // set up the vertex array
396                 qglInterleavedArrays(GL_T2F_C4UB_V3F, 0, transvert);
397                 for (i = 0;i < transpolyindices;i++)
398                 {
399                         p = &transpoly[transpolyindex[i]];
400                         if (p->texnum != texnum || p->transpolytype != tpolytype)
401                         {
402                                 if (p->texnum != texnum)
403                                 {
404                                         texnum = p->texnum;
405                                         glBindTexture(GL_TEXTURE_2D, texnum);
406                                 }
407                                 if (p->transpolytype != tpolytype)
408                                 {
409                                         tpolytype = p->transpolytype;
410                                         if (tpolytype == TPOLYTYPE_ADD) // additive
411                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
412                                         else // alpha
413                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
414                                 }
415                         }
416                         qglDrawArrays(GL_TRIANGLE_FAN, p->firstvert, p->verts);
417                         if (p->glowtexnum)
418                         {
419                                 texnum = p->glowtexnum; // highly unlikely to match next poly, but...
420                                 glBindTexture(GL_TEXTURE_2D, texnum);
421                                 tpolytype = TPOLYTYPE_ADD; // might match next poly
422                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
423                                 qglDrawArrays(GL_TRIANGLE_FAN, p->firstvert, p->verts);
424                         }
425                 }
426                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
427                 glDisableClientState(GL_COLOR_ARRAY);
428                 glDisableClientState(GL_VERTEX_ARRAY);
429         }
430         else
431         */
432         {
433                 int points = -1;
434                 translistitem *item;
435                 transvert_t *vert;
436                 for (i = 4095;i >= 0;i--)
437                 {
438                         item = translisthash[i];
439                         while (item)
440                         {
441                                 p = item->poly;
442                                 item = item->next;
443                                 if (p->texnum != texnum || p->verts != points || p->transpolytype != tpolytype)
444                                 {
445                                         glEnd();
446                                         if (isG200)
447                                         {
448                                                 if (p->fogtexnum) // alpha
449                                                         glEnable(GL_ALPHA_TEST);
450                                                 else
451                                                         glDisable(GL_ALPHA_TEST);
452                                         }
453                                         if (p->texnum != texnum)
454                                         {
455                                                 texnum = p->texnum;
456                                                 glBindTexture(GL_TEXTURE_2D, texnum);
457                                         }
458                                         if (p->transpolytype != tpolytype)
459                                         {
460                                                 tpolytype = p->transpolytype;
461                                                 if (tpolytype == TPOLYTYPE_ADD) // additive
462                                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
463                                                 else // alpha
464                                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
465                                         }
466                                         points = p->verts;
467                                         switch (points)
468                                         {
469                                         case 3:
470                                                 glBegin(GL_TRIANGLES);
471                                                 break;
472                                         case 4:
473                                                 glBegin(GL_QUADS);
474                                                 break;
475                                         default:
476                                                 glBegin(GL_TRIANGLE_FAN);
477                                                 points = -1; // to force a reinit on the next poly
478                                                 break;
479                                         }
480                                 }
481                                 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
482                                 {
483                                         // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
484                                         glTexCoord2f(vert->s, vert->t);
485                                         // again, vector version isn't supported I think
486                                         glColor4ub(vert->r, vert->g, vert->b, vert->a);
487                                         glVertex3fv(vert->v);
488                                 }
489                                 if (p->glowtexnum)
490                                 {
491                                         glEnd();
492                                         texnum = p->glowtexnum; // highly unlikely to match next poly, but...
493                                         glBindTexture(GL_TEXTURE_2D, texnum);
494                                         if (tpolytype != TPOLYTYPE_ADD)
495                                         {
496                                                 tpolytype = TPOLYTYPE_ADD; // might match next poly
497                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
498                                         }
499                                         points = -1;
500                                         glBegin(GL_TRIANGLE_FAN);
501                                         for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
502                                         {
503                                                 glColor4ub(255,255,255,vert->a);
504                                                 // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
505                                                 glTexCoord2f(vert->s, vert->t);
506                                                 glVertex3fv(vert->v);
507                                         }
508                                         glEnd();
509                                 }
510                                 if (fogenabled && p->transpolytype == TPOLYTYPE_ALPHA)
511                                 {
512                                         vec3_t diff;
513                                         glEnd();
514                                         points = -1; // to force a reinit on the next poly
515                                         if (tpolytype != TPOLYTYPE_ALPHA)
516                                         {
517                                                 tpolytype = TPOLYTYPE_ALPHA; // probably matchs next poly
518                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
519                                         }
520                                         if (p->fogtexnum)
521                                         {
522                                                 if (texnum != p->fogtexnum) // highly unlikely to match next poly, but...
523                                                 {
524                                                         texnum = p->fogtexnum;
525                                                         glBindTexture(GL_TEXTURE_2D, texnum);
526                                                 }
527                                                 glBegin(GL_TRIANGLE_FAN);
528                                                 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
529                                                 {
530                                                         VectorSubtract(vert->v, r_refdef.vieworg,diff);
531                                                         glTexCoord2f(vert->s, vert->t);
532                                                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], vert->a*(1.0f/255.0f)*exp(fogdensity/DotProduct(diff,diff)));
533                                                         glVertex3fv(vert->v);
534                                                 }
535                                                 glEnd ();
536                                         }
537                                         else
538                                         {
539                                                 glDisable(GL_TEXTURE_2D);
540                                                 glBegin(GL_TRIANGLE_FAN);
541                                                 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
542                                                 {
543                                                         VectorSubtract(vert->v, r_refdef.vieworg,diff);
544                                                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], vert->a*(1.0f/255.0f)*exp(fogdensity/DotProduct(diff,diff)));
545                                                         glVertex3fv(vert->v);
546                                                 }
547                                                 glEnd ();
548                                                 glEnable(GL_TEXTURE_2D);
549                                         }
550                                 }
551                         }
552                 }
553                 glEnd();
554         }
555
556         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
557         glDepthMask(1); // enable zbuffer updates
558         glDisable(GL_ALPHA_TEST);
559 }
560
561 void wallpolyclear()
562 {
563         currentwallpoly = currentwallvert = 0;
564 }
565
566 void wallpolyrender()
567 {
568         int i, j, texnum, lighttexnum;
569         wallpoly_t *p;
570         wallvert_t *vert;
571         if (!r_render.value)
572                 return;
573         if (currentwallpoly < 1)
574                 return;
575         c_brush_polys += currentwallpoly;
576         // testing
577         //Con_DPrintf("wallpolyrender: %i polys %i vertices\n", currentwallpoly, currentwallvert);
578         if (!gl_mtexable)
579                 gl_multitexture.value = 0;
580         glDisable(GL_BLEND);
581         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
582         glShadeModel(GL_FLAT);
583         // make sure zbuffer is enabled
584         glEnable(GL_DEPTH_TEST);
585         glDisable(GL_ALPHA_TEST);
586         glDepthMask(1);
587         glColor3f(1,1,1);
588         if (r_fullbright.value) // LordHavoc: easy to do fullbright...
589         {
590                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
591                 texnum = -1;
592                 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
593                 {
594                         if (p->texnum != texnum)
595                         {
596                                 texnum = p->texnum;
597                                 glBindTexture(GL_TEXTURE_2D, texnum);
598                         }
599                         vert = &wallvert[p->firstvert];
600                         glBegin(GL_POLYGON);
601                         for (j=0 ; j<p->numverts ; j++, vert++)
602                         {
603                                 glTexCoord2f (vert->s, vert->t);
604                                 glVertex3fv (vert->vert);
605                         }
606                         glEnd ();
607                 }
608         }
609         else if (gl_multitexture.value)
610         {
611                 qglSelectTexture(gl_mtex_enum+0);
612                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
613                 glEnable(GL_TEXTURE_2D);
614                 qglSelectTexture(gl_mtex_enum+1);
615                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
616                 glEnable(GL_TEXTURE_2D);
617                 texnum = -1;
618                 lighttexnum = -1;
619                 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
620                 {
621                         if (p->texnum != texnum || p->lighttexnum != lighttexnum)
622                         {
623                                 texnum = p->texnum;
624                                 lighttexnum = p->lighttexnum;
625                                 qglSelectTexture(gl_mtex_enum+0);
626                                 glBindTexture(GL_TEXTURE_2D, texnum);
627                                 qglSelectTexture(gl_mtex_enum+1);
628                                 glBindTexture(GL_TEXTURE_2D, lighttexnum);
629                         }
630                         vert = &wallvert[p->firstvert];
631                         glBegin(GL_POLYGON);
632                         for (j=0 ; j<p->numverts ; j++, vert++)
633                         {
634                                 qglMTexCoord2f(gl_mtex_enum, vert->s, vert->t); // texture
635                                 qglMTexCoord2f((gl_mtex_enum+1), vert->u, vert->v); // lightmap
636                                 glVertex3fv (vert->vert);
637                         }
638                         glEnd ();
639                 }
640
641                 qglSelectTexture(gl_mtex_enum+1);
642                 glDisable(GL_TEXTURE_2D);
643                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
644                 qglSelectTexture(gl_mtex_enum+0);
645                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
646         }
647         else
648         {
649                 // first do the textures
650                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
651                 texnum = -1;
652                 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
653                 {
654                         if (p->texnum != texnum)
655                         {
656                                 texnum = p->texnum;
657                                 glBindTexture(GL_TEXTURE_2D, texnum);
658                         }
659                         vert = &wallvert[p->firstvert];
660                         glBegin(GL_POLYGON);
661                         for (j=0 ; j<p->numverts ; j++, vert++)
662                         {
663                                 glTexCoord2f (vert->s, vert->t);
664                                 glVertex3fv (vert->vert);
665                         }
666                         glEnd ();
667                 }
668                 // then modulate using the lightmaps
669                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
670                 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
671                 glEnable(GL_BLEND);
672                 texnum = -1;
673                 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
674                 {
675                         if (p->lighttexnum != texnum)
676                         {
677                                 texnum = p->lighttexnum;
678                                 glBindTexture(GL_TEXTURE_2D, texnum);
679                         }
680                         vert = &wallvert[p->firstvert];
681                         glBegin(GL_POLYGON);
682                         for (j=0 ; j<p->numverts ; j++, vert++)
683                         {
684                                 glTexCoord2f (vert->u, vert->v);
685                                 glVertex3fv (vert->vert);
686                         }
687                         glEnd ();
688                 }
689         }
690         // switch to additive mode settings
691         glDepthMask(0);
692         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
693         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
694         glEnable(GL_BLEND);
695         glDisable(GL_ALPHA_TEST);
696         glShadeModel(GL_SMOOTH);
697         // render vertex lit overlays ontop
698         texnum = -1;
699         for (i = 0, p = wallpoly;i < currentwallpoly;i++, p++)
700         {
701                 if (!p->lit)
702                         continue;
703                 for (j = 0,vert = &wallvert[p->firstvert];j < p->numverts;j++, vert++)
704                         if (vert->r || vert->g || vert->b)
705                                 goto lit;
706                 continue;
707 lit:
708                 c_light_polys++;
709                 if (p->texnum != texnum)
710                 {
711                         texnum = p->texnum;
712                         glBindTexture(GL_TEXTURE_2D, texnum);
713                 }
714                 glBegin(GL_POLYGON);
715                 for (j = 0,vert = &wallvert[p->firstvert];j < p->numverts;j++, vert++)
716                 {
717                         // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
718                         glTexCoord2f(vert->s, vert->t);
719                         // again, vector version isn't supported I think
720                         glColor3ub(vert->r, vert->g, vert->b);
721                         glVertex3fv(vert->vert);
722                 }
723                 glEnd();
724         }
725         // render glow textures
726         glShadeModel(GL_FLAT);
727         glBlendFunc(GL_ONE, GL_ONE);
728         if (lighthalf)
729                 glColor3f(0.5,0.5,0.5);
730         else
731                 glColor3f(1,1,1);
732         texnum = -1;
733         for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
734         {
735                 if (!p->glowtexnum)
736                         continue;
737                 if (p->glowtexnum != texnum)
738                 {
739                         texnum = p->glowtexnum;
740                         glBindTexture(GL_TEXTURE_2D, texnum);
741                 }
742                 vert = &wallvert[p->firstvert];
743                 glBegin(GL_POLYGON);
744                 for (j=0 ; j<p->numverts ; j++, vert++)
745                 {
746                         glTexCoord2f (vert->s, vert->t);
747                         glVertex3fv (vert->vert);
748                 }
749                 glEnd();
750         }
751         glColor3f(1,1,1);
752         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
753         glShadeModel(GL_SMOOTH);
754         if (fogenabled)
755         {
756                 vec3_t diff;
757                 glDisable(GL_TEXTURE_2D);
758                 for (i = 0,p = &wallpoly[0];i < currentwallpoly;i++, p++)
759                 {
760                         vert = &wallvert[p->firstvert];
761                         glBegin(GL_POLYGON);
762                         for (j=0 ; j<p->numverts ; j++, vert++)
763                         {
764                                 VectorSubtract(vert->vert, r_refdef.vieworg,diff);
765                                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
766                                 glVertex3fv (vert->vert);
767                         }
768                         glEnd ();
769                 }
770                 glEnable(GL_TEXTURE_2D);
771         }
772         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
773         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
774         glDisable(GL_ALPHA_TEST);
775         glShadeModel(GL_SMOOTH);
776         glDisable(GL_BLEND);
777         glDepthMask(1);
778 }
779
780 void skypolyclear()
781 {
782         currentskypoly = currentskyvert = 0;
783 }
784
785 extern char skyname[];
786 extern int solidskytexture, alphaskytexture;
787 void skypolyrender()
788 {
789         int i, j;
790         skypoly_t *p;
791         skyvert_t *vert;
792         float length, speedscale;
793         vec3_t dir;
794         if (!r_render.value)
795                 return;
796         if (currentskypoly < 1)
797                 return;
798         // testing
799 //      Con_DPrintf("skypolyrender: %i polys %i vertices\n", currentskypoly, currentskyvert);
800         glDisable(GL_ALPHA_TEST);
801         glDisable(GL_BLEND);
802         // make sure zbuffer is enabled
803         glEnable(GL_DEPTH_TEST);
804         glDepthMask(1);
805         if (!fogenabled && !skyname[0]) // normal quake sky
806         {
807                 if(lighthalf)
808                         glColor3f(0.5f, 0.5f, 0.5f);
809                 else
810                         glColor3f(1.0f,1.0f,1.0f);
811                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
812                 glEnable(GL_TEXTURE_2D);
813                 glDisable(GL_BLEND);
814                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
815                 glBindTexture(GL_TEXTURE_2D, solidskytexture); // upper clouds
816                 speedscale = cl.time*8;
817                 speedscale -= (int)speedscale & ~127 ;
818                 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
819                 {
820                         vert = &skyvert[p->firstvert];
821                         glBegin(GL_POLYGON);
822                         for (j=0 ; j<p->verts ; j++, vert++)
823                         {
824                                 VectorSubtract (vert->v, r_origin, dir);
825                                 dir[2] *= 3;    // flatten the sphere
826
827                                 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
828                                 length = sqrt (length);
829                                 length = 6*63/length;
830
831                                 glTexCoord2f ((speedscale + dir[0] * length) * (1.0/128), (speedscale + dir[1] * length) * (1.0/128));
832                                 glVertex3fv (vert->v);
833                         }
834                         glEnd ();
835                 }
836                 glEnable(GL_BLEND);
837                 glDepthMask(0);
838                 glBindTexture(GL_TEXTURE_2D, alphaskytexture); // lower clouds
839                 speedscale = cl.time*16;
840                 speedscale -= (int)speedscale & ~127 ;
841                 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
842                 {
843                         vert = &skyvert[p->firstvert];
844                         glBegin(GL_POLYGON);
845                         for (j=0 ; j<p->verts ; j++, vert++)
846                         {
847                                 VectorSubtract (vert->v, r_origin, dir);
848                                 dir[2] *= 3;    // flatten the sphere
849
850                                 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
851                                 length = sqrt (length);
852                                 length = 6*63/length;
853
854                                 glTexCoord2f ((speedscale + dir[0] * length) * (1.0/128), (speedscale + dir[1] * length) * (1.0/128));
855                                 glVertex3fv (vert->v);
856                         }
857                         glEnd ();
858                 }
859                 glDisable(GL_BLEND);
860                 glColor3f(1,1,1);
861                 glDepthMask(1);
862         }
863         else
864         {
865                 glDisable(GL_TEXTURE_2D);
866                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
867                 glColor3fv(fogcolor); // note: gets rendered over by skybox if fog is not enabled
868                 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
869                 {
870                         vert = &skyvert[p->firstvert];
871                         glBegin(GL_POLYGON);
872                         for (j=0 ; j<p->verts ; j++, vert++)
873                                 glVertex3fv (vert->v);
874                         glEnd ();
875                 }
876                 glColor3f(1,1,1);
877                 glEnable(GL_TEXTURE_2D);
878         }
879 }