]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_warp.c
bmodel rotation physics now work
[xonotic/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 extern  model_t *loadmodel;
25
26 int             skytexturenum;
27
28 int             solidskytexture;
29 int             alphaskytexture;
30 float   speedscale;             // for top sky and bottom sky
31
32 msurface_t      *warpface;
33
34 extern cvar_t gl_subdivide_size;
35
36 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 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
178
179 int skyboxside[6];
180
181 char skyname[256];
182
183 // LordHavoc: moved LoadTGA and LoadPCX to gl_draw.c
184
185 /*
186 ==================
187 R_LoadSkyBox
188 ==================
189 */
190 char    *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
191 void R_LoadSkyBox (void)
192 {
193         int             i;
194         char    name[64];
195         byte*   image_rgba;
196
197         for (i=0 ; i<6 ; i++)
198         {
199                 sprintf (name, "env/%s%s", skyname, suf[i]);
200                 if (!(image_rgba = loadimagepixels(name, FALSE, 0, 0)))
201                 {
202                         sprintf (name, "gfx/env/%s%s", skyname, suf[i]);
203                         if (!(image_rgba = loadimagepixels(name, FALSE, 0, 0)))
204                         {
205                                 Con_Printf ("Couldn't load %s\n", name);
206                                 continue;
207                         }
208                 }
209                 skyboxside[i] = GL_LoadTexture(va("skyboxside%d", i), image_width, image_height, image_rgba, false, false, 4);
210                 free (image_rgba);
211         }
212 }
213
214 void R_SetSkyBox (char *sky)
215 {
216         strcpy(skyname, sky);
217         R_LoadSkyBox ();
218 }
219
220 // LordHavoc: added LoadSky console command
221 void LoadSky_f (void)
222 {
223         switch (Cmd_Argc())
224         {
225         case 1:
226                 if (skyname[0])
227                         Con_Printf("current sky: %s\n", skyname);
228                 else
229                         Con_Printf("no skybox has been set\n", skyname);
230                 break;
231         case 2:
232                 R_SetSkyBox(Cmd_Argv(1));
233                 Con_Printf("skybox set to %s\n", skyname);
234                 break;
235         default:
236                 Con_Printf("usage: loadsky skyname\n");
237                 break;
238         }
239 }
240
241 extern cvar_t r_farclip;
242
243 #define R_SkyBoxPolyVec(s,t,x,y,z) \
244         glTexCoord2f((s) * (254.0f/256.0f) + (1.0f/256.0f), (t) * (254.0f/256.0f) + (1.0f/256.0f));\
245         glVertex3f((x) * 1024.0 + r_refdef.vieworg[0], (y) * 1024.0 + r_refdef.vieworg[1], (z) * 1024.0 + r_refdef.vieworg[2]);
246
247 void R_SkyBox()
248 {
249         glDisable (GL_BLEND);
250         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
251         if (lighthalf)
252                 glColor3f(0.5,0.5,0.5);
253         else
254                 glColor3f(1,1,1);
255         glBindTexture(GL_TEXTURE_2D, skyboxside[3]); // front
256         glBegin(GL_QUADS);
257         R_SkyBoxPolyVec(1, 0,  1, -1,  1);
258         R_SkyBoxPolyVec(1, 1,  1, -1, -1);
259         R_SkyBoxPolyVec(0, 1,  1,  1, -1);
260         R_SkyBoxPolyVec(0, 0,  1,  1,  1);
261         glEnd();
262         glBindTexture(GL_TEXTURE_2D, skyboxside[1]); // back
263         glBegin(GL_QUADS);
264         R_SkyBoxPolyVec(1, 0, -1,  1,  1);
265         R_SkyBoxPolyVec(1, 1, -1,  1, -1);
266         R_SkyBoxPolyVec(0, 1, -1, -1, -1);
267         R_SkyBoxPolyVec(0, 0, -1, -1,  1);
268         glEnd();
269         glBindTexture(GL_TEXTURE_2D, skyboxside[0]); // right
270         glBegin(GL_QUADS);
271         R_SkyBoxPolyVec(1, 0,  1,  1,  1);
272         R_SkyBoxPolyVec(1, 1,  1,  1, -1);
273         R_SkyBoxPolyVec(0, 1, -1,  1, -1);
274         R_SkyBoxPolyVec(0, 0, -1,  1,  1);
275         glEnd();
276         glBindTexture(GL_TEXTURE_2D, skyboxside[2]); // left
277         glBegin(GL_QUADS);
278         R_SkyBoxPolyVec(1, 0, -1, -1,  1);
279         R_SkyBoxPolyVec(1, 1, -1, -1, -1);
280         R_SkyBoxPolyVec(0, 1,  1, -1, -1);
281         R_SkyBoxPolyVec(0, 0,  1, -1,  1);
282         glEnd();
283         glBindTexture(GL_TEXTURE_2D, skyboxside[4]); // up
284         glBegin(GL_QUADS);
285         R_SkyBoxPolyVec(1, 0,  1, -1,  1);
286         R_SkyBoxPolyVec(1, 1,  1,  1,  1);
287         R_SkyBoxPolyVec(0, 1, -1,  1,  1);
288         R_SkyBoxPolyVec(0, 0, -1, -1,  1);
289         glEnd();
290         glBindTexture(GL_TEXTURE_2D, skyboxside[5]); // down
291         glBegin(GL_QUADS);
292         R_SkyBoxPolyVec(1, 0,  1,  1, -1);
293         R_SkyBoxPolyVec(1, 1,  1, -1, -1);
294         R_SkyBoxPolyVec(0, 1, -1, -1, -1);
295         R_SkyBoxPolyVec(0, 0, -1,  1, -1);
296         glEnd();
297 }
298
299 /*
300 float skydomeouter[33*33*3];
301 float skydomeinner[33*33*3];
302 unsigned short skydomeindices[32*66];
303 qboolean skydomeinitialized = 0;
304 void skydomecalc(float *dome, float dx, float dy, float dz)
305 {
306         float a, b, x, ax, ay;
307         int i;
308         unsigned short *index;
309         for (a = 0;a <= 1;a += (1.0 / 32.0))
310         {
311                 ax = cos(a * M_PI * 2);
312                 ay = -sin(a * M_PI * 2);
313                 for (b = 0;b <= 1;b += (1.0 / 32.0))
314                 {
315                         x = cos(b * M_PI * 2);
316                         *dome++ = ax*x * dx;
317                         *dome++ = ay*x * dy;
318                         *dome++ = -sin(b * M_PI * 2) * dz;
319                 }
320         }
321         index = skydomeindices;
322         for (i = 0;i < (32*33);i++)
323         {
324                 *index++ = i;
325                 *index++ = i + 33;
326         }
327 }
328
329 extern cvar_t gl_vertexarrays;
330 void skydome(float *source, float s, float texscale)
331 {
332         vec_t vert[33*33][3], tex[33*33][2], *v, *t;
333         int i, j;
334         unsigned short *index;
335         v = &vert[0][0];t = &tex[0][0];
336         for (i = 0;i < (33*33);i++)
337         {
338                 *t++ = source[0] * texscale + s;
339                 *t++ = source[1] * texscale + s;
340                 *v++ = *source++ + r_refdef.vieworg[0];
341                 *v++ = *source++ + r_refdef.vieworg[1];
342                 *v++ = *source++ + r_refdef.vieworg[2];
343         }
344         if (gl_vertexarrays.value)
345         {
346                 qglTexCoordPointer(2, GL_FLOAT, 0, tex);
347                 qglVertexPointer(3, GL_FLOAT, 0, vert);
348                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
349                 glEnableClientState(GL_VERTEX_ARRAY);
350 //              qglInterleavedArrays(GL_T2F_V3F, 0, vert);
351                 for (i = 0;i < (32*66);i+=66)
352                         qglDrawElements(GL_TRIANGLE_STRIP, 66, GL_UNSIGNED_SHORT, &skydomeindices[i]);
353                 glDisableClientState(GL_VERTEX_ARRAY);
354                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
355         }
356         else
357         {
358                 index = skydomeindices;
359                 for (i = 0;i < (32*66);i+=66)
360                 {
361                         glBegin(GL_TRIANGLE_STRIP);
362                         for (j = 0;j < 66;j++)
363                         {
364                                 // Matrox G200 (and possibly G400) drivers don't support TexCoord2fv...
365                                 glTexCoord2f(tex[*index][0], tex[*index][1]);
366                                 glVertex3fv(&vert[*index++][0]);
367                         }
368                         glEnd();
369                 }
370         }
371 }
372
373 void R_SkyDome()
374 {
375         glDisable (GL_BLEND);
376         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
377         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
378         if (lighthalf)
379                 glColor3f(0.5,0.5,0.5);
380         else
381                 glColor3f(1,1,1);
382         glBindTexture(GL_TEXTURE_2D, solidskytexture); // upper clouds
383         if (!skydomeinitialized)
384         {
385                 skydomeinitialized = true;
386                 skydomecalc(skydomeouter, 1024, 1024, 256);
387                 skydomecalc(skydomeinner, 512, 512, 128);
388         }
389         speedscale = realtime*8.0/256.0;
390         speedscale -= (int)speedscale;
391         skydome(skydomeouter, speedscale, 1.0 / 256.0);
392         glEnable (GL_BLEND);
393         glBindTexture(GL_TEXTURE_2D, alphaskytexture); // lower clouds
394         speedscale = realtime*8.0/128.0;
395         speedscale -= (int)speedscale;
396         skydome(skydomeinner, speedscale, 1.0 / 128.0);
397         glDisable (GL_BLEND);
398 }
399 */
400
401 void R_Sky()
402 {
403         if (!r_render.value)
404                 return;
405         if (!skyname[0])
406                 return;
407         glDisable(GL_DEPTH_TEST);
408         glDepthMask(0);
409 //      if (skyname[0])
410                 R_SkyBox();
411 //      else // classic quake sky
412 //              R_SkyDome();
413         glDepthMask(1);
414         glEnable (GL_DEPTH_TEST);
415         glColor3f (1,1,1);
416 }
417
418 //===============================================================
419
420 /*
421 =============
422 R_InitSky
423
424 A sky texture is 256*128, with the right side being a masked overlay
425 ==============
426 */
427 void R_InitSky (byte *src, int bytesperpixel)
428 {
429         int                     i, j, p;
430         unsigned        trans[128*128];
431         unsigned        transpix;
432         int                     r, g, b;
433         unsigned        *rgba;
434
435         if (bytesperpixel == 4)
436         {
437                 for (i = 0;i < 128;i++)
438                         for (j = 0;j < 128;j++)
439                                 trans[(i*128) + j] = src[i*256+j+128];
440         }
441         else
442         {
443                 // make an average value for the back to avoid
444                 // a fringe on the top level
445                 r = g = b = 0;
446                 for (i=0 ; i<128 ; i++)
447                         for (j=0 ; j<128 ; j++)
448                         {
449                                 p = src[i*256 + j + 128];
450                                 rgba = &d_8to24table[p];
451                                 trans[(i*128) + j] = *rgba;
452                                 r += ((byte *)rgba)[0];
453                                 g += ((byte *)rgba)[1];
454                                 b += ((byte *)rgba)[2];
455                         }
456
457                 ((byte *)&transpix)[0] = r/(128*128);
458                 ((byte *)&transpix)[1] = g/(128*128);
459                 ((byte *)&transpix)[2] = b/(128*128);
460                 ((byte *)&transpix)[3] = 0;
461         }
462
463         solidskytexture = GL_LoadTexture ("sky_solidtexture", 128, 128, (byte *) trans, false, false, 4);
464
465         if (bytesperpixel == 4)
466         {
467                 for (i = 0;i < 128;i++)
468                         for (j = 0;j < 128;j++)
469                                 trans[(i*128) + j] = src[i*256+j];
470         }
471         else
472         {
473                 for (i=0 ; i<128 ; i++)
474                         for (j=0 ; j<128 ; j++)
475                         {
476                                 p = src[i*256 + j];
477                                 if (p == 0)
478                                         trans[(i*128) + j] = transpix;
479                                 else
480                                         trans[(i*128) + j] = d_8to24table[p];
481                         }
482         }
483
484         alphaskytexture = GL_LoadTexture ("sky_alphatexture", 128, 128, (byte *) trans, false, true, 4);
485 }
486