~agrip-hackers/sns/trunk

« back to all changes in this revision

Viewing changes to zq-repo/zquake/vidnull/gl_warp.c

  • Committer: Matthew Tylee Atkinson
  • Date: 2008-03-07 20:15:20 UTC
  • Revision ID: mta@agrip.org.uk-20080307201520-uj9sa2jrytx91b2t
Remove non-SNS components.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (C) 1996-1997 Id Software, Inc.
3
 
 
4
 
This program is free software; you can redistribute it and/or
5
 
modify it under the terms of the GNU General Public License
6
 
as published by the Free Software Foundation; either version 2
7
 
of the License, or (at your option) any later version.
8
 
 
9
 
This program is distributed in the hope that it will be useful,
10
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
 
 
13
 
See the GNU General Public License for more details.
14
 
 
15
 
You should have received a copy of the GNU General Public License
16
 
along with this program; if not, write to the Free Software
17
 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
 
19
 
*/
20
 
// gl_warp.c -- sky and water polygons
21
 
 
22
 
#include "gl_local.h"
23
 
#include "rc_image.h"
24
 
 
25
 
extern  model_t *loadmodel;
26
 
 
27
 
int             solidskytexture;
28
 
int             alphaskytexture;
29
 
float   speedscale;             // for top sky and bottom sky
30
 
 
31
 
qbool   r_skyboxloaded;
32
 
 
33
 
 
34
 
static msurface_t       *warpface;
35
 
 
36
 
static void BoundPoly (int numverts, float *verts, vec3_t mins, vec3_t maxs)
37
 
{
38
 
        /*int           i, j;
39
 
        float   *v;
40
 
 
41
 
        mins[0] = mins[1] = mins[2] = 9999;
42
 
        maxs[0] = maxs[1] = maxs[2] = -9999;
43
 
        v = verts;
44
 
        for (i=0 ; i<numverts ; i++)
45
 
                for (j=0 ; j<3 ; j++, v++)
46
 
                {
47
 
                        if (*v < mins[j])
48
 
                                mins[j] = *v;
49
 
                        if (*v > maxs[j])
50
 
                                maxs[j] = *v;
51
 
                }*/
52
 
}
53
 
 
54
 
static void SubdividePolygon (int numverts, float *verts)
55
 
{
56
 
/*      int             i, j, k;
57
 
        vec3_t  mins, maxs;
58
 
        float   m;
59
 
        float   *v;
60
 
        vec3_t  front[64], back[64];
61
 
        int             f, b;
62
 
        float   dist[64];
63
 
        float   frac;
64
 
        glpoly_t        *poly;
65
 
        float   s, t;
66
 
 
67
 
        if (numverts > 60)
68
 
                Sys_Error ("numverts = %i", numverts);
69
 
 
70
 
        BoundPoly (numverts, verts, mins, maxs);
71
 
 
72
 
        for (i=0 ; i<3 ; i++)
73
 
        {
74
 
                m = (mins[i] + maxs[i]) * 0.5;
75
 
                m = gl_subdivide_size.value * floor (m/gl_subdivide_size.value + 0.5);
76
 
                if (maxs[i] - m < 8)
77
 
                        continue;
78
 
                if (m - mins[i] < 8)
79
 
                        continue;
80
 
 
81
 
                // cut it
82
 
                v = verts + i;
83
 
                for (j=0 ; j<numverts ; j++, v+= 3)
84
 
                        dist[j] = *v - m;
85
 
 
86
 
                // wrap cases
87
 
                dist[j] = dist[0];
88
 
                v-=i;
89
 
                VectorCopy (verts, v);
90
 
 
91
 
                f = b = 0;
92
 
                v = verts;
93
 
                for (j=0 ; j<numverts ; j++, v+= 3)
94
 
                {
95
 
                        if (dist[j] >= 0)
96
 
                        {
97
 
                                VectorCopy (v, front[f]);
98
 
                                f++;
99
 
                        }
100
 
                        if (dist[j] <= 0)
101
 
                        {
102
 
                                VectorCopy (v, back[b]);
103
 
                                b++;
104
 
                        }
105
 
                        if (dist[j] == 0 || dist[j+1] == 0)
106
 
                                continue;
107
 
                        if ( (dist[j] > 0) != (dist[j+1] > 0) )
108
 
                        {
109
 
                                // clip point
110
 
                                frac = dist[j] / (dist[j] - dist[j+1]);
111
 
                                for (k=0 ; k<3 ; k++)
112
 
                                        front[f][k] = back[b][k] = v[k] + frac*(v[3+k] - v[k]);
113
 
                                f++;
114
 
                                b++;
115
 
                        }
116
 
                }
117
 
 
118
 
                SubdividePolygon (f, front[0]);
119
 
                SubdividePolygon (b, back[0]);
120
 
                return;
121
 
        }
122
 
 
123
 
        poly = Hunk_Alloc (sizeof(glpoly_t) + (numverts-4) * VERTEXSIZE*sizeof(float));
124
 
        poly->next = warpface->polys;
125
 
        warpface->polys = poly;
126
 
        poly->numverts = numverts;
127
 
        for (i=0 ; i<numverts ; i++, verts+= 3)
128
 
        {
129
 
                VectorCopy (verts, poly->verts[i]);
130
 
                s = DotProduct (verts, warpface->texinfo->vecs[0]);
131
 
                t = DotProduct (verts, warpface->texinfo->vecs[1]);
132
 
                poly->verts[i][3] = s;
133
 
                poly->verts[i][4] = t;
134
 
        }*/
135
 
}
136
 
 
137
 
/*
138
 
================
139
 
GL_SubdivideSurface
140
 
 
141
 
Breaks a polygon up along axial 64 unit
142
 
boundaries so that turbulent and sky warps
143
 
can be done reasonably.
144
 
================
145
 
*/
146
 
void GL_SubdivideSurface (msurface_t *fa)
147
 
{
148
 
        /*vec3_t                verts[64];
149
 
        int                     numverts;
150
 
        int                     i;
151
 
        int                     lindex;
152
 
        float           *vec;
153
 
 
154
 
        warpface = fa;
155
 
 
156
 
        //
157
 
        // convert edges back to a normal polygon
158
 
        //
159
 
        numverts = 0;
160
 
        for (i=0 ; i<fa->numedges ; i++)
161
 
        {
162
 
                lindex = loadmodel->surfedges[fa->firstedge + i];
163
 
 
164
 
                if (lindex > 0)
165
 
                        vec = loadmodel->vertexes[loadmodel->edges[lindex].v[0]].position;
166
 
                else
167
 
                        vec = loadmodel->vertexes[loadmodel->edges[-lindex].v[1]].position;
168
 
                VectorCopy (vec, verts[numverts]);
169
 
                numverts++;
170
 
        }
171
 
 
172
 
        SubdividePolygon (numverts, verts[0]);*/
173
 
}
174
 
 
175
 
/*
176
 
================
177
 
GL_BuildSkySurfacePoly
178
 
 
179
 
Just build the gl polys, don't subdivide
180
 
================
181
 
*/
182
 
void GL_BuildSkySurfacePolys (msurface_t *fa)
183
 
{
184
 
        /*vec3_t                verts[64];
185
 
        int                     numverts;
186
 
        int                     i;
187
 
        int                     lindex;
188
 
        float           *vec;
189
 
        glpoly_t        *poly;
190
 
        float           *vert;
191
 
 
192
 
        //
193
 
        // convert edges back to a normal polygon
194
 
        //
195
 
        numverts = 0;
196
 
        for (i=0 ; i<fa->numedges ; i++)
197
 
        {
198
 
                lindex = loadmodel->surfedges[fa->firstedge + i];
199
 
 
200
 
                if (lindex > 0)
201
 
                        vec = loadmodel->vertexes[loadmodel->edges[lindex].v[0]].position;
202
 
                else
203
 
                        vec = loadmodel->vertexes[loadmodel->edges[-lindex].v[1]].position;
204
 
                VectorCopy (vec, verts[numverts]);
205
 
                numverts++;
206
 
        }
207
 
 
208
 
        poly = Hunk_Alloc (sizeof(glpoly_t) + (numverts-4) * VERTEXSIZE*sizeof(float));
209
 
        poly->next = NULL;
210
 
        fa->polys = poly;
211
 
        poly->numverts = numverts;
212
 
        vert = verts[0];
213
 
        for (i=0 ; i<numverts ; i++, vert+= 3)
214
 
                VectorCopy (vert, poly->verts[i]);*/
215
 
}
216
 
 
217
 
//=========================================================
218
 
 
219
 
 
220
 
// speed up sin calculations - Ed
221
 
float   turbsin[] =
222
 
{
223
 
        #include "gl_warp_sin.h"
224
 
};
225
 
#define TURBSCALE (256.0 / (2 * M_PI))
226
 
 
227
 
 
228
 
#define TURBWARP_OLD(s, t)      \
229
 
        ((s + turbsin[(int) ((t * 0.125 + r_refdef2.time) * TURBSCALE) & 255]) * 1 / 64.0f)
230
 
 
231
 
#define TURBWARP_NEW(s, t)      \
232
 
        ((s + turbsin[(int) ((t * 2) + r_refdef2.time * TURBSCALE) & 255]) * 1 / 64.0f)
233
 
 
234
 
 
235
 
/*
236
 
=============
237
 
EmitWaterPolys
238
 
 
239
 
Does a water warp on the pre-fragmented glpoly_t chain
240
 
=============
241
 
*/
242
 
void EmitWaterPolys (msurface_t *fa)
243
 
{
244
 
/*      glpoly_t        *p;
245
 
        float           *v;
246
 
        int                     i;
247
 
 
248
 
 
249
 
        for (p=fa->polys ; p ; p=p->next)
250
 
        {
251
 
                glBegin (GL_POLYGON);
252
 
                for (i=0,v=p->verts[0] ; i<p->numverts ; i++, v+=VERTEXSIZE)
253
 
                {
254
 
                        glTexCoord2f (TURBWARP_NEW(v[3], v[4]), TURBWARP_NEW(v[4], v[3]));
255
 
                        glVertex3fv (v);
256
 
                }
257
 
                glEnd ();
258
 
        }*/
259
 
}
260
 
 
261
 
/*
262
 
=============
263
 
EmitSkyPolys
264
 
=============
265
 
*/
266
 
static void EmitSkyPolys (msurface_t *fa)
267
 
{
268
 
/*      glpoly_t        *p;
269
 
        float           *v;
270
 
        int                     i;
271
 
        float   s, t;
272
 
        vec3_t  dir;
273
 
        float   length;
274
 
 
275
 
        for (p=fa->polys ; p ; p=p->next)
276
 
        {
277
 
                glBegin (GL_POLYGON);
278
 
                for (i=0,v=p->verts[0] ; i<p->numverts ; i++, v+=VERTEXSIZE)
279
 
                {
280
 
                        VectorSubtract (v, r_origin, dir);
281
 
                        dir[2] *= 3;    // flatten the sphere
282
 
 
283
 
                        length = VectorLength (dir);
284
 
                        length = 6*63/length;
285
 
 
286
 
                        dir[0] *= length;
287
 
                        dir[1] *= length;
288
 
 
289
 
                        s = (speedscale + dir[0]) * (1.0/128);
290
 
                        t = (speedscale + dir[1]) * (1.0/128);
291
 
 
292
 
                        glTexCoord2f (s, t);
293
 
                        glVertex3fv (v);
294
 
                }
295
 
                glEnd ();
296
 
        }*/
297
 
}
298
 
 
299
 
/*
300
 
=============
301
 
EmitFlatPoly
302
 
=============
303
 
*/
304
 
void EmitFlatPoly (msurface_t *fa)
305
 
{
306
 
/*      glpoly_t        *p;
307
 
        float           *v;
308
 
        int                     i;
309
 
 
310
 
        for (p=fa->polys ; p ; p=p->next)
311
 
        {
312
 
                glBegin (GL_POLYGON);
313
 
                for (i=0,v=p->verts[0] ; i<p->numverts ; i++, v+=VERTEXSIZE)
314
 
                        glVertex3fv (v);
315
 
                glEnd ();
316
 
        }*/
317
 
}
318
 
 
319
 
/*
320
 
===============
321
 
EmitBothSkyLayers
322
 
 
323
 
Does a sky warp on the pre-fragmented glpoly_t chain
324
 
This will be called for brushmodels, the world
325
 
will have them chained together.
326
 
===============
327
 
*/
328
 
void EmitBothSkyLayers (msurface_t *fa)
329
 
{
330
 
/*      GL_DisableMultitexture();
331
 
 
332
 
        if (r_fastsky.value) {
333
 
                glDisable (GL_TEXTURE_2D);
334
 
                glColor3ubv ((byte *)&d_8to24table[(byte)r_skycolor.value]);
335
 
 
336
 
                EmitFlatPoly (fa);
337
 
 
338
 
                glEnable (GL_TEXTURE_2D);
339
 
                glColor3f (1, 1, 1);
340
 
                return;
341
 
        }
342
 
 
343
 
        GL_Bind (solidskytexture);
344
 
        speedscale = r_refdef2.time*8;
345
 
        speedscale -= (int)speedscale & ~127;
346
 
 
347
 
        EmitSkyPolys (fa);
348
 
 
349
 
        glEnable (GL_BLEND);
350
 
        GL_Bind (alphaskytexture);
351
 
        speedscale = r_refdef2.time*16;
352
 
        speedscale -= (int)speedscale & ~127;
353
 
 
354
 
        EmitSkyPolys (fa);
355
 
 
356
 
        glDisable (GL_BLEND);*/
357
 
}
358
 
 
359
 
//===============================================================
360
 
 
361
 
/*
362
 
=============
363
 
R_InitSky
364
 
 
365
 
A sky texture is 256*128, with the right side being a masked overlay
366
 
==============
367
 
*/
368
 
void R_InitSky (texture_t *mt)
369
 
{
370
 
/*      int                     i, j, p;
371
 
        byte            *src;
372
 
        unsigned        trans[128*128];
373
 
        unsigned        transpix;
374
 
        int                     r, g, b;
375
 
        unsigned        *rgba;
376
 
 
377
 
        src = (byte *)mt + mt->offsets[0];
378
 
 
379
 
        // make an average value for the back to avoid
380
 
        // a fringe on the top level
381
 
 
382
 
        r = g = b = 0;
383
 
        for (i=0 ; i<128 ; i++)
384
 
                for (j=0 ; j<128 ; j++)
385
 
                {
386
 
                        p = src[i*256 + j + 128];
387
 
                        rgba = &d_8to24table[p];
388
 
                        trans[(i*128) + j] = *rgba;
389
 
                        r += ((byte *)rgba)[0];
390
 
                        g += ((byte *)rgba)[1];
391
 
                        b += ((byte *)rgba)[2];
392
 
                }
393
 
 
394
 
        ((byte *)&transpix)[0] = r/(128*128);
395
 
        ((byte *)&transpix)[1] = g/(128*128);
396
 
        ((byte *)&transpix)[2] = b/(128*128);
397
 
        ((byte *)&transpix)[3] = 0;
398
 
 
399
 
 
400
 
        if (!solidskytexture)
401
 
                solidskytexture = texture_extension_number++;
402
 
        GL_Bind (solidskytexture );
403
 
        glTexImage2D (GL_TEXTURE_2D, 0, gl_solid_format, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, trans);
404
 
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
405
 
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
406
 
 
407
 
 
408
 
        for (i=0 ; i<128 ; i++)
409
 
                for (j=0 ; j<128 ; j++)
410
 
                {
411
 
                        p = src[i*256 + j];
412
 
                        if (p == 0)
413
 
                                trans[(i*128) + j] = transpix;
414
 
                        else
415
 
                                trans[(i*128) + j] = d_8to24table[p];
416
 
                }
417
 
 
418
 
        if (!alphaskytexture)
419
 
                alphaskytexture = texture_extension_number++;
420
 
        GL_Bind(alphaskytexture);
421
 
        glTexImage2D (GL_TEXTURE_2D, 0, gl_alpha_format, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, trans);
422
 
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
423
 
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);*/
424
 
}
425
 
 
426
 
 
427
 
/*
428
 
=================================================================
429
 
 
430
 
  Quake 2 environment sky
431
 
 
432
 
=================================================================
433
 
*/
434
 
 
435
 
 
436
 
/*
437
 
==================
438
 
R_SetSky
439
 
==================
440
 
*/
441
 
static char     *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
442
 
void R_SetSky (char *name)
443
 
{
444
 
        /*int           i;
445
 
        byte    *pic;
446
 
        int             width, height;
447
 
        char    pathname[MAX_OSPATH];
448
 
 
449
 
        if (!name[0]) {
450
 
                // disable skybox
451
 
                r_skyboxloaded = false;
452
 
                return;
453
 
        }
454
 
 
455
 
        for (i=0 ; i<6 ; i++)
456
 
        {
457
 
                Q_snprintfz (pathname, sizeof(pathname), "env/%s%s.tga", name, suf[i]);
458
 
                LoadTGA (pathname, &pic, &width, &height);
459
 
                if (!pic)
460
 
                {
461
 
                        Com_Printf ("Couldn't load %s\n", pathname);
462
 
                        r_skyboxloaded = false;
463
 
                        return;
464
 
                }
465
 
                if (width > 512 || height > 512)        // just a sanity check
466
 
                {
467
 
                        Com_Printf ("Bad image dimensions in %s\n", pathname);
468
 
                        Q_free (pic);   // Q_malloc'ed by LoadTGA
469
 
                        r_skyboxloaded = false;
470
 
                        return;
471
 
                }
472
 
 
473
 
                // FIXME, scale image down if larger than gl_max_size
474
 
                // We're gonna run into trouble on a Voodoo
475
 
 
476
 
                GL_Bind (skyboxtextures + i);
477
 
                glTexImage2D (GL_TEXTURE_2D, 0, gl_solid_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pic);
478
 
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
479
 
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
480
 
 
481
 
                Q_free (pic);   // Q_malloc'ed by LoadTGA
482
 
        }
483
 
 
484
 
        r_skyboxloaded = true;*/
485
 
}
486
 
 
487
 
 
488
 
static vec3_t   skyclip[6] = {
489
 
        {1,1,0},
490
 
        {1,-1,0},
491
 
        {0,-1,1},
492
 
        {0,1,1},
493
 
        {1,0,1},
494
 
        {-1,0,1} 
495
 
};
496
 
 
497
 
#define skybox_range    2400.0
498
 
 
499
 
// 1 = s, 2 = t, 3 = 2048
500
 
static int      st_to_vec[6][3] =
501
 
{
502
 
        {3,-1,2},
503
 
        {-3,1,2},
504
 
 
505
 
        {1,3,2},
506
 
        {-1,-3,2},
507
 
 
508
 
        {-2,-1,3},              // 0 degrees yaw, look straight up
509
 
        {2,-1,-3}               // look straight down
510
 
 
511
 
//      {-1,2,3},
512
 
//      {1,2,-3}
513
 
};
514
 
 
515
 
// s = [0]/[2], t = [1]/[2]
516
 
static int      vec_to_st[6][3] =
517
 
{
518
 
        {-2,3,1},
519
 
        {2,3,-1},
520
 
 
521
 
        {1,3,2},
522
 
        {-1,3,-2},
523
 
 
524
 
        {-2,-1,3},
525
 
        {-2,1,-3}
526
 
 
527
 
//      {-1,2,3},
528
 
//      {1,2,-3}
529
 
};
530
 
 
531
 
static float    skymins[2][6], skymaxs[2][6];
532
 
 
533
 
static void DrawSkyPolygon (int nump, vec3_t vecs)
534
 
{
535
 
        /*int           i,j;
536
 
        vec3_t  v, av;
537
 
        float   s, t, dv;
538
 
        int             axis;
539
 
        float   *vp;
540
 
 
541
 
        // decide which face it maps to
542
 
        VectorClear (v);
543
 
        for (i=0, vp=vecs ; i<nump ; i++, vp+=3)
544
 
        {
545
 
                VectorAdd (vp, v, v);
546
 
        }
547
 
        av[0] = fabs(v[0]);
548
 
        av[1] = fabs(v[1]);
549
 
        av[2] = fabs(v[2]);
550
 
        if (av[0] > av[1] && av[0] > av[2])
551
 
        {
552
 
                if (v[0] < 0)
553
 
                        axis = 1;
554
 
                else
555
 
                        axis = 0;
556
 
        }
557
 
        else if (av[1] > av[2] && av[1] > av[0])
558
 
        {
559
 
                if (v[1] < 0)
560
 
                        axis = 3;
561
 
                else
562
 
                        axis = 2;
563
 
        }
564
 
        else
565
 
        {
566
 
                if (v[2] < 0)
567
 
                        axis = 5;
568
 
                else
569
 
                        axis = 4;
570
 
        }
571
 
 
572
 
        // project new texture coords
573
 
        for (i=0 ; i<nump ; i++, vecs+=3)
574
 
        {
575
 
                j = vec_to_st[axis][2];
576
 
                if (j > 0)
577
 
                        dv = vecs[j - 1];
578
 
                else
579
 
                        dv = -vecs[-j - 1];
580
 
 
581
 
                j = vec_to_st[axis][0];
582
 
                if (j < 0)
583
 
                        s = -vecs[-j -1] / dv;
584
 
                else
585
 
                        s = vecs[j-1] / dv;
586
 
                j = vec_to_st[axis][1];
587
 
                if (j < 0)
588
 
                        t = -vecs[-j -1] / dv;
589
 
                else
590
 
                        t = vecs[j-1] / dv;
591
 
 
592
 
                if (s < skymins[0][axis])
593
 
                        skymins[0][axis] = s;
594
 
                if (t < skymins[1][axis])
595
 
                        skymins[1][axis] = t;
596
 
                if (s > skymaxs[0][axis])
597
 
                        skymaxs[0][axis] = s;
598
 
                if (t > skymaxs[1][axis])
599
 
                        skymaxs[1][axis] = t;
600
 
        }*/
601
 
}
602
 
 
603
 
#define MAX_CLIP_VERTS  64
604
 
static void ClipSkyPolygon (int nump, vec3_t vecs, int stage)
605
 
{
606
 
        /*float *norm;
607
 
        float   *v;
608
 
        qbool   front, back;
609
 
        float   d, e;
610
 
        float   dists[MAX_CLIP_VERTS];
611
 
        int             sides[MAX_CLIP_VERTS];
612
 
        vec3_t  newv[2][MAX_CLIP_VERTS];
613
 
        int             newc[2];
614
 
        int             i, j;
615
 
 
616
 
        if (nump > MAX_CLIP_VERTS-2)
617
 
                Sys_Error ("ClipSkyPolygon: MAX_CLIP_VERTS");
618
 
        if (stage == 6)
619
 
        {       // fully clipped, so draw it
620
 
                DrawSkyPolygon (nump, vecs);
621
 
                return;
622
 
        }
623
 
 
624
 
        front = back = false;
625
 
        norm = skyclip[stage];
626
 
        for (i=0, v = vecs ; i<nump ; i++, v+=3)
627
 
        {
628
 
                d = DotProduct (v, norm);
629
 
                if (d > ON_EPSILON)
630
 
                {
631
 
                        front = true;
632
 
                        sides[i] = SIDE_FRONT;
633
 
                }
634
 
                else if (d < ON_EPSILON)
635
 
                {
636
 
                        back = true;
637
 
                        sides[i] = SIDE_BACK;
638
 
                }
639
 
                else
640
 
                        sides[i] = SIDE_ON;
641
 
                dists[i] = d;
642
 
        }
643
 
 
644
 
        if (!front || !back)
645
 
        {       // not clipped
646
 
                ClipSkyPolygon (nump, vecs, stage+1);
647
 
                return;
648
 
        }
649
 
 
650
 
        // clip it
651
 
        sides[i] = sides[0];
652
 
        dists[i] = dists[0];
653
 
        VectorCopy (vecs, (vecs+(i*3)) );
654
 
        newc[0] = newc[1] = 0;
655
 
 
656
 
        for (i=0, v = vecs ; i<nump ; i++, v+=3)
657
 
        {
658
 
                switch (sides[i])
659
 
                {
660
 
                case SIDE_FRONT:
661
 
                        VectorCopy (v, newv[0][newc[0]]);
662
 
                        newc[0]++;
663
 
                        break;
664
 
                case SIDE_BACK:
665
 
                        VectorCopy (v, newv[1][newc[1]]);
666
 
                        newc[1]++;
667
 
                        break;
668
 
                case SIDE_ON:
669
 
                        VectorCopy (v, newv[0][newc[0]]);
670
 
                        newc[0]++;
671
 
                        VectorCopy (v, newv[1][newc[1]]);
672
 
                        newc[1]++;
673
 
                        break;
674
 
                }
675
 
 
676
 
                if (sides[i] == SIDE_ON || sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
677
 
                        continue;
678
 
 
679
 
                d = dists[i] / (dists[i] - dists[i+1]);
680
 
                for (j=0 ; j<3 ; j++)
681
 
                {
682
 
                        e = v[j] + d*(v[j+3] - v[j]);
683
 
                        newv[0][newc[0]][j] = e;
684
 
                        newv[1][newc[1]][j] = e;
685
 
                }
686
 
                newc[0]++;
687
 
                newc[1]++;
688
 
        }
689
 
 
690
 
        // continue
691
 
        ClipSkyPolygon (newc[0], newv[0][0], stage+1);
692
 
        ClipSkyPolygon (newc[1], newv[1][0], stage+1);*/
693
 
}
694
 
 
695
 
/*
696
 
=================
697
 
R_AddSkyBoxSurface
698
 
=================
699
 
*/
700
 
void R_AddSkyBoxSurface (msurface_t *fa)
701
 
{
702
 
/*      int             i;
703
 
        vec3_t  verts[MAX_CLIP_VERTS];
704
 
        glpoly_t        *p;
705
 
 
706
 
        // calculate vertex values for sky box
707
 
        for (p=fa->polys ; p ; p=p->next)
708
 
        {
709
 
                for (i=0 ; i<p->numverts ; i++)
710
 
                {
711
 
                        VectorSubtract (p->verts[i], r_origin, verts[i]);
712
 
                }
713
 
                ClipSkyPolygon (p->numverts, verts[0], 0);
714
 
        }*/
715
 
}
716
 
 
717
 
 
718
 
/*
719
 
==============
720
 
R_ClearSky
721
 
==============
722
 
*/
723
 
void R_ClearSky (void)
724
 
{
725
 
/*      int             i;
726
 
 
727
 
        for (i=0 ; i<6 ; i++)
728
 
        {
729
 
                skymins[0][i] = skymins[1][i] = 9999;
730
 
                skymaxs[0][i] = skymaxs[1][i] = -9999;
731
 
        }*/
732
 
}
733
 
 
734
 
static void MakeSkyVec (float s, float t, int axis)
735
 
{
736
 
/*vec3_t                v, b;
737
 
        int                     j, k;
738
 
 
739
 
        b[0] = s*skybox_range;
740
 
        b[1] = t*skybox_range;
741
 
        b[2] = skybox_range;
742
 
 
743
 
        for (j=0 ; j<3 ; j++)
744
 
        {
745
 
                k = st_to_vec[axis][j];
746
 
                if (k < 0)
747
 
                        v[j] = -b[-k - 1];
748
 
                else
749
 
                        v[j] = b[k - 1];
750
 
                v[j] += r_origin[j];
751
 
        }
752
 
 
753
 
        // avoid bilerp seam
754
 
        s = (s+1)*0.5;
755
 
        t = (t+1)*0.5;
756
 
 
757
 
        if (s < 1.0/512)
758
 
                s = 1.0/512;
759
 
        else if (s > 511.0/512)
760
 
                s = 511.0/512;
761
 
        if (t < 1.0/512)
762
 
                t = 1.0/512;
763
 
        else if (t > 511.0/512)
764
 
                t = 511.0/512;
765
 
 
766
 
        t = 1.0 - t;
767
 
        glTexCoord2f (s, t);
768
 
        glVertex3fv (v);*/
769
 
}
770
 
 
771
 
/*
772
 
==============
773
 
R_DrawSkyBox
774
 
==============
775
 
*/
776
 
static int      skytexorder[6] = {0,2,1,3,4,5};
777
 
static void R_DrawSkyBox (void)
778
 
{
779
 
/*      int             i;
780
 
 
781
 
        for (i = 0; i < 6; i++)
782
 
        {
783
 
                if ((skymins[0][i] >= skymaxs[0][i]     || skymins[1][i] >= skymaxs[1][i]))
784
 
                        continue;
785
 
 
786
 
                GL_Bind (skyboxtextures + skytexorder[i]);
787
 
 
788
 
                glBegin (GL_QUADS);
789
 
                MakeSkyVec (skymins[0][i], skymins[1][i], i);
790
 
                MakeSkyVec (skymins[0][i], skymaxs[1][i], i);
791
 
                MakeSkyVec (skymaxs[0][i], skymaxs[1][i], i);
792
 
                MakeSkyVec (skymaxs[0][i], skymins[1][i], i);
793
 
                glEnd ();
794
 
        }*/
795
 
}
796
 
 
797
 
static void EmitSkyVert (vec3_t v)
798
 
{
799
 
/*      vec3_t dir;
800
 
        float   s, t;
801
 
        float   length;
802
 
 
803
 
        VectorSubtract (v, r_origin, dir);
804
 
        dir[2] *= 3;    // flatten the sphere
805
 
 
806
 
        length = VectorLength (dir);
807
 
        length = 6*63/length;
808
 
 
809
 
        dir[0] *= length;
810
 
        dir[1] *= length;
811
 
 
812
 
        s = (speedscale + dir[0]) * (1.0/128);
813
 
        t = (speedscale + dir[1]) * (1.0/128);
814
 
 
815
 
        glTexCoord2f (s, t);
816
 
        glVertex3fv (v);*/
817
 
}
818
 
 
819
 
// s and t range from -1 to 1
820
 
static void MakeSkyVec2 (float s, float t, int axis, vec3_t v)
821
 
{
822
 
        /*vec3_t                b;
823
 
        int                     j, k;
824
 
 
825
 
        b[0] = s*skybox_range;
826
 
        b[1] = t*skybox_range;
827
 
        b[2] = skybox_range;
828
 
 
829
 
        for (j=0 ; j<3 ; j++)
830
 
        {
831
 
                k = st_to_vec[axis][j];
832
 
                if (k < 0)
833
 
                        v[j] = -b[-k - 1];
834
 
                else
835
 
                        v[j] = b[k - 1];
836
 
                v[j] += r_origin[j];
837
 
        }*/
838
 
 
839
 
}
840
 
 
841
 
#define SUBDIVISIONS    10
842
 
 
843
 
static void DrawSkyFace (int axis)
844
 
{
845
 
        /*int i, j;
846
 
        vec3_t  vecs[4];
847
 
        float s, t;
848
 
 
849
 
        float fstep = 2.0 / SUBDIVISIONS;
850
 
 
851
 
        glBegin (GL_QUADS);
852
 
 
853
 
        for (i = 0; i < SUBDIVISIONS; i++)
854
 
        {
855
 
                s = (float)(i*2 - SUBDIVISIONS) / SUBDIVISIONS;
856
 
 
857
 
                if (s + fstep < skymins[0][axis] || s > skymaxs[0][axis])
858
 
                        continue;
859
 
 
860
 
                for (j = 0; j < SUBDIVISIONS; j++) {
861
 
                        t = (float)(j*2 - SUBDIVISIONS) / SUBDIVISIONS;
862
 
 
863
 
                        if (t + fstep < skymins[1][axis] || t > skymaxs[1][axis])
864
 
                                continue;
865
 
 
866
 
                        MakeSkyVec2 (s, t, axis, vecs[0]);
867
 
                        MakeSkyVec2 (s, t + fstep, axis, vecs[1]);
868
 
                        MakeSkyVec2 (s + fstep, t + fstep, axis, vecs[2]);
869
 
                        MakeSkyVec2 (s + fstep, t, axis, vecs[3]);
870
 
 
871
 
                        EmitSkyVert (vecs[0]);
872
 
                        EmitSkyVert (vecs[1]);
873
 
                        EmitSkyVert (vecs[2]);
874
 
                        EmitSkyVert (vecs[3]);
875
 
                }
876
 
        }
877
 
 
878
 
        glEnd ();*/
879
 
}
880
 
 
881
 
 
882
 
static void R_DrawSkyDome (void)
883
 
{
884
 
/*      int i;
885
 
 
886
 
        GL_DisableMultitexture();
887
 
        GL_Bind (solidskytexture);
888
 
 
889
 
        speedscale = r_refdef2.time*8;
890
 
        speedscale -= (int)speedscale & ~127;
891
 
 
892
 
        for (i = 0; i < 6; i++) {
893
 
                if ((skymins[0][i] >= skymaxs[0][i]     || skymins[1][i] >= skymaxs[1][i]))
894
 
                        continue;
895
 
                DrawSkyFace (i);
896
 
        }
897
 
 
898
 
        glEnable (GL_BLEND);
899
 
        GL_Bind (alphaskytexture);
900
 
 
901
 
        speedscale = r_refdef2.time*16;
902
 
        speedscale -= (int)speedscale & ~127;
903
 
 
904
 
        for (i = 0; i < 6; i++) {
905
 
                if ((skymins[0][i] >= skymaxs[0][i]     || skymins[1][i] >= skymaxs[1][i]))
906
 
                        continue;
907
 
                DrawSkyFace (i);
908
 
        }*/
909
 
}
910
 
 
911
 
/*
912
 
==============
913
 
R_DrawSky
914
 
 
915
 
Draw either the classic cloudy quake sky or a skybox
916
 
==============
917
 
*/
918
 
void R_DrawSky (void)
919
 
{
920
 
/*      msurface_t      *fa;
921
 
        qbool           ignore_z;
922
 
        extern msurface_t *skychain;
923
 
 
924
 
        GL_DisableMultitexture ();
925
 
 
926
 
        if (r_fastsky.value) {
927
 
                glDisable (GL_TEXTURE_2D);
928
 
                glColor3ubv ((byte *)&d_8to24table[(byte)r_skycolor.value]);
929
 
                
930
 
                for (fa = skychain; fa; fa = fa->texturechain)
931
 
                        EmitFlatPoly (fa);
932
 
                skychain = NULL;
933
 
 
934
 
                glEnable (GL_TEXTURE_2D);
935
 
                glColor3f (1, 1, 1);
936
 
                return;
937
 
        }
938
 
 
939
 
        if (r_viewleaf->contents == CONTENTS_SOLID) {
940
 
                // always draw if we're in a solid leaf (probably outside the level)
941
 
                // FIXME: we don't really want to add all six planes every time!
942
 
                // FIXME: also handle r_fastsky case
943
 
                int i;
944
 
                for (i = 0; i < 6; i++) {
945
 
                        skymins[0][i] = skymins[1][i] = -1;
946
 
                        skymaxs[0][i] = skymaxs[1][i] = 1;
947
 
                }
948
 
                ignore_z = true;
949
 
        }
950
 
        else {
951
 
                if (!skychain)
952
 
                        return;         // no sky at all
953
 
 
954
 
                // figure out how much of the sky box we need to draw
955
 
                for (fa = skychain; fa; fa = fa->texturechain)
956
 
                        R_AddSkyBoxSurface (fa);
957
 
 
958
 
                ignore_z = false;
959
 
        }
960
 
 
961
 
        // turn off Z tests & writes to avoid problems on large maps
962
 
        glDisable (GL_DEPTH_TEST);
963
 
 
964
 
        // draw a skybox or classic quake clouds
965
 
        if (r_skyboxloaded)
966
 
                R_DrawSkyBox ();
967
 
        else
968
 
                R_DrawSkyDome ();
969
 
 
970
 
        glEnable (GL_DEPTH_TEST);
971
 
 
972
 
        // draw the sky polys into the Z buffer
973
 
        // don't need depth test yet
974
 
        if (!ignore_z) {
975
 
                glDisable(GL_TEXTURE_2D);
976
 
                glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
977
 
 
978
 
                glEnable(GL_BLEND);
979
 
                glBlendFunc(GL_ZERO, GL_ONE);
980
 
 
981
 
                for (fa = skychain; fa; fa = fa->texturechain)
982
 
                        EmitFlatPoly (fa);
983
 
 
984
 
                glEnable (GL_TEXTURE_2D);
985
 
                glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
986
 
                glDisable(GL_BLEND);
987
 
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
988
 
        }
989
 
 
990
 
        skychain = NULL;*/
991
 
}
992
 
 
993
 
/*
994
 
 
995
 
This works better (with gl_ztrick 0) on aerowalk where otherwise the sky
996
 
shows in bright pixels through holes in walls, of which on aerowalk there
997
 
are plenty.
998
 
But problems arise on large maps like q1dm17 where the sky tends
999
 
to hide parts of the level.
1000
 
We could just put the far clip plane further of course...
1001
 
 
1002
 
void R_DrawSky (void)
1003
 
{
1004
 
        msurface_t      *fa;
1005
 
        qbool           ignore_z;
1006
 
        extern cvar_t   gl_ztrick;
1007
 
        extern int              gl_ztrickframe;
1008
 
        extern msurface_t *skychain;
1009
 
 
1010
 
        GL_DisableMultitexture ();
1011
 
 
1012
 
        if (r_fastsky.value) {
1013
 
                glDisable (GL_TEXTURE_2D);
1014
 
                glColor3ubv ((byte *)&d_8to24table[(byte)r_skycolor.value]);
1015
 
                
1016
 
                for (fa = skychain; fa; fa = fa->texturechain)
1017
 
                        EmitFlatPoly (fa);
1018
 
                skychain = NULL;
1019
 
 
1020
 
                glEnable (GL_TEXTURE_2D);
1021
 
                glColor3f (1, 1, 1);
1022
 
                return;
1023
 
        }
1024
 
 
1025
 
        if (r_viewleaf->contents == CONTENTS_SOLID) {
1026
 
                // always draw if we're in a solid leaf (probably outside the level)
1027
 
                // FIXME: we don't really want to add all six planes every time!
1028
 
                int i;
1029
 
                for (i = 0; i < 6; i++) {
1030
 
                        skymins[0][i] = skymins[1][i] = -1;
1031
 
                        skymaxs[0][i] = skymaxs[1][i] = 1;
1032
 
                }
1033
 
                ignore_z = true;
1034
 
        }
1035
 
        else {
1036
 
                if (!skychain)
1037
 
                        return;         // no sky at all
1038
 
 
1039
 
                // figure out how much of the sky box we need to draw
1040
 
                for (fa = skychain; fa; fa = fa->texturechain)
1041
 
                        R_AddSkyBoxSurface (fa);
1042
 
 
1043
 
                ignore_z = false;
1044
 
        }
1045
 
 
1046
 
        // draw the sky polys into the Z buffer
1047
 
        if (!ignore_z) {
1048
 
                glDisable(GL_TEXTURE_2D);
1049
 
                glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1050
 
 
1051
 
                glEnable(GL_BLEND);
1052
 
                glBlendFunc(GL_ZERO, GL_ONE);
1053
 
 
1054
 
                for (fa = skychain; fa; fa = fa->texturechain)
1055
 
                        EmitFlatPoly (fa);
1056
 
 
1057
 
                glEnable (GL_TEXTURE_2D);
1058
 
                glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1059
 
                glDisable(GL_BLEND);
1060
 
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1061
 
        }
1062
 
 
1063
 
        if (!ignore_z) {
1064
 
                // only write portions of the sky behind the Z polygons we rendered
1065
 
                if (gl_ztrick.value && !gl_ztrickframe)
1066
 
                        glDepthFunc (GL_LEQUAL);
1067
 
                else
1068
 
                        glDepthFunc (GL_GEQUAL);
1069
 
 
1070
 
                glDepthMask (GL_FALSE); // don't bother writing Z
1071
 
        }
1072
 
 
1073
 
        // draw a skybox or classic quake clouds
1074
 
        if (r_skyboxloaded)
1075
 
                R_DrawSkyBox ();
1076
 
        else
1077
 
                R_DrawSkyDome ();
1078
 
 
1079
 
        if (!ignore_z) {
1080
 
                // back to normal Z test
1081
 
                if (gl_ztrick.value && !gl_ztrickframe)
1082
 
                        glDepthFunc (GL_GEQUAL);
1083
 
                else
1084
 
                        glDepthFunc (GL_LEQUAL);
1085
 
 
1086
 
                glDepthMask (GL_TRUE);  // re-enable Z writes
1087
 
        }
1088
 
 
1089
 
        skychain = NULL;
1090
 
}
1091
 
*/