3 transvert_t *transvert;
4 transpoly_t *transpoly;
5 unsigned short *transpolyindex;
11 unsigned short currenttranspoly;
12 unsigned short currenttransvert;
13 unsigned short currentwallpoly;
14 unsigned short currentwallvert;
15 unsigned short currentskypoly;
16 unsigned short currentskyvert;
18 cvar_t gl_multitexture = {"gl_multitexture", "1"};
19 cvar_t gl_vertexarrays = {"gl_vertexarrays", "1"};
21 typedef struct translistitem_s
24 struct translistitem_s *next;
28 translistitem translist[MAX_TRANSPOLYS];
29 translistitem *currenttranslist;
31 translistitem *translisthash[4096];
33 float transviewdist; // distance of view origin along the view normal
35 float transreciptable[256];
40 transvert = qmalloc(MAX_TRANSVERTS * sizeof(transvert_t));
41 transpoly = qmalloc(MAX_TRANSPOLYS * sizeof(transpoly_t));
42 transpolyindex = qmalloc(MAX_TRANSPOLYS * sizeof(unsigned short));
43 wallvert = qmalloc(MAX_WALLVERTS * sizeof(wallvert_t));
44 wallpoly = qmalloc(MAX_WALLPOLYS * sizeof(wallpoly_t));
45 skyvert = qmalloc(MAX_SKYVERTS * sizeof(skyvert_t));
46 skypoly = qmalloc(MAX_SKYPOLYS * sizeof(skypoly_t));
47 transreciptable[0] = 0.0f;
48 for (i = 1;i < 256;i++)
49 transreciptable[i] = 1.0f / i;
51 void gl_poly_shutdown()
55 qfree(transpolyindex);
64 Cvar_RegisterVariable (&gl_multitexture);
65 Cvar_RegisterVariable (&gl_vertexarrays);
66 R_RegisterModule("GL_Poly", gl_poly_start, gl_poly_shutdown);
71 currenttranspoly = currenttransvert = 0;
72 currenttranslist = translist;
73 memset(translisthash, 0, sizeof(translisthash));
74 transviewdist = DotProduct(r_refdef.vieworg, vpn);
77 // turned into a #define
79 void transpolybegin(int texnum, int glowtexnum, int fogtexnum, int transpolytype)
81 if (currenttranspoly >= MAX_TRANSPOLYS || currenttransvert >= MAX_TRANSVERTS)
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
93 // turned into a #define
95 void transpolyvert(float x, float y, float z, float s, float t, int r, int g, int b, int a)
98 if (currenttranspoly >= MAX_TRANSPOLYS || currenttransvert >= MAX_TRANSVERTS)
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;
110 transpoly[currenttranspoly].verts++;
116 float center, d, maxdist;
119 if (currenttranspoly >= MAX_TRANSPOLYS || currenttransvert >= MAX_TRANSVERTS)
121 if (transpoly[currenttranspoly].verts < 3) // skip invalid polygons
123 currenttransvert = transpoly[currenttranspoly].firstvert; // reset vert pointer
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++)
130 d = DotProduct(v->v, vpn);
135 maxdist -= transviewdist;
136 if (maxdist < 4.0f) // behind view
138 currenttransvert = transpoly[currenttranspoly].firstvert; // reset vert pointer
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;
151 int transpolyindices;
154 void transpolyrenderminmax()
156 int i, j, k, lastvert;
157 vec_t d, min, max, viewdist, s, average;
160 transpolyindices = 0;
161 viewdist = DotProduct(r_refdef.vieworg, vpn);
162 for (i = 0;i < currenttranspoly;i++)
164 if (transpoly[i].verts < 3) // only process valid polygons
166 min = 1000000;max = -1000000;
167 s = 1.0f / transpoly[i].verts;
168 lastvert = transpoly[i].firstvert + transpoly[i].verts;
170 for (j = transpoly[i].firstvert;j < lastvert;j++)
172 d = DotProduct(transvert[j].v, vpn)-viewdist;
173 if (d < min) min = d;
174 if (d > max) max = d;
177 if (max < 4) // free to check here, so skip polys behind the view
179 transpoly[i].distance = average;
182 transpoly[i].mindistance = min;
183 transpoly[i].maxdistance = max;
184 // calculate normal (eek)
185 VectorSubtract(transvert[transpoly[i].firstvert ].v, transvert[transpoly[i].firstvert+1].v, v1);
186 VectorSubtract(transvert[transpoly[i].firstvert+2].v, transvert[transpoly[i].firstvert+1].v, v2);
189 if (transpoly[i].verts > 3 && fabs(DotProduct(v1, v2)) >= (1.0f - (1.0f / 256.0f))) // colinear edges, find a better triple
191 VectorSubtract(transvert[transpoly[i].firstvert + transpoly[i].verts - 1].v, transvert[transpoly[i].firstvert].v, v1);
192 VectorSubtract(transvert[transpoly[i].firstvert + 1].v, transvert[transpoly[i].firstvert].v, v2);
195 if (fabs(DotProduct(v1, v2)) < (1.0f - (1.0f / 256.0f))) // found a good triple
197 for (k = transpoly[i].firstvert + 2;k < (transpoly[i].firstvert + transpoly[i].verts - 1);k++)
199 VectorSubtract(transvert[k-1].v, transvert[k].v, v1);
200 VectorSubtract(transvert[k+1].v, transvert[k].v, v2);
203 if (fabs(DotProduct(v1, v2)) < (1.0f - (1.0f / 256.0f))) // found a good triple
206 VectorSubtract(transvert[k-1].v, transvert[k].v, v1);
207 VectorSubtract(transvert[transpoly[i].firstvert].v, transvert[k].v, v2);
210 if (fabs(DotProduct(v1, v2)) >= (1.0f - (1.0f / 256.0f))) // no good triples; the polygon is a line, skip it
214 CrossProduct(v1, v2, n);
216 ndist = DotProduct(transvert[transpoly[i].firstvert+1].v, n);
218 for (j = 0;j < transpolyindices;j++)
221 if (transpoly[transpolyindex[j]].mindistance > max)
223 if (transpoly[transpolyindex[j]].maxdistance < min)
225 // hard case, check side
226 for (k = transpoly[transpolyindex[j]].firstvert;k < (transpoly[transpolyindex[j]].firstvert + transpoly[transpolyindex[j]].verts);k++)
227 if (DotProduct(transvert[k].v, n) < ndist)
236 for (j = 0;j < transpolyindices;j++)
237 if (transpoly[transpolyindex[j]].distance < average)
239 for (k = transpolyindices;k > j;k--)
240 transpolyindex[k] = transpolyindex[k-1];
242 transpolyindex[j] = i;
247 // LordHavoc: qsort compare function
248 int transpolyqsort(const void *ia, const void *ib)
252 a = &transpoly[*((unsigned short *)ia)];
253 b = &transpoly[*((unsigned short *)ib)];
255 if (a->mindistance > b->mindistance && a->maxdistance > b->maxdistance)
257 if (a->mindistance < b->mindistance && a->maxdistance < b->maxdistance)
262 // calculate normal (eek)
264 VectorSubtract(transvert[a->firstvert ].v, transvert[a->firstvert+1].v, v1);
265 VectorSubtract(transvert[a->firstvert+2].v, transvert[a->firstvert+1].v, v2);
266 CrossProduct(v1, v2, a->n);
267 VectorNormalize(a->n);
268 a->ndist = DotProduct(transvert[a->firstvert ].v, a->n);
271 for (i = b->firstvert, j = 0;i < (b->firstvert + b->verts);i++)
272 j += DotProduct(transvert[i].v, a->n) < a->ndist; // (1) b is infront of a
274 return -1; // (-1) a is behind b
275 return j == b->verts; // (1) a is infront of b (0) a and b intersect
276 // return (transpoly[*((unsigned short *)ib)].mindistance + transpoly[*((unsigned short *)ib)].maxdistance) - (transpoly[*((unsigned short *)ia)].mindistance + transpoly[*((unsigned short *)ia)].maxdistance);
279 return ((transpoly_t*)ia)->distance - ((transpoly_t*)ib)->distance;
284 int transpolyqsort(const void *ia, const void *ib)
286 return (transpoly[*((unsigned short *)ib)].distance - transpoly[*((unsigned short *)ia)].distance);
291 void transpolyrenderminmax()
294 vec_t d, max, viewdist, average;
295 transpolyindices = 0;
296 viewdist = DotProduct(r_refdef.vieworg, vpn);
297 for (i = 0;i < currenttranspoly;i++)
299 if (transpoly[i].verts < 3) // only process valid polygons
302 lastvert = transpoly[i].firstvert + transpoly[i].verts;
304 for (j = transpoly[i].firstvert;j < lastvert;j++)
306 d = DotProduct(transvert[j].v, vpn)-viewdist;
311 if (max < 4) // free to check here, so skip polys behind the view
313 transpoly[i].distance = average / transpoly[i].verts;
314 transpolyindex[transpolyindices++] = i;
316 qsort(&transpolyindex[0], transpolyindices, sizeof(unsigned short), transpolyqsort);
325 for (i = 1;i < transpolyindices;i++)
328 if (transpoly[transpolyindex[i - 1]].mindistance > transpoly[transpolyindex[i]].mindistance && transpoly[transpolyindex[i - 1]].maxdistance > transpoly[transpolyindex[i]].maxdistance)
329 continue; // previous is behind (no swap)
330 if (transpoly[transpolyindex[i - 1]].mindistance < transpoly[transpolyindex[i]].mindistance && transpoly[transpolyindex[i - 1]].maxdistance < transpoly[transpolyindex[i]].maxdistance)
331 goto swap; // previous is infront (swap)
335 if (!transpoly[transpolyindex[i - 1]].ndist)
337 // calculate normal (eek)
339 VectorSubtract(transvert[transpoly[transpolyindex[i - 1]].firstvert ].v, transvert[transpoly[transpolyindex[i - 1]].firstvert+1].v, v1);
340 VectorSubtract(transvert[transpoly[transpolyindex[i - 1]].firstvert+2].v, transvert[transpoly[transpolyindex[i - 1]].firstvert+1].v, v2);
341 CrossProduct(v1, v2, transpoly[transpolyindex[i - 1]].n);
342 VectorNormalize(transpoly[transpolyindex[i - 1]].n);
343 transpoly[transpolyindex[i - 1]].ndist = DotProduct(transvert[transpoly[transpolyindex[i - 1]].firstvert ].v, transpoly[transpolyindex[i - 1]].n);
345 if (DotProduct(transpoly[transpolyindex[i - 1]].n, vpn) >= 0.0f) // backface
350 for (i = transpoly[transpolyindex[i]].firstvert;i < (transpoly[transpolyindex[i]].firstvert + transpoly[transpolyindex[i]].verts);i++)
351 if (DotProduct(transvert[i].v, transpoly[transpolyindex[i - 1]].n) >= transpoly[transpolyindex[i - 1]].ndist)
352 goto noswap; // previous is behind or they intersect
354 // previous is infront (swap)
355 j = transpolyindex[i];
356 transpolyindex[i] = transpolyindex[i - 1];
357 transpolyindex[i - 1] = j;
366 void transpolyrender()
368 int i, j, tpolytype, texnum;
372 if (currenttranspoly < 1)
374 // transpolyrenderminmax();
375 // if (transpolyindices < 1)
378 // Con_DPrintf("transpolyrender: %i polys %i infront %i vertices\n", currenttranspoly, transpolyindices, currenttransvert);
379 // if (transpolyindices >= 2)
381 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
383 glShadeModel(GL_SMOOTH);
384 glDepthMask(0); // disable zbuffer updates
385 if (isG200) // Matrox G200 cards can't handle per pixel alpha
386 glEnable(GL_ALPHA_TEST);
388 glDisable(GL_ALPHA_TEST);
389 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
390 tpolytype = TPOLYTYPE_ALPHA;
393 if (gl_vertexarrays.value)
395 // set up the vertex array
396 qglInterleavedArrays(GL_T2F_C4UB_V3F, 0, transvert);
397 for (i = 0;i < transpolyindices;i++)
399 p = &transpoly[transpolyindex[i]];
400 if (p->texnum != texnum || p->transpolytype != tpolytype)
402 if (p->texnum != texnum)
405 glBindTexture(GL_TEXTURE_2D, texnum);
407 if (p->transpolytype != tpolytype)
409 tpolytype = p->transpolytype;
410 if (tpolytype == TPOLYTYPE_ADD) // additive
411 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
413 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
416 qglDrawArrays(GL_POLYGON, p->firstvert, p->verts);
419 texnum = p->glowtexnum; // highly unlikely to match next poly, but...
420 glBindTexture(GL_TEXTURE_2D, texnum);
421 tpolytype = TPOLYTYPE_ADD; // might match next poly
422 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
423 qglDrawArrays(GL_POLYGON, p->firstvert, p->verts);
426 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
427 glDisableClientState(GL_COLOR_ARRAY);
428 glDisableClientState(GL_VERTEX_ARRAY);
436 for (i = 4095;i >= 0;i--)
438 item = translisthash[i];
443 if (p->texnum != texnum || p->verts != points || p->transpolytype != tpolytype)
448 if (p->fogtexnum) // alpha
449 glEnable(GL_ALPHA_TEST);
451 glDisable(GL_ALPHA_TEST);
453 if (p->texnum != texnum)
456 glBindTexture(GL_TEXTURE_2D, texnum);
458 if (p->transpolytype != tpolytype)
460 tpolytype = p->transpolytype;
461 if (tpolytype == TPOLYTYPE_ADD) // additive
462 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
464 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
470 glBegin(GL_TRIANGLES);
477 points = -1; // to force a reinit on the next poly
481 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
483 // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
484 glTexCoord2f(vert->s, vert->t);
485 // again, vector version isn't supported I think
486 glColor4ub(vert->r, vert->g, vert->b, vert->a);
487 glVertex3fv(vert->v);
492 texnum = p->glowtexnum; // highly unlikely to match next poly, but...
493 glBindTexture(GL_TEXTURE_2D, texnum);
494 if (tpolytype != TPOLYTYPE_ADD)
496 tpolytype = TPOLYTYPE_ADD; // might match next poly
497 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
501 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
503 glColor4ub(255,255,255,vert->a);
504 // would be 2fv, but windoze Matrox G200 and probably G400 drivers don't support that (dumb...)
505 glTexCoord2f(vert->s, vert->t);
506 glVertex3fv(vert->v);
510 if (fogenabled && p->transpolytype == TPOLYTYPE_ALPHA)
514 points = -1; // to force a reinit on the next poly
515 if (tpolytype != TPOLYTYPE_ALPHA)
517 tpolytype = TPOLYTYPE_ALPHA; // probably matchs next poly
518 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
522 if (texnum != p->fogtexnum) // highly unlikely to match next poly, but...
524 texnum = p->fogtexnum;
525 glBindTexture(GL_TEXTURE_2D, texnum);
528 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
530 VectorSubtract(vert->v, r_refdef.vieworg,diff);
531 glTexCoord2f(vert->s, vert->t);
532 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], vert->a*(1.0f/255.0f)*exp(fogdensity/DotProduct(diff,diff)));
533 glVertex3fv(vert->v);
539 glDisable(GL_TEXTURE_2D);
541 for (j = 0,vert = &transvert[p->firstvert];j < p->verts;j++, vert++)
543 VectorSubtract(vert->v, r_refdef.vieworg,diff);
544 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], vert->a*(1.0f/255.0f)*exp(fogdensity/DotProduct(diff,diff)));
545 glVertex3fv(vert->v);
548 glEnable(GL_TEXTURE_2D);
556 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
557 glDepthMask(1); // enable zbuffer updates
558 glDisable(GL_ALPHA_TEST);
563 currentwallpoly = currentwallvert = 0;
566 void wallpolyrender()
568 int i, j, texnum, lighttexnum;
573 if (currentwallpoly < 1)
575 c_brush_polys += currentwallpoly;
577 //Con_DPrintf("wallpolyrender: %i polys %i vertices\n", currentwallpoly, currentwallvert);
579 gl_multitexture.value = 0;
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);
588 if (r_fullbright.value) // LordHavoc: easy to do fullbright...
590 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
592 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
594 if (p->texnum != texnum)
597 glBindTexture(GL_TEXTURE_2D, texnum);
599 vert = &wallvert[p->firstvert];
601 for (j=0 ; j<p->numverts ; j++, vert++)
603 glTexCoord2f (vert->s, vert->t);
604 glVertex3fv (vert->vert);
609 else if (gl_multitexture.value)
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);
619 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
621 if (p->texnum != texnum || p->lighttexnum != lighttexnum)
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);
630 vert = &wallvert[p->firstvert];
632 for (j=0 ; j<p->numverts ; j++, vert++)
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);
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);
649 // first do the textures
650 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
652 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
654 if (p->texnum != texnum)
657 glBindTexture(GL_TEXTURE_2D, texnum);
659 vert = &wallvert[p->firstvert];
661 for (j=0 ; j<p->numverts ; j++, vert++)
663 glTexCoord2f (vert->s, vert->t);
664 glVertex3fv (vert->vert);
668 // then modulate using the lightmaps
669 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
670 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
673 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
675 if (p->lighttexnum != texnum)
677 texnum = p->lighttexnum;
678 glBindTexture(GL_TEXTURE_2D, texnum);
680 vert = &wallvert[p->firstvert];
682 for (j=0 ; j<p->numverts ; j++, vert++)
684 glTexCoord2f (vert->u, vert->v);
685 glVertex3fv (vert->vert);
690 // switch to additive mode settings
692 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
693 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
695 glDisable(GL_ALPHA_TEST);
696 glShadeModel(GL_SMOOTH);
697 // render vertex lit overlays ontop
699 for (i = 0, p = wallpoly;i < currentwallpoly;i++, p++)
703 for (j = 0,vert = &wallvert[p->firstvert];j < p->numverts;j++, vert++)
704 if (vert->r || vert->g || vert->b)
709 if (p->texnum != texnum)
712 glBindTexture(GL_TEXTURE_2D, texnum);
715 for (j = 0,vert = &wallvert[p->firstvert];j < p->numverts;j++, vert++)
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);
725 // render glow textures
726 glShadeModel(GL_FLAT);
727 glBlendFunc(GL_ONE, GL_ONE);
729 glColor3f(0.5,0.5,0.5);
733 for (i = 0,p = wallpoly;i < currentwallpoly;i++, p++)
737 if (p->glowtexnum != texnum)
739 texnum = p->glowtexnum;
740 glBindTexture(GL_TEXTURE_2D, texnum);
742 vert = &wallvert[p->firstvert];
744 for (j=0 ; j<p->numverts ; j++, vert++)
746 glTexCoord2f (vert->s, vert->t);
747 glVertex3fv (vert->vert);
752 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
753 glShadeModel(GL_SMOOTH);
757 glDisable(GL_TEXTURE_2D);
758 for (i = 0,p = &wallpoly[0];i < currentwallpoly;i++, p++)
760 vert = &wallvert[p->firstvert];
762 for (j=0 ; j<p->numverts ; j++, vert++)
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);
770 glEnable(GL_TEXTURE_2D);
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);
782 currentskypoly = currentskyvert = 0;
785 extern char skyname[];
786 extern int solidskytexture, alphaskytexture;
792 float length, speedscale;
796 if (currentskypoly < 1)
799 // Con_DPrintf("skypolyrender: %i polys %i vertices\n", currentskypoly, currentskyvert);
800 glDisable(GL_ALPHA_TEST);
802 // make sure zbuffer is enabled
803 glEnable(GL_DEPTH_TEST);
805 if (!fogenabled && !skyname[0]) // normal quake sky
808 glColor3f(0.5f, 0.5f, 0.5f);
810 glColor3f(1.0f,1.0f,1.0f);
811 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
812 glEnable(GL_TEXTURE_2D);
814 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
815 glBindTexture(GL_TEXTURE_2D, solidskytexture); // upper clouds
816 speedscale = cl.time*8;
817 speedscale -= (int)speedscale & ~127 ;
818 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
820 vert = &skyvert[p->firstvert];
822 for (j=0 ; j<p->verts ; j++, vert++)
824 VectorSubtract (vert->v, r_origin, dir);
825 dir[2] *= 3; // flatten the sphere
827 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
828 length = sqrt (length);
829 length = 6*63/length;
831 glTexCoord2f ((speedscale + dir[0] * length) * (1.0/128), (speedscale + dir[1] * length) * (1.0/128));
832 glVertex3fv (vert->v);
838 glBindTexture(GL_TEXTURE_2D, alphaskytexture); // lower clouds
839 speedscale = cl.time*16;
840 speedscale -= (int)speedscale & ~127 ;
841 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
843 vert = &skyvert[p->firstvert];
845 for (j=0 ; j<p->verts ; j++, vert++)
847 VectorSubtract (vert->v, r_origin, dir);
848 dir[2] *= 3; // flatten the sphere
850 length = dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2];
851 length = sqrt (length);
852 length = 6*63/length;
854 glTexCoord2f ((speedscale + dir[0] * length) * (1.0/128), (speedscale + dir[1] * length) * (1.0/128));
855 glVertex3fv (vert->v);
865 glDisable(GL_TEXTURE_2D);
866 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
867 glColor3fv(fogcolor); // note: gets rendered over by skybox if fog is not enabled
868 for (i = 0,p = &skypoly[0];i < currentskypoly;i++, p++)
870 vert = &skyvert[p->firstvert];
872 for (j=0 ; j<p->verts ; j++, vert++)
873 glVertex3fv (vert->v);
877 glEnable(GL_TEXTURE_2D);