changed sky rendering to occur after depthmask polygons, instead of before
[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", "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"};
9 int skyrenderlater;
10 int skyrendermasked;
11
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;
18
19 typedef struct suffixinfo_s
20 {
21         const char *suffix;
22         qboolean flipx, flipy, flipdiagonal;
23 }
24 suffixinfo_t;
25 static const 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",   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}
50         }
51 };
52
53 static rtexture_t *skyboxside[6];
54
55 void R_SkyStartFrame(void)
56 {
57         skyrendersphere = false;
58         skyrenderbox = false;
59         skyrendermasked = false;
60         // for depth-masked sky, we need to know whether any sky was rendered
61         skyrenderlater = false;
62         if (r_sky.integer && !(r_refdef.fogenabled && r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1] < (1.0f / 256.0f)))
63         {
64                 if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
65                         skyrenderbox = true;
66                 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.solidskytexture)
67                         skyrendersphere = true;
68                 skyrendermasked = true;
69         }
70 }
71
72 /*
73 ==================
74 R_SetSkyBox
75 ==================
76 */
77 void R_UnloadSkyBox(void)
78 {
79         int i;
80         int c = 0;
81         for (i = 0;i < 6;i++)
82         {
83                 if (skyboxside[i])
84                 {
85                         R_FreeTexture(skyboxside[i]);
86                         c++;
87                 }
88                 skyboxside[i] = NULL;
89         }
90         if (c && developer_loading.integer)
91                 Con_Printf("unloading skybox\n");
92 }
93
94 int R_LoadSkyBox(void)
95 {
96         int i, j, success;
97         int indices[4] = {0,1,2,3};
98         char name[MAX_INPUTLINE];
99         unsigned char *image_buffer;
100         unsigned char *temp;
101
102         R_UnloadSkyBox();
103
104         if (!skyname[0])
105                 return true;
106
107         for (j=0; j<3; j++)
108         {
109                 success = 0;
110                 for (i=0; i<6; i++)
111                 {
112                         if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
113                         {
114                                 if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
115                                 {
116                                         if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
117                                         {
118                                                 if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
119                                                         continue;
120                                         }
121                                 }
122                         }
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                         skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, temp, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PRECACHE | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), NULL);
126                         Mem_Free(image_buffer);
127                         Mem_Free(temp);
128                         success++;
129                 }
130
131                 if (success)
132                         break;
133         }
134
135         if (j == 3)
136                 return false;
137
138         if (developer_loading.integer)
139                 Con_Printf("loading skybox \"%s\"\n", name);
140
141         return true;
142 }
143
144 int R_SetSkyBox(const char *sky)
145 {
146         if (strcmp(sky, skyname) == 0) // no change
147                 return true;
148
149         if (strlen(sky) > 1000)
150         {
151                 Con_Printf("sky name too long (%i, max is 1000)\n", (int)strlen(sky));
152                 return false;
153         }
154
155         strlcpy(skyname, sky, sizeof(skyname));
156
157         return R_LoadSkyBox();
158 }
159
160 // LordHavoc: added LoadSky console command
161 void LoadSky_f (void)
162 {
163         switch (Cmd_Argc())
164         {
165         case 1:
166                 if (skyname[0])
167                         Con_Printf("current sky: %s\n", skyname);
168                 else
169                         Con_Print("no skybox has been set\n");
170                 break;
171         case 2:
172                 if (R_SetSkyBox(Cmd_Argv(1)))
173                 {
174                         if (skyname[0])
175                                 Con_Printf("skybox set to %s\n", skyname);
176                         else
177                                 Con_Print("skybox disabled\n");
178                 }
179                 else
180                         Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
181                 break;
182         default:
183                 Con_Print("usage: loadsky skyname\n");
184                 break;
185         }
186 }
187
188 static const float skyboxvertex3f[6*4*3] =
189 {
190         // skyside[0]
191          16, -16,  16,
192          16, -16, -16,
193          16,  16, -16,
194          16,  16,  16,
195         // skyside[1]
196         -16,  16,  16,
197         -16,  16, -16,
198         -16, -16, -16,
199         -16, -16,  16,
200         // skyside[2]
201          16,  16,  16,
202          16,  16, -16,
203         -16,  16, -16,
204         -16,  16,  16,
205         // skyside[3]
206         -16, -16,  16,
207         -16, -16, -16,
208          16, -16, -16,
209          16, -16,  16,
210         // skyside[4]
211         -16, -16,  16,
212          16, -16,  16,
213          16,  16,  16,
214         -16,  16,  16,
215         // skyside[5]
216          16, -16, -16,
217         -16, -16, -16,
218         -16,  16, -16,
219          16,  16, -16
220 };
221
222 static const float skyboxtexcoord2f[6*4*2] =
223 {
224         // skyside[0]
225         0, 1,
226         1, 1,
227         1, 0,
228         0, 0,
229         // skyside[1]
230         1, 0,
231         0, 0,
232         0, 1,
233         1, 1,
234         // skyside[2]
235         1, 1,
236         1, 0,
237         0, 0,
238         0, 1,
239         // skyside[3]
240         0, 0,
241         0, 1,
242         1, 1,
243         1, 0,
244         // skyside[4]
245         0, 1,
246         1, 1,
247         1, 0,
248         0, 0,
249         // skyside[5]
250         0, 1,
251         1, 1,
252         1, 0,
253         0, 0
254 };
255
256 static const unsigned short skyboxelements[6*2*3] =
257 {
258         // skyside[3]
259          0,  1,  2,
260          0,  2,  3,
261         // skyside[1]
262          4,  5,  6,
263          4,  6,  7,
264         // skyside[0]
265          8,  9, 10,
266          8, 10, 11,
267         // skyside[2]
268         12, 13, 14,
269         12, 14, 15,
270         // skyside[4]
271         16, 17, 18,
272         16, 18, 19,
273         // skyside[5]
274         20, 21, 22,
275         20, 22, 23
276 };
277
278 static void R_SkyBox(void)
279 {
280         int i;
281         // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
282         GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
283         GL_BlendFunc(GL_ONE, GL_ZERO);
284         GL_CullFace(GL_NONE);
285         GL_DepthMask(false);
286         GL_DepthRange(0, 1);
287         GL_PolygonOffset(0, 0);
288         GL_DepthTest(false); // don't modify or read zbuffer
289         R_Mesh_VertexPointer(skyboxvertex3f, 0, 0);
290         R_Mesh_ColorPointer(NULL, 0, 0);
291         R_Mesh_ResetTextureState();
292         R_Mesh_TexCoordPointer(0, 2, skyboxtexcoord2f, 0, 0);
293         R_SetupGenericShader(true);
294         GL_LockArrays(0, 6*4);
295         for (i = 0;i < 6;i++)
296         {
297                 R_Mesh_TexBind(0, R_GetTexture(skyboxside[i]));
298                 R_Mesh_Draw(0, 6*4, i*2, 2, NULL, skyboxelements, 0, 0);
299         }
300
301         if(r_refdef.fogenabled)
302         {
303                 R_SetupGenericShader(false);
304                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
305                 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
306                 for (i = 0;i < 6;i++)
307                 {
308                         R_Mesh_TexBind(0, 0);
309                         R_Mesh_Draw(0, 6*4, i*2, 2, NULL, skyboxelements, 0, 0);
310                 }
311         }
312
313         GL_LockArrays(0, 0);
314 }
315
316 #define skygridx 32
317 #define skygridx1 (skygridx + 1)
318 #define skygridxrecip (1.0f / (skygridx))
319 #define skygridy 32
320 #define skygridy1 (skygridy + 1)
321 #define skygridyrecip (1.0f / (skygridy))
322 #define skysphere_numverts (skygridx1 * skygridy1)
323 #define skysphere_numtriangles (skygridx * skygridy * 2)
324 static float skysphere_vertex3f[skysphere_numverts * 3];
325 static float skysphere_texcoord2f[skysphere_numverts * 2];
326 static unsigned short skysphere_elements[skysphere_numtriangles * 3];
327
328 static void skyspherecalc(void)
329 {
330         int i, j;
331         unsigned short *e;
332         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
333         float dx, dy, dz;
334         dx = 16.0f;
335         dy = 16.0f;
336         dz = 16.0f / 3.0f;
337         vertex3f = skysphere_vertex3f;
338         texcoord2f = skysphere_texcoord2f;
339         for (j = 0;j <= skygridy;j++)
340         {
341                 a = j * skygridyrecip;
342                 ax = cos(a * M_PI * 2);
343                 ay = -sin(a * M_PI * 2);
344                 for (i = 0;i <= skygridx;i++)
345                 {
346                         b = i * skygridxrecip;
347                         x = cos((b + 0.5) * M_PI);
348                         v[0] = ax*x * dx;
349                         v[1] = ay*x * dy;
350                         v[2] = -sin((b + 0.5) * M_PI) * dz;
351                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
352                         *texcoord2f++ = v[0] * length;
353                         *texcoord2f++ = v[1] * length;
354                         *vertex3f++ = v[0];
355                         *vertex3f++ = v[1];
356                         *vertex3f++ = v[2];
357                 }
358         }
359         e = skysphere_elements;
360         for (j = 0;j < skygridy;j++)
361         {
362                 for (i = 0;i < skygridx;i++)
363                 {
364                         *e++ =  j      * skygridx1 + i;
365                         *e++ =  j      * skygridx1 + i + 1;
366                         *e++ = (j + 1) * skygridx1 + i;
367
368                         *e++ =  j      * skygridx1 + i + 1;
369                         *e++ = (j + 1) * skygridx1 + i + 1;
370                         *e++ = (j + 1) * skygridx1 + i;
371                 }
372         }
373 }
374
375 static void R_SkySphere(void)
376 {
377         double speedscale;
378         static qboolean skysphereinitialized = false;
379         matrix4x4_t scroll1matrix, scroll2matrix;
380         if (!skysphereinitialized)
381         {
382                 skysphereinitialized = true;
383                 skyspherecalc();
384         }
385
386         // wrap the scroll values just to be extra kind to float accuracy
387
388         // scroll speed for upper layer
389         speedscale = r_refdef.scene.time*r_skyscroll1.value*8.0/128.0;
390         speedscale -= floor(speedscale);
391         Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
392         // scroll speed for lower layer (transparent layer)
393         speedscale = r_refdef.scene.time*r_skyscroll2.value*8.0/128.0;
394         speedscale -= floor(speedscale);
395         Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
396
397         // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
398         GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
399         GL_BlendFunc(GL_ONE, GL_ZERO);
400         GL_CullFace(GL_NONE);
401         GL_DepthMask(true);
402         GL_DepthRange(0, 1);
403         GL_PolygonOffset(0, 0);
404         GL_DepthTest(false); // don't modify or read zbuffer
405         R_Mesh_VertexPointer(skysphere_vertex3f, 0, 0);
406         R_Mesh_ColorPointer(NULL, 0, 0);
407         R_Mesh_ResetTextureState();
408         R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.solidskytexture));
409         R_Mesh_TexCoordPointer(0, 2, skysphere_texcoord2f, 0, 0);
410         R_Mesh_TexMatrix(0, &scroll1matrix);
411         if (r_textureunits.integer >= 2 && r_refdef.view.colorscale == 1)
412         {
413                 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
414                 R_SetupGenericTwoTextureShader(GL_DECAL);
415                 R_Mesh_TexBind(1, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
416                 R_Mesh_TexCoordPointer(1, 2, skysphere_texcoord2f, 0, 0);
417                 R_Mesh_TexMatrix(1, &scroll2matrix);
418                 GL_LockArrays(0, skysphere_numverts);
419                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, NULL, skysphere_elements, 0, 0);
420                 GL_LockArrays(0, 0);
421                 R_Mesh_TexBind(1, 0);
422         }
423         else
424         {
425                 // two pass
426                 R_SetupGenericShader(true);
427                 GL_LockArrays(0, skysphere_numverts);
428                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, NULL, skysphere_elements, 0, 0);
429
430                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
431                 R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
432                 R_Mesh_TexMatrix(0, &scroll2matrix);
433                 GL_LockArrays(0, skysphere_numverts);
434                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, NULL, skysphere_elements, 0, 0);
435                 GL_LockArrays(0, 0);
436         }
437
438         if(r_refdef.fogenabled)
439         {
440                 R_SetupGenericShader(false);
441                 R_Mesh_TexBind(0, 0);
442                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
443                 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
444                 GL_LockArrays(0, skysphere_numverts);
445                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, NULL, skysphere_elements, 0, 0);
446                 GL_LockArrays(0, 0);
447         }
448 }
449
450 void R_Sky(void)
451 {
452         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));
453         Matrix4x4_Invert_Simple(&skyinversematrix, &skymatrix);
454
455         R_Mesh_Matrix(&skymatrix);
456         if (skyrendersphere)
457         {
458                 // this does not modify depth buffer
459                 R_SkySphere();
460         }
461         else if (skyrenderbox)
462         {
463                 // this does not modify depth buffer
464                 R_SkyBox();
465         }
466         /* this will be skyroom someday
467         else
468         {
469                 // this modifies the depth buffer so we have to clear it afterward
470                 //R_SkyRoom();
471                 // clear the depthbuffer that was used while rendering the skyroom
472                 //GL_Clear(GL_DEPTH_BUFFER_BIT);
473         }
474         */
475         GL_DepthRange(0, 1);
476         GL_DepthTest(true);
477         GL_DepthMask(true);
478 }
479
480 //===============================================================
481
482 void R_ResetSkyBox(void)
483 {
484         R_UnloadSkyBox();
485         skyname[0] = 0;
486         R_LoadSkyBox();
487 }
488
489 static void r_sky_start(void)
490 {
491         skytexturepool = R_AllocTexturePool();
492         R_LoadSkyBox();
493 }
494
495 static void r_sky_shutdown(void)
496 {
497         R_UnloadSkyBox();
498         R_FreeTexturePool(&skytexturepool);
499 }
500
501 static void r_sky_newmap(void)
502 {
503 }
504
505
506 void R_Sky_Init(void)
507 {
508         Cmd_AddCommand ("loadsky", &LoadSky_f, "load a skybox by basename (for example loadsky mtnsun_ loads mtnsun_ft.tga and so on)");
509         Cvar_RegisterVariable (&r_sky);
510         Cvar_RegisterVariable (&r_skyscroll1);
511         Cvar_RegisterVariable (&r_skyscroll2);
512         memset(&skyboxside, 0, sizeof(skyboxside));
513         skyname[0] = 0;
514         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
515 }
516