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