]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_poly.c
updated to version 1.50, build 75.
[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 (currenttranspoly < 1)
372                 return;
373 //      transpolyrenderminmax();
374 //      if (transpolyindices < 1)
375 //              return;
376         // testing
377 //      Con_DPrintf("transpolyrender: %i polys %i infront %i vertices\n", currenttranspoly, transpolyindices, currenttransvert);
378 //      if (transpolyindices >= 2)
379 //              transpolysort();
380         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
381         glEnable(GL_BLEND);
382         glShadeModel(GL_SMOOTH);
383         glDepthMask(0); // disable zbuffer updates
384         if (isG200) // Matrox G200 cards can't handle per pixel alpha
385                 glEnable(GL_ALPHA_TEST);
386         else
387                 glDisable(GL_ALPHA_TEST);
388         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
389         tpolytype = TPOLYTYPE_ALPHA;
390         texnum = -1;
391         /*
392         if (gl_vertexarrays.value)
393         {
394                 // set up the vertex array
395                 qglInterleavedArrays(GL_T2F_C4UB_V3F, 0, transvert);
396                 for (i = 0;i < transpolyindices;i++)
397                 {
398                         p = &transpoly[transpolyindex[i]];
399                         if (p->texnum != texnum || p->transpolytype != tpolytype)
400                         {
401                                 if (p->texnum != texnum)
402                                 {
403                                         texnum = p->texnum;
404                                         glBindTexture(GL_TEXTURE_2D, texnum);
405                                 }
406                                 if (p->transpolytype != tpolytype)
407                                 {
408                                         tpolytype = p->transpolytype;
409                                         if (tpolytype == TPOLYTYPE_ADD) // additive
410                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
411                                         else // alpha
412                                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
413                                 }
414                         }
415                         qglDrawArrays(GL_TRIANGLE_FAN, p->firstvert, p->verts);
416                         if (p->glowtexnum)
417                         {
418                                 texnum = p->glowtexnum; // highly unlikely to match next poly, but...
419                                 glBindTexture(GL_TEXTURE_2D, texnum);
420                                 tpolytype = TPOLYTYPE_ADD; // might match next poly
421                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
422                                 qglDrawArrays(GL_TRIANGLE_FAN, p->firstvert, p->verts);
423                         }
424                 }
425                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
426                 glDisableClientState(GL_COLOR_ARRAY);
427                 glDisableClientState(GL_VERTEX_ARRAY);
428         }
429         else
430         */
431         {
432                 int points = -1;
433                 translistitem *item;
434                 transvert_t *vert;
435                 for (i = 4095;i >= 0;i--)
436                 {
437                         item = translisthash[i];
438                         while (item)
439                         {
440                                 p = item->poly;
441                                 item = item->next;
442                                 if (p->texnum != texnum || p->verts != points || p->transpolytype != tpolytype)
443                                 {
444                                         glEnd();
445                                         if (isG200)
446                                         {
447                                                 if (p->fogtexnum) // alpha
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_TRIANGLE_FAN);
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_TRIANGLE_FAN);
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_TRIANGLE_FAN);
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_TRIANGLE_FAN);
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 extern qboolean isG200;
561
562 void wallpolyclear()
563 {
564         currentwallpoly = currentwallvert = 0;
565 }
566
567 extern qboolean lighthalf;
568 void wallpolyrender()
569 {
570         int i, j, texnum, lighttexnum;
571         wallpoly_t *p;
572         wallvert_t *vert;
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 qboolean isATI;
786
787 extern char skyname[];
788 extern int solidskytexture, alphaskytexture;
789 void skypolyrender()
790 {
791         int i, j;
792         skypoly_t *p;
793         skyvert_t *vert;
794         float length, speedscale;
795         vec3_t dir;
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                 glColor3f(0.5f, 0.5f, 0.5f);
808                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
809                 glEnable(GL_TEXTURE_2D);
810                 glDisable(GL_BLEND);
811                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
812                 glBindTexture(GL_TEXTURE_2D, solidskytexture); // upper clouds
813                 speedscale = realtime*8;
814                 speedscale -= (int)speedscale & ~127 ;
815                 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
816                 {
817                         vert = &skyvert[p->firstvert];
818                         glBegin(GL_POLYGON);
819                         for (j=0 ; j<p->verts ; j++, vert++)
820                         {
821                                 VectorSubtract (vert->v, r_origin, dir);
822                                 dir[2] *= 3;    // flatten the sphere
823
824                                 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
825                                 length = sqrt (length);
826                                 length = 6*63/length;
827
828                                 glTexCoord2f ((speedscale + dir[0] * length) * (1.0/128), (speedscale + dir[1] * length) * (1.0/128));
829                                 glVertex3fv (vert->v);
830                         }
831                         glEnd ();
832                 }
833                 glEnable(GL_BLEND);
834                 glDepthMask(0);
835                 glBindTexture(GL_TEXTURE_2D, alphaskytexture); // lower clouds
836                 speedscale = realtime*16;
837                 speedscale -= (int)speedscale & ~127 ;
838                 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
839                 {
840                         vert = &skyvert[p->firstvert];
841                         glBegin(GL_POLYGON);
842                         for (j=0 ; j<p->verts ; j++, vert++)
843                         {
844                                 VectorSubtract (vert->v, r_origin, dir);
845                                 dir[2] *= 3;    // flatten the sphere
846
847                                 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
848                                 length = sqrt (length);
849                                 length = 6*63/length;
850
851                                 glTexCoord2f ((speedscale + dir[0] * length) * (1.0/128), (speedscale + dir[1] * length) * (1.0/128));
852                                 glVertex3fv (vert->v);
853                         }
854                         glEnd ();
855                 }
856                 glDisable(GL_BLEND);
857                 glColor3f(1,1,1);
858                 glDepthMask(1);
859         }
860         else
861         {
862                 glDisable(GL_TEXTURE_2D);
863                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
864                 glColor3fv(fogcolor); // note: gets rendered over by skybox if fog is not enabled
865                 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
866                 {
867                         vert = &skyvert[p->firstvert];
868                         glBegin(GL_POLYGON);
869                         for (j=0 ; j<p->verts ; j++, vert++)
870                                 glVertex3fv (vert->v);
871                         glEnd ();
872                 }
873                 glColor3f(1,1,1);
874                 glEnable(GL_TEXTURE_2D);
875         }
876 }