5 // FIXME: fix skybox after vid_restart
6 cvar_t r_sky = {CVAR_CLIENT | CVAR_SAVE, "r_sky", "1", "enables sky rendering (black otherwise)"};
7 cvar_t r_skyscroll1 = {CVAR_CLIENT | CVAR_SAVE, "r_skyscroll1", "1", "speed at which upper clouds layer scrolls in quake sky"};
8 cvar_t r_skyscroll2 = {CVAR_CLIENT | CVAR_SAVE, "r_skyscroll2", "2", "speed at which lower clouds layer scrolls in quake sky"};
9 cvar_t r_sky_scissor = {CVAR_CLIENT, "r_sky_scissor", "1", "limit rendering of sky to approximately the area of the sky surfaces"};
14 static int skyrendersphere;
15 static int skyrenderbox;
16 static rtexturepool_t *skytexturepool;
17 static char skyname[MAX_QPATH];
18 static matrix4x4_t skymatrix;
19 static matrix4x4_t skyinversematrix;
21 typedef struct suffixinfo_s
24 qboolean flipx, flipy, flipdiagonal;
27 static const suffixinfo_t suffix[3][6] =
30 {"px", false, false, false},
31 {"nx", false, false, false},
32 {"py", false, false, false},
33 {"ny", false, false, false},
34 {"pz", false, false, false},
35 {"nz", false, false, false}
38 {"posx", false, false, false},
39 {"negx", false, false, false},
40 {"posy", false, false, false},
41 {"negy", false, false, false},
42 {"posz", false, false, false},
43 {"negz", false, false, false}
46 {"rt", false, false, true},
47 {"lf", true, true, true},
48 {"bk", false, true, false},
49 {"ft", true, false, false},
50 {"up", false, false, true},
51 {"dn", false, false, true}
55 static skinframe_t *skyboxskinframe[6];
57 void R_SkyStartFrame(void)
59 skyrendersphere = false;
61 skyrendermasked = false;
62 // for depth-masked sky, we need to know whether any sky was rendered
63 skyrenderlater = false;
64 // we can scissor the sky to just the relevant area
65 Vector4Clear(skyscissor);
68 if (skyboxskinframe[0] || skyboxskinframe[1] || skyboxskinframe[2] || skyboxskinframe[3] || skyboxskinframe[4] || skyboxskinframe[5])
70 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.solidskyskinframe)
71 skyrendersphere = true;
72 skyrendermasked = true;
81 static void R_UnloadSkyBox(void)
87 if (skyboxskinframe[i])
89 R_SkinFrame_PurgeSkinFrame(skyboxskinframe[i]);
92 skyboxskinframe[i] = NULL;
94 if (c && developer_loading.integer)
95 Con_Printf("unloading skybox\n");
98 static int R_LoadSkyBox(void)
101 int indices[4] = {0,1,2,3};
102 char name[MAX_INPUTLINE];
103 unsigned char *image_buffer;
117 if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
119 if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
121 if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
123 if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
128 temp = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
129 Image_CopyMux (temp, image_buffer, image_width, image_height, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, indices);
130 skyboxskinframe[i] = R_SkinFrame_LoadInternalBGRA(va(vabuf, sizeof(vabuf), "skyboxside%d", i), TEXF_CLAMP | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), temp, image_width, image_height, 0, 0, 0, vid.sRGB3D);
131 Mem_Free(image_buffer);
143 if (developer_loading.integer)
144 Con_Printf("loading skybox \"%s\"\n", name);
149 int R_SetSkyBox(const char *sky)
151 if (strcmp(sky, skyname) == 0) // no change
154 if (strlen(sky) > 1000)
156 Con_Printf("sky name too long (%i, max is 1000)\n", (int)strlen(sky));
160 strlcpy(skyname, sky, sizeof(skyname));
162 return R_LoadSkyBox();
165 // LadyHavoc: added LoadSky console command
166 static void LoadSky_f(cmd_state_t *cmd)
168 switch (Cmd_Argc(cmd))
172 Con_Printf("current sky: %s\n", skyname);
174 Con_Print("no skybox has been set\n");
177 if (R_SetSkyBox(Cmd_Argv(cmd, 1)))
180 Con_Printf("skybox set to %s\n", skyname);
182 Con_Print("skybox disabled\n");
185 Con_Errorf("failed to load skybox %s\n", Cmd_Argv(cmd, 1));
188 Con_Print("usage: loadsky skyname\n");
193 static const float skyboxvertex3f[6*4*3] =
227 static const float skyboxtexcoord2f[6*4*2] =
261 static const int skyboxelement3i[6*2*3] =
283 static const unsigned short skyboxelement3s[6*2*3] =
305 static void R_SkyBox(void)
308 RSurf_ActiveCustomEntity(&skymatrix, &skyinversematrix, 0, 0, 1, 1, 1, 1, 6*4, skyboxvertex3f, skyboxtexcoord2f, NULL, NULL, NULL, NULL, 6*2, skyboxelement3i, skyboxelement3s, false, false);
309 for (i = 0;i < 6;i++)
310 if(skyboxskinframe[i])
311 R_DrawCustomSurface(skyboxskinframe[i], &identitymatrix, MATERIALFLAG_SKY | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST, i*4, 4, i*2, 2, false, false, false);
315 #define skygridx1 (skygridx + 1)
316 #define skygridxrecip (1.0f / (skygridx))
318 #define skygridy1 (skygridy + 1)
319 #define skygridyrecip (1.0f / (skygridy))
320 #define skysphere_numverts (skygridx1 * skygridy1)
321 #define skysphere_numtriangles (skygridx * skygridy * 2)
322 static float skysphere_vertex3f[skysphere_numverts * 3];
323 static float skysphere_texcoord2f[skysphere_numverts * 2];
324 static int skysphere_element3i[skysphere_numtriangles * 3];
325 static unsigned short skysphere_element3s[skysphere_numtriangles * 3];
327 static void skyspherecalc(void)
331 float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
336 vertex3f = skysphere_vertex3f;
337 texcoord2f = skysphere_texcoord2f;
338 for (j = 0;j <= skygridy;j++)
340 a = j * skygridyrecip;
341 ax = cos(a * M_PI * 2);
342 ay = -sin(a * M_PI * 2);
343 for (i = 0;i <= skygridx;i++)
345 b = i * skygridxrecip;
346 x = cos((b + 0.5) * M_PI);
349 v[2] = -sin((b + 0.5) * M_PI) * dz;
350 length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
351 *texcoord2f++ = v[0] * length;
352 *texcoord2f++ = v[1] * length;
358 e = skysphere_element3s;
359 for (j = 0;j < skygridy;j++)
361 for (i = 0;i < skygridx;i++)
363 *e++ = j * skygridx1 + i;
364 *e++ = j * skygridx1 + i + 1;
365 *e++ = (j + 1) * skygridx1 + i;
367 *e++ = j * skygridx1 + i + 1;
368 *e++ = (j + 1) * skygridx1 + i + 1;
369 *e++ = (j + 1) * skygridx1 + i;
372 for (i = 0;i < skysphere_numtriangles*3;i++)
373 skysphere_element3i[i] = skysphere_element3s[i];
376 static void R_SkySphere(void)
379 static qboolean skysphereinitialized = false;
380 matrix4x4_t scroll1matrix, scroll2matrix;
381 if (!skysphereinitialized)
383 skysphereinitialized = true;
387 // wrap the scroll values just to be extra kind to float accuracy
389 // scroll speed for upper layer
390 speedscale = r_refdef.scene.time*r_skyscroll1.value*8.0/128.0;
391 speedscale -= floor(speedscale);
392 Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
393 // scroll speed for lower layer (transparent layer)
394 speedscale = r_refdef.scene.time*r_skyscroll2.value*8.0/128.0;
395 speedscale -= floor(speedscale);
396 Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
398 RSurf_ActiveCustomEntity(&skymatrix, &skyinversematrix, 0, 0, 1, 1, 1, 1, skysphere_numverts, skysphere_vertex3f, skysphere_texcoord2f, NULL, NULL, NULL, NULL, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, false, false);
399 R_DrawCustomSurface(r_refdef.scene.worldmodel->brush.solidskyskinframe, &scroll1matrix, MATERIALFLAG_SKY | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST , 0, skysphere_numverts, 0, skysphere_numtriangles, false, false, false);
400 R_DrawCustomSurface(r_refdef.scene.worldmodel->brush.alphaskyskinframe, &scroll2matrix, MATERIALFLAG_SKY | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED, 0, skysphere_numverts, 0, skysphere_numtriangles, false, false, false);
405 Matrix4x4_CreateFromQuakeEntity(&skymatrix, r_refdef.view.origin[0], r_refdef.view.origin[1], r_refdef.view.origin[2], 0, 0, 0, r_refdef.farclip * (0.5f / 16.0f));
406 Matrix4x4_Invert_Simple(&skyinversematrix, &skymatrix);
408 if (r_sky_scissor.integer)
410 // if the scissor is empty just return
411 if (skyscissor[2] == 0 || skyscissor[3] == 0)
413 GL_Scissor(skyscissor[0], skyscissor[1], skyscissor[2], skyscissor[3]);
414 GL_ScissorTest(true);
418 // this does not modify depth buffer
421 else if (skyrenderbox)
423 // this does not modify depth buffer
426 /* this will be skyroom someday
429 // this modifies the depth buffer so we have to clear it afterward
431 // clear the depthbuffer that was used while rendering the skyroom
432 //GL_Clear(GL_DEPTH_BUFFER_BIT);
435 GL_Scissor(0, 0, r_fb.screentexturewidth, r_fb.screentextureheight);
438 //===============================================================
440 void R_ResetSkyBox(void)
443 memset(skyname,0,MAX_QPATH);
447 static void r_sky_start(void)
449 skytexturepool = R_AllocTexturePool();
453 static void r_sky_shutdown(void)
456 R_FreeTexturePool(&skytexturepool);
459 static void r_sky_newmap(void)
464 void R_Sky_Init(void)
466 Cmd_AddCommand(CMD_CLIENT, "loadsky", &LoadSky_f, "load a skybox by basename (for example loadsky mtnsun_ loads mtnsun_ft.tga and so on)");
467 Cvar_RegisterVariable (&r_sky);
468 Cvar_RegisterVariable (&r_skyscroll1);
469 Cvar_RegisterVariable (&r_skyscroll2);
470 Cvar_RegisterVariable (&r_sky_scissor);
471 memset(&skyboxskinframe, 0, sizeof(skyboxskinframe));
473 R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap, NULL, NULL);