optimized AngleVectors calls (pass NULL for vectors that should not be generated)
[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 wallvertcolor_t *wallvertcolor;
8 wallpoly_t *wallpoly;
9 skyvert_t *skyvert;
10 skypoly_t *skypoly;
11
12 int currenttranspoly;
13 int currenttransvert;
14 int currentwallpoly;
15 int currentwallvert;
16 int currentskypoly;
17 int currentskyvert;
18
19 cvar_t gl_multitexture = {"gl_multitexture", "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         wallvertcolor = qmalloc(MAX_WALLVERTS * sizeof(wallvertcolor_t));
45         wallpoly = qmalloc(MAX_WALLPOLYS * sizeof(wallpoly_t));
46         skyvert = qmalloc(MAX_SKYVERTS * sizeof(skyvert_t));
47         skypoly = qmalloc(MAX_SKYPOLYS * sizeof(skypoly_t));
48         transreciptable[0] = 0.0f;
49         for (i = 1;i < 256;i++)
50                 transreciptable[i] = 1.0f / i;
51 }
52 void gl_poly_shutdown()
53 {
54         qfree(transvert);
55         qfree(transpoly);
56         qfree(transpolyindex);
57         qfree(wallvert);
58         qfree(wallvertcolor);
59         qfree(wallpoly);
60         qfree(skyvert);
61         qfree(skypoly);
62 }
63
64 void GL_Poly_Init()
65 {
66         Cvar_RegisterVariable (&gl_multitexture);
67         R_RegisterModule("GL_Poly", gl_poly_start, gl_poly_shutdown);
68 }
69
70 void transpolyclear()
71 {
72         currenttranspoly = currenttransvert = 0;
73         currenttranslist = translist;
74         memset(translisthash, 0, sizeof(translisthash));
75         transviewdist = DotProduct(r_refdef.vieworg, vpn);
76 }
77
78 // turned into a #define
79 /*
80 void transpolybegin(int texnum, int glowtexnum, int fogtexnum, int transpolytype)
81 {
82         if (currenttranspoly >= MAX_TRANSPOLYS || currenttransvert >= MAX_TRANSVERTS)
83                 return;
84         transpoly[currenttranspoly].texnum = (unsigned short) texnum;
85         transpoly[currenttranspoly].glowtexnum = (unsigned short) glowtexnum;
86         transpoly[currenttranspoly].fogtexnum = (unsigned short) fogtexnum;
87         transpoly[currenttranspoly].transpolytype = (unsigned short) transpolytype;
88         transpoly[currenttranspoly].firstvert = currenttransvert;
89         transpoly[currenttranspoly].verts = 0;
90 //      transpoly[currenttranspoly].ndist = 0; // clear the normal
91 }
92 */
93
94 // turned into a #define
95 /*
96 void transpolyvert(float x, float y, float z, float s, float t, int r, int g, int b, int a)
97 {
98         int i;
99         if (currenttranspoly >= MAX_TRANSPOLYS || currenttransvert >= MAX_TRANSVERTS)
100                 return;
101         transvert[currenttransvert].s = s;
102         transvert[currenttransvert].t = t;
103         transvert[currenttransvert].r = bound(0, r, 255);
104         transvert[currenttransvert].g = bound(0, g, 255);
105         transvert[currenttransvert].b = bound(0, b, 255);
106         transvert[currenttransvert].a = bound(0, a, 255);
107         transvert[currenttransvert].v[0] = x;
108         transvert[currenttransvert].v[1] = y;
109         transvert[currenttransvert].v[2] = z;
110         currenttransvert++;
111         transpoly[currenttranspoly].verts++;
112 }
113 */
114
115 void transpolyend()
116 {
117         float center, d, maxdist;
118         int i;
119         transvert_t *v;
120         if (currenttranspoly >= MAX_TRANSPOLYS || currenttransvert >= MAX_TRANSVERTS)
121                 return;
122         if (transpoly[currenttranspoly].verts < 3) // skip invalid polygons
123         {
124                 currenttransvert = transpoly[currenttranspoly].firstvert; // reset vert pointer
125                 return;
126         }
127         center = 0;
128         maxdist = -1000000000000000.0f; // eh, it's definitely behind it, so...
129         for (i = 0,v = &transvert[transpoly[currenttranspoly].firstvert];i < transpoly[currenttranspoly].verts;i++, v++)
130         {
131                 d = DotProduct(v->v, vpn);
132                 center += d;
133                 if (d > maxdist)
134                         maxdist = d;
135         }
136         maxdist -= transviewdist;
137         if (maxdist < 4.0f) // behind view
138         {
139                 currenttransvert = transpoly[currenttranspoly].firstvert; // reset vert pointer
140                 return;
141         }
142         center *= transreciptable[transpoly[currenttranspoly].verts];
143         center -= transviewdist;
144         i = bound(0, (int) center, 4095);
145         currenttranslist->next = translisthash[i];
146         currenttranslist->poly = transpoly + currenttranspoly;
147         translisthash[i] = currenttranslist;
148         currenttranslist++;
149         currenttranspoly++;
150 }
151
152 int transpolyindices;
153
154 /*
155 void transpolyrenderminmax()
156 {
157         int i, j, k, lastvert;
158         vec_t d, min, max, viewdist, s, average;
159         //vec_t ndist;
160         //vec3_t v1, v2, n;
161         transpolyindices = 0;
162         viewdist = DotProduct(r_refdef.vieworg, vpn);
163         for (i = 0;i < currenttranspoly;i++)
164         {
165                 if (transpoly[i].verts < 3) // only process valid polygons
166                         continue;
167                 min = 1000000;max = -1000000;
168                 s = 1.0f / transpoly[i].verts;
169                 lastvert = transpoly[i].firstvert + transpoly[i].verts;
170                 average = 0;
171                 for (j = transpoly[i].firstvert;j < lastvert;j++)
172                 {
173                         d = DotProduct(transvert[j].v, vpn)-viewdist;
174                         if (d < min) min = d;
175                         if (d > max) max = d;
176                         average += d * s;
177                 }
178                 if (max < 4) // free to check here, so skip polys behind the view
179                         continue;
180                 transpoly[i].distance = average;
181 */
182                 /*
183                 transpoly[i].mindistance = min;
184                 transpoly[i].maxdistance = max;
185                 // calculate normal (eek)
186                 VectorSubtract(transvert[transpoly[i].firstvert  ].v, transvert[transpoly[i].firstvert+1].v, v1);
187                 VectorSubtract(transvert[transpoly[i].firstvert+2].v, transvert[transpoly[i].firstvert+1].v, v2);
188                 VectorNormalize(v1);
189                 VectorNormalize(v2);
190                 if (transpoly[i].verts > 3 && fabs(DotProduct(v1, v2)) >= (1.0f - (1.0f / 256.0f))) // colinear edges, find a better triple
191                 {
192                         VectorSubtract(transvert[transpoly[i].firstvert + transpoly[i].verts - 1].v, transvert[transpoly[i].firstvert].v, v1);
193                         VectorSubtract(transvert[transpoly[i].firstvert + 1].v, transvert[transpoly[i].firstvert].v, v2);
194                         VectorNormalize(v1);
195                         VectorNormalize(v2);
196                         if (fabs(DotProduct(v1, v2)) < (1.0f - (1.0f / 256.0f))) // found a good triple
197                                 goto foundtriple;
198                         for (k = transpoly[i].firstvert + 2;k < (transpoly[i].firstvert + transpoly[i].verts - 1);k++)
199                         {
200                                 VectorSubtract(transvert[k-1].v, transvert[k].v, v1);
201                                 VectorSubtract(transvert[k+1].v, transvert[k].v, v2);
202                                 VectorNormalize(v1);
203                                 VectorNormalize(v2);
204                                 if (fabs(DotProduct(v1, v2)) < (1.0f - (1.0f / 256.0f))) // found a good triple
205                                         goto foundtriple;
206                         }
207                         VectorSubtract(transvert[k-1].v, transvert[k].v, v1);
208                         VectorSubtract(transvert[transpoly[i].firstvert].v, transvert[k].v, v2);
209                         VectorNormalize(v1);
210                         VectorNormalize(v2);
211                         if (fabs(DotProduct(v1, v2)) >= (1.0f - (1.0f / 256.0f))) // no good triples; the polygon is a line, skip it
212                                 continue;
213                 }
214 foundtriple:
215                 CrossProduct(v1, v2, n);
216                 VectorNormalize(n);
217                 ndist = DotProduct(transvert[transpoly[i].firstvert+1].v, n);
218                 // sorted insert
219                 for (j = 0;j < transpolyindices;j++)
220                 {
221                         // easy cases
222                         if (transpoly[transpolyindex[j]].mindistance > max)
223                                 continue;
224                         if (transpoly[transpolyindex[j]].maxdistance < min)
225                                 break;
226                         // hard case, check side
227                         for (k = transpoly[transpolyindex[j]].firstvert;k < (transpoly[transpolyindex[j]].firstvert + transpoly[transpolyindex[j]].verts);k++)
228                                 if (DotProduct(transvert[k].v, n) < ndist)
229                                         goto skip;
230                         break;
231 skip:
232                         ;
233                 }
234                 */
235 /*
236                 // sorted insert
237                 for (j = 0;j < transpolyindices;j++)
238                         if (transpoly[transpolyindex[j]].distance < average)
239                                 break;
240                 for (k = transpolyindices;k > j;k--)
241                         transpolyindex[k] = transpolyindex[k-1];
242                 transpolyindices++;
243                 transpolyindex[j] = i;
244         }
245 }
246 */
247 /*
248 // LordHavoc: qsort compare function
249 int transpolyqsort(const void *ia, const void *ib)
250 {
251         transpoly_t *a, *b;
252         int i, j;
253         a = &transpoly[*((unsigned short *)ia)];
254         b = &transpoly[*((unsigned short *)ib)];
255         // easy cases
256         if (a->mindistance > b->mindistance && a->maxdistance > b->maxdistance)
257                 return -1; // behind
258         if (a->mindistance < b->mindistance && a->maxdistance < b->maxdistance)
259                 return 1; // infront
260         // hard case
261         if (!a->ndist)
262         {
263                 // calculate normal (eek)
264                 vec3_t v1, v2;
265                 VectorSubtract(transvert[a->firstvert  ].v, transvert[a->firstvert+1].v, v1);
266                 VectorSubtract(transvert[a->firstvert+2].v, transvert[a->firstvert+1].v, v2);
267                 CrossProduct(v1, v2, a->n);
268                 VectorNormalize(a->n);
269                 a->ndist = DotProduct(transvert[a->firstvert  ].v, a->n);
270         }
271         // check side
272         for (i = b->firstvert, j = 0;i < (b->firstvert + b->verts);i++)
273                 j += DotProduct(transvert[i].v, a->n) < a->ndist; // (1) b is infront of a
274         if (j == 0)
275                 return -1; // (-1) a is behind b
276         return j == b->verts; // (1) a is infront of b    (0) a and b intersect
277 //      return (transpoly[*((unsigned short *)ib)].mindistance + transpoly[*((unsigned short *)ib)].maxdistance) - (transpoly[*((unsigned short *)ia)].mindistance + transpoly[*((unsigned short *)ia)].maxdistance);
278         */
279 /*
280         return ((transpoly_t*)ia)->distance - ((transpoly_t*)ib)->distance;
281 }
282 */
283
284 /*
285 int transpolyqsort(const void *ia, const void *ib)
286 {
287         return (transpoly[*((unsigned short *)ib)].distance - transpoly[*((unsigned short *)ia)].distance);
288 }
289 */
290
291 /*
292 void transpolyrenderminmax()
293 {
294         int i, j, lastvert;
295         vec_t d, max, viewdist, average;
296         transpolyindices = 0;
297         viewdist = DotProduct(r_refdef.vieworg, vpn);
298         for (i = 0;i < currenttranspoly;i++)
299         {
300                 if (transpoly[i].verts < 3) // only process valid polygons
301                         continue;
302                 max = -1000000;
303                 lastvert = transpoly[i].firstvert + transpoly[i].verts;
304                 average = 0;
305                 for (j = transpoly[i].firstvert;j < lastvert;j++)
306                 {
307                         d = DotProduct(transvert[j].v, vpn)-viewdist;
308                         average += d;
309                         if (d > max)
310                                 max = d;
311                 }
312                 if (max < 4) // free to check here, so skip polys behind the view
313                         continue;
314                 transpoly[i].distance = average / transpoly[i].verts;
315                 transpolyindex[transpolyindices++] = i;
316         }
317         qsort(&transpolyindex[0], transpolyindices, sizeof(unsigned short), transpolyqsort);
318 }
319 */
320 /*
321         int i, j, a;
322         a = true;
323         while(a)
324         {
325                 a = false;
326                 for (i = 1;i < transpolyindices;i++)
327                 {
328                         // easy cases
329                         if (transpoly[transpolyindex[i - 1]].mindistance > transpoly[transpolyindex[i]].mindistance && transpoly[transpolyindex[i - 1]].maxdistance > transpoly[transpolyindex[i]].maxdistance)
330                                 continue; // previous is behind (no swap)
331                         if (transpoly[transpolyindex[i - 1]].mindistance < transpoly[transpolyindex[i]].mindistance && transpoly[transpolyindex[i - 1]].maxdistance < transpoly[transpolyindex[i]].maxdistance)
332                                 goto swap; // previous is infront (swap)
333                         // hard case
334 */
335                         /*
336                         if (!transpoly[transpolyindex[i - 1]].ndist)
337                         {
338                                 // calculate normal (eek)
339                                 vec3_t v1, v2;
340                                 VectorSubtract(transvert[transpoly[transpolyindex[i - 1]].firstvert  ].v, transvert[transpoly[transpolyindex[i - 1]].firstvert+1].v, v1);
341                                 VectorSubtract(transvert[transpoly[transpolyindex[i - 1]].firstvert+2].v, transvert[transpoly[transpolyindex[i - 1]].firstvert+1].v, v2);
342                                 CrossProduct(v1, v2, transpoly[transpolyindex[i - 1]].n);
343                                 VectorNormalize(transpoly[transpolyindex[i - 1]].n);
344                                 transpoly[transpolyindex[i - 1]].ndist = DotProduct(transvert[transpoly[transpolyindex[i - 1]].firstvert  ].v, transpoly[transpolyindex[i - 1]].n);
345                         }
346                         if (DotProduct(transpoly[transpolyindex[i - 1]].n, vpn) >= 0.0f) // backface
347                                 continue;
348                         */
349 /*
350                         // check side
351                         for (i = transpoly[transpolyindex[i]].firstvert;i < (transpoly[transpolyindex[i]].firstvert + transpoly[transpolyindex[i]].verts);i++)
352                                 if (DotProduct(transvert[i].v, transpoly[transpolyindex[i - 1]].n) >= transpoly[transpolyindex[i - 1]].ndist)
353                                         goto noswap; // previous is behind or they intersect
354 swap:
355                         // previous is infront (swap)
356                         j = transpolyindex[i];
357                         transpolyindex[i] = transpolyindex[i - 1];
358                         transpolyindex[i - 1] = j;
359                         a = true;
360 noswap:
361                         ;
362                 }
363         }
364 }
365 */
366
367 void transpolyrender()
368 {
369         int i, j, tpolytype, texnum;
370         transpoly_t *p;
371         if (!r_render.value)
372                 return;
373         if (currenttranspoly < 1)
374                 return;
375 //      transpolyrenderminmax();
376 //      if (transpolyindices < 1)
377 //              return;
378         // testing
379 //      Con_DPrintf("transpolyrender: %i polys %i infront %i vertices\n", currenttranspoly, transpolyindices, currenttransvert);
380 //      if (transpolyindices >= 2)
381 //              transpolysort();
382         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
383         glEnable(GL_BLEND);
384         glShadeModel(GL_SMOOTH);
385         glDepthMask(0); // disable zbuffer updates
386         glDisable(GL_ALPHA_TEST);
387         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
388         tpolytype = TPOLYTYPE_ALPHA;
389         texnum = -1;
390         /*
391         if (gl_vertexarrays.value)
392         {
393                 // set up the vertex array
394                 glInterleavedArrays(GL_T2F_C4UB_V3F, 0, transvert);
395                 for (i = 0;i < transpolyindices;i++)
396                 {
397                         p = &transpoly[transpolyindex[i]];
398                         if (p->texnum != texnum || p->transpolytype != tpolytype)
399                         {
400                                 if (p->texnum != texnum)
401                                 {
402                                         texnum = p->texnum;
403                                         glBindTexture(GL_TEXTURE_2D, texnum);
404                                 }
405                                 if (p->transpolytype != tpolytype)
406                                 {
407                                         tpolytype = p->transpolytype;
408                                         if (tpolytype == TPOLYTYPE_ADD) // additive
409                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
410                                         else // alpha
411                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
412                                 }
413                         }
414                         glDrawArrays(GL_POLYGON, p->firstvert, p->verts);
415                         if (p->glowtexnum)
416                         {
417                                 texnum = p->glowtexnum; // highly unlikely to match next poly, but...
418                                 glBindTexture(GL_TEXTURE_2D, texnum);
419                                 tpolytype = TPOLYTYPE_ADD; // might match next poly
420                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
421                                 glDrawArrays(GL_POLYGON, p->firstvert, p->verts);
422                         }
423                 }
424                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
425                 glDisableClientState(GL_COLOR_ARRAY);
426                 glDisableClientState(GL_VERTEX_ARRAY);
427         }
428         else
429         */
430         {
431                 int points = -1;
432                 translistitem *item;
433                 transvert_t *vert;
434                 for (i = 4095;i >= 0;i--)
435                 {
436                         item = translisthash[i];
437                         while (item)
438                         {
439                                 p = item->poly;
440                                 item = item->next;
441                                 if (p->texnum != texnum || p->verts != points || p->transpolytype != tpolytype)
442                                 {
443                                         glEnd();
444                                         if (isG200)
445                                         {
446                                                 // LordHavoc: Matrox G200 cards can't handle per pixel alpha
447                                                 if (p->fogtexnum)
448                                                         glEnable(GL_ALPHA_TEST);
449                                                 else
450                                                         glDisable(GL_ALPHA_TEST);
451                                         }
452                                         if (p->texnum != texnum)
453                                         {
454                                                 texnum = p->texnum;
455                                                 glBindTexture(GL_TEXTURE_2D, texnum);
456                                         }
457                                         if (p->transpolytype != tpolytype)
458                                         {
459                                                 tpolytype = p->transpolytype;
460                                                 if (tpolytype == TPOLYTYPE_ADD) // additive
461                                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
462                                                 else // alpha
463                                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
464                                         }
465                                         points = p->verts;
466                                         switch (points)
467                                         {
468                                         case 3:
469                                                 glBegin(GL_TRIANGLES);
470                                                 break;
471                                         case 4:
472                                                 glBegin(GL_QUADS);
473                                                 break;
474                                         default:
475                                                 glBegin(GL_POLYGON);
476                                                 points = -1; // to force a reinit on the next poly
477                                                 break;
478                                         }
479                                 }
480                                 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
481                                 {
482                                         // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
483                                         glTexCoord2f(vert->s, vert->t);
484                                         // again, vector version isn't supported I think
485                                         glColor4ub(vert->r, vert->g, vert->b, vert->a);
486                                         glVertex3fv(vert->v);
487                                 }
488                                 if (p->glowtexnum)
489                                 {
490                                         glEnd();
491                                         texnum = p->glowtexnum; // highly unlikely to match next poly, but...
492                                         glBindTexture(GL_TEXTURE_2D, texnum);
493                                         if (tpolytype != TPOLYTYPE_ADD)
494                                         {
495                                                 tpolytype = TPOLYTYPE_ADD; // might match next poly
496                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
497                                         }
498                                         points = -1;
499                                         glBegin(GL_POLYGON);
500                                         for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
501                                         {
502                                                 glColor4ub(255,255,255,vert->a);
503                                                 // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
504                                                 glTexCoord2f(vert->s, vert->t);
505                                                 glVertex3fv(vert->v);
506                                         }
507                                         glEnd();
508                                 }
509                                 if (fogenabled && p->transpolytype == TPOLYTYPE_ALPHA)
510                                 {
511                                         vec3_t diff;
512                                         glEnd();
513                                         points = -1; // to force a reinit on the next poly
514                                         if (tpolytype != TPOLYTYPE_ALPHA)
515                                         {
516                                                 tpolytype = TPOLYTYPE_ALPHA; // probably matchs next poly
517                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
518                                         }
519                                         if (p->fogtexnum)
520                                         {
521                                                 if (texnum != p->fogtexnum) // highly unlikely to match next poly, but...
522                                                 {
523                                                         texnum = p->fogtexnum;
524                                                         glBindTexture(GL_TEXTURE_2D, texnum);
525                                                 }
526                                                 glBegin(GL_POLYGON);
527                                                 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
528                                                 {
529                                                         VectorSubtract(vert->v, r_refdef.vieworg,diff);
530                                                         glTexCoord2f(vert->s, vert->t);
531                                                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], vert->a*(1.0f/255.0f)*exp(fogdensity/DotProduct(diff,diff)));
532                                                         glVertex3fv(vert->v);
533                                                 }
534                                                 glEnd ();
535                                         }
536                                         else
537                                         {
538                                                 glDisable(GL_TEXTURE_2D);
539                                                 glBegin(GL_POLYGON);
540                                                 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
541                                                 {
542                                                         VectorSubtract(vert->v, r_refdef.vieworg,diff);
543                                                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], vert->a*(1.0f/255.0f)*exp(fogdensity/DotProduct(diff,diff)));
544                                                         glVertex3fv(vert->v);
545                                                 }
546                                                 glEnd ();
547                                                 glEnable(GL_TEXTURE_2D);
548                                         }
549                                 }
550                         }
551                 }
552                 glEnd();
553         }
554
555         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
556         glDepthMask(1); // enable zbuffer updates
557         glDisable(GL_ALPHA_TEST);
558 }
559
560 void wallpolyclear()
561 {
562         currentwallpoly = currentwallvert = 0;
563 }
564
565 void wallpolyrender()
566 {
567         int i, j, texnum, lighttexnum;
568         wallpoly_t *p;
569         wallvert_t *vert;
570         wallvertcolor_t *vertcolor;
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->vert[3], vert->vert[4]);
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->vert[3], vert->vert[4]); // texture
635                                 qglMTexCoord2f((gl_mtex_enum+1), vert->vert[5], vert->vert[6]); // 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->vert[3], vert->vert[4]);
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->vert[5], vert->vert[6]);
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,vertcolor = &wallvertcolor[p->firstvert];j < p->numverts;j++, vertcolor++)
704                         if (vertcolor->r || vertcolor->g || vertcolor->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], vertcolor = &wallvertcolor[p->firstvert];j < p->numverts;j++, vert++, vertcolor++)
716                 {
717                         // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
718                         glTexCoord2f(vert->vert[3], vert->vert[4]);
719                         // again, vector version isn't supported I think
720                         glColor3ub(vertcolor->r, vertcolor->g, vertcolor->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->vert[3], vert->vert[4]);
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 rtexture_t *solidskytexture, *alphaskytexture;
787 void skypolyrender()
788 {
789         int i, j, numskyverts;
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                 glInterleavedArrays(GL_T2F_V3F, 0, skyvert);
808 //              glTexCoordPointer(2, GL_FLOAT, sizeof(skyvert_t) - sizeof(float) * 2, &skyvert[0].tex[0]);
809 //              glEnableClientState(GL_TEXTURE_COORD_ARRAY);
810 //              glVertexPointer(3, GL_FLOAT, sizeof(skyvert_t) - sizeof(float) * 3, &skyvert[0].v[0]);
811 //              glEnableClientState(GL_VERTEX_ARRAY);
812                 if(lighthalf)
813                         glColor3f(0.5f, 0.5f, 0.5f);
814                 else
815                         glColor3f(1.0f,1.0f,1.0f);
816                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
817                 glEnable(GL_TEXTURE_2D);
818                 glDisable(GL_BLEND);
819                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
820                 glBindTexture(GL_TEXTURE_2D, R_GetTexture(solidskytexture)); // upper clouds
821                 speedscale = cl.time*8;
822                 speedscale -= (int)speedscale & ~127 ;
823                 numskyverts = 0;
824                 for (i = 0, p = &skypoly[0];i < currentskypoly;i++, p++)
825                 {
826                         vert = skyvert + p->firstvert;
827                         for (j = 0;j < p->verts;j++, vert++)
828                         {
829                                 VectorSubtract (vert->v, r_origin, dir);
830                                 dir[2] *= 3;    // flatten the sphere
831
832                                 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
833                                 length = sqrt (length);
834                                 length = 6*63/length;
835
836                                 vert->tex[0] = (speedscale + dir[0] * length) * (1.0/128);
837                                 vert->tex[1] = (speedscale + dir[1] * length) * (1.0/128);
838                         }
839                         numskyverts += p->verts;
840                 }
841                 GL_LockArray(0, numskyverts);
842                 for (i = 0, p = &skypoly[0];i < currentskypoly;i++, p++)
843                         glDrawArrays(GL_POLYGON, p->firstvert, p->verts);
844                 GL_UnlockArray();
845                 glEnable(GL_BLEND);
846                 glDepthMask(0);
847                 glBindTexture(GL_TEXTURE_2D, R_GetTexture(alphaskytexture)); // lower clouds
848                 speedscale = cl.time*16;
849                 speedscale -= (int)speedscale & ~127 ;
850                 for (i = 0, p = &skypoly[0];i < currentskypoly;i++, p++)
851                 {
852                         vert = skyvert + p->firstvert;
853                         for (j = 0;j < p->verts;j++, vert++)
854                         {
855                                 VectorSubtract (vert->v, r_origin, dir);
856                                 dir[2] *= 3;    // flatten the sphere
857
858                                 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
859                                 length = sqrt (length);
860                                 length = 6*63/length;
861
862                                 vert->tex[0] = (speedscale + dir[0] * length) * (1.0/128);
863                                 vert->tex[1] = (speedscale + dir[1] * length) * (1.0/128);
864                         }
865                 }
866                 GL_LockArray(0, numskyverts);
867                 for (i = 0, p = &skypoly[0];i < currentskypoly;i++, p++)
868                         glDrawArrays(GL_POLYGON, p->firstvert, p->verts);
869                 GL_UnlockArray();
870                 glDisable(GL_BLEND);
871                 glColor3f(1,1,1);
872                 glDepthMask(1);
873                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
874                 glDisableClientState(GL_VERTEX_ARRAY);
875         }
876         else
877         {
878                 glVertexPointer(3, GL_FLOAT, sizeof(skyvert_t) - sizeof(float) * 3, &skyvert[0].v[0]);
879                 glEnableClientState(GL_VERTEX_ARRAY);
880                 glDisable(GL_TEXTURE_2D);
881                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
882                 glColor3fv(fogcolor); // note: gets rendered over by skybox if fog is not enabled
883                 numskyverts = 0;
884                 for (i = 0, p = &skypoly[0];i < currentskypoly;i++, p++)
885                         numskyverts += p->verts;
886                 GL_LockArray(0, numskyverts);
887                 for (i = 0, p = &skypoly[0];i < currentskypoly;i++, p++)
888                         glDrawArrays(GL_POLYGON, p->firstvert, p->verts);
889                 GL_UnlockArray();
890                 glColor3f(1,1,1);
891                 glEnable(GL_TEXTURE_2D);
892                 glDisableClientState(GL_VERTEX_ARRAY);
893         }
894 }