2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // gl_warp.c -- sky and water polygons
24 extern model_t *loadmodel;
30 float speedscale; // for top sky and bottom sky
34 extern cvar_t gl_subdivide_size;
36 void BoundPoly (int numverts, float *verts, vec3_t mins, vec3_t maxs)
41 mins[0] = mins[1] = mins[2] = 9999;
42 maxs[0] = maxs[1] = maxs[2] = -9999;
44 for (i=0 ; i<numverts ; i++)
45 for (j=0 ; j<3 ; j++, v++)
54 void SubdividePolygon (int numverts, float *verts)
60 vec3_t front[64], back[64];
68 Sys_Error ("numverts = %i", numverts);
70 BoundPoly (numverts, verts, mins, maxs);
74 m = (mins[i] + maxs[i]) * 0.5;
75 m = gl_subdivide_size.value * floor (m/gl_subdivide_size.value + 0.5);
83 for (j=0 ; j<numverts ; j++, v+= 3)
89 VectorCopy (verts, v);
93 for (j=0 ; j<numverts ; j++, v+= 3)
97 VectorCopy (v, front[f]);
102 VectorCopy (v, back[b]);
105 if (dist[j] == 0 || dist[j+1] == 0)
107 if ( (dist[j] > 0) != (dist[j+1] > 0) )
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]);
118 SubdividePolygon (f, front[0]);
119 SubdividePolygon (b, back[0]);
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)
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;
141 Breaks a polygon up along axial 64 unit
142 boundaries so that turbulent and sky warps
143 can be done reasonably.
146 void GL_SubdivideSurface (msurface_t *fa)
157 // convert edges back to a normal polygon
160 for (i=0 ; i<fa->numedges ; i++)
162 lindex = loadmodel->surfedges[fa->firstedge + i];
165 vec = loadmodel->vertexes[loadmodel->edges[lindex].v[0]].position;
167 vec = loadmodel->vertexes[loadmodel->edges[-lindex].v[1]].position;
168 VectorCopy (vec, verts[numverts]);
172 SubdividePolygon (numverts, verts[0]);
175 //=========================================================
183 // LordHavoc: moved LoadTGA and LoadPCX to gl_draw.c
190 char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
191 void R_LoadSkyBox (void)
197 if (strlen(skyname) >= 1000)
199 Con_Printf ("sky name too long (%i, max is 1000)\n", strlen(skyname));
202 for (i=0 ; i<6 ; i++)
204 sprintf (name, "env/%s%s", skyname, suf[i]);
205 if (!(image_rgba = loadimagepixels(name, FALSE, 0, 0)))
207 sprintf (name, "gfx/env/%s%s", skyname, suf[i]);
208 if (!(image_rgba = loadimagepixels(name, FALSE, 0, 0)))
210 Con_Printf ("Couldn't load %s\n", name);
214 skyboxside[i] = GL_LoadTexture(va("skyboxside%d", i), image_width, image_height, image_rgba, false, false, 4);
219 void R_SetSkyBox (char *sky)
221 strcpy(skyname, sky);
225 // LordHavoc: added LoadSky console command
226 void LoadSky_f (void)
232 Con_Printf("current sky: %s\n", skyname);
234 Con_Printf("no skybox has been set\n", skyname);
237 R_SetSkyBox(Cmd_Argv(1));
238 Con_Printf("skybox set to %s\n", skyname);
241 Con_Printf("usage: loadsky skyname\n");
246 extern cvar_t r_farclip;
248 #define R_SkyBoxPolyVec(s,t,x,y,z) \
249 glTexCoord2f((s) * (254.0f/256.0f) + (1.0f/256.0f), (t) * (254.0f/256.0f) + (1.0f/256.0f));\
250 glVertex3f((x) * 1024.0 + r_refdef.vieworg[0], (y) * 1024.0 + r_refdef.vieworg[1], (z) * 1024.0 + r_refdef.vieworg[2]);
254 glDisable (GL_BLEND);
255 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
257 glColor3f(0.5,0.5,0.5);
260 glBindTexture(GL_TEXTURE_2D, skyboxside[3]); // front
262 R_SkyBoxPolyVec(1, 0, 1, -1, 1);
263 R_SkyBoxPolyVec(1, 1, 1, -1, -1);
264 R_SkyBoxPolyVec(0, 1, 1, 1, -1);
265 R_SkyBoxPolyVec(0, 0, 1, 1, 1);
267 glBindTexture(GL_TEXTURE_2D, skyboxside[1]); // back
269 R_SkyBoxPolyVec(1, 0, -1, 1, 1);
270 R_SkyBoxPolyVec(1, 1, -1, 1, -1);
271 R_SkyBoxPolyVec(0, 1, -1, -1, -1);
272 R_SkyBoxPolyVec(0, 0, -1, -1, 1);
274 glBindTexture(GL_TEXTURE_2D, skyboxside[0]); // right
276 R_SkyBoxPolyVec(1, 0, 1, 1, 1);
277 R_SkyBoxPolyVec(1, 1, 1, 1, -1);
278 R_SkyBoxPolyVec(0, 1, -1, 1, -1);
279 R_SkyBoxPolyVec(0, 0, -1, 1, 1);
281 glBindTexture(GL_TEXTURE_2D, skyboxside[2]); // left
283 R_SkyBoxPolyVec(1, 0, -1, -1, 1);
284 R_SkyBoxPolyVec(1, 1, -1, -1, -1);
285 R_SkyBoxPolyVec(0, 1, 1, -1, -1);
286 R_SkyBoxPolyVec(0, 0, 1, -1, 1);
288 glBindTexture(GL_TEXTURE_2D, skyboxside[4]); // up
290 R_SkyBoxPolyVec(1, 0, 1, -1, 1);
291 R_SkyBoxPolyVec(1, 1, 1, 1, 1);
292 R_SkyBoxPolyVec(0, 1, -1, 1, 1);
293 R_SkyBoxPolyVec(0, 0, -1, -1, 1);
295 glBindTexture(GL_TEXTURE_2D, skyboxside[5]); // down
297 R_SkyBoxPolyVec(1, 0, 1, 1, -1);
298 R_SkyBoxPolyVec(1, 1, 1, -1, -1);
299 R_SkyBoxPolyVec(0, 1, -1, -1, -1);
300 R_SkyBoxPolyVec(0, 0, -1, 1, -1);
305 float skydomeouter[33*33*3];
306 float skydomeinner[33*33*3];
307 unsigned short skydomeindices[32*66];
308 qboolean skydomeinitialized = 0;
309 void skydomecalc(float *dome, float dx, float dy, float dz)
311 float a, b, x, ax, ay;
313 unsigned short *index;
314 for (a = 0;a <= 1;a += (1.0 / 32.0))
316 ax = cos(a * M_PI * 2);
317 ay = -sin(a * M_PI * 2);
318 for (b = 0;b <= 1;b += (1.0 / 32.0))
320 x = cos(b * M_PI * 2);
323 *dome++ = -sin(b * M_PI * 2) * dz;
326 index = skydomeindices;
327 for (i = 0;i < (32*33);i++)
334 extern cvar_t gl_vertexarrays;
335 void skydome(float *source, float s, float texscale)
337 vec_t vert[33*33][3], tex[33*33][2], *v, *t;
339 unsigned short *index;
340 v = &vert[0][0];t = &tex[0][0];
341 for (i = 0;i < (33*33);i++)
343 *t++ = source[0] * texscale + s;
344 *t++ = source[1] * texscale + s;
345 *v++ = *source++ + r_refdef.vieworg[0];
346 *v++ = *source++ + r_refdef.vieworg[1];
347 *v++ = *source++ + r_refdef.vieworg[2];
349 if (gl_vertexarrays.value)
351 qglTexCoordPointer(2, GL_FLOAT, 0, tex);
352 qglVertexPointer(3, GL_FLOAT, 0, vert);
353 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
354 glEnableClientState(GL_VERTEX_ARRAY);
355 // qglInterleavedArrays(GL_T2F_V3F, 0, vert);
356 for (i = 0;i < (32*66);i+=66)
357 qglDrawElements(GL_TRIANGLE_STRIP, 66, GL_UNSIGNED_SHORT, &skydomeindices[i]);
358 glDisableClientState(GL_VERTEX_ARRAY);
359 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
363 index = skydomeindices;
364 for (i = 0;i < (32*66);i+=66)
366 glBegin(GL_TRIANGLE_STRIP);
367 for (j = 0;j < 66;j++)
369 // Matrox G200 (and possibly G400) drivers don't support TexCoord2fv...
370 glTexCoord2f(tex[*index][0], tex[*index][1]);
371 glVertex3fv(&vert[*index++][0]);
380 glDisable (GL_BLEND);
381 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
382 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
384 glColor3f(0.5,0.5,0.5);
387 glBindTexture(GL_TEXTURE_2D, solidskytexture); // upper clouds
388 if (!skydomeinitialized)
390 skydomeinitialized = true;
391 skydomecalc(skydomeouter, 1024, 1024, 256);
392 skydomecalc(skydomeinner, 512, 512, 128);
394 speedscale = realtime*8.0/256.0;
395 speedscale -= (int)speedscale;
396 skydome(skydomeouter, speedscale, 1.0 / 256.0);
398 glBindTexture(GL_TEXTURE_2D, alphaskytexture); // lower clouds
399 speedscale = realtime*8.0/128.0;
400 speedscale -= (int)speedscale;
401 skydome(skydomeinner, speedscale, 1.0 / 128.0);
402 glDisable (GL_BLEND);
412 glDisable(GL_DEPTH_TEST);
416 // else // classic quake sky
419 glEnable (GL_DEPTH_TEST);
423 //===============================================================
429 A sky texture is 256*128, with the right side being a masked overlay
432 void R_InitSky (byte *src, int bytesperpixel)
435 unsigned trans[128*128];
440 if (bytesperpixel == 4)
442 for (i = 0;i < 128;i++)
443 for (j = 0;j < 128;j++)
444 trans[(i*128) + j] = src[i*256+j+128];
448 // make an average value for the back to avoid
449 // a fringe on the top level
451 for (i=0 ; i<128 ; i++)
452 for (j=0 ; j<128 ; j++)
454 p = src[i*256 + j + 128];
455 rgba = &d_8to24table[p];
456 trans[(i*128) + j] = *rgba;
457 r += ((byte *)rgba)[0];
458 g += ((byte *)rgba)[1];
459 b += ((byte *)rgba)[2];
462 ((byte *)&transpix)[0] = r/(128*128);
463 ((byte *)&transpix)[1] = g/(128*128);
464 ((byte *)&transpix)[2] = b/(128*128);
465 ((byte *)&transpix)[3] = 0;
468 solidskytexture = GL_LoadTexture ("sky_solidtexture", 128, 128, (byte *) trans, false, false, 4);
470 if (bytesperpixel == 4)
472 for (i = 0;i < 128;i++)
473 for (j = 0;j < 128;j++)
474 trans[(i*128) + j] = src[i*256+j];
478 for (i=0 ; i<128 ; i++)
479 for (j=0 ; j<128 ; j++)
483 trans[(i*128) + j] = transpix;
485 trans[(i*128) + j] = d_8to24table[p];
489 alphaskytexture = GL_LoadTexture ("sky_alphatexture", 128, 128, (byte *) trans, false, true, 4);