5 // FIXME: fix skybox after vid_restart
6 cvar_t r_sky = {CVAR_SAVE, "r_sky", "1", "enables sky rendering (black otherwise)"};
7 cvar_t r_skyscroll1 = {CVAR_SAVE, "r_skyscroll1", "1", "speed at which upper clouds layer scrolls in quake sky"};
8 cvar_t r_skyscroll2 = {CVAR_SAVE, "r_skyscroll2", "2", "speed at which lower clouds layer scrolls in quake sky"};
12 static int skyrendersphere;
13 static int skyrenderbox;
14 static rtexturepool_t *skytexturepool;
15 static char skyname[MAX_QPATH];
17 typedef struct suffixinfo_s
20 qboolean flipx, flipy, flipdiagonal;
23 static const suffixinfo_t suffix[3][6] =
26 {"px", false, false, false},
27 {"nx", false, false, false},
28 {"py", false, false, false},
29 {"ny", false, false, false},
30 {"pz", false, false, false},
31 {"nz", false, false, false}
34 {"posx", false, false, false},
35 {"negx", false, false, false},
36 {"posy", false, false, false},
37 {"negy", false, false, false},
38 {"posz", false, false, false},
39 {"negz", false, false, false}
42 {"rt", false, false, true},
43 {"lf", true, true, true},
44 {"bk", false, true, false},
45 {"ft", true, false, false},
46 {"up", false, false, true},
47 {"dn", false, false, true}
51 static rtexture_t *skyboxside[6];
53 void R_SkyStartFrame(void)
56 skyrendersphere = false;
58 skyrendermasked = false;
59 if (r_sky.integer && !(r_refdef.fogenabled && r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1] < (1.0f / 256.0f)))
61 if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
63 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.solidskytexture)
64 skyrendersphere = true;
65 // for depth-masked sky, render the sky on the first sky surface encountered
67 skyrendermasked = true;
76 void R_UnloadSkyBox(void)
82 R_FreeTexture(skyboxside[i]);
87 int R_LoadSkyBox(void)
90 int indices[4] = {0,1,2,3};
91 char name[MAX_INPUTLINE];
92 unsigned char *image_buffer;
105 if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
107 if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
109 if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
111 if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
116 temp = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
117 Image_CopyMux (temp, image_buffer, image_width, image_height, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, indices);
118 skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, temp, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PRECACHE | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), NULL);
119 Mem_Free(image_buffer);
134 int R_SetSkyBox(const char *sky)
136 if (strcmp(sky, skyname) == 0) // no change
139 if (strlen(sky) > 1000)
141 Con_Printf("sky name too long (%i, max is 1000)\n", (int)strlen(sky));
145 strlcpy(skyname, sky, sizeof(skyname));
147 return R_LoadSkyBox();
150 // LordHavoc: added LoadSky console command
151 void LoadSky_f (void)
157 Con_Printf("current sky: %s\n", skyname);
159 Con_Print("no skybox has been set\n");
162 if (R_SetSkyBox(Cmd_Argv(1)))
165 Con_Printf("skybox set to %s\n", skyname);
167 Con_Print("skybox disabled\n");
170 Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
173 Con_Print("usage: loadsky skyname\n");
178 static const float skyboxvertex3f[6*4*3] =
212 static const float skyboxtexcoord2f[6*4*2] =
246 static const int skyboxelements[6*2*3] =
268 static void R_SkyBox(void)
271 // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
272 GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
273 GL_BlendFunc(GL_ONE, GL_ZERO);
274 GL_CullFace(GL_NONE);
277 GL_PolygonOffset(0, 0);
278 GL_DepthTest(false); // don't modify or read zbuffer
279 R_Mesh_VertexPointer(skyboxvertex3f, 0, 0);
280 R_Mesh_ColorPointer(NULL, 0, 0);
281 R_Mesh_ResetTextureState();
282 R_Mesh_TexCoordPointer(0, 2, skyboxtexcoord2f, 0, 0);
283 R_SetupGenericShader(true);
284 GL_LockArrays(0, 6*4);
285 for (i = 0;i < 6;i++)
287 R_Mesh_TexBind(0, R_GetTexture(skyboxside[i]));
288 R_Mesh_Draw(0, 6*4, 2, skyboxelements + i * 6, 0, 0);
291 if(r_refdef.fogenabled)
293 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
294 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
295 for (i = 0;i < 6;i++)
297 R_Mesh_TexBind(0, 0);
298 R_Mesh_Draw(0, 6*4, 2, skyboxelements + i * 6, 0, 0);
306 #define skygridx1 (skygridx + 1)
307 #define skygridxrecip (1.0f / (skygridx))
309 #define skygridy1 (skygridy + 1)
310 #define skygridyrecip (1.0f / (skygridy))
311 #define skysphere_numverts (skygridx1 * skygridy1)
312 #define skysphere_numtriangles (skygridx * skygridy * 2)
313 static float skysphere_vertex3f[skysphere_numverts * 3];
314 static float skysphere_texcoord2f[skysphere_numverts * 2];
315 static int skysphere_element3i[skysphere_numtriangles * 3];
317 static void skyspherecalc(void)
320 float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
325 vertex3f = skysphere_vertex3f;
326 texcoord2f = skysphere_texcoord2f;
327 for (j = 0;j <= skygridy;j++)
329 a = j * skygridyrecip;
330 ax = cos(a * M_PI * 2);
331 ay = -sin(a * M_PI * 2);
332 for (i = 0;i <= skygridx;i++)
334 b = i * skygridxrecip;
335 x = cos((b + 0.5) * M_PI);
338 v[2] = -sin((b + 0.5) * M_PI) * dz;
339 length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
340 *texcoord2f++ = v[0] * length;
341 *texcoord2f++ = v[1] * length;
347 e = skysphere_element3i;
348 for (j = 0;j < skygridy;j++)
350 for (i = 0;i < skygridx;i++)
352 *e++ = j * skygridx1 + i;
353 *e++ = j * skygridx1 + i + 1;
354 *e++ = (j + 1) * skygridx1 + i;
356 *e++ = j * skygridx1 + i + 1;
357 *e++ = (j + 1) * skygridx1 + i + 1;
358 *e++ = (j + 1) * skygridx1 + i;
363 static void R_SkySphere(void)
366 static qboolean skysphereinitialized = false;
367 matrix4x4_t scroll1matrix, scroll2matrix;
368 if (!skysphereinitialized)
370 skysphereinitialized = true;
374 // wrap the scroll values just to be extra kind to float accuracy
376 // scroll speed for upper layer
377 speedscale = r_refdef.scene.time*r_skyscroll1.value*8.0/128.0;
378 speedscale -= (int)speedscale;
379 Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
380 // scroll speed for lower layer (transparent layer)
381 speedscale = r_refdef.scene.time*r_skyscroll2.value*8.0/128.0;
382 speedscale -= (int)speedscale;
383 Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
385 // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
386 GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
387 GL_BlendFunc(GL_ONE, GL_ZERO);
388 GL_CullFace(GL_NONE);
391 GL_PolygonOffset(0, 0);
392 GL_DepthTest(false); // don't modify or read zbuffer
393 R_Mesh_VertexPointer(skysphere_vertex3f, 0, 0);
394 R_Mesh_ColorPointer(NULL, 0, 0);
395 R_Mesh_ResetTextureState();
396 R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.solidskytexture));
397 R_Mesh_TexCoordPointer(0, 2, skysphere_texcoord2f, 0, 0);
398 R_Mesh_TexMatrix(0, &scroll1matrix);
399 if (r_textureunits.integer >= 2 && r_refdef.view.colorscale == 1)
401 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
402 R_SetupGenericTwoTextureShader(GL_DECAL);
403 R_Mesh_TexBind(1, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
404 R_Mesh_TexCoordPointer(1, 2, skysphere_texcoord2f, 0, 0);
405 R_Mesh_TexMatrix(1, &scroll2matrix);
406 GL_LockArrays(0, skysphere_numverts);
407 R_Mesh_Draw(0, skysphere_numverts, skysphere_numtriangles, skysphere_element3i, 0, 0);
409 R_Mesh_TexBind(1, 0);
414 R_SetupGenericShader(true);
415 GL_LockArrays(0, skysphere_numverts);
416 R_Mesh_Draw(0, skysphere_numverts, skysphere_numtriangles, skysphere_element3i, 0, 0);
418 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
419 R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
420 R_Mesh_TexMatrix(0, &scroll2matrix);
421 GL_LockArrays(0, skysphere_numverts);
422 R_Mesh_Draw(0, skysphere_numverts, skysphere_numtriangles, skysphere_element3i, 0, 0);
426 if(r_refdef.fogenabled)
428 R_SetupGenericShader(false);
429 R_Mesh_TexBind(0, 0);
430 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
431 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
432 GL_LockArrays(0, skysphere_numverts);
433 R_Mesh_Draw(0, skysphere_numverts, skysphere_numtriangles, skysphere_element3i, 0, 0);
440 matrix4x4_t skymatrix;
443 Matrix4x4_CreateTranslate(&skymatrix, r_refdef.view.origin[0], r_refdef.view.origin[1], r_refdef.view.origin[2]);
444 R_Mesh_Matrix(&skymatrix);
447 // this does not modify depth buffer
450 else if (skyrenderbox)
452 // this does not modify depth buffer
455 /* this will be skyroom someday
458 // this modifies the depth buffer so we have to clear it afterward
460 // clear the depthbuffer that was used while rendering the skyroom
461 //GL_Clear(GL_DEPTH_BUFFER_BIT);
470 //===============================================================
472 void R_ResetSkyBox(void)
479 static void r_sky_start(void)
481 skytexturepool = R_AllocTexturePool();
485 static void r_sky_shutdown(void)
488 R_FreeTexturePool(&skytexturepool);
491 static void r_sky_newmap(void)
496 void R_Sky_Init(void)
498 Cmd_AddCommand ("loadsky", &LoadSky_f, "load a skybox by basename (for example loadsky mtnsun_ loads mtnsun_ft.tga and so on)");
499 Cvar_RegisterVariable (&r_sky);
500 Cvar_RegisterVariable (&r_skyscroll1);
501 Cvar_RegisterVariable (&r_skyscroll2);
502 memset(&skyboxside, 0, sizeof(skyboxside));
504 R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);