6 cvar_t r_skyquality = {CVAR_SAVE, "r_skyquality", "2"};
7 cvar_t r_skyflush = {0, "r_skyflush", "0"};
9 static char skyworldname[1024];
10 rtexture_t *solidskytexture;
11 rtexture_t *alphaskytexture;
12 static qboolean skyavailable_quake;
13 static qboolean skyavailable_box;
14 static rtexturepool_t *skytexturepool;
20 static void r_sky_start(void)
22 skytexturepool = R_AllocTexturePool();
23 solidskytexture = NULL;
24 alphaskytexture = NULL;
27 static void r_sky_shutdown(void)
29 R_FreeTexturePool(&skytexturepool);
30 solidskytexture = NULL;
31 alphaskytexture = NULL;
34 int R_SetSkyBox(char *sky);
36 static void r_sky_newmap(void)
38 skyavailable_quake = false;
39 if (!strcmp(skyworldname, cl.worldmodel->name))
40 skyavailable_quake = true;
45 Cmd_AddCommand ("loadsky", &LoadSky_f);
46 Cvar_RegisterVariable (&r_skyquality);
47 Cvar_RegisterVariable (&r_skyflush);
48 R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
51 static int skyrendersphere;
52 static int skyrenderbox;
54 void R_SkyStartFrame(void)
57 skyrendersphere = false;
59 skyrenderglquake = false;
60 skyrendermasked = false;
61 if (r_skyquality.integer >= 1 && !fogenabled)
65 else if (skyavailable_quake)
67 switch(r_skyquality.integer)
70 skyrenderglquake = true;
74 skyrendersphere = true;
78 if (skyrenderbox || skyrendersphere)
80 // for depth-masked sky, render the sky on the first sky surface encountered
82 skyrendermasked = true;
87 static char skyname[256];
94 static char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
95 static rtexture_t *skyboxside[6];
96 int R_SetSkyBox(char *sky)
102 if (strcmp(sky, skyname) == 0) // no change
105 skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
106 skyavailable_box = false;
112 if (strlen(sky) > 1000)
114 Con_Printf ("sky name too long (%i, max is 1000)\n", strlen(sky));
118 for (i = 0;i < 6;i++)
120 sprintf (name, "env/%s%s", sky, suf[i]);
121 if (!(image_rgba = loadimagepixels(name, false, 0, 0)))
123 sprintf (name, "gfx/env/%s%s", sky, suf[i]);
124 if (!(image_rgba = loadimagepixels(name, false, 0, 0)))
126 Con_Printf ("Couldn't load env/%s%s or gfx/env/%s%s\n", sky, suf[i], sky, suf[i]);
130 skyboxside[i] = R_LoadTexture(skytexturepool, va("skyboxside%d", i), image_width, image_height, image_rgba, TEXTYPE_RGBA, TEXF_PRECACHE);
131 Mem_Free(image_rgba);
134 if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
136 skyavailable_box = true;
137 strcpy(skyname, sky);
143 // LordHavoc: added LoadSky console command
144 void LoadSky_f (void)
150 Con_Printf("current sky: %s\n", skyname);
152 Con_Printf("no skybox has been set\n");
155 if (R_SetSkyBox(Cmd_Argv(1)))
158 Con_Printf("skybox set to %s\n", skyname);
160 Con_Printf("skybox disabled\n");
163 Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
166 Con_Printf("usage: loadsky skyname\n");
171 #define R_SkyBoxPolyVec(i,s,t,x,y,z) \
172 vert[i][0] = (x) * 1024.0f + r_origin[0];\
173 vert[i][1] = (y) * 1024.0f + r_origin[1];\
174 vert[i][2] = (z) * 1024.0f + r_origin[2];\
175 st[i][0] = (s) * (254.0f/256.0f) + (1.0f/256.0f);\
176 st[i][1] = (t) * (254.0f/256.0f) + (1.0f/256.0f);
178 int skyboxindex[6] = {0, 1, 2, 0, 2, 3};
180 static void R_SkyBox(void)
182 float vert[4][4], st[4][2];
184 memset(&m, 0, sizeof(m));
185 m.transparent = false;
186 m.blendfunc1 = GL_ONE;
187 m.blendfunc2 = GL_ZERO;
190 m.index = skyboxindex;
191 m.vertex = &vert[0][0];
192 m.vertexstep = sizeof(float[4]);
197 m.texcoords[0] = &st[0][0];
198 m.texcoordstep[0] = sizeof(float[2]);
199 m.tex[0] = R_GetTexture(skyboxside[3]); // front
200 R_SkyBoxPolyVec(0, 1, 0, 1, -1, 1);
201 R_SkyBoxPolyVec(1, 1, 1, 1, -1, -1);
202 R_SkyBoxPolyVec(2, 0, 1, 1, 1, -1);
203 R_SkyBoxPolyVec(3, 0, 0, 1, 1, 1);
205 m.tex[0] = R_GetTexture(skyboxside[1]); // back
206 R_SkyBoxPolyVec(0, 1, 0, -1, 1, 1);
207 R_SkyBoxPolyVec(1, 1, 1, -1, 1, -1);
208 R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
209 R_SkyBoxPolyVec(3, 0, 0, -1, -1, 1);
211 m.tex[0] = R_GetTexture(skyboxside[0]); // right
212 R_SkyBoxPolyVec(0, 1, 0, 1, 1, 1);
213 R_SkyBoxPolyVec(1, 1, 1, 1, 1, -1);
214 R_SkyBoxPolyVec(2, 0, 1, -1, 1, -1);
215 R_SkyBoxPolyVec(3, 0, 0, -1, 1, 1);
217 m.tex[0] = R_GetTexture(skyboxside[2]); // left
218 R_SkyBoxPolyVec(0, 1, 0, -1, -1, 1);
219 R_SkyBoxPolyVec(1, 1, 1, -1, -1, -1);
220 R_SkyBoxPolyVec(2, 0, 1, 1, -1, -1);
221 R_SkyBoxPolyVec(3, 0, 0, 1, -1, 1);
223 m.tex[0] = R_GetTexture(skyboxside[4]); // up
224 R_SkyBoxPolyVec(0, 1, 0, 1, -1, 1);
225 R_SkyBoxPolyVec(1, 1, 1, 1, 1, 1);
226 R_SkyBoxPolyVec(2, 0, 1, -1, 1, 1);
227 R_SkyBoxPolyVec(3, 0, 0, -1, -1, 1);
229 m.tex[0] = R_GetTexture(skyboxside[5]); // down
230 R_SkyBoxPolyVec(0, 1, 0, 1, 1, -1);
231 R_SkyBoxPolyVec(1, 1, 1, 1, -1, -1);
232 R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
233 R_SkyBoxPolyVec(3, 0, 0, -1, 1, -1);
238 #define skygridx1 (skygridx + 1)
239 #define skygridxrecip (1.0f / (skygridx))
241 #define skygridy1 (skygridy + 1)
242 #define skygridyrecip (1.0f / (skygridy))
244 static float skysphere[skygridx1*skygridy1*5];
245 static int skysphereindices[skygridx*skygridy*6];
246 static void skyspherecalc(float *sphere, float dx, float dy, float dz)
248 float a, b, x, ax, ay, v[3], length;
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 * M_PI * 2);
261 v[2] = -sin(b * M_PI * 2) * dz;
262 length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
263 *sphere++ = v[0] * length;
264 *sphere++ = v[1] * length;
270 index = skysphereindices;
271 for (j = 0;j < skygridy;j++)
273 for (i = 0;i < skygridx;i++)
275 *index++ = j * skygridx1 + i;
276 *index++ = j * skygridx1 + i + 1;
277 *index++ = (j + 1) * skygridx1 + i;
279 *index++ = j * skygridx1 + i + 1;
280 *index++ = (j + 1) * skygridx1 + i + 1;
281 *index++ = (j + 1) * skygridx1 + i;
287 static void skyspherearrays(float *vert, float *tex, float *tex2, float *source, float s, float s2)
289 float *v, *t, *t2, radius;
294 radius = r_farclip - 8;
295 for (i = 0;i < (skygridx1*skygridy1);i++)
297 *t++ = source[0] + s;
298 *t++ = source[1] + s;
299 *t2++ = source[0] + s2;
300 *t2++ = source[1] + s2;
301 *v++ = source[2] + r_origin[0];
302 *v++ = source[3] + r_origin[1];
303 *v++ = source[4] + r_origin[2];
309 static void R_SkySphere(void)
311 float speedscale, speedscale2;
312 float vert[skygridx1*skygridy1*4], tex[skygridx1*skygridy1*2], tex2[skygridx1*skygridy1*2];
313 static qboolean skysphereinitialized = false;
315 if (!skysphereinitialized)
317 skysphereinitialized = true;
318 skyspherecalc(skysphere, 1024, 1024, 1024 / 3);
320 memset(&m, 0, sizeof(m));
321 m.transparent = false;
322 m.blendfunc1 = GL_ONE;
323 m.blendfunc2 = GL_ZERO;
324 m.numtriangles = skygridx*skygridy*2;
325 m.numverts = skygridx1*skygridy1;
326 m.index = skysphereindices;
328 m.vertexstep = sizeof(float[4]);
333 m.texcoords[0] = tex;
334 m.texcoordstep[0] = sizeof(float[2]);
335 speedscale = cl.time*8.0/128.0;
336 speedscale -= (int)speedscale;
337 speedscale2 = cl.time*16.0/128.0;
338 speedscale2 -= (int)speedscale2;
339 skyspherearrays(vert, tex, tex2, skysphere, speedscale, speedscale2);
340 m.tex[0] = R_GetTexture(solidskytexture);
343 m.blendfunc1 = GL_SRC_ALPHA;
344 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
345 m.tex[0] = R_GetTexture(alphaskytexture);
346 m.texcoords[0] = tex2;
356 else if (skyrenderbox)
359 // clear the zbuffer that was used while rendering the sky
364 //===============================================================
370 A sky texture is 256*128, with the right side being a masked overlay
373 void R_InitSky (qbyte *src, int bytesperpixel)
375 int i, j, p, r, g, b;
376 qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
377 unsigned trans[128*128], transpix, *rgba;
379 strcpy(skyworldname, loadmodel->name);
381 // flush skytexturepool so we won't build up a leak from uploading textures multiple times
382 R_FreeTexturePool(&skytexturepool);
383 skytexturepool = R_AllocTexturePool();
384 solidskytexture = NULL;
385 alphaskytexture = NULL;
387 if (bytesperpixel == 4)
389 for (i = 0;i < 128;i++)
390 for (j = 0;j < 128;j++)
391 trans[(i*128) + j] = src[i*256+j+128];
395 // make an average value for the back to avoid
396 // a fringe on the top level
398 for (i=0 ; i<128 ; i++)
400 for (j=0 ; j<128 ; j++)
402 p = src[i*256 + j + 128];
403 rgba = &d_8to24table[p];
404 trans[(i*128) + j] = *rgba;
405 r += ((qbyte *)rgba)[0];
406 g += ((qbyte *)rgba)[1];
407 b += ((qbyte *)rgba)[2];
411 ((qbyte *)&transpix)[0] = r/(128*128);
412 ((qbyte *)&transpix)[1] = g/(128*128);
413 ((qbyte *)&transpix)[2] = b/(128*128);
414 ((qbyte *)&transpix)[3] = 0;
417 memcpy(skyupperlayerpixels, trans, 128*128*4);
419 solidskytexture = R_LoadTexture (skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE);
421 if (bytesperpixel == 4)
423 for (i = 0;i < 128;i++)
424 for (j = 0;j < 128;j++)
425 trans[(i*128) + j] = src[i*256+j];
429 for (i=0 ; i<128 ; i++)
431 for (j=0 ; j<128 ; j++)
435 trans[(i*128) + j] = transpix;
437 trans[(i*128) + j] = d_8to24table[p];
442 memcpy(skylowerlayerpixels, trans, 128*128*4);
444 alphaskytexture = R_LoadTexture (skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);