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 //=========================================================
179 extern qboolean lighthalf;
185 // LordHavoc: moved LoadTGA and LoadPCX to gl_draw.c
187 extern int image_width, image_height;
189 byte* loadimagepixels (char* filename, qboolean complain, int matchwidth, int matchheight);
195 char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
196 void R_LoadSkyBox (void)
202 for (i=0 ; i<6 ; i++)
204 glBindTexture(GL_TEXTURE_2D, SKY_TEX + i);
205 sprintf (name, "env/%s%s", skyname, suf[i]);
206 if (!(image_rgba = loadimagepixels(name, FALSE, 0, 0)))
208 sprintf (name, "gfx/env/%s%s", skyname, suf[i]);
209 if (!(image_rgba = loadimagepixels(name, FALSE, 0, 0)))
211 Con_Printf ("Couldn't load %s\n", name);
215 glTexImage2D (GL_TEXTURE_2D, 0, gl_solid_format, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_rgba);
217 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
218 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
222 void R_SetSkyBox (char *sky)
224 strcpy(skyname, sky);
228 // LordHavoc: added LoadSky console command
229 void LoadSky_f (void)
235 Con_Printf("current sky: %s\n", skyname);
237 Con_Printf("no skybox has been set\n", skyname);
240 R_SetSkyBox(Cmd_Argv(1));
241 Con_Printf("skybox set to %s\n", skyname);
244 Con_Printf("usage: loadsky skyname\n");
249 extern cvar_t r_skyboxsize;
251 #define R_SkyBoxPolyVec(s,t,x,y,z) \
252 glTexCoord2f((s) * (254.0f/256.0f) + (1.0f/256.0f), (t) * (254.0f/256.0f) + (1.0f/256.0f));\
253 glVertex3f((x) * 1024.0 + r_refdef.vieworg[0], (y) * 1024.0 + r_refdef.vieworg[1], (z) * 1024.0 + r_refdef.vieworg[2]);
257 glDisable (GL_BLEND);
258 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
260 glColor3f(0.5,0.5,0.5);
263 glBindTexture(GL_TEXTURE_2D, SKY_TEX + 3); // front
265 R_SkyBoxPolyVec(1, 0, 1, -1, 1);
266 R_SkyBoxPolyVec(1, 1, 1, -1, -1);
267 R_SkyBoxPolyVec(0, 1, 1, 1, -1);
268 R_SkyBoxPolyVec(0, 0, 1, 1, 1);
270 glBindTexture(GL_TEXTURE_2D, SKY_TEX + 1); // back
272 R_SkyBoxPolyVec(1, 0, -1, 1, 1);
273 R_SkyBoxPolyVec(1, 1, -1, 1, -1);
274 R_SkyBoxPolyVec(0, 1, -1, -1, -1);
275 R_SkyBoxPolyVec(0, 0, -1, -1, 1);
277 glBindTexture(GL_TEXTURE_2D, SKY_TEX + 0); // right
279 R_SkyBoxPolyVec(1, 0, 1, 1, 1);
280 R_SkyBoxPolyVec(1, 1, 1, 1, -1);
281 R_SkyBoxPolyVec(0, 1, -1, 1, -1);
282 R_SkyBoxPolyVec(0, 0, -1, 1, 1);
284 glBindTexture(GL_TEXTURE_2D, SKY_TEX + 2); // left
286 R_SkyBoxPolyVec(1, 0, -1, -1, 1);
287 R_SkyBoxPolyVec(1, 1, -1, -1, -1);
288 R_SkyBoxPolyVec(0, 1, 1, -1, -1);
289 R_SkyBoxPolyVec(0, 0, 1, -1, 1);
291 glBindTexture(GL_TEXTURE_2D, SKY_TEX + 4); // up
293 R_SkyBoxPolyVec(1, 0, 1, -1, 1);
294 R_SkyBoxPolyVec(1, 1, 1, 1, 1);
295 R_SkyBoxPolyVec(0, 1, -1, 1, 1);
296 R_SkyBoxPolyVec(0, 0, -1, -1, 1);
298 glBindTexture(GL_TEXTURE_2D, SKY_TEX + 5); // down
300 R_SkyBoxPolyVec(1, 0, 1, 1, -1);
301 R_SkyBoxPolyVec(1, 1, 1, -1, -1);
302 R_SkyBoxPolyVec(0, 1, -1, -1, -1);
303 R_SkyBoxPolyVec(0, 0, -1, 1, -1);
307 float skydomeouter[33*33*3];
308 float skydomeinner[33*33*3];
309 unsigned short skydomeindices[32*66];
310 qboolean skydomeinitialized = 0;
311 void skydomecalc(float *dome, float dx, float dy, float dz)
313 float a, b, x, ax, ay;
315 unsigned short *index;
316 for (a = 0;a <= 1;a += (1.0 / 32.0))
318 ax = cos(a * M_PI * 2);
319 ay = -sin(a * M_PI * 2);
320 for (b = 0;b <= 1;b += (1.0 / 32.0))
322 x = cos(b * M_PI * 2);
325 *dome++ = -sin(b * M_PI * 2) * dz;
328 index = skydomeindices;
329 for (i = 0;i < (32*33);i++)
336 extern cvar_t gl_vertexarrays;
337 void skydome(float *source, float s, float texscale)
339 vec_t vert[33*33][3], tex[33*33][2], *v, *t;
341 unsigned short *index;
342 v = &vert[0][0];t = &tex[0][0];
343 for (i = 0;i < (33*33);i++)
345 *t++ = source[0] * texscale + s;
346 *t++ = source[1] * texscale + s;
347 *v++ = *source++ + r_refdef.vieworg[0];
348 *v++ = *source++ + r_refdef.vieworg[1];
349 *v++ = *source++ + r_refdef.vieworg[2];
351 if (gl_vertexarrays.value)
353 qglTexCoordPointer(2, GL_FLOAT, 0, tex);
354 qglVertexPointer(3, GL_FLOAT, 0, vert);
355 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
356 glEnableClientState(GL_VERTEX_ARRAY);
357 // qglInterleavedArrays(GL_T2F_V3F, 0, vert);
358 for (i = 0;i < (32*66);i+=66)
359 qglDrawElements(GL_TRIANGLE_STRIP, 66, GL_UNSIGNED_SHORT, &skydomeindices[i]);
360 glDisableClientState(GL_VERTEX_ARRAY);
361 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
365 index = skydomeindices;
366 for (i = 0;i < (32*66);i+=66)
368 glBegin(GL_TRIANGLE_STRIP);
369 for (j = 0;j < 66;j++)
371 // Matrox G200 (and possibly G400) drivers don't support TexCoord2fv...
372 glTexCoord2f(tex[*index][0], tex[*index][1]);
373 glVertex3fv(&vert[*index++][0]);
382 glDisable (GL_BLEND);
383 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
384 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
386 glColor3f(0.5,0.5,0.5);
389 glBindTexture(GL_TEXTURE_2D, solidskytexture); // upper clouds
390 if (!skydomeinitialized)
392 skydomeinitialized = true;
393 skydomecalc(skydomeouter, 1024, 1024, 256);
394 skydomecalc(skydomeinner, 512, 512, 128);
396 speedscale = realtime*8.0/256.0;
397 speedscale -= (int)speedscale;
398 skydome(skydomeouter, speedscale, 1.0 / 256.0);
400 glBindTexture(GL_TEXTURE_2D, alphaskytexture); // lower clouds
401 speedscale = realtime*8.0/128.0;
402 speedscale -= (int)speedscale;
403 skydome(skydomeinner, speedscale, 1.0 / 128.0);
404 glDisable (GL_BLEND);
409 glDisable(GL_DEPTH_TEST);
413 else // classic quake sky
416 glEnable (GL_DEPTH_TEST);
420 //===============================================================
426 A sky texture is 256*128, with the right side being a masked overlay
429 // LordHavoc: changed this for GLQuake
430 void R_InitSky (byte *src, int bytesperpixel) //texture_t *mt)
434 unsigned trans[128*128];
438 extern int skytexturenum;
440 // src = (byte *)mt + mt->offsets[0];
442 if (bytesperpixel == 4)
444 for (i = 0;i < 128;i++)
445 for (j = 0;j < 128;j++)
446 trans[(i*128) + j] = src[i*256+j+128];
450 // make an average value for the back to avoid
451 // a fringe on the top level
453 for (i=0 ; i<128 ; i++)
454 for (j=0 ; j<128 ; j++)
456 p = src[i*256 + j + 128];
457 rgba = &d_8to24table[p];
458 trans[(i*128) + j] = *rgba;
459 r += ((byte *)rgba)[0];
460 g += ((byte *)rgba)[1];
461 b += ((byte *)rgba)[2];
464 ((byte *)&transpix)[0] = r/(128*128);
465 ((byte *)&transpix)[1] = g/(128*128);
466 ((byte *)&transpix)[2] = b/(128*128);
467 ((byte *)&transpix)[3] = 0;
470 if (!solidskytexture)
471 solidskytexture = texture_extension_number++;
474 glBindTexture(GL_TEXTURE_2D, solidskytexture );
475 glTexImage2D (GL_TEXTURE_2D, 0, gl_solid_format, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, trans);
476 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
477 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
481 if (bytesperpixel == 4)
483 for (i = 0;i < 128;i++)
484 for (j = 0;j < 128;j++)
485 trans[(i*128) + j] = src[i*256+j];
489 for (i=0 ; i<128 ; i++)
490 for (j=0 ; j<128 ; j++)
494 trans[(i*128) + j] = transpix;
496 trans[(i*128) + j] = d_8to24table[p];
500 if (!alphaskytexture)
501 alphaskytexture = texture_extension_number++;
504 glBindTexture(GL_TEXTURE_2D, alphaskytexture);
505 glTexImage2D (GL_TEXTURE_2D, 0, gl_alpha_format, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, trans);
506 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
507 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);