]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_poly.c
very minor cruft removal
[divverent/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 = malloc(MAX_TRANSVERTS * sizeof(transvert_t));
41         transpoly = malloc(MAX_TRANSPOLYS * sizeof(transpoly_t));
42         transpolyindex = malloc(MAX_TRANSPOLYS * sizeof(unsigned short));
43         wallvert = malloc(MAX_WALLVERTS * sizeof(wallvert_t));
44         wallpoly = malloc(MAX_WALLPOLYS * sizeof(wallpoly_t));
45         skyvert = malloc(MAX_SKYVERTS * sizeof(skyvert_t));
46         skypoly = malloc(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         free(transvert);
54         free(transpoly);
55         free(transpolyindex);
56         free(wallvert);
57         free(wallpoly);
58         free(skyvert);
59         free(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 extern qboolean isG200;
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         if (isG200) // Matrox G200 cards can't handle per pixel alpha
387                 glEnable(GL_ALPHA_TEST);
388         else
389                 glDisable(GL_ALPHA_TEST);
390         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
391         tpolytype = TPOLYTYPE_ALPHA;
392         texnum = -1;
393         /*
394         if (gl_vertexarrays.value)
395         {
396                 // set up the vertex array
397                 qglInterleavedArrays(GL_T2F_C4UB_V3F, 0, transvert);
398                 for (i = 0;i < transpolyindices;i++)
399                 {
400                         p = &transpoly[transpolyindex[i]];
401                         if (p->texnum != texnum || p->transpolytype != tpolytype)
402                         {
403                                 if (p->texnum != texnum)
404                                 {
405                                         texnum = p->texnum;
406                                         glBindTexture(GL_TEXTURE_2D, texnum);
407                                 }
408                                 if (p->transpolytype != tpolytype)
409                                 {
410                                         tpolytype = p->transpolytype;
411                                         if (tpolytype == TPOLYTYPE_ADD) // additive
412                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
413                                         else // alpha
414                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
415                                 }
416                         }
417                         qglDrawArrays(GL_TRIANGLE_FAN, p->firstvert, p->verts);
418                         if (p->glowtexnum)
419                         {
420                                 texnum = p->glowtexnum; // highly unlikely to match next poly, but...
421                                 glBindTexture(GL_TEXTURE_2D, texnum);
422                                 tpolytype = TPOLYTYPE_ADD; // might match next poly
423                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
424                                 qglDrawArrays(GL_TRIANGLE_FAN, p->firstvert, p->verts);
425                         }
426                 }
427                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
428                 glDisableClientState(GL_COLOR_ARRAY);
429                 glDisableClientState(GL_VERTEX_ARRAY);
430         }
431         else
432         */
433         {
434                 int points = -1;
435                 translistitem *item;
436                 transvert_t *vert;
437                 for (i = 4095;i >= 0;i--)
438                 {
439                         item = translisthash[i];
440                         while (item)
441                         {
442                                 p = item->poly;
443                                 item = item->next;
444                                 if (p->texnum != texnum || p->verts != points || p->transpolytype != tpolytype)
445                                 {
446                                         glEnd();
447                                         if (isG200)
448                                         {
449                                                 if (p->fogtexnum) // alpha
450                                                         glEnable(GL_ALPHA_TEST);
451                                                 else
452                                                         glDisable(GL_ALPHA_TEST);
453                                         }
454                                         if (p->texnum != texnum)
455                                         {
456                                                 texnum = p->texnum;
457                                                 glBindTexture(GL_TEXTURE_2D, texnum);
458                                         }
459                                         if (p->transpolytype != tpolytype)
460                                         {
461                                                 tpolytype = p->transpolytype;
462                                                 if (tpolytype == TPOLYTYPE_ADD) // additive
463                                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
464                                                 else // alpha
465                                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
466                                         }
467                                         points = p->verts;
468                                         switch (points)
469                                         {
470                                         case 3:
471                                                 glBegin(GL_TRIANGLES);
472                                                 break;
473                                         case 4:
474                                                 glBegin(GL_QUADS);
475                                                 break;
476                                         default:
477                                                 glBegin(GL_TRIANGLE_FAN);
478                                                 points = -1; // to force a reinit on the next poly
479                                                 break;
480                                         }
481                                 }
482                                 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
483                                 {
484                                         // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
485                                         glTexCoord2f(vert->s, vert->t);
486                                         // again, vector version isn't supported I think
487                                         glColor4ub(vert->r, vert->g, vert->b, vert->a);
488                                         glVertex3fv(vert->v);
489                                 }
490                                 if (p->glowtexnum)
491                                 {
492                                         glEnd();
493                                         texnum = p->glowtexnum; // highly unlikely to match next poly, but...
494                                         glBindTexture(GL_TEXTURE_2D, texnum);
495                                         if (tpolytype != TPOLYTYPE_ADD)
496                                         {
497                                                 tpolytype = TPOLYTYPE_ADD; // might match next poly
498                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
499                                         }
500                                         points = -1;
501                                         glBegin(GL_TRIANGLE_FAN);
502                                         for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
503                                         {
504                                                 glColor4ub(255,255,255,vert->a);
505                                                 // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
506                                                 glTexCoord2f(vert->s, vert->t);
507                                                 glVertex3fv(vert->v);
508                                         }
509                                         glEnd();
510                                 }
511                                 if (fogenabled && p->transpolytype == TPOLYTYPE_ALPHA)
512                                 {
513                                         vec3_t diff;
514                                         glEnd();
515                                         points = -1; // to force a reinit on the next poly
516                                         if (tpolytype != TPOLYTYPE_ALPHA)
517                                         {
518                                                 tpolytype = TPOLYTYPE_ALPHA; // probably matchs next poly
519                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
520                                         }
521                                         if (p->fogtexnum)
522                                         {
523                                                 if (texnum != p->fogtexnum) // highly unlikely to match next poly, but...
524                                                 {
525                                                         texnum = p->fogtexnum;
526                                                         glBindTexture(GL_TEXTURE_2D, texnum);
527                                                 }
528                                                 glBegin(GL_TRIANGLE_FAN);
529                                                 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
530                                                 {
531                                                         VectorSubtract(vert->v, r_refdef.vieworg,diff);
532                                                         glTexCoord2f(vert->s, vert->t);
533                                                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], vert->a*(1.0f/255.0f)*exp(fogdensity/DotProduct(diff,diff)));
534                                                         glVertex3fv(vert->v);
535                                                 }
536                                                 glEnd ();
537                                         }
538                                         else
539                                         {
540                                                 glDisable(GL_TEXTURE_2D);
541                                                 glBegin(GL_TRIANGLE_FAN);
542                                                 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
543                                                 {
544                                                         VectorSubtract(vert->v, r_refdef.vieworg,diff);
545                                                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], vert->a*(1.0f/255.0f)*exp(fogdensity/DotProduct(diff,diff)));
546                                                         glVertex3fv(vert->v);
547                                                 }
548                                                 glEnd ();
549                                                 glEnable(GL_TEXTURE_2D);
550                                         }
551                                 }
552                         }
553                 }
554                 glEnd();
555         }
556
557         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
558         glDepthMask(1); // enable zbuffer updates
559         glDisable(GL_ALPHA_TEST);
560 }
561
562 extern qboolean isG200;
563
564 void wallpolyclear()
565 {
566         currentwallpoly = currentwallvert = 0;
567 }
568
569 extern qboolean lighthalf;
570 void wallpolyrender()
571 {
572         int i, j, texnum, lighttexnum;
573         wallpoly_t *p;
574         wallvert_t *vert;
575         if (!r_render.value)
576                 return;
577         if (currentwallpoly < 1)
578                 return;
579         c_brush_polys += currentwallpoly;
580         // testing
581         //Con_DPrintf("wallpolyrender: %i polys %i vertices\n", currentwallpoly, currentwallvert);
582         if (!gl_mtexable)
583                 gl_multitexture.value = 0;
584         glDisable(GL_BLEND);
585         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
586         glShadeModel(GL_FLAT);
587         // make sure zbuffer is enabled
588         glEnable(GL_DEPTH_TEST);
589         glDisable(GL_ALPHA_TEST);
590         glDepthMask(1);
591         glColor3f(1,1,1);
592         if (r_fullbright.value) // LordHavoc: easy to do fullbright...
593         {
594                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
595                 texnum = -1;
596                 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
597                 {
598                         if (p->texnum != texnum)
599                         {
600                                 texnum = p->texnum;
601                                 glBindTexture(GL_TEXTURE_2D, texnum);
602                         }
603                         vert = &wallvert[p->firstvert];
604                         glBegin(GL_POLYGON);
605                         for (j=0 ; j<p->numverts ; j++, vert++)
606                         {
607                                 glTexCoord2f (vert->s, vert->t);
608                                 glVertex3fv (vert->vert);
609                         }
610                         glEnd ();
611                 }
612         }
613         else if (gl_multitexture.value)
614         {
615                 qglSelectTexture(gl_mtex_enum+0);
616                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
617                 glEnable(GL_TEXTURE_2D);
618                 qglSelectTexture(gl_mtex_enum+1);
619                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
620                 glEnable(GL_TEXTURE_2D);
621                 texnum = -1;
622                 lighttexnum = -1;
623                 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
624                 {
625                         if (p->texnum != texnum || p->lighttexnum != lighttexnum)
626                         {
627                                 texnum = p->texnum;
628                                 lighttexnum = p->lighttexnum;
629                                 qglSelectTexture(gl_mtex_enum+0);
630                                 glBindTexture(GL_TEXTURE_2D, texnum);
631                                 qglSelectTexture(gl_mtex_enum+1);
632                                 glBindTexture(GL_TEXTURE_2D, lighttexnum);
633                         }
634                         vert = &wallvert[p->firstvert];
635                         glBegin(GL_POLYGON);
636                         for (j=0 ; j<p->numverts ; j++, vert++)
637                         {
638                                 qglMTexCoord2f(gl_mtex_enum, vert->s, vert->t); // texture
639                                 qglMTexCoord2f((gl_mtex_enum+1), vert->u, vert->v); // lightmap
640                                 glVertex3fv (vert->vert);
641                         }
642                         glEnd ();
643                 }
644
645                 qglSelectTexture(gl_mtex_enum+1);
646                 glDisable(GL_TEXTURE_2D);
647                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
648                 qglSelectTexture(gl_mtex_enum+0);
649                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
650         }
651         else
652         {
653                 // first do the textures
654                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
655                 texnum = -1;
656                 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
657                 {
658                         if (p->texnum != texnum)
659                         {
660                                 texnum = p->texnum;
661                                 glBindTexture(GL_TEXTURE_2D, texnum);
662                         }
663                         vert = &wallvert[p->firstvert];
664                         glBegin(GL_POLYGON);
665                         for (j=0 ; j<p->numverts ; j++, vert++)
666                         {
667                                 glTexCoord2f (vert->s, vert->t);
668                                 glVertex3fv (vert->vert);
669                         }
670                         glEnd ();
671                 }
672                 // then modulate using the lightmaps
673                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
674                 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
675                 glEnable(GL_BLEND);
676                 texnum = -1;
677                 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
678                 {
679                         if (p->lighttexnum != texnum)
680                         {
681                                 texnum = p->lighttexnum;
682                                 glBindTexture(GL_TEXTURE_2D, texnum);
683                         }
684                         vert = &wallvert[p->firstvert];
685                         glBegin(GL_POLYGON);
686                         for (j=0 ; j<p->numverts ; j++, vert++)
687                         {
688                                 glTexCoord2f (vert->u, vert->v);
689                                 glVertex3fv (vert->vert);
690                         }
691                         glEnd ();
692                 }
693         }
694         // switch to additive mode settings
695         glDepthMask(0);
696         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
697         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
698         glEnable(GL_BLEND);
699         glDisable(GL_ALPHA_TEST);
700         glShadeModel(GL_SMOOTH);
701         // render vertex lit overlays ontop
702         texnum = -1;
703         for (i = 0, p = wallpoly;i < currentwallpoly;i++, p++)
704         {
705                 if (!p->lit)
706                         continue;
707                 for (j = 0,vert = &wallvert[p->firstvert];j < p->numverts;j++, vert++)
708                         if (vert->r || vert->g || vert->b)
709                                 goto lit;
710                 continue;
711 lit:
712                 c_light_polys++;
713                 if (p->texnum != texnum)
714                 {
715                         texnum = p->texnum;
716                         glBindTexture(GL_TEXTURE_2D, texnum);
717                 }
718                 glBegin(GL_POLYGON);
719                 for (j = 0,vert = &wallvert[p->firstvert];j < p->numverts;j++, vert++)
720                 {
721                         // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
722                         glTexCoord2f(vert->s, vert->t);
723                         // again, vector version isn't supported I think
724                         glColor3ub(vert->r, vert->g, vert->b);
725                         glVertex3fv(vert->vert);
726                 }
727                 glEnd();
728         }
729         // render glow textures
730         glShadeModel(GL_FLAT);
731         glBlendFunc(GL_ONE, GL_ONE);
732         if (lighthalf)
733                 glColor3f(0.5,0.5,0.5);
734         else
735                 glColor3f(1,1,1);
736         texnum = -1;
737         for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
738         {
739                 if (!p->glowtexnum)
740                         continue;
741                 if (p->glowtexnum != texnum)
742                 {
743                         texnum = p->glowtexnum;
744                         glBindTexture(GL_TEXTURE_2D, texnum);
745                 }
746                 vert = &wallvert[p->firstvert];
747                 glBegin(GL_POLYGON);
748                 for (j=0 ; j<p->numverts ; j++, vert++)
749                 {
750                         glTexCoord2f (vert->s, vert->t);
751                         glVertex3fv (vert->vert);
752                 }
753                 glEnd();
754         }
755         glColor3f(1,1,1);
756         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
757         glShadeModel(GL_SMOOTH);
758         if (fogenabled)
759         {
760                 vec3_t diff;
761                 glDisable(GL_TEXTURE_2D);
762                 for (i = 0,p = &wallpoly[0];i < currentwallpoly;i++, p++)
763                 {
764                         vert = &wallvert[p->firstvert];
765                         glBegin(GL_POLYGON);
766                         for (j=0 ; j<p->numverts ; j++, vert++)
767                         {
768                                 VectorSubtract(vert->vert, r_refdef.vieworg,diff);
769                                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
770                                 glVertex3fv (vert->vert);
771                         }
772                         glEnd ();
773                 }
774                 glEnable(GL_TEXTURE_2D);
775         }
776         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
777         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
778         glDisable(GL_ALPHA_TEST);
779         glShadeModel(GL_SMOOTH);
780         glDisable(GL_BLEND);
781         glDepthMask(1);
782 }
783
784 void skypolyclear()
785 {
786         currentskypoly = currentskyvert = 0;
787 }
788
789 extern qboolean isATI;
790
791 extern char skyname[];
792 extern int solidskytexture, alphaskytexture;
793 void skypolyrender()
794 {
795         int i, j;
796         skypoly_t *p;
797         skyvert_t *vert;
798         float length, speedscale;
799         vec3_t dir;
800         if (!r_render.value)
801                 return;
802         if (currentskypoly < 1)
803                 return;
804         // testing
805 //      Con_DPrintf("skypolyrender: %i polys %i vertices\n", currentskypoly, currentskyvert);
806         glDisable(GL_ALPHA_TEST);
807         glDisable(GL_BLEND);
808         // make sure zbuffer is enabled
809         glEnable(GL_DEPTH_TEST);
810         glDepthMask(1);
811         if (!fogenabled && !skyname[0]) // normal quake sky
812         {
813                 glColor3f(0.5f, 0.5f, 0.5f);
814                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
815                 glEnable(GL_TEXTURE_2D);
816                 glDisable(GL_BLEND);
817                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
818                 glBindTexture(GL_TEXTURE_2D, solidskytexture); // upper clouds
819                 speedscale = realtime*8;
820                 speedscale -= (int)speedscale & ~127 ;
821                 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
822                 {
823                         vert = &skyvert[p->firstvert];
824                         glBegin(GL_POLYGON);
825                         for (j=0 ; j<p->verts ; j++, vert++)
826                         {
827                                 VectorSubtract (vert->v, r_origin, dir);
828                                 dir[2] *= 3;    // flatten the sphere
829
830                                 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
831                                 length = sqrt (length);
832                                 length = 6*63/length;
833
834                                 glTexCoord2f ((speedscale + dir[0] * length) * (1.0/128), (speedscale + dir[1] * length) * (1.0/128));
835                                 glVertex3fv (vert->v);
836                         }
837                         glEnd ();
838                 }
839                 glEnable(GL_BLEND);
840                 glDepthMask(0);
841                 glBindTexture(GL_TEXTURE_2D, alphaskytexture); // lower clouds
842                 speedscale = realtime*16;
843                 speedscale -= (int)speedscale & ~127 ;
844                 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
845                 {
846                         vert = &skyvert[p->firstvert];
847                         glBegin(GL_POLYGON);
848                         for (j=0 ; j<p->verts ; j++, vert++)
849                         {
850                                 VectorSubtract (vert->v, r_origin, dir);
851                                 dir[2] *= 3;    // flatten the sphere
852
853                                 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
854                                 length = sqrt (length);
855                                 length = 6*63/length;
856
857                                 glTexCoord2f ((speedscale + dir[0] * length) * (1.0/128), (speedscale + dir[1] * length) * (1.0/128));
858                                 glVertex3fv (vert->v);
859                         }
860                         glEnd ();
861                 }
862                 glDisable(GL_BLEND);
863                 glColor3f(1,1,1);
864                 glDepthMask(1);
865         }
866         else
867         {
868                 glDisable(GL_TEXTURE_2D);
869                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
870                 glColor3fv(fogcolor); // note: gets rendered over by skybox if fog is not enabled
871                 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
872                 {
873                         vert = &skyvert[p->firstvert];
874                         glBegin(GL_POLYGON);
875                         for (j=0 ; j<p->verts ; j++, vert++)
876                                 glVertex3fv (vert->v);
877                         glEnd ();
878                 }
879                 glColor3f(1,1,1);
880                 glEnable(GL_TEXTURE_2D);
881         }
882 }