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];
16 static matrix4x4_t skymatrix;
17 static matrix4x4_t skyinversematrix;
19 typedef struct suffixinfo_s
22 qboolean flipx, flipy, flipdiagonal;
25 static const suffixinfo_t suffix[3][6] =
28 {"px", false, false, false},
29 {"nx", false, false, false},
30 {"py", false, false, false},
31 {"ny", false, false, false},
32 {"pz", false, false, false},
33 {"nz", false, false, false}
36 {"posx", false, false, false},
37 {"negx", false, false, false},
38 {"posy", false, false, false},
39 {"negy", false, false, false},
40 {"posz", false, false, false},
41 {"negz", false, false, false}
44 {"rt", false, false, true},
45 {"lf", true, true, true},
46 {"bk", false, true, false},
47 {"ft", true, false, false},
48 {"up", false, false, true},
49 {"dn", false, false, true}
53 static skinframe_t *skyboxskinframe[6];
55 void R_SkyStartFrame(void)
57 skyrendersphere = false;
59 skyrendermasked = false;
60 // for depth-masked sky, we need to know whether any sky was rendered
61 skyrenderlater = false;
64 if (skyboxskinframe[0] || skyboxskinframe[1] || skyboxskinframe[2] || skyboxskinframe[3] || skyboxskinframe[4] || skyboxskinframe[5])
66 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.solidskyskinframe)
67 skyrendersphere = true;
68 skyrendermasked = true;
77 void R_UnloadSkyBox(void)
83 if (skyboxskinframe[i])
85 // TODO: make a R_SkinFrame_Purge for single skins...
88 skyboxskinframe[i] = NULL;
90 if (c && developer_loading.integer)
91 Con_Printf("unloading skybox\n");
94 int R_LoadSkyBox(void)
97 int indices[4] = {0,1,2,3};
98 char name[MAX_INPUTLINE];
99 unsigned char *image_buffer;
112 if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, r_texture_convertsRGB_skybox.integer)))
114 if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, r_texture_convertsRGB_skybox.integer)))
116 if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, r_texture_convertsRGB_skybox.integer)))
118 if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, r_texture_convertsRGB_skybox.integer)))
123 temp = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
124 Image_CopyMux (temp, image_buffer, image_width, image_height, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, indices);
125 skyboxskinframe[i] = R_SkinFrame_LoadInternalBGRA(va("skyboxside%d", i), TEXF_CLAMP | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), temp, image_width, image_height);
126 Mem_Free(image_buffer);
138 if (developer_loading.integer)
139 Con_Printf("loading skybox \"%s\"\n", name);
144 int R_SetSkyBox(const char *sky)
146 if (strcmp(sky, skyname) == 0) // no change
149 if (strlen(sky) > 1000)
151 Con_Printf("sky name too long (%i, max is 1000)\n", (int)strlen(sky));
155 strlcpy(skyname, sky, sizeof(skyname));
157 return R_LoadSkyBox();
160 // LordHavoc: added LoadSky console command
161 void LoadSky_f (void)
167 Con_Printf("current sky: %s\n", skyname);
169 Con_Print("no skybox has been set\n");
172 if (R_SetSkyBox(Cmd_Argv(1)))
175 Con_Printf("skybox set to %s\n", skyname);
177 Con_Print("skybox disabled\n");
180 Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
183 Con_Print("usage: loadsky skyname\n");
188 static const float skyboxvertex3f[6*4*3] =
222 static const float skyboxtexcoord2f[6*4*2] =
256 static const int skyboxelement3i[6*2*3] =
278 static const unsigned short skyboxelement3s[6*2*3] =
300 static void R_SkyBox(void)
303 RSurf_ActiveCustomEntity(&skymatrix, &skyinversematrix, 0, 0, 1, 1, 1, 1, 6*4, skyboxvertex3f, skyboxtexcoord2f, NULL, NULL, NULL, NULL, 6*2, skyboxelement3i, skyboxelement3s, false, false);
304 for (i = 0;i < 6;i++)
305 R_DrawCustomSurface(skyboxskinframe[i], &identitymatrix, MATERIALFLAG_SKY | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST, i*4, 4, i*2, 2, false, false);
309 #define skygridx1 (skygridx + 1)
310 #define skygridxrecip (1.0f / (skygridx))
312 #define skygridy1 (skygridy + 1)
313 #define skygridyrecip (1.0f / (skygridy))
314 #define skysphere_numverts (skygridx1 * skygridy1)
315 #define skysphere_numtriangles (skygridx * skygridy * 2)
316 static float skysphere_vertex3f[skysphere_numverts * 3];
317 static float skysphere_texcoord2f[skysphere_numverts * 2];
318 static int skysphere_element3i[skysphere_numtriangles * 3];
319 static unsigned short skysphere_element3s[skysphere_numtriangles * 3];
321 static void skyspherecalc(void)
325 float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
330 vertex3f = skysphere_vertex3f;
331 texcoord2f = skysphere_texcoord2f;
332 for (j = 0;j <= skygridy;j++)
334 a = j * skygridyrecip;
335 ax = cos(a * M_PI * 2);
336 ay = -sin(a * M_PI * 2);
337 for (i = 0;i <= skygridx;i++)
339 b = i * skygridxrecip;
340 x = cos((b + 0.5) * M_PI);
343 v[2] = -sin((b + 0.5) * M_PI) * dz;
344 length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
345 *texcoord2f++ = v[0] * length;
346 *texcoord2f++ = v[1] * length;
352 e = skysphere_element3s;
353 for (j = 0;j < skygridy;j++)
355 for (i = 0;i < skygridx;i++)
357 *e++ = j * skygridx1 + i;
358 *e++ = j * skygridx1 + i + 1;
359 *e++ = (j + 1) * skygridx1 + i;
361 *e++ = j * skygridx1 + i + 1;
362 *e++ = (j + 1) * skygridx1 + i + 1;
363 *e++ = (j + 1) * skygridx1 + i;
366 for (i = 0;i < skysphere_numtriangles*3;i++)
367 skysphere_element3i[i] = skysphere_element3s[i];
370 static void R_SkySphere(void)
373 static qboolean skysphereinitialized = false;
374 matrix4x4_t scroll1matrix, scroll2matrix;
375 if (!skysphereinitialized)
377 skysphereinitialized = true;
381 // wrap the scroll values just to be extra kind to float accuracy
383 // scroll speed for upper layer
384 speedscale = r_refdef.scene.time*r_skyscroll1.value*8.0/128.0;
385 speedscale -= floor(speedscale);
386 Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
387 // scroll speed for lower layer (transparent layer)
388 speedscale = r_refdef.scene.time*r_skyscroll2.value*8.0/128.0;
389 speedscale -= floor(speedscale);
390 Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
392 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);
393 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);
394 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);
399 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));
400 Matrix4x4_Invert_Simple(&skyinversematrix, &skymatrix);
404 // this does not modify depth buffer
407 else if (skyrenderbox)
409 // this does not modify depth buffer
412 /* this will be skyroom someday
415 // this modifies the depth buffer so we have to clear it afterward
417 // clear the depthbuffer that was used while rendering the skyroom
418 //GL_Clear(GL_DEPTH_BUFFER_BIT);
423 //===============================================================
425 void R_ResetSkyBox(void)
432 static void r_sky_start(void)
434 skytexturepool = R_AllocTexturePool();
438 static void r_sky_shutdown(void)
441 R_FreeTexturePool(&skytexturepool);
444 static void r_sky_newmap(void)
449 void R_Sky_Init(void)
451 Cmd_AddCommand ("loadsky", &LoadSky_f, "load a skybox by basename (for example loadsky mtnsun_ loads mtnsun_ft.tga and so on)");
452 Cvar_RegisterVariable (&r_sky);
453 Cvar_RegisterVariable (&r_skyscroll1);
454 Cvar_RegisterVariable (&r_skyscroll2);
455 memset(&skyboxskinframe, 0, sizeof(skyboxskinframe));
457 R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);