5 // FIXME: fix skybox after vid_restart
6 cvar_t r_sky = {CVAR_SAVE, "r_sky", "1"};
7 qboolean skyavailable_quake;
8 qboolean skyavailable_box;
12 static rtexture_t *solidskytexture;
13 static rtexture_t *alphaskytexture;
14 static int skyrendersphere;
15 static int skyrenderbox;
16 static rtexturepool_t *skytexturepool;
17 static char skyname[256];
18 static char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
19 static rtexture_t *skyboxside[6];
21 void R_SkyStartFrame(void)
24 skyrendersphere = false;
26 skyrendermasked = false;
27 if (r_sky.integer && !fogenabled)
31 else if (skyavailable_quake)
32 skyrendersphere = true;
33 // for depth-masked sky, render the sky on the first sky surface encountered
35 skyrendermasked = true;
44 int R_SetSkyBox(const char *sky)
50 if (strcmp(sky, skyname) == 0) // no change
53 skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
54 skyavailable_box = false;
60 if (strlen(sky) > 1000)
62 Con_Printf ("sky name too long (%i, max is 1000)\n", strlen(sky));
68 sprintf (name, "env/%s%s", sky, suf[i]);
69 if (!(image_rgba = loadimagepixels(name, false, 0, 0)))
71 sprintf (name, "gfx/env/%s%s", sky, suf[i]);
72 if (!(image_rgba = loadimagepixels(name, false, 0, 0)))
74 Con_Printf ("Couldn't load env/%s%s or gfx/env/%s%s\n", sky, suf[i], sky, suf[i]);
78 skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, image_rgba, TEXTYPE_RGBA, TEXF_CLAMP | TEXF_PRECACHE, NULL);
82 if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
84 skyavailable_box = true;
91 // LordHavoc: added LoadSky console command
98 Con_Printf("current sky: %s\n", skyname);
100 Con_Printf("no skybox has been set\n");
103 if (R_SetSkyBox(Cmd_Argv(1)))
106 Con_Printf("skybox set to %s\n", skyname);
108 Con_Printf("skybox disabled\n");
111 Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
114 Con_Printf("usage: loadsky skyname\n");
119 float skyboxvertex3f[6*4*3] =
153 float skyboxtexcoord2f[6*4*2] =
187 int skyboxelements[6*2*3] =
209 static void R_SkyBox(void)
213 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
214 memset(&m, 0, sizeof(m));
215 GL_BlendFunc(GL_ONE, GL_ZERO);
217 GL_DepthTest(false); // don't modify or read zbuffer
218 GL_VertexPointer(skyboxvertex3f);
219 m.pointer_texcoord[0] = skyboxtexcoord2f;
220 for (i = 0;i < 6;i++)
222 m.tex[0] = R_GetTexture(skyboxside[i]);
223 R_Mesh_State_Texture(&m);
224 R_Mesh_Draw(4, 2, skyboxelements + i * 6);
229 #define skygridx1 (skygridx + 1)
230 #define skygridxrecip (1.0f / (skygridx))
232 #define skygridy1 (skygridy + 1)
233 #define skygridyrecip (1.0f / (skygridy))
234 #define skysphere_numverts (skygridx1 * skygridy1)
235 #define skysphere_numtriangles (skygridx * skygridy * 2)
236 static float skysphere_vertex3f[skysphere_numverts * 3];
237 static float skysphere_texcoord2f[skysphere_numverts * 2];
238 static int skysphere_element3i[skysphere_numtriangles * 3];
240 static void skyspherecalc(void)
243 float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
248 vertex3f = skysphere_vertex3f;
249 texcoord2f = skysphere_texcoord2f;
250 for (j = 0;j <= skygridy;j++)
252 a = j * skygridyrecip;
253 ax = cos(a * M_PI * 2);
254 ay = -sin(a * M_PI * 2);
255 for (i = 0;i <= skygridx;i++)
257 b = i * skygridxrecip;
258 x = cos((b + 0.5) * M_PI);
261 v[2] = -sin((b + 0.5) * M_PI) * dz;
262 length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
263 *texcoord2f++ = v[0] * length;
264 *texcoord2f++ = v[1] * length;
270 e = skysphere_element3i;
271 for (j = 0;j < skygridy;j++)
273 for (i = 0;i < skygridx;i++)
275 *e++ = j * skygridx1 + i;
276 *e++ = j * skygridx1 + i + 1;
277 *e++ = (j + 1) * skygridx1 + i;
279 *e++ = j * skygridx1 + i + 1;
280 *e++ = (j + 1) * skygridx1 + i + 1;
281 *e++ = (j + 1) * skygridx1 + i;
287 static void R_SkySphere(void)
290 static qboolean skysphereinitialized = false;
292 matrix4x4_t scroll1matrix, scroll2matrix, identitymatrix;
293 if (!skysphereinitialized)
295 skysphereinitialized = true;
299 // scroll speed for upper layer
300 speedscale = cl.time*8.0/128.0;
301 // wrap the scroll just to be extra kind to float accuracy
302 speedscale -= (int)speedscale;
304 // scroll the lower cloud layer twice as fast (just like quake did)
305 Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
306 Matrix4x4_CreateTranslate(&scroll2matrix, speedscale * 2, speedscale * 2, 0);
307 Matrix4x4_CreateIdentity(&identitymatrix);
309 GL_VertexPointer(skysphere_vertex3f);
310 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
311 GL_BlendFunc(GL_ONE, GL_ZERO);
313 GL_DepthTest(false); // don't modify or read zbuffer
314 memset(&m, 0, sizeof(m));
315 m.tex[0] = R_GetTexture(solidskytexture);
316 m.pointer_texcoord[0] = skysphere_texcoord2f;
317 R_Mesh_TextureMatrix(0, &scroll1matrix);
318 if (r_colorscale == 1 && r_textureunits.integer >= 2)
320 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
321 // LordHavoc: note that color is not set here because it does not
322 // matter with GL_REPLACE
323 m.tex[1] = R_GetTexture(alphaskytexture);
324 m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
325 m.pointer_texcoord[1] = skysphere_texcoord2f;
326 R_Mesh_State_Texture(&m);
327 R_Mesh_TextureMatrix(1, &scroll2matrix);
328 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
329 R_Mesh_TextureMatrix(1, &identitymatrix);
334 R_Mesh_State_Texture(&m);
335 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
337 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
338 m.tex[0] = R_GetTexture(alphaskytexture);
339 R_Mesh_State_Texture(&m);
340 R_Mesh_TextureMatrix(0, &scroll2matrix);
341 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
343 R_Mesh_TextureMatrix(0, &identitymatrix);
348 matrix4x4_t skymatrix;
351 Matrix4x4_CreateTranslate(&skymatrix, r_origin[0], r_origin[1], r_origin[2]);
352 R_Mesh_Matrix(&skymatrix);
355 // this does not modify depth buffer
358 else if (skyrenderbox)
360 // this does not modify depth buffer
363 /* this will be skyroom someday
366 // this modifies the depth buffer so we have to clear it afterward
368 // clear the depthbuffer that was used while rendering the skyroom
369 //qglClear(GL_DEPTH_BUFFER_BIT);
375 //===============================================================
381 A sky texture is 256*128, with the right side being a masked overlay
384 void R_InitSky (qbyte *src, int bytesperpixel)
386 int i, j, p, r, g, b;
387 qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
388 unsigned trans[128*128], transpix, *rgba;
390 skyavailable_quake = true;
392 // flush skytexturepool so we won't build up a leak from uploading textures multiple times
393 R_FreeTexturePool(&skytexturepool);
394 skytexturepool = R_AllocTexturePool();
395 solidskytexture = NULL;
396 alphaskytexture = NULL;
398 if (bytesperpixel == 4)
400 for (i = 0;i < 128;i++)
401 for (j = 0;j < 128;j++)
402 trans[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
406 // make an average value for the back to avoid
407 // a fringe on the top level
409 for (i=0 ; i<128 ; i++)
411 for (j=0 ; j<128 ; j++)
413 p = src[i*256 + j + 128];
414 rgba = &palette_complete[p];
415 trans[(i*128) + j] = *rgba;
416 r += ((qbyte *)rgba)[0];
417 g += ((qbyte *)rgba)[1];
418 b += ((qbyte *)rgba)[2];
422 ((qbyte *)&transpix)[0] = r/(128*128);
423 ((qbyte *)&transpix)[1] = g/(128*128);
424 ((qbyte *)&transpix)[2] = b/(128*128);
425 ((qbyte *)&transpix)[3] = 0;
428 memcpy(skyupperlayerpixels, trans, 128*128*4);
430 solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
432 if (bytesperpixel == 4)
434 for (i = 0;i < 128;i++)
435 for (j = 0;j < 128;j++)
436 trans[(i*128) + j] = ((unsigned *)src)[i*256+j];
440 for (i=0 ; i<128 ; i++)
442 for (j=0 ; j<128 ; j++)
446 trans[(i*128) + j] = transpix;
448 trans[(i*128) + j] = palette_complete[p];
453 memcpy(skylowerlayerpixels, trans, 128*128*4);
455 alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
458 void R_ResetQuakeSky(void)
460 skyavailable_quake = false;
463 void R_ResetSkyBox(void)
465 skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
467 skyavailable_box = false;
470 static void r_sky_start(void)
472 skytexturepool = R_AllocTexturePool();
473 solidskytexture = NULL;
474 alphaskytexture = NULL;
477 static void r_sky_shutdown(void)
479 R_FreeTexturePool(&skytexturepool);
480 solidskytexture = NULL;
481 alphaskytexture = NULL;
484 static void r_sky_newmap(void)
488 void R_Sky_Init(void)
490 Cmd_AddCommand ("loadsky", &LoadSky_f);
491 Cvar_RegisterVariable (&r_sky);
492 R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);