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 void R_UnloadSkyBox(void)
50 R_FreeTexture(skyboxside[i]);
51 skyboxside[i] = NULL;;
55 void R_LoadSkyBox(void)
65 if (snprintf(name, sizeof(name), "%s_%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
67 if (snprintf(name, sizeof(name), "%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
69 if (snprintf(name, sizeof(name), "env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
71 if (snprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
73 Con_Printf("Couldn't load %s_%s or %s%s or env/%s%s or gfx/env/%s%s\n", skyname, suf[i], skyname, suf[i], skyname, suf[i], skyname, suf[i]);
79 skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, image_rgba, TEXTYPE_RGBA, TEXF_CLAMP | TEXF_PRECACHE, NULL);
84 int R_SetSkyBox(const char *sky)
86 if (strcmp(sky, skyname) == 0) // no change
89 if (strlen(sky) > 1000)
91 Con_Printf("sky name too long (%i, max is 1000)\n", strlen(sky));
95 skyavailable_box = false;
104 if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
106 skyavailable_box = true;
112 // LordHavoc: added LoadSky console command
113 void LoadSky_f (void)
119 Con_Printf("current sky: %s\n", skyname);
121 Con_Print("no skybox has been set\n");
124 if (R_SetSkyBox(Cmd_Argv(1)))
127 Con_Printf("skybox set to %s\n", skyname);
129 Con_Print("skybox disabled\n");
132 Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
135 Con_Print("usage: loadsky skyname\n");
140 float skyboxvertex3f[6*4*3] =
174 float skyboxtexcoord2f[6*4*2] =
208 int skyboxelements[6*2*3] =
230 static void R_SkyBox(void)
234 GL_Color(1, 1, 1, 1);
235 memset(&m, 0, sizeof(m));
236 GL_BlendFunc(GL_ONE, GL_ZERO);
238 GL_DepthTest(false); // don't modify or read zbuffer
239 m.pointer_vertex = skyboxvertex3f;
240 m.pointer_texcoord[0] = skyboxtexcoord2f;
241 for (i = 0;i < 6;i++)
243 m.tex[0] = R_GetTexture(skyboxside[i]);
245 R_Mesh_Draw(6*4, 2, skyboxelements + i * 6);
250 #define skygridx1 (skygridx + 1)
251 #define skygridxrecip (1.0f / (skygridx))
253 #define skygridy1 (skygridy + 1)
254 #define skygridyrecip (1.0f / (skygridy))
255 #define skysphere_numverts (skygridx1 * skygridy1)
256 #define skysphere_numtriangles (skygridx * skygridy * 2)
257 static float skysphere_vertex3f[skysphere_numverts * 3];
258 static float skysphere_texcoord2f[skysphere_numverts * 2];
259 static int skysphere_element3i[skysphere_numtriangles * 3];
261 static void skyspherecalc(void)
264 float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
269 vertex3f = skysphere_vertex3f;
270 texcoord2f = skysphere_texcoord2f;
271 for (j = 0;j <= skygridy;j++)
273 a = j * skygridyrecip;
274 ax = cos(a * M_PI * 2);
275 ay = -sin(a * M_PI * 2);
276 for (i = 0;i <= skygridx;i++)
278 b = i * skygridxrecip;
279 x = cos((b + 0.5) * M_PI);
282 v[2] = -sin((b + 0.5) * M_PI) * dz;
283 length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
284 *texcoord2f++ = v[0] * length;
285 *texcoord2f++ = v[1] * length;
291 e = skysphere_element3i;
292 for (j = 0;j < skygridy;j++)
294 for (i = 0;i < skygridx;i++)
296 *e++ = j * skygridx1 + i;
297 *e++ = j * skygridx1 + i + 1;
298 *e++ = (j + 1) * skygridx1 + i;
300 *e++ = j * skygridx1 + i + 1;
301 *e++ = (j + 1) * skygridx1 + i + 1;
302 *e++ = (j + 1) * skygridx1 + i;
307 static void R_SkySphere(void)
310 static qboolean skysphereinitialized = false;
312 matrix4x4_t scroll1matrix, scroll2matrix, identitymatrix;
313 if (!skysphereinitialized)
315 skysphereinitialized = true;
319 // scroll speed for upper layer
320 speedscale = cl.time*8.0/128.0;
321 // wrap the scroll just to be extra kind to float accuracy
322 speedscale -= (int)speedscale;
324 // scroll the lower cloud layer twice as fast (just like quake did)
325 Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
326 Matrix4x4_CreateTranslate(&scroll2matrix, speedscale * 2, speedscale * 2, 0);
327 Matrix4x4_CreateIdentity(&identitymatrix);
329 GL_Color(1, 1, 1, 1);
330 GL_BlendFunc(GL_ONE, GL_ZERO);
332 GL_DepthTest(false); // don't modify or read zbuffer
333 memset(&m, 0, sizeof(m));
334 m.pointer_vertex = skysphere_vertex3f;
335 m.tex[0] = R_GetTexture(solidskytexture);
336 m.pointer_texcoord[0] = skysphere_texcoord2f;
337 R_Mesh_TextureMatrix(0, &scroll1matrix);
338 if (r_textureunits.integer >= 2)
340 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
341 m.tex[1] = R_GetTexture(alphaskytexture);
342 m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
343 m.pointer_texcoord[1] = skysphere_texcoord2f;
345 R_Mesh_TextureMatrix(1, &scroll2matrix);
346 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
347 R_Mesh_TextureMatrix(1, &identitymatrix);
353 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
355 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
356 m.tex[0] = R_GetTexture(alphaskytexture);
358 R_Mesh_TextureMatrix(0, &scroll2matrix);
359 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
361 R_Mesh_TextureMatrix(0, &identitymatrix);
366 matrix4x4_t skymatrix;
369 Matrix4x4_CreateTranslate(&skymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2]);
370 R_Mesh_Matrix(&skymatrix);
373 // this does not modify depth buffer
376 else if (skyrenderbox)
378 // this does not modify depth buffer
381 /* this will be skyroom someday
384 // this modifies the depth buffer so we have to clear it afterward
386 // clear the depthbuffer that was used while rendering the skyroom
387 //GL_Clear(GL_DEPTH_BUFFER_BIT);
393 //===============================================================
399 A sky texture is 256*128, with the right side being a masked overlay
402 void R_InitSky (qbyte *src, int bytesperpixel)
405 unsigned solidpixels[128*128], alphapixels[128*128];
407 skyavailable_quake = true;
409 // flush skytexturepool so we won't build up a leak from uploading textures multiple times
410 R_FreeTexturePool(&skytexturepool);
411 skytexturepool = R_AllocTexturePool();
412 solidskytexture = NULL;
413 alphaskytexture = NULL;
415 if (bytesperpixel == 4)
417 for (i = 0;i < 128;i++)
419 for (j = 0;j < 128;j++)
421 solidpixels[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
422 alphapixels[(i*128) + j] = ((unsigned *)src)[i*256+j];
428 // make an average value for the back to avoid
429 // a fringe on the top level
438 for (i = 0;i < 128;i++)
440 for (j = 0;j < 128;j++)
442 rgba.i = palette_complete[src[i*256 + j + 128]];
448 rgba.b[0] = r/(128*128);
449 rgba.b[1] = g/(128*128);
450 rgba.b[2] = b/(128*128);
452 for (i = 0;i < 128;i++)
454 for (j = 0;j < 128;j++)
456 solidpixels[(i*128) + j] = palette_complete[src[i*256 + j + 128]];
457 alphapixels[(i*128) + j] = (p = src[i*256 + j]) ? palette_complete[p] : rgba.i;
462 solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) solidpixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
463 alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) alphapixels, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
466 void R_ResetQuakeSky(void)
468 skyavailable_quake = false;
471 void R_ResetSkyBox(void)
473 skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
475 skyavailable_box = false;
478 static void r_sky_start(void)
480 skytexturepool = R_AllocTexturePool();
481 solidskytexture = NULL;
482 alphaskytexture = NULL;
486 static void r_sky_shutdown(void)
489 R_FreeTexturePool(&skytexturepool);
490 solidskytexture = NULL;
491 alphaskytexture = NULL;
494 static void r_sky_newmap(void)
498 void R_Sky_Init(void)
500 Cmd_AddCommand ("loadsky", &LoadSky_f);
501 Cvar_RegisterVariable (&r_sky);
504 R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);