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