a830e85b391de20ebb21a493622738c18d1db172
[xonotic/darkplaces.git] / r_sky.c
1
2 #include "quakedef.h"
3 #include "image.h"
4
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;
9 int skyrendernow;
10 int skyrendermasked;
11
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];
20
21 void R_SkyStartFrame(void)
22 {
23         skyrendernow = false;
24         skyrendersphere = false;
25         skyrenderbox = false;
26         skyrendermasked = false;
27         if (r_sky.integer && !fogenabled)
28         {
29                 if (skyavailable_box)
30                         skyrenderbox = true;
31                 else if (skyavailable_quake)
32                         skyrendersphere = true;
33                 // for depth-masked sky, render the sky on the first sky surface encountered
34                 skyrendernow = true;
35                 skyrendermasked = true;
36         }
37 }
38
39 /*
40 ==================
41 R_SetSkyBox
42 ==================
43 */
44 void R_UnloadSkyBox(void)
45 {
46         int i;
47         for (i = 0;i < 6;i++)
48         {
49                 if (skyboxside[i])
50                         R_FreeTexture(skyboxside[i]);
51                 skyboxside[i] = NULL;;
52         }
53 }
54
55 void R_LoadSkyBox(void)
56 {
57         int i;
58         char name[1024];
59         qbyte *image_rgba;
60         R_UnloadSkyBox();
61         if (!skyname[0])
62                 return;
63         for (i = 0;i < 6;i++)
64         {
65                 if (snprintf(name, sizeof(name), "%s_%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
66                 {
67                         if (snprintf(name, sizeof(name), "%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
68                         {
69                                 if (snprintf(name, sizeof(name), "env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
70                                 {
71                                         if (snprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
72                                         {
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]);
74                                                 continue;
75                                         }
76                                 }
77                         }
78                 }
79                 skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, image_rgba, TEXTYPE_RGBA, TEXF_CLAMP | TEXF_PRECACHE, NULL);
80                 Mem_Free(image_rgba);
81         }
82 }
83
84 int R_SetSkyBox(const char *sky)
85 {
86         if (strcmp(sky, skyname) == 0) // no change
87                 return true;
88
89         if (strlen(sky) > 1000)
90         {
91                 Con_Printf("sky name too long (%i, max is 1000)\n", strlen(sky));
92                 return false;
93         }
94
95         skyavailable_box = false;
96         strcpy(skyname, sky);
97
98         R_UnloadSkyBox();
99         R_LoadSkyBox();
100
101         if (!skyname[0])
102                 return true;
103
104         if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
105         {
106                 skyavailable_box = true;
107                 return true;
108         }
109         return false;
110 }
111
112 // LordHavoc: added LoadSky console command
113 void LoadSky_f (void)
114 {
115         switch (Cmd_Argc())
116         {
117         case 1:
118                 if (skyname[0])
119                         Con_Printf("current sky: %s\n", skyname);
120                 else
121                         Con_Print("no skybox has been set\n");
122                 break;
123         case 2:
124                 if (R_SetSkyBox(Cmd_Argv(1)))
125                 {
126                         if (skyname[0])
127                                 Con_Printf("skybox set to %s\n", skyname);
128                         else
129                                 Con_Print("skybox disabled\n");
130                 }
131                 else
132                         Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
133                 break;
134         default:
135                 Con_Print("usage: loadsky skyname\n");
136                 break;
137         }
138 }
139
140 float skyboxvertex3f[6*4*3] =
141 {
142         // skyside[0]
143          16,  16,  16,
144          16,  16, -16,
145         -16,  16, -16,
146         -16,  16,  16,
147         // skyside[1]
148         -16,  16,  16,
149         -16,  16, -16,
150         -16, -16, -16,
151         -16, -16,  16,
152         // skyside[2]
153         -16, -16,  16,
154         -16, -16, -16,
155          16, -16, -16,
156          16, -16,  16,
157         // skyside[3]
158          16, -16,  16,
159          16, -16, -16,
160          16,  16, -16,
161          16,  16,  16,
162         // skyside[4]
163          16, -16,  16,
164          16,  16,  16,
165         -16,  16,  16,
166         -16, -16,  16,
167         // skyside[5]
168          16,  16, -16,
169          16, -16, -16,
170         -16, -16, -16,
171         -16,  16, -16
172 };
173
174 float skyboxtexcoord2f[6*4*2] =
175 {
176         // skyside[0]
177         1, 0,
178         1, 1,
179         0, 1,
180         0, 0,
181         // skyside[1]
182         1, 0,
183         1, 1,
184         0, 1,
185         0, 0,
186         // skyside[2]
187         1, 0,
188         1, 1,
189         0, 1,
190         0, 0,
191         // skyside[3]
192         1, 0,
193         1, 1,
194         0, 1,
195         0, 0,
196         // skyside[4]
197         1, 0,
198         1, 1,
199         0, 1,
200         0, 0,
201         // skyside[5]
202         1, 0,
203         1, 1,
204         0, 1,
205         0, 0
206 };
207
208 int skyboxelements[6*2*3] =
209 {
210         // skyside[3]
211          0,  1,  2,
212          0,  2,  3,
213         // skyside[1]
214          4,  5,  6,
215          4,  6,  7,
216         // skyside[0]
217          8,  9, 10,
218          8, 10, 11,
219         // skyside[2]
220         12, 13, 14,
221         12, 14, 15,
222         // skyside[4]
223         16, 17, 18,
224         16, 18, 19,
225         // skyside[5]
226         20, 21, 22,
227         20, 22, 23
228 };
229
230 static void R_SkyBox(void)
231 {
232         int i;
233         rmeshstate_t m;
234         GL_Color(1, 1, 1, 1);
235         memset(&m, 0, sizeof(m));
236         GL_BlendFunc(GL_ONE, GL_ZERO);
237         GL_DepthMask(true);
238         GL_DepthTest(false); // don't modify or read zbuffer
239         m.pointer_vertex = skyboxvertex3f;
240         m.pointer_texcoord[0] = skyboxtexcoord2f;
241         GL_LockArrays(0, 6*4);
242         for (i = 0;i < 6;i++)
243         {
244                 m.tex[0] = R_GetTexture(skyboxside[i]);
245                 R_Mesh_State(&m);
246                 R_Mesh_Draw(6*4, 2, skyboxelements + i * 6);
247         }
248         GL_LockArrays(0, 0);
249 }
250
251 #define skygridx 32
252 #define skygridx1 (skygridx + 1)
253 #define skygridxrecip (1.0f / (skygridx))
254 #define skygridy 32
255 #define skygridy1 (skygridy + 1)
256 #define skygridyrecip (1.0f / (skygridy))
257 #define skysphere_numverts (skygridx1 * skygridy1)
258 #define skysphere_numtriangles (skygridx * skygridy * 2)
259 static float skysphere_vertex3f[skysphere_numverts * 3];
260 static float skysphere_texcoord2f[skysphere_numverts * 2];
261 static int skysphere_element3i[skysphere_numtriangles * 3];
262
263 static void skyspherecalc(void)
264 {
265         int i, j, *e;
266         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
267         float dx, dy, dz;
268         dx = 16;
269         dy = 16;
270         dz = 16 / 3;
271         vertex3f = skysphere_vertex3f;
272         texcoord2f = skysphere_texcoord2f;
273         for (j = 0;j <= skygridy;j++)
274         {
275                 a = j * skygridyrecip;
276                 ax = cos(a * M_PI * 2);
277                 ay = -sin(a * M_PI * 2);
278                 for (i = 0;i <= skygridx;i++)
279                 {
280                         b = i * skygridxrecip;
281                         x = cos((b + 0.5) * M_PI);
282                         v[0] = ax*x * dx;
283                         v[1] = ay*x * dy;
284                         v[2] = -sin((b + 0.5) * M_PI) * dz;
285                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
286                         *texcoord2f++ = v[0] * length;
287                         *texcoord2f++ = v[1] * length;
288                         *vertex3f++ = v[0];
289                         *vertex3f++ = v[1];
290                         *vertex3f++ = v[2];
291                 }
292         }
293         e = skysphere_element3i;
294         for (j = 0;j < skygridy;j++)
295         {
296                 for (i = 0;i < skygridx;i++)
297                 {
298                         *e++ =  j      * skygridx1 + i;
299                         *e++ =  j      * skygridx1 + i + 1;
300                         *e++ = (j + 1) * skygridx1 + i;
301
302                         *e++ =  j      * skygridx1 + i + 1;
303                         *e++ = (j + 1) * skygridx1 + i + 1;
304                         *e++ = (j + 1) * skygridx1 + i;
305                 }
306         }
307 }
308
309 static void R_SkySphere(void)
310 {
311         float speedscale;
312         static qboolean skysphereinitialized = false;
313         rmeshstate_t m;
314         matrix4x4_t scroll1matrix, scroll2matrix;
315         if (!skysphereinitialized)
316         {
317                 skysphereinitialized = true;
318                 skyspherecalc();
319         }
320
321         // scroll speed for upper layer
322         speedscale = cl.time*8.0/128.0;
323         // wrap the scroll just to be extra kind to float accuracy
324         speedscale -= (int)speedscale;
325
326         // scroll the lower cloud layer twice as fast (just like quake did)
327         Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
328         Matrix4x4_CreateTranslate(&scroll2matrix, speedscale * 2, speedscale * 2, 0);
329
330         GL_Color(1, 1, 1, 1);
331         GL_BlendFunc(GL_ONE, GL_ZERO);
332         GL_DepthMask(true);
333         GL_DepthTest(false); // don't modify or read zbuffer
334         memset(&m, 0, sizeof(m));
335         m.pointer_vertex = skysphere_vertex3f;
336         m.tex[0] = R_GetTexture(solidskytexture);
337         m.pointer_texcoord[0] = skysphere_texcoord2f;
338         m.texmatrix[0] = scroll1matrix;
339         if (r_textureunits.integer >= 2)
340         {
341                 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
342                 m.tex[1] = R_GetTexture(alphaskytexture);
343                 m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
344                 m.pointer_texcoord[1] = skysphere_texcoord2f;
345                 m.texmatrix[1] = scroll2matrix;
346                 R_Mesh_State(&m);
347                 GL_LockArrays(0, skysphere_numverts);
348                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
349                 GL_LockArrays(0, 0);
350         }
351         else
352         {
353                 // two pass
354                 R_Mesh_State(&m);
355                 GL_LockArrays(0, skysphere_numverts);
356                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
357                 GL_LockArrays(0, 0);
358
359                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
360                 m.tex[0] = R_GetTexture(alphaskytexture);
361                 m.texmatrix[0] = scroll2matrix;
362                 R_Mesh_State(&m);
363                 GL_LockArrays(0, skysphere_numverts);
364                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
365                 GL_LockArrays(0, 0);
366         }
367 }
368
369 void R_Sky(void)
370 {
371         matrix4x4_t skymatrix;
372         if (skyrendermasked)
373         {
374                 Matrix4x4_CreateTranslate(&skymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2]);
375                 R_Mesh_Matrix(&skymatrix);
376                 if (skyrendersphere)
377                 {
378                         // this does not modify depth buffer
379                         R_SkySphere();
380                 }
381                 else if (skyrenderbox)
382                 {
383                         // this does not modify depth buffer
384                         R_SkyBox();
385                 }
386                 /* this will be skyroom someday
387                 else
388                 {
389                         // this modifies the depth buffer so we have to clear it afterward
390                         //R_SkyRoom();
391                         // clear the depthbuffer that was used while rendering the skyroom
392                         //GL_Clear(GL_DEPTH_BUFFER_BIT);
393                 }
394                 */
395         }
396 }
397
398 //===============================================================
399
400 /*
401 =============
402 R_InitSky
403
404 A sky texture is 256*128, with the right side being a masked overlay
405 ==============
406 */
407 void R_InitSky (qbyte *src, int bytesperpixel)
408 {
409         int i, j;
410         unsigned solidpixels[128*128], alphapixels[128*128];
411
412         skyavailable_quake = true;
413
414         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
415         R_FreeTexturePool(&skytexturepool);
416         skytexturepool = R_AllocTexturePool();
417         solidskytexture = NULL;
418         alphaskytexture = NULL;
419
420         if (bytesperpixel == 4)
421         {
422                 for (i = 0;i < 128;i++)
423                 {
424                         for (j = 0;j < 128;j++)
425                         {
426                                 solidpixels[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
427                                 alphapixels[(i*128) + j] = ((unsigned *)src)[i*256+j];
428                         }
429                 }
430         }
431         else
432         {
433                 // make an average value for the back to avoid
434                 // a fringe on the top level
435                 int p, r, g, b;
436                 union
437                 {
438                         unsigned int i;
439                         unsigned char b[4];
440                 }
441                 rgba;
442                 r = g = b = 0;
443                 for (i = 0;i < 128;i++)
444                 {
445                         for (j = 0;j < 128;j++)
446                         {
447                                 rgba.i = palette_complete[src[i*256 + j + 128]];
448                                 r += rgba.b[0];
449                                 g += rgba.b[1];
450                                 b += rgba.b[2];
451                         }
452                 }
453                 rgba.b[0] = r/(128*128);
454                 rgba.b[1] = g/(128*128);
455                 rgba.b[2] = b/(128*128);
456                 rgba.b[3] = 0;
457                 for (i = 0;i < 128;i++)
458                 {
459                         for (j = 0;j < 128;j++)
460                         {
461                                 solidpixels[(i*128) + j] = palette_complete[src[i*256 + j + 128]];
462                                 alphapixels[(i*128) + j] = (p = src[i*256 + j]) ? palette_complete[p] : rgba.i;
463                         }
464                 }
465         }
466
467         solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) solidpixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
468         alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) alphapixels, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
469 }
470
471 void R_ResetQuakeSky(void)
472 {
473         skyavailable_quake = false;
474 }
475
476 void R_ResetSkyBox(void)
477 {
478         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
479         skyname[0] = 0;
480         skyavailable_box = false;
481 }
482
483 static void r_sky_start(void)
484 {
485         skytexturepool = R_AllocTexturePool();
486         solidskytexture = NULL;
487         alphaskytexture = NULL;
488         R_LoadSkyBox();
489 }
490
491 static void r_sky_shutdown(void)
492 {
493         R_UnloadSkyBox();
494         R_FreeTexturePool(&skytexturepool);
495         solidskytexture = NULL;
496         alphaskytexture = NULL;
497 }
498
499 static void r_sky_newmap(void)
500 {
501 }
502
503 void R_Sky_Init(void)
504 {
505         Cmd_AddCommand ("loadsky", &LoadSky_f);
506         Cvar_RegisterVariable (&r_sky);
507         R_ResetSkyBox();
508         R_ResetQuakeSky();
509         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
510 }
511