]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_warp.c
added sky quality and show framerate options to menu
[divverent/darkplaces.git] / gl_warp.c
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 "quakedef.h"
23
24 msurface_t      *warpface;
25
26 void BoundPoly (int numverts, float *verts, vec3_t mins, vec3_t maxs)
27 {
28         int             i, j;
29         float   *v;
30
31         mins[0] = mins[1] = mins[2] = 9999;
32         maxs[0] = maxs[1] = maxs[2] = -9999;
33         v = verts;
34         for (i=0 ; i<numverts ; i++)
35                 for (j=0 ; j<3 ; j++, v++)
36                 {
37                         if (*v < mins[j])
38                                 mins[j] = *v;
39                         if (*v > maxs[j])
40                                 maxs[j] = *v;
41                 }
42 }
43
44 void SubdividePolygon (int numverts, float *verts)
45 {
46         int             i, j, k;
47         vec3_t  mins, maxs;
48         float   m;
49         float   *v;
50         vec3_t  front[64], back[64];
51         int             f, b;
52         float   dist[64];
53         float   frac;
54         glpoly_t        *poly;
55         float   s, t;
56
57         if (numverts > 60)
58                 Sys_Error ("numverts = %i", numverts);
59
60         BoundPoly (numverts, verts, mins, maxs);
61
62         for (i=0 ; i<3 ; i++)
63         {
64                 m = (mins[i] + maxs[i]) * 0.5;
65                 m = gl_subdivide_size.value * floor (m/gl_subdivide_size.value + 0.5);
66                 if (maxs[i] - m < 8)
67                         continue;
68                 if (m - mins[i] < 8)
69                         continue;
70
71                 // cut it
72                 v = verts + i;
73                 for (j=0 ; j<numverts ; j++, v+= 3)
74                         dist[j] = *v - m;
75
76                 // wrap cases
77                 dist[j] = dist[0];
78                 v-=i;
79                 VectorCopy (verts, v);
80
81                 f = b = 0;
82                 v = verts;
83                 for (j=0 ; j<numverts ; j++, v+= 3)
84                 {
85                         if (dist[j] >= 0)
86                         {
87                                 VectorCopy (v, front[f]);
88                                 f++;
89                         }
90                         if (dist[j] <= 0)
91                         {
92                                 VectorCopy (v, back[b]);
93                                 b++;
94                         }
95                         if (dist[j] == 0 || dist[j+1] == 0)
96                                 continue;
97                         if ( (dist[j] > 0) != (dist[j+1] > 0) )
98                         {
99                                 // clip point
100                                 frac = dist[j] / (dist[j] - dist[j+1]);
101                                 for (k=0 ; k<3 ; k++)
102                                         front[f][k] = back[b][k] = v[k] + frac*(v[3+k] - v[k]);
103                                 f++;
104                                 b++;
105                         }
106                 }
107
108                 SubdividePolygon (f, front[0]);
109                 SubdividePolygon (b, back[0]);
110                 return;
111         }
112
113         poly = Hunk_AllocName (sizeof(glpoly_t) + (numverts-4) * VERTEXSIZE*sizeof(float), "surfaces");
114         poly->next = warpface->polys;
115         warpface->polys = poly;
116         poly->numverts = numverts;
117         for (i=0 ; i<numverts ; i++, verts+= 3)
118         {
119                 VectorCopy (verts, poly->verts[i]);
120                 s = DotProduct (verts, warpface->texinfo->vecs[0]);
121                 t = DotProduct (verts, warpface->texinfo->vecs[1]);
122                 poly->verts[i][3] = s;
123                 poly->verts[i][4] = t;
124         }
125 }
126
127 /*
128 ================
129 GL_SubdivideSurface
130
131 Breaks a polygon up along axial 64 unit
132 boundaries so that turbulent and sky warps
133 can be done reasonably.
134 ================
135 */
136 void GL_SubdivideSurface (msurface_t *fa)
137 {
138         vec3_t          verts[64];
139         int                     numverts;
140         int                     i;
141         int                     lindex;
142         float           *vec;
143
144         warpface = fa;
145
146         //
147         // convert edges back to a normal polygon
148         //
149         numverts = 0;
150         for (i=0 ; i<fa->numedges ; i++)
151         {
152                 lindex = loadmodel->surfedges[fa->firstedge + i];
153
154                 if (lindex > 0)
155                         vec = loadmodel->vertexes[loadmodel->edges[lindex].v[0]].position;
156                 else
157                         vec = loadmodel->vertexes[loadmodel->edges[-lindex].v[1]].position;
158                 VectorCopy (vec, verts[numverts]);
159                 numverts++;
160         }
161
162         SubdividePolygon (numverts, verts[0]);
163 }