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